RICPLT-3014 Subs multiple rmr 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) HasEndpoint(ep *RmrEndpoint) bool {
95         for i := range eplist.Endpoints {
96                 if (eplist.Endpoints[i].Addr == ep.Addr) && (eplist.Endpoints[i].Port == ep.Port) {
97                         return true
98                 }
99         }
100         return false
101 }
102
103 func NewRmrEndpoint(src string) *RmrEndpoint {
104         ep := &RmrEndpoint{}
105         if ep.Set(src) == false {
106                 return nil
107         }
108         return ep
109 }
110
111 //-----------------------------------------------------------------------------
112 //
113 //-----------------------------------------------------------------------------
114 type Action int
115
116 func (act Action) String() string {
117         actions := [...]string{
118                 "CREATE",
119                 "MERGE",
120                 "NONE",
121                 "DELETE",
122         }
123
124         if act < CREATE || act > DELETE {
125                 return "UNKNOWN"
126         }
127         return actions[act]
128 }
129
130 //-----------------------------------------------------------------------------
131 // To add own method for rmrparams
132 //-----------------------------------------------------------------------------
133 type RMRParams struct {
134         *xapp.RMRParams
135 }
136
137 func (params *RMRParams) String() string {
138         var b bytes.Buffer
139         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)
140         return b.String()
141 }