From 03f6633c26778b7a5fe9cece5ffd46d57f8975dc Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik <93182827+NikitaSkrynnik@users.noreply.github.com> Date: Wed, 14 Aug 2024 20:39:07 +1100 Subject: [PATCH] Delete unused helper functions (#152) * delete some helper functions Signed-off-by: Nikita Skrynnik * delete more hepler functions Signed-off-by: Nikita Skrynnik * delete more unused helper function Signed-off-by: Nikita Skrynnik * fix linter Signed-off-by: Nikita Skrynnik * return som e helper functions Signed-off-by: Nikita Skrynnik * fix CI Signed-off-by: NikitaSkrynnik --------- Signed-off-by: Nikita Skrynnik Signed-off-by: NikitaSkrynnik --- pkg/api/networkservice/connection_helpers.go | 95 +------------------ .../connectioncontext_helpers.go | 67 +------------ pkg/api/networkservice/helpers.go | 41 +------- pkg/api/networkservice/ipcontext_helpers.go | 28 +----- pkg/api/networkservice/mechanism_helpers.go | 29 +----- .../mechanisms/kernel/helpers.go | 22 +---- pkg/api/networkservice/path_helpers.go | 14 +-- 7 files changed, 12 insertions(+), 284 deletions(-) diff --git a/pkg/api/networkservice/connection_helpers.go b/pkg/api/networkservice/connection_helpers.go index 98ee2b7..c37ef85 100644 --- a/pkg/api/networkservice/connection_helpers.go +++ b/pkg/api/networkservice/connection_helpers.go @@ -2,7 +2,7 @@ // // Copyright (c) 2021 Doc.ai and/or its affiliates. // -// Copyright (c) 2023 Cisco and/or its affiliates. +// Copyright (c) 2023-2024 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,42 +21,10 @@ package networkservice import ( - "github.com/pkg/errors" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" ) -// IsRemote returns if connection is remote -func (x *Connection) IsRemote() bool { - if x == nil { - return false - } - // If we have two or more, it is remote - return len(x.GetPath().GetPathSegments()) > 1 -} - -// GetSourceNetworkServiceManagerName - return source network service manager name -func (x *Connection) GetSourceNetworkServiceManagerName() string { - if x == nil { - return "" - } - if len(x.GetPath().GetPathSegments()) > 0 { - return x.GetPath().GetPathSegments()[0].GetName() - } - return "" -} - -// GetDestinationNetworkServiceManagerName - return destination network service manager name -func (x *Connection) GetDestinationNetworkServiceManagerName() string { - if x == nil { - return "" - } - if len(x.GetPath().GetPathSegments()) >= 2 { - return x.GetPath().GetPathSegments()[1].GetName() - } - return "" -} - // Equals returns if connection equals given connection func (x *Connection) Equals(connection protoreflect.ProtoMessage) bool { // use as proto.Message @@ -69,64 +37,7 @@ func (x *Connection) Clone() *Connection { return proto.Clone(x).(*Connection) } -// UpdateContext checks and tries to set connection context -func (x *Connection) UpdateContext(context *ConnectionContext) error { - if err := context.MeetsRequirements(x.Context); err != nil { - return err - } - - oldContext := x.Context - x.Context = context - - if err := x.IsValid(); err != nil { - x.Context = oldContext - return err - } - - return nil -} - -// IsValid checks if connection is minimally valid -func (x *Connection) IsValid() error { - if x == nil { - return errors.New("connection cannot be nil") - } - - if x.GetNetworkService() == "" { - return errors.Errorf("NetworkService cannot be empty: %v", x) - } - - if x.GetMechanism() != nil { - if err := x.GetMechanism().IsValid(); err != nil { - return errors.Wrapf(err, "invalid Mechanism in %v", x) - } - } - - if err := x.GetPath().IsValid(); err != nil { - return err - } - - return nil -} - -// IsComplete checks if connection is complete valid -func (x *Connection) IsComplete() error { - if err := x.IsValid(); err != nil { - return err - } - - if x.GetId() == "" { - return errors.Errorf("Id cannot be empty: %v", x) - } - - if err := x.GetContext().IsValid(); err != nil { - return err - } - - return nil -} - -// MatchesMonitorScopeSelector - Returns true if the connection matches the selector +// MatchesMonitorScopeSelector - Returns true of the connection matches the selector func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) bool { if x == nil { return false @@ -179,7 +90,7 @@ func (x *Connection) GetCurrentPathSegment() *PathSegment { if len(x.GetPath().GetPathSegments()) == 0 { return nil } - if len(x.GetPath().GetPathSegments())-1 < int(x.GetPath().GetIndex()) { + if int(x.GetPath().GetIndex()) > len(x.GetPath().GetPathSegments())-1 { return nil } return x.GetPath().GetPathSegments()[x.GetPath().GetIndex()] diff --git a/pkg/api/networkservice/connectioncontext_helpers.go b/pkg/api/networkservice/connectioncontext_helpers.go index 2a2f15a..f72ed96 100644 --- a/pkg/api/networkservice/connectioncontext_helpers.go +++ b/pkg/api/networkservice/connectioncontext_helpers.go @@ -1,6 +1,9 @@ // Copyright (c) 2020-2022 Cisco and/or its affiliates. +// // Copyright (c) 2022 Nordix Foundation // +// Copyright (c) 2024 Cisco and/or its affiliates. +// // SPDX-License-Identifier: Apache-2.0 // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,76 +21,12 @@ package networkservice import ( - "net" "strconv" "strings" "github.com/pkg/errors" ) -// IsEthernetContextEmtpy returns true if ethernet config is empty -func (x *ConnectionContext) IsEthernetContextEmtpy() bool { - return x.EthernetContext == nil || (x.EthernetContext.SrcMac == "" && x.EthernetContext.DstMac == "") -} - -// IsValid - checks ConnectionContext validation -func (x *ConnectionContext) IsValid() error { - if x == nil { - return errors.New("ConnectionContext should not be nil") - } - ip := x.GetIpContext() - for _, route := range append(ip.GetSrcRoutes(), ip.GetDstRoutes()...) { - if route.GetPrefix() == "" { - return errors.Errorf("ConnectionContext.Route.Prefix is required and cannot be empty/nil: %v", ip) - } - _, _, err := net.ParseCIDR(route.GetPrefix()) - if err != nil { - return errors.Errorf("ConnectionContext.Route.Prefix should be a valid CIDR address: %v", ip) - } - } - - for _, neighbor := range ip.GetIpNeighbors() { - if neighbor.GetIp() == "" { - return errors.Errorf("ConnectionContext.IpNeighbors.Ip is required and cannot be empty/nil: %v", ip) - } - if neighbor.GetHardwareAddress() == "" { - return errors.Errorf("ConnectionContext.IpNeighbors.HardwareAddress is required and cannot be empty/nil: %v", ip) - } - } - return nil -} - -// MeetsRequirements - checks required context parameters have bin set -func (x *ConnectionContext) MeetsRequirements(original *ConnectionContext) error { - if x == nil { - return errors.New("ConnectionContext should not be nil") - } - - err := x.IsValid() - if err != nil { - return err - } - if original.GetIpContext().GetDstIpRequired() && len(x.GetIpContext().GetDstIpAddrs()) > 0 { - return errors.Errorf("ConnectionContext.DestIp is required and cannot be empty/nil: %v", x) - } - if original.GetIpContext().GetSrcIpRequired() && len(x.GetIpContext().GetSrcIpAddrs()) > 0 { - return errors.Errorf("ConnectionContext.SrcIp is required cannot be empty/nil: %v", x) - } - - return nil -} - -// Validate - checks DNSConfig and returns error if DNSConfig is not valid -func (c *DNSConfig) Validate() error { - if c == nil { - return errors.New(DNSConfigShouldNotBeNil) - } - if len(c.DnsServerIps) == 0 { - return errors.New(DNSServerIpsShouldHaveRecords) - } - return nil -} - // IsValid - checks ExtraPrefixRequest validation func (c *ExtraPrefixRequest) IsValid() error { if c == nil { diff --git a/pkg/api/networkservice/helpers.go b/pkg/api/networkservice/helpers.go index e1417bd..0de2165 100644 --- a/pkg/api/networkservice/helpers.go +++ b/pkg/api/networkservice/helpers.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Cisco and/or its affiliates. +// Copyright (c) 2020-2024 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -17,7 +17,6 @@ package networkservice import ( - "github.com/pkg/errors" "google.golang.org/protobuf/proto" ) @@ -31,14 +30,6 @@ func (x *NetworkServiceRequest) GetRequestConnection() *Connection { return x.GetConnection() } -// SetRequestConnection sets request connection -func (x *NetworkServiceRequest) SetRequestConnection(conn *Connection) *NetworkServiceRequest { - if x != nil { - x.Connection = conn - } - return x -} - // GetRequestMechanismPreferences returns request mechanism preferences func (x *NetworkServiceRequest) GetRequestMechanismPreferences() []*Mechanism { preferences := make([]*Mechanism, 0, len(x.MechanismPreferences)) @@ -47,36 +38,6 @@ func (x *NetworkServiceRequest) GetRequestMechanismPreferences() []*Mechanism { return preferences } -// SetRequestMechanismPreferences sets request mechanism preferences -func (x *NetworkServiceRequest) SetRequestMechanismPreferences(mechanismPreferences []*Mechanism) { - x.MechanismPreferences = mechanismPreferences -} - -// IsValid returns if request is valid -func (x *NetworkServiceRequest) IsValid() error { - if x == nil { - return errors.New("request cannot be nil") - } - - if x.GetConnection() == nil { - return errors.Errorf("request.Connection cannot be nil %v", x) - } - - if err := x.GetConnection().IsValid(); err != nil { - return errors.Errorf("request.Connection is invalid: %s: %v", err, x) - } - - if x.GetMechanismPreferences() == nil { - return errors.Errorf("request.MechanismPreferences cannot be nil: %v", x) - } - - if len(x.GetMechanismPreferences()) < 1 { - return errors.Errorf("request.MechanismPreferences must have at least one entry: %v", x) - } - - return nil -} - // ServiceNames - returns grpc ServiceNames implemented by impl func ServiceNames(impl interface{}, existingServiceNames ...string) []string { if _, ok := impl.(NetworkServiceServer); ok { diff --git a/pkg/api/networkservice/ipcontext_helpers.go b/pkg/api/networkservice/ipcontext_helpers.go index 8db408b..617f659 100644 --- a/pkg/api/networkservice/ipcontext_helpers.go +++ b/pkg/api/networkservice/ipcontext_helpers.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 Cisco and/or its affiliates. +// Copyright (c) 2020-2024 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -133,29 +133,6 @@ func contains(prefixes []*net.IPNet, ip net.IP) bool { return false } -// GetExcludedPrefixesIPNet - GetExcludedPrefixes() converted to []*net.IPNet prefixes that are empty or cannot be parsed are omitted -func (i *IPContext) GetExcludedPrefixesIPNet() []*net.IPNet { - var prefixes []*net.IPNet - for _, prefixStr := range i.GetExcludedPrefixes() { - prefixes = append(prefixes, strToIPNet(prefixStr)) - } - return prefixes -} - -// GetGetExtraPrefixesIPNet - GetExtraPrefixes() converted to []*net.IPNet prefixes that are empty or cannot be parsed are omitted -func (i *IPContext) GetGetExtraPrefixesIPNet() []*net.IPNet { - var prefixes []*net.IPNet - for _, prefixStr := range i.GetExtraPrefixes() { - prefixes = append(prefixes, strToIPNet(prefixStr)) - } - return prefixes -} - -// GetIP - GetIp() - converted to *net.IP or nil if empty or cannot be parsed -func (n *IpNeighbor) GetIP() net.IP { - return net.ParseIP(n.GetIp()) -} - // GetPrefixIPNet - GetPrefix() converted to *net.IPNet or nil if empty or cannot be parsed func (r *Route) GetPrefixIPNet() *net.IPNet { return strToIPNet(r.GetPrefix()) @@ -185,10 +162,9 @@ func strToIPNet(in string) *net.IPNet { if in == "" { return nil } - ip, ipNet, err := net.ParseCIDR(in) + _, ipNet, err := net.ParseCIDR(in) if err != nil { return nil } - ipNet.IP = ip return ipNet } diff --git a/pkg/api/networkservice/mechanism_helpers.go b/pkg/api/networkservice/mechanism_helpers.go index 489f241..08af67c 100644 --- a/pkg/api/networkservice/mechanism_helpers.go +++ b/pkg/api/networkservice/mechanism_helpers.go @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2020 Cisco Systems, Inc. +// Copyright (c) 2018-2024 Cisco Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -17,9 +17,6 @@ package networkservice import ( - "sync" - - "github.com/pkg/errors" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -34,27 +31,3 @@ func (x *Mechanism) Equals(mechanism protoreflect.ProtoMessage) bool { func (x *Mechanism) Clone() *Mechanism { return proto.Clone(x).(*Mechanism) } - -var mechanismValidators map[string]func(*Mechanism) error -var mechanismValidatorsMutex sync.Mutex - -// AddMechanism adds a Mechanism -func AddMechanism(mtype string, validator func(*Mechanism) error) { - mechanismValidatorsMutex.Lock() - defer mechanismValidatorsMutex.Unlock() - mechanismValidators[mtype] = validator -} - -// IsValid - is the Mechanism Valid? -func (x *Mechanism) IsValid() error { - if x == nil { - return errors.New("mechanism cannot be nil") - } - validator, ok := mechanismValidators[x.GetType()] - if ok { - return validator(x) - } - // NOTE: this means that we intentionally decide that Mechanisms are valid - // unless we have a Validator that says otherwise - return nil -} diff --git a/pkg/api/networkservice/mechanisms/kernel/helpers.go b/pkg/api/networkservice/mechanisms/kernel/helpers.go index dab5aea..1565208 100644 --- a/pkg/api/networkservice/mechanisms/kernel/helpers.go +++ b/pkg/api/networkservice/mechanisms/kernel/helpers.go @@ -2,7 +2,7 @@ // // Copyright (c) 2021 Doc.ai and/or its affiliates. // -// Copyright (c) 2022 Xored Software Inc and others. +// Copyright (c) 2022-2024 Xored Software Inc and others. // // SPDX-License-Identifier: Apache-2.0 // @@ -71,31 +71,11 @@ func (m *Mechanism) GetParameters() map[string]string { return m.Parameters } -// GetNetNSInode returns the NetNS inode -func (m *Mechanism) GetNetNSInode() string { - return m.GetParameters()[NetNSInodeKey] -} - -// SetNetNSInode sets the NetNS inode -func (m *Mechanism) SetNetNSInode(netNSInode string) { - m.GetParameters()[NetNSInodeKey] = netNSInode -} - // GetPCIAddress returns the PCI address of the device func (m *Mechanism) GetPCIAddress() string { return m.GetParameters()[PCIAddressKey] } -// SetPCIAddress sets the PCI address of the device -func (m *Mechanism) SetPCIAddress(pciAddress string) { - m.GetParameters()[PCIAddressKey] = pciAddress -} - -// IsPCIDevice returns if this mechanism is for a PCI device -func (m *Mechanism) IsPCIDevice() bool { - return m.GetPCIAddress() != "" -} - // GetDeviceTokenID returns device token ID func (m *Mechanism) GetDeviceTokenID() string { return m.Parameters[DeviceTokenIDKey] diff --git a/pkg/api/networkservice/path_helpers.go b/pkg/api/networkservice/path_helpers.go index 4118202..6264418 100644 --- a/pkg/api/networkservice/path_helpers.go +++ b/pkg/api/networkservice/path_helpers.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020 Cisco Systems, Inc. +// Copyright (c) 2020-2024 Cisco Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -17,7 +17,6 @@ package networkservice import ( - "github.com/pkg/errors" "google.golang.org/protobuf/proto" ) @@ -25,14 +24,3 @@ import ( func (x *Path) Clone() *Path { return proto.Clone(x).(*Path) } - -// IsValid returns true if Path p is Valid -func (x *Path) IsValid() error { - if x == nil { - return nil - } - if int(x.GetIndex()) >= len(x.GetPathSegments()) { - return errors.New("Path.Index >= len(Path.PathSegments)") - } - return nil -}