Adding scope of RICPlatform that are under Apache License
[ric-plt/rtmgr.git] / pkg / rtmgr / rtmgr.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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
19    platform project (RICP).
20
21 ==================================================================================
22 */
23 /*
24   Mnemonic:     rtmgr/rtmgr.go
25   Abstract:     Contains RTMGR (Routing Manager) module's generic variables and functions
26   Date:         26 March 2019
27 */
28
29 package rtmgr
30
31 import (
32         "encoding/json"
33         "errors"
34         "io/ioutil"
35         "os"
36         "strings"
37
38         "github.com/jcelliott/lumber"
39 )
40
41 var (
42         //TODO: temporary solution
43         // CamelCase Message Types are for being able to test with old fashioned admin control xApps
44         // TODO: Add a separate message definition file (Not using the one from RMR to not create dependency on that library).
45         MessageTypes = map[string]string{
46                 "HandoverPreparation":              "0",
47                 "HandoverCancel":                   "1",
48                 "LoadIndication":                   "2",
49                 "ErrorIndication":                  "3",
50                 "SNStatusTransfer":                 "4",
51                 "UEContextRelease":                 "5",
52                 "X2Setup":                          "6",
53                 "Reset":                            "7",
54                 "E2_TERM_INIT":                     "1100",
55                 "RAN_CONNECTED":                    "1200",
56                 "RAN_RESTARTED":                    "1210",
57                 "RAN_RECONFIGURED":                 "1220",
58                 "RIC_SCTP_CLEAR_ALL":               "1090",
59                 "RIC_SCTP_CONNECTION_FAILURE":      "1080",
60                 "RIC_X2_SETUP":                     "10000",
61                 "RIC_X2_RESPONSE":                  "10001",
62                 "RIC_X2_RESOURCE_STATUS_REQUEST":   "10002",
63                 "RIC_X2_RESOURCE_STATUS_RESPONSE":  "10003",
64                 "RIC_X2_LOAD_INFORMATION":          "10004",
65                 "RIC_E2_TERMINATION_HC_REQUEST":    "10005",
66                 "RIC_E2_TERMINATION_HC_RESPONSE":   "10006",
67                 "RIC_E2_MANAGER_HC_REQUEST":        "10007",
68                 "RIC_E2_MANAGER_HC_RESPONSE":       "10008",
69                 "RIC_ENB_LOAD_INFORMATION":         "10020",
70                 "RIC_ERROR_INDICATION":             "10030",
71                 "RIC_X2_SETUP_REQ":                 "10060",
72                 "RIC_X2_SETUP_RESP":                "10061",
73                 "RIC_X2_SETUP_FAILURE":             "10062",
74                 "RIC_X2_RESET_REQ":                 "10070",
75                 "RIC_X2_RESET_RESP":                "10071",
76                 "RIC_ENB_CONF_UPDATE":              "10080",
77                 "RIC_ENB_CONF_UPDATE_ACK":          "10081",
78                 "RIC_ENB_CONF_UPDATE_FAILURE":      "10082",
79                 "RIC_RES_STATUS_REQ":               "10090",
80                 "RIC_RES_STATUS_RESP":              "10091",
81                 "RIC_RES_STATUS_FAILURE":           "10092",
82                 "RIC_RESOURCE_STATUS_UPDATE":       "10100",
83                 "RIC_ENDC_X2_SETUP_REQ":            "10360",
84                 "RIC_ENDC_X2_SETUP_RESP":           "10361",
85                 "RIC_ENDC_X2_SETUP_FAILURE":        "10362",
86                 "RIC_ENDC_CONF_UPDATE":             "10370",
87                 "RIC_ENDC_CONF_UPDATE_ACK":         "10371",
88                 "RIC_ENDC_CONF_UPDATE_FAILURE":     "10372",
89                 "RIC_GNB_STATUS_INDICATION":        "10450",
90                 "RIC_SUB_REQ":                      "12010",
91                 "RIC_SUB_RESP":                     "12011",
92                 "RIC_SUB_FAILURE":                  "12012",
93                 "RIC_SUB_DEL_REQ":                  "12020",
94                 "RIC_SUB_DEL_RESP":                 "12021",
95                 "RIC_SUB_DEL_FAILURE":              "12022",
96                 "RIC_CONTROL_REQ":                  "12040",
97                 "RIC_CONTROL_ACK":                  "12041",
98                 "RIC_CONTROL_FAILURE":              "12042",
99                 "RIC_INDICATION":                   "12050",
100                 "DC_ADM_INT_CONTROL":               "20000",
101                 "DC_ADM_INT_CONTROL_ACK":           "20001",
102                 "RIC_CONTROL_XAPP_CONFIG_REQUEST":  "100000",
103                 "RIC_CONTROL_XAPP_CONFIG_RESPONSE": "100001",
104         }
105
106         // Messagetype mappings for the platform components.
107         // This implements static default routes needed by the RIC. Needs to be changed in case new components/message types needes to be added/updated.
108         // Representation : {"componentName1": {"tx": <tx message type list>, "rx": <rx message type list>}}
109         PLATFORMMESSAGETYPES = map[string]map[string][]string{
110                 "E2TERM": {"tx": []string{"RIC_X2_SETUP_REQ", "RIC_X2_SETUP_RESP", "RIC_X2_SETUP_FAILURE", "RIC_X2_RESET", "RIC_X2_RESET_RESP", "RIC_ENDC_X2_SETUP_REQ", "RIC_ENDC_X2_SETUP_RESP", "RIC_ENDC_X2_SETUP_FAILURE", "RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE"}, "rx": []string{"RIC_X2_SETUP_REQ", "RIC_X2_SETUP_RESP", "RIC_X2_SETUP_FAILURE", "RIC_X2_RESET", "RIC_X2_RESET_RESP", "RIC_ENDC_X2_SETUP_REQ", "RIC_ENDC_X2_SETUP_RESP", "RIC_ENDC_X2_SETUP_FAILURE", "RIC_SUB_REQ", "RIC_SUB_DEL_REQ", "RIC_CONTROL_REQ"}},
111                 "E2MAN":  {"tx": []string{"RIC_X2_SETUP_REQ", "RIC_X2_SETUP_RESP", "RIC_X2_SETUP_FAILURE", "RIC_X2_RESET", "RIC_X2_RESET_RESP", "RIC_ENDC_X2_SETUP_REQ", "RIC_ENDC_X2_SETUP_RESP", "RIC_ENDC_X2_SETUP_FAILURE"}, "rx": []string{"RIC_X2_SETUP_REQ", "RIC_X2_SETUP_RESP", "RIC_X2_SETUP_FAILURE", "RIC_X2_RESET", "RIC_X2_RESET_RESP", "RIC_ENDC_X2_SETUP_REQ", "RIC_ENDC_X2_SETUP_RESP", "RIC_ENDC_X2_SETUP_FAILURE"}},
112                 "SUBMAN": {"tx": []string{"RIC_SUB_REQ", "RIC_SUB_DEL_REQ"}, "rx": []string{"RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE"}},
113                 "UEMAN":  {"tx": []string{"RIC_CONTROL_REQ"}, "rx": []string{}},
114                 "RSM":    {"tx": []string{"RIC_RES_STATUS_REQ"}, "rx": []string{"RAN_CONNECTED", "RAN_RESTARTED", "RAN_RECONFIGURED"}},
115         }
116
117         Logger = lumber.NewConsoleLogger(lumber.INFO)
118         Eps    Endpoints
119         Subs   SubscriptionList
120 )
121
122 func SetLogLevel(loglevel string) error {
123         switch strings.ToUpper(loglevel) {
124         case "INFO":
125                 Logger.Level(lumber.INFO)
126                 return nil
127         case "WARN":
128                 Logger.Level(lumber.WARN)
129                 return nil
130         case "ERROR":
131                 Logger.Level(lumber.ERROR)
132                 return nil
133         case "DEBUG":
134                 Logger.Info("Debug mode")
135                 Logger.Level(lumber.DEBUG)
136                 return nil
137         case "TRACE":
138                 Logger.Info("Trace mode")
139                 Logger.Level(lumber.TRACE)
140                 return nil
141         default:
142                 Logger.Error("invalid log mode, setting info")
143                 Logger.Level(lumber.INFO)
144                 return errors.New("invalid log level, setting info")
145         }
146 }
147
148 func GetPlatformComponents(configfile string) (*PlatformComponents, error) {
149         Logger.Debug("Invoked rtmgr.GetPlatformComponents(" + configfile + ")")
150         var rcfg ConfigRtmgr
151         jsonFile, err := os.Open(configfile)
152         if err != nil {
153                 return nil, errors.New("cannot open the file due to: " + err.Error())
154         }
155         defer jsonFile.Close()
156         byteValue, err := ioutil.ReadAll(jsonFile)
157         if err != nil {
158                 return nil, errors.New("cannot read the file due to: " + err.Error())
159         }
160         err = json.Unmarshal(byteValue, &rcfg)
161         if err != nil {
162                 return nil, errors.New("cannot parse data due to: " + err.Error())
163         }
164         Logger.Debug("Platform components read from the configfile:  %v", rcfg.Pcs)
165         return &(rcfg.Pcs), nil
166 }