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