Skip to content

Commit

Permalink
Delete unused helper functions (#152)
Browse files Browse the repository at this point in the history
* delete some helper functions

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* delete more hepler functions

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* delete more unused helper function

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix linter

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* return som e helper functions

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix CI

Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>

---------

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik authored Aug 14, 2024
1 parent 69c7ba9 commit 03f6633
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 284 deletions.
95 changes: 3 additions & 92 deletions pkg/api/networkservice/connection_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()]
Expand Down
67 changes: 3 additions & 64 deletions pkg/api/networkservice/connectioncontext_helpers.go
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 {
Expand Down
41 changes: 1 addition & 40 deletions pkg/api/networkservice/helpers.go
Original file line number Diff line number Diff line change
@@ -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
//
Expand All @@ -17,7 +17,6 @@
package networkservice

import (
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)

Expand All @@ -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))
Expand All @@ -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 {
Expand Down
28 changes: 2 additions & 26 deletions pkg/api/networkservice/ipcontext_helpers.go
Original file line number Diff line number Diff line change
@@ -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
//
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
29 changes: 1 addition & 28 deletions pkg/api/networkservice/mechanism_helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2020 Cisco Systems, Inc.
// Copyright (c) 2018-2024 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -17,9 +17,6 @@
package networkservice

import (
"sync"

"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand All @@ -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
}
Loading

0 comments on commit 03f6633

Please sign in to comment.