RIC-79 intial implementation to fetch subscriptions via rest
[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         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
24         "strconv"
25         "strings"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type RequestId struct {
32         e2ap.RequestId
33 }
34
35 func (rid *RequestId) String() string {
36         return "reqid(" + rid.RequestId.String() + ")"
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.Addr + ":" + strconv.FormatUint(uint64(endpoint.Port), 10)
49 }
50
51 func (endpoint *RmrEndpoint) Equal(ep *RmrEndpoint) bool {
52         if (endpoint.Addr == ep.Addr) &&
53                 (endpoint.Port == ep.Port) {
54                 return true
55         }
56         return false
57 }
58
59 func (endpoint *RmrEndpoint) GetAddr() string {
60         return endpoint.Addr
61 }
62
63 func (endpoint *RmrEndpoint) GetPort() uint16 {
64         return endpoint.Port
65 }
66
67 func (endpoint *RmrEndpoint) Set(src string) bool {
68         elems := strings.Split(src, ":")
69         if len(elems) == 2 {
70                 srcAddr := elems[0]
71                 srcPort, err := strconv.ParseUint(elems[1], 10, 16)
72                 if err == nil {
73                         endpoint.Addr = srcAddr
74                         endpoint.Port = uint16(srcPort)
75                         return true
76                 }
77         }
78         return false
79 }
80
81 //-----------------------------------------------------------------------------
82 //
83 //-----------------------------------------------------------------------------
84 type RmrEndpointList struct {
85         Endpoints []RmrEndpoint
86 }
87
88 func (eplist *RmrEndpointList) String() string {
89         valuesText := eplist.StringList()
90         return strings.Join(valuesText, ",")
91 }
92
93 func (eplist *RmrEndpointList) StringList() []string {
94         tmpList := eplist.Endpoints
95         valuesText := []string{}
96         for i := range tmpList {
97                 valuesText = append(valuesText, tmpList[i].String())
98         }
99         return valuesText
100 }
101
102 func (eplist *RmrEndpointList) Size() int {
103         return len(eplist.Endpoints)
104 }
105
106 func (eplist *RmrEndpointList) AddEndpoint(ep *RmrEndpoint) bool {
107         for i := range eplist.Endpoints {
108                 if eplist.Endpoints[i].Equal(ep) {
109                         return false
110                 }
111         }
112         eplist.Endpoints = append(eplist.Endpoints, *ep)
113         return true
114 }
115
116 func (eplist *RmrEndpointList) DelEndpoint(ep *RmrEndpoint) bool {
117         for i := range eplist.Endpoints {
118                 if eplist.Endpoints[i].Equal(ep) {
119                         eplist.Endpoints[i] = eplist.Endpoints[len(eplist.Endpoints)-1]
120                         eplist.Endpoints[len(eplist.Endpoints)-1] = RmrEndpoint{"", 0}
121                         eplist.Endpoints = eplist.Endpoints[:len(eplist.Endpoints)-1]
122                         return true
123                 }
124         }
125         return false
126 }
127
128 func (eplist *RmrEndpointList) DelEndpoints(otheplist *RmrEndpointList) bool {
129         var retval bool = false
130         for i := range otheplist.Endpoints {
131                 if eplist.DelEndpoint(&otheplist.Endpoints[i]) {
132                         retval = true
133                 }
134         }
135         return retval
136 }
137
138 func (eplist *RmrEndpointList) HasEndpoint(ep *RmrEndpoint) bool {
139         for i := range eplist.Endpoints {
140                 if eplist.Endpoints[i].Equal(ep) {
141                         return true
142                 }
143         }
144         return false
145 }
146
147 func NewRmrEndpoint(src string) *RmrEndpoint {
148         ep := &RmrEndpoint{}
149         if ep.Set(src) == false {
150                 return nil
151         }
152         return ep
153 }