Skip to content

Commit

Permalink
return som e helper functions
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Skrynnik <[email protected]>
  • Loading branch information
NikitaSkrynnik committed Dec 29, 2022
1 parent 134587a commit b9bfb44
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/api/networkservice/connection_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ func (x *Connection) Clone() *Connection {
return proto.Clone(x).(*Connection)
}

// MatchesMonitorScopeSelector - Returns true of the connection matches the selector
func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) bool {
if x == nil {
return false
}
// Note: Empty selector always matches a non-nil Connection
if len(selector.GetPathSegments()) == 0 {
return true
}
// Iterate through the Connection.NetworkServiceManagers array looking for a subarray that matches
// the selector.NetworkServiceManagers array, treating "" in the selector.NetworkServiceManagers array
// as a wildcard
for i := range x.GetPath().GetPathSegments() {
// If there aren't enough elements left in the Connection.NetworkServiceManagers array to match
// all of the elements in the select.NetworkServiceManager array...clearly we can't match
if i+len(selector.GetPathSegments()) > len(x.GetPath().GetPathSegments()) {
return false
}
// Iterate through the selector.NetworkServiceManagers array to see is the subarray starting at
// Connection.NetworkServiceManagers[i] matches selector.NetworkServiceManagers
for j := range selector.GetPathSegments() {
// "" matches as a wildcard... failure to match either as wildcard or exact match means the subarray
// starting at Connection.NetworkServiceManagers[i] doesn't match selectors.NetworkServiceManagers
if selector.GetPathSegments()[j].GetName() != "" && x.GetPath().GetPathSegments()[i+j].GetName() != selector.GetPathSegments()[j].GetName() {
break
}
// If this is the last element in the selector.NetworkServiceManagers array and we still are matching...
// return true
if j == len(selector.GetPathSegments())-1 {
return true
}
}
}
return false
}

// GetCurrentPathSegment - Get the current path segment of the connection
func (x *Connection) GetCurrentPathSegment() *PathSegment {
if x == nil {
Expand Down Expand Up @@ -81,3 +117,14 @@ func (x *Connection) GetNextPathSegment() *PathSegment {
}
return x.GetPath().GetPathSegments()[x.GetPath().GetIndex()+1]
}

// FilterMapOnManagerScopeSelector - Filters out of map[string]*Connection Connections not matching the selector
func FilterMapOnManagerScopeSelector(c map[string]*Connection, selector *MonitorScopeSelector) map[string]*Connection {
rv := make(map[string]*Connection)
for k, v := range c {
if v != nil && v.MatchesMonitorScopeSelector(selector) {
rv[k] = v
}
}
return rv
}

0 comments on commit b9bfb44

Please sign in to comment.