RIC:1060: Change in PTL
[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         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35         "github.com/ghodss/yaml"
36         "io/ioutil"
37         "os"
38         "strings"
39 )
40
41 var (
42         Eps              Endpoints
43         Subs             SubscriptionList
44         PrsCfg           *PlatformRoutes
45         Mtype            MessageTypeList
46         DynamicRouteList []string
47         RMRConnStatus    map[string]bool
48 )
49
50 func GetPlatformComponents(configfile string) (*PlatformComponents, error) {
51         xapp.Logger.Debug("Invoked rtmgr.GetPlatformComponents(" + configfile + ")")
52         var rcfg ConfigRtmgr
53         var rtroutes RtmgrRoutes
54         var mtypes MessageTypeIdentifier
55         yamlFile, err := os.Open(configfile)
56         if err != nil {
57                 return nil, errors.New("cannot open the file due to: " + err.Error())
58         }
59         defer yamlFile.Close()
60         byteValue, err := ioutil.ReadAll(yamlFile)
61         if err != nil {
62                 return nil, errors.New("cannot read the file due to: " + err.Error())
63         }
64         jsonByteValue, err := yaml.YAMLToJSON(byteValue)
65         if err != nil {
66                 return nil, errors.New("cannot read the file due to: " + err.Error())
67         }
68         err = json.Unmarshal(jsonByteValue, &rtroutes)
69         if err != nil {
70                 return nil, errors.New("cannot parse data due to: " + err.Error())
71         }
72         PrsCfg = &(rtroutes.Prs)
73
74         err = json.Unmarshal(jsonByteValue, &mtypes)
75         if err != nil {
76                 return nil, errors.New("cannot parse data due to: " + err.Error())
77         } else {
78                 xapp.Logger.Debug("Messgaetypes = %v", mtypes)
79                 for _, m := range mtypes.Mit {
80                         splitstr := strings.Split(m, "=")
81                         Mtype[splitstr[0]] = splitstr[1]
82                 }
83         }
84         err = json.Unmarshal(jsonByteValue, &rcfg)
85         if err != nil {
86                 return nil, errors.New("cannot parse data due to: " + err.Error())
87         }
88         xapp.Logger.Debug("Platform components read from the configfile:  %v", rcfg.Pcs)
89         return &(rcfg.Pcs), nil
90 }