forked from cisco-app-networking/networkservicemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
114 lines (95 loc) · 2.51 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package crossconnect
import (
"github.com/pkg/errors"
"cisco-app-networking.github.io/networkservicemesh/controlplane/api/connection"
)
// NewCrossConnect creates a new crossConnect
func NewCrossConnect(id, payload string, src, dst *connection.Connection) *CrossConnect {
c := &CrossConnect{
Id: id,
Payload: payload,
Source: src,
Destination: dst,
}
return c
}
// IsValid checks if crossConnect is minimally valid
func (c *CrossConnect) IsValid() error {
if c == nil {
return errors.New("crossConnect cannot be nil")
}
if c.GetId() == "" {
return errors.Errorf("crossConnect.Id cannot be empty: %v", c)
}
src := c.GetSource()
if src == nil {
return errors.Errorf("crossConnect.Source cannot be nil: %v", c)
}
if err := src.IsValid(); err != nil {
return errors.Wrapf(err, "crossConnect.Source %v invalid", c)
}
dst := c.GetDestination()
if dst == nil {
return errors.Errorf("crossConnect.Destination cannot be nil: %v", c)
}
if err := dst.IsValid(); err != nil {
return errors.Wrapf(err, "crossConnect.Destination %v invalid", c)
}
if c.GetPayload() == "" {
return errors.Errorf("crossConnect.Payload cannot be empty: %v", c)
}
return nil
}
// IsComplete checks if crossConnect is complete valid
func (c *CrossConnect) IsComplete() error {
if err := c.IsValid(); err != nil {
return err
}
if err := c.GetSource().IsComplete(); err != nil {
return errors.Wrapf(err, "crossConnect.Source %v invalid", c)
}
if err := c.GetDestination().IsComplete(); err != nil {
return errors.Wrapf(err, "crossConnect.Destination %v invalid", c)
}
return nil
}
// GetLocalSource - return a source and check if it is local
func (c *CrossConnect) GetLocalSource() *connection.Connection {
if c == nil {
return nil
}
if c.Source.IsRemote() {
return nil
}
return c.Source
}
// GetRemoteSource - return a source and check if it is remote
func (c *CrossConnect) GetRemoteSource() *connection.Connection {
if c == nil {
return nil
}
if !c.Source.IsRemote() {
return nil
}
return c.Source
}
// GetLocalDestination - return a destination and check if it is local
func (c *CrossConnect) GetLocalDestination() *connection.Connection {
if c == nil {
return nil
}
if c.Destination.IsRemote() {
return nil
}
return c.Destination
}
// GetRemoteDestination - return a destination and check if it is remote
func (c *CrossConnect) GetRemoteDestination() *connection.Connection {
if c == nil {
return nil
}
if !c.Destination.IsRemote() {
return nil
}
return c.Destination
}