From 943ae5b1bbdf18ce7237fed21093962ab7265997 Mon Sep 17 00:00:00 2001 From: Denis Tingaikin Date: Thu, 29 Jun 2023 20:00:03 +0300 Subject: [PATCH 1/2] add a new helper function to simplify naming and id search for path segments and connection events Signed-off-by: Denis Tingaikin --- pkg/api/networkservice/connection_event_helpers.go | 10 ++++++++++ pkg/api/networkservice/connection_helpers.go | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/api/networkservice/connection_event_helpers.go b/pkg/api/networkservice/connection_event_helpers.go index 7bc633e..b527bb6 100644 --- a/pkg/api/networkservice/connection_event_helpers.go +++ b/pkg/api/networkservice/connection_event_helpers.go @@ -22,3 +22,13 @@ import "google.golang.org/protobuf/proto" func (x *ConnectionEvent) Clone() *ConnectionEvent { return proto.Clone(x).(*ConnectionEvent) } + +// GetEventSenderName returns name of the segment from the path who send the event +func (x *ConnectionEvent) GetEventSenderName() string { + for v, k := range x.GetConnections() { + if segment := k.GetPathSegmentById(v); segment != nil { + return segment.GetName() + } + } + return "unknown" +} diff --git a/pkg/api/networkservice/connection_helpers.go b/pkg/api/networkservice/connection_helpers.go index 7a85cba..ad028d7 100644 --- a/pkg/api/networkservice/connection_helpers.go +++ b/pkg/api/networkservice/connection_helpers.go @@ -160,6 +160,16 @@ func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) return false } +// GetPathSegmentById gets the path segment by passed id +func (x *Connection) GetPathSegmentById(id string) *PathSegment { + for _, segment := range x.GetPath().GetPathSegments() { + if segment.GetId() == id { + return segment + } + } + return nil +} + // GetCurrentPathSegment - Get the current path segment of the connection func (x *Connection) GetCurrentPathSegment() *PathSegment { if x == nil { From 35c345ec31d17fc8e018e3fffcbc78100503cc71 Mon Sep 17 00:00:00 2001 From: Denis Tingaikin Date: Thu, 29 Jun 2023 20:05:27 +0300 Subject: [PATCH 2/2] fix linter Signed-off-by: Denis Tingaikin --- pkg/api/networkservice/connection_event_helpers.go | 4 ++-- pkg/api/networkservice/connection_helpers.go | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/api/networkservice/connection_event_helpers.go b/pkg/api/networkservice/connection_event_helpers.go index b527bb6..dfe18a3 100644 --- a/pkg/api/networkservice/connection_event_helpers.go +++ b/pkg/api/networkservice/connection_event_helpers.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Cisco and/or its affiliates. +// Copyright (c) 2020-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -26,7 +26,7 @@ func (x *ConnectionEvent) Clone() *ConnectionEvent { // GetEventSenderName returns name of the segment from the path who send the event func (x *ConnectionEvent) GetEventSenderName() string { for v, k := range x.GetConnections() { - if segment := k.GetPathSegmentById(v); segment != nil { + if segment := k.GetPathSegmentByID(v); segment != nil { return segment.GetName() } } diff --git a/pkg/api/networkservice/connection_helpers.go b/pkg/api/networkservice/connection_helpers.go index ad028d7..17230e0 100644 --- a/pkg/api/networkservice/connection_helpers.go +++ b/pkg/api/networkservice/connection_helpers.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Doc.ai and/or its affiliates. // +// Copyright (c) 2023 Cisco and/or its affiliates. +// // SPDX-License-Identifier: Apache-2.0 // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -160,8 +162,8 @@ func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) return false } -// GetPathSegmentById gets the path segment by passed id -func (x *Connection) GetPathSegmentById(id string) *PathSegment { +// GetPathSegmentByID gets the path segment by passed id +func (x *Connection) GetPathSegmentByID(id string) *PathSegment { for _, segment := range x.GetPath().GetPathSegments() { if segment.GetId() == id { return segment