00674c71023de37ba3e11dc118b9a0508727c64a
[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 SubRouteInfo struct {
43         Command Action
44         Address string
45         Port    uint16
46         SubID   uint16
47 }
48
49 //-----------------------------------------------------------------------------
50 //
51 //-----------------------------------------------------------------------------
52 type RmrEndpoint struct {
53         Addr string // xapp addr
54         Port uint16 // xapp port
55 }
56
57 func (endpoint RmrEndpoint) String() string {
58         return endpoint.Get()
59 }
60
61 func (endpoint *RmrEndpoint) GetAddr() string {
62         return endpoint.Addr
63 }
64
65 func (endpoint *RmrEndpoint) GetPort() uint16 {
66         return endpoint.Port
67 }
68
69 func (endpoint *RmrEndpoint) Get() string {
70         return endpoint.Addr + ":" + strconv.FormatUint(uint64(endpoint.Port), 10)
71 }
72
73 func (endpoint *RmrEndpoint) Set(src string) bool {
74         elems := strings.Split(src, ":")
75         if len(elems) == 2 {
76                 srcAddr := elems[0]
77                 srcPort, err := strconv.ParseUint(elems[1], 10, 16)
78                 if err == nil {
79                         endpoint.Addr = srcAddr
80                         endpoint.Port = uint16(srcPort)
81                         return true
82                 }
83         }
84         return false
85 }
86
87 func NewRmrEndpoint(src string) *RmrEndpoint {
88         ep := &RmrEndpoint{}
89         if ep.Set(src) == false {
90                 return nil
91         }
92         return ep
93 }
94
95 //-----------------------------------------------------------------------------
96 //
97 //-----------------------------------------------------------------------------
98 type Action int
99
100 func (act Action) String() string {
101         actions := [...]string{
102                 "CREATE",
103                 "MERGE",
104                 "NONE",
105                 "DELETE",
106         }
107
108         if act < CREATE || act > DELETE {
109                 return "UNKNOWN"
110         }
111         return actions[act]
112 }
113
114 //-----------------------------------------------------------------------------
115 // To add own method for rmrparams
116 //-----------------------------------------------------------------------------
117 type RMRParams struct {
118         *xapp.RMRParams
119 }
120
121 func (params *RMRParams) String() string {
122         var b bytes.Buffer
123         fmt.Fprintf(&b, "Src=%s Mtype=%s(%d) SubId=%v Xid=%s Meid=%v", params.Src, xapp.RicMessageTypeToName[params.Mtype], params.Mtype, params.SubId, params.Xid, params.Meid)
124         return b.String()
125 }