RICPLT-3014 Subscription multiple endpoints
[ric-plt/submgr.git] / pkg / control / types.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package control
21
22 import (
23         "bytes"
24         "fmt"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "strconv"
27         "strings"
28 )
29
30 //-----------------------------------------------------------------------------
31 //
32 //-----------------------------------------------------------------------------
33 type RmrDatagram struct {
34         MessageType    int
35         SubscriptionId uint16
36         Payload        []byte
37 }
38
39 //-----------------------------------------------------------------------------
40 //
41 //-----------------------------------------------------------------------------
42 type RmrEndpoint struct {
43         Addr string // xapp addr
44         Port uint16 // xapp port
45 }
46
47 func (endpoint RmrEndpoint) String() string {
48         return endpoint.Get()
49 }
50
51 func (endpoint *RmrEndpoint) GetAddr() string {
52         return endpoint.Addr
53 }
54
55 func (endpoint *RmrEndpoint) GetPort() uint16 {
56         return endpoint.Port
57 }
58
59 func (endpoint *RmrEndpoint) Get() string {
60         return endpoint.Addr + ":" + strconv.FormatUint(uint64(endpoint.Port), 10)
61 }
62
63 func (endpoint *RmrEndpoint) Set(src string) bool {
64         elems := strings.Split(src, ":")
65         if len(elems) == 2 {
66                 srcAddr := elems[0]
67                 srcPort, err := strconv.ParseUint(elems[1], 10, 16)
68                 if err == nil {
69                         endpoint.Addr = srcAddr
70                         endpoint.Port = uint16(srcPort)
71                         return true
72                 }
73         }
74         return false
75 }
76
77 //-----------------------------------------------------------------------------
78 //
79 //-----------------------------------------------------------------------------
80 type RmrEndpointList struct {
81         Endpoints []RmrEndpoint
82 }
83
84 func (eplist *RmrEndpointList) String() string {
85         valuesText := []string{}
86         for i := range eplist.Endpoints {
87                 ep := eplist.Endpoints[i]
88                 text := ep.String()
89                 valuesText = append(valuesText, text)
90         }
91         return strings.Join(valuesText, ",")
92 }
93
94 func (eplist *RmrEndpointList) Size() int {
95         return len(eplist.Endpoints)
96 }
97
98 func (eplist *RmrEndpointList) AddEndpoint(ep *RmrEndpoint) bool {
99         for i := range eplist.Endpoints {
100                 if (eplist.Endpoints[i].Addr == ep.Addr) && (eplist.Endpoints[i].Port == ep.Port) {
101                         return false
102                 }
103         }
104         eplist.Endpoints = append(eplist.Endpoints, *ep)
105         return true
106 }
107
108 func (eplist *RmrEndpointList) DelEndpoint(ep *RmrEndpoint) bool {
109         for i := range eplist.Endpoints {
110                 if (eplist.Endpoints[i].Addr == ep.Addr) && (eplist.Endpoints[i].Port == ep.Port) {
111                         eplist.Endpoints[i] = eplist.Endpoints[len(eplist.Endpoints)-1]
112                         eplist.Endpoints[len(eplist.Endpoints)-1] = RmrEndpoint{"", 0}
113                         eplist.Endpoints = eplist.Endpoints[:len(eplist.Endpoints)-1]
114                         return true
115                 }
116         }
117         return false
118 }
119
120 func (eplist *RmrEndpointList) DelEndpoints(otheplist *RmrEndpointList) bool {
121         var retval bool = false
122         for i := range otheplist.Endpoints {
123                 if eplist.DelEndpoint(&eplist.Endpoints[i]) {
124                         retval = true
125                 }
126         }
127         return retval
128 }
129
130 func (eplist *RmrEndpointList) HasEndpoint(ep *RmrEndpoint) bool {
131         for i := range eplist.Endpoints {
132                 if (eplist.Endpoints[i].Addr == ep.Addr) && (eplist.Endpoints[i].Port == ep.Port) {
133                         return true
134                 }
135         }
136         return false
137 }
138
139 func NewRmrEndpoint(src string) *RmrEndpoint {
140         ep := &RmrEndpoint{}
141         if ep.Set(src) == false {
142                 return nil
143         }
144         return ep
145 }
146
147 //-----------------------------------------------------------------------------
148 //
149 //-----------------------------------------------------------------------------
150 type Action int
151
152 func (act Action) String() string {
153         actions := [...]string{
154                 "CREATE",
155                 "MERGE",
156                 "NONE",
157                 "DELETE",
158         }
159
160         if act < CREATE || act > DELETE {
161                 return "UNKNOWN"
162         }
163         return actions[act]
164 }
165
166 //-----------------------------------------------------------------------------
167 // To add own method for rmrparams
168 //-----------------------------------------------------------------------------
169 type RMRParams struct {
170         *xapp.RMRParams
171 }
172
173 func (params *RMRParams) String() string {
174         var b bytes.Buffer
175         fmt.Fprintf(&b, "params(Src=%s Mtype=%s(%d) SubId=%v Xid=%s Meid=%s)", params.Src, xapp.RicMessageTypeToName[params.Mtype], params.Mtype, params.SubId, params.Xid, params.Meid.RanName)
176         return b.String()
177 }