e992cc39d985365a0e835f6784b6cba887bd9cb9
[ric-plt/resource-status-manager.git] / RSM / rmrcgo / rmr_c_go_types.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 package rmrcgo
19
20 // #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
21 // #include <rmr/rmr.h>
22 // #include <rmr/RIC_message_types.h>
23 // #include <stdlib.h>
24 import "C"
25 import (
26         "fmt"
27         "rsm/logger"
28         "unsafe"
29 )
30
31 func NewMBuf(mType int, len int, meid string, payload *[]byte, xAction *[]byte) *MBuf {
32         return &MBuf{
33                 MType:   mType,
34                 Len:     len,
35                 Meid:    meid,
36                 Payload: payload,
37                 XAction: xAction,
38         }
39 }
40
41 func NewContext(maxMsgSize int, flags int, ctx unsafe.Pointer, logger *logger.Logger) *Context {
42         return &Context{
43                 MaxMsgSize: maxMsgSize,
44                 Flags:      flags,
45                 RmrCtx:     ctx,
46                 Logger:     logger,
47         }
48 }
49
50 //TODO: consider declaring using its own type
51 const (
52         // messages
53         RanConnected        = C.RAN_CONNECTED
54         RanRestarted        = C.RAN_RESTARTED
55         RanReconfigured     = C.RAN_RECONFIGURED
56         RicResStatusResp    = C.RIC_RES_STATUS_RESP
57         RicResStatusFailure = C.RIC_RES_STATUS_FAILURE
58         RicResStatusReq     = C.RIC_RES_STATUS_REQ
59 )
60
61 const (
62         RMR_MAX_XACTION_LEN = int(C.RMR_MAX_XID)
63         RMR_MAX_MEID_LEN    = int(C.RMR_MAX_MEID)
64
65         //states
66         RMR_OK             = C.RMR_OK
67         RMR_ERR_BADARG     = C.RMR_ERR_BADARG
68         RMR_ERR_NOENDPT    = C.RMR_ERR_NOENDPT
69         RMR_ERR_EMPTY      = C.RMR_ERR_EMPTY
70         RMR_ERR_NOHDR      = C.RMR_ERR_NOHDR
71         RMR_ERR_SENDFAILED = C.RMR_ERR_SENDFAILED
72         RMR_ERR_CALLFAILED = C.RMR_ERR_CALLFAILED
73         RMR_ERR_NOWHOPEN   = C.RMR_ERR_NOWHOPEN
74         RMR_ERR_WHID       = C.RMR_ERR_WHID
75         RMR_ERR_OVERFLOW   = C.RMR_ERR_OVERFLOW
76         RMR_ERR_RETRY      = C.RMR_ERR_RETRY
77         RMR_ERR_RCVFAILED  = C.RMR_ERR_RCVFAILED
78         RMR_ERR_TIMEOUT    = C.RMR_ERR_TIMEOUT
79         RMR_ERR_UNSET      = C.RMR_ERR_UNSET
80         RMR_ERR_TRUNC      = C.RMR_ERR_TRUNC
81         RMR_ERR_INITFAILED = C.RMR_ERR_INITFAILED
82 )
83
84 var states = map[int]string{
85         RMR_OK:             "state is good",
86         RMR_ERR_BADARG:     "argument passd to function was unusable",
87         RMR_ERR_NOENDPT:    "send/call could not find an endpoint based on msg type",
88         RMR_ERR_EMPTY:      "msg received had no payload; attempt to send an empty message",
89         RMR_ERR_NOHDR:      "message didn't contain a valid header",
90         RMR_ERR_SENDFAILED: "send failed; errno has nano reason",
91         RMR_ERR_CALLFAILED: "unable to send call() message",
92         RMR_ERR_NOWHOPEN:   "no wormholes are open",
93         RMR_ERR_WHID:       "wormhole id was invalid",
94         RMR_ERR_OVERFLOW:   "operation would have busted through a buffer/field size",
95         RMR_ERR_RETRY:      "request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)",
96         RMR_ERR_RCVFAILED:  "receive failed (hard error)",
97         RMR_ERR_TIMEOUT:    "message processing call timed out",
98         RMR_ERR_UNSET:      "the message hasn't been populated with a transport buffer",
99         RMR_ERR_TRUNC:      "received message likely truncated",
100         RMR_ERR_INITFAILED: "initialisation of something (probably message) failed",
101 }
102
103 type MBuf struct {
104         MType   int
105         Len     int
106         Meid    string //Managed entity id (RAN name)
107         Payload *[]byte
108         XAction *[]byte
109 }
110
111 func (m MBuf) String() string {
112         return fmt.Sprintf("{ MType: %d, Len: %d, Meid: %q, Xaction: %q, Payload: [%x] }", m.MType, m.Len, m.Meid, m.XAction, m.Payload)
113 }
114
115 type Context struct {
116         MaxMsgSize int
117         Flags      int
118         RmrCtx     unsafe.Pointer
119         Logger     *logger.Logger
120 }
121
122 type RmrMessenger interface {
123         Init(readyIntervalSec int, port string, maxMsgSize int, flags int, logger *logger.Logger) RmrMessenger
124         SendMsg(msg *MBuf) (*MBuf, error)
125         RecvMsg() (*MBuf, error)
126         IsReady() bool
127         Close()
128 }