blob: 468fbd71d930636c56b22f5fa5830d8850b79f6a [file] [log] [blame]
Balint Uveges871fa392019-04-02 20:31:11 +00001/*
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.
wahidw761934a2019-11-27 06:07:26 +000017
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
20
Balint Uveges871fa392019-04-02 20:31:11 +000021==================================================================================
22*/
23/*
24 Mnemonic: file.go
25 Abstract: File SDL implementation. Only for testing purpose.
26 Date: 16 March 2019
27*/
28
29package sdl
30
31import (
32 "encoding/json"
33 "errors"
wahidwa8596ec2019-12-05 06:30:42 +000034 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Balint Uveges871fa392019-04-02 20:31:11 +000035 "io/ioutil"
36 "os"
prabhukaliswamye110ee02019-12-23 09:51:01 +000037 "routing-manager/pkg/models"
wahidw0770de62021-04-30 13:58:07 +000038 "routing-manager/pkg/rtmgr"
39 "strings"
Balint Uveges871fa392019-04-02 20:31:11 +000040)
41
42/*
43Reads the content of the rt.json file
44Parses the JSON content and loads each xApp entry into an xApp object
45Returns an array os xApp object
46*/
kalnagy92162652019-07-02 15:15:49 +020047
48type File struct {
49 Sdl
50}
51
52func NewFile() *File {
53 instance := new(File)
54 return instance
55}
56
57func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
wahidwa8596ec2019-12-05 06:30:42 +000058 xapp.Logger.Debug("Invoked sdl.ReadAll(" + file + ")")
kalnagy92162652019-07-02 15:15:49 +020059 var rcs *rtmgr.RicComponents
Balint Uveges871fa392019-04-02 20:31:11 +000060 jsonFile, err := os.Open(file)
61 if err != nil {
wahidw2bff80d2021-10-15 02:34:33 +000062 return nil, errors.New("Cannot open the file due to: " + err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +000063 }
64 defer jsonFile.Close()
65 byteValue, err := ioutil.ReadAll(jsonFile)
66 if err != nil {
67 return nil, errors.New("cannot read the file due to: " + err.Error())
68 }
prabhukaliswamye110ee02019-12-23 09:51:01 +000069
kalnagy92162652019-07-02 15:15:49 +020070 err = json.Unmarshal(byteValue, &rcs)
Balint Uveges871fa392019-04-02 20:31:11 +000071 if err != nil {
72 return nil, errors.New("cannot parse data due to: " + err.Error())
73 }
wahidwa8596ec2019-12-05 06:30:42 +000074 xapp.Logger.Debug("file.fileReadAll returns: %v", rcs)
kalnagy92162652019-07-02 15:15:49 +020075 return rcs, nil
Balint Uveges871fa392019-04-02 20:31:11 +000076}
77
kalnagy92162652019-07-02 15:15:49 +020078func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
wahidw2bff80d2021-10-15 02:34:33 +000079 xapp.Logger.Debug("Invoked sdl.WriteAll:" + file + ", file.fileWriteAll writes data: %v", *rcs )
kalnagy92162652019-07-02 15:15:49 +020080 byteValue, err := json.Marshal(rcs)
81 if err != nil {
wahidw2bff80d2021-10-15 02:34:33 +000082 return errors.New("Cannot convert data due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020083 }
84 err = ioutil.WriteFile(file, byteValue, 0644)
85 if err != nil {
86 return errors.New("cannot write file due to: " + err.Error())
87 }
88 return nil
89}
90
zkoczkaaaf8d392019-10-02 17:16:06 +020091func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
wahidw2bff80d2021-10-15 02:34:33 +000092 xapp.Logger.Debug("Invoked sdl.WriteXApps: " + file + ", file.fileWriteXApps writes data: %v", *xApps )
kalnagy92162652019-07-02 15:15:49 +020093
94 ricData, err := NewFile().ReadAll(file)
zkoczkaaaf8d392019-10-02 17:16:06 +020095 if err != nil {
wahidw2bff80d2021-10-15 02:34:33 +000096 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
97 return errors.New("Cannot read full ric data to modify xApps data, due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020098 }
zkoczkaaaf8d392019-10-02 17:16:06 +020099 ricData.XApps = *xApps
kalnagy92162652019-07-02 15:15:49 +0200100
101 byteValue, err := json.Marshal(ricData)
Balint Uveges871fa392019-04-02 20:31:11 +0000102 if err != nil {
103 return errors.New("cannot convert data due to: " + err.Error())
104 }
105 err = ioutil.WriteFile(file, byteValue, 0644)
106 if err != nil {
107 return errors.New("cannot write file due to: " + err.Error())
108 }
109 return nil
110}
rangajal749099b2019-12-10 09:37:08 +0000111
wahidw0770de62021-04-30 13:58:07 +0000112func (f *File) WriteNewE2TInstance(file string, E2TInst *rtmgr.E2TInstance, meiddata string) error {
wahidw2bff80d2021-10-15 02:34:33 +0000113 xapp.Logger.Debug("Invoked sdl.WriteNewE2TInstance: " + file + ", file.WriteNewE2TInstance writes data: %v", *E2TInst )
rangajal749099b2019-12-10 09:37:08 +0000114
wahidw0770de62021-04-30 13:58:07 +0000115 ricData, err := NewFile().ReadAll(file)
wahidw2bff80d2021-10-15 02:34:33 +0000116 if err != nil {
117 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
118 return errors.New("cannot read full ric data to modify xApps data due to: " + err.Error())
wahidw51db0122020-01-12 04:24:29 +0000119 }
wahidw0770de62021-04-30 13:58:07 +0000120 ricData.E2Ts[E2TInst.Fqdn] = *E2TInst
121 if len(meiddata) > 0 {
122 ricData.MeidMap = append(ricData.MeidMap, meiddata)
123 }
124 /*{
125 ricData.MeidMap = []string {meiddata}
126 }
127 else {
128 ricData.MeidMap = []string {}
129 }*/
wahidw51db0122020-01-12 04:24:29 +0000130
wahidw0770de62021-04-30 13:58:07 +0000131 byteValue, err := json.Marshal(ricData)
132 if err != nil {
133 return errors.New("cannot convert data due to: " + err.Error())
134 }
135 err = ioutil.WriteFile(file, byteValue, 0644)
136 if err != nil {
137 return errors.New("cannot write file due to: " + err.Error())
138 }
139 return nil
rangajal749099b2019-12-10 09:37:08 +0000140}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000141
142func (f *File) WriteAssRANToE2TInstance(file string, rane2tmap models.RanE2tMap) error {
wahidw2bff80d2021-10-15 02:34:33 +0000143 xapp.Logger.Debug("Invoked sdl.WriteAssRANToE2TInstance: " + file + ", file.WriteAssRANToE2TInstance writes data: %v", rane2tmap)
prabhukaliswamye110ee02019-12-23 09:51:01 +0000144
wahidw0770de62021-04-30 13:58:07 +0000145 ricData, err := NewFile().ReadAll(file)
146 if err != nil {
wahidw2bff80d2021-10-15 02:34:33 +0000147 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
148 return errors.New("cannot read full ric data to modify xApps data due to: " + err.Error())
wahidw0770de62021-04-30 13:58:07 +0000149 }
wahidw51db0122020-01-12 04:24:29 +0000150
wahidw0770de62021-04-30 13:58:07 +0000151 //ricData.MeidMap = []string{}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000152 for _, element := range rane2tmap {
wahidw2bff80d2021-10-15 02:34:33 +0000153 xapp.Logger.Info("Data received")
wahidw0770de62021-04-30 13:58:07 +0000154 var str, meidar string
wahidw51db0122020-01-12 04:24:29 +0000155 for _, meid := range element.RanNamelist {
wahidw0770de62021-04-30 13:58:07 +0000156 meidar += meid + " "
wahidw51db0122020-01-12 04:24:29 +0000157 }
wahidw0770de62021-04-30 13:58:07 +0000158 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar, " ")
159 ricData.MeidMap = append(ricData.MeidMap, str)
wahidw51db0122020-01-12 04:24:29 +0000160
prabhukaliswamye110ee02019-12-23 09:51:01 +0000161 for key, _ := range ricData.E2Ts {
162 if key == *element.E2TAddress {
163 var estObj rtmgr.E2TInstance
164 estObj = ricData.E2Ts[key]
165 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
wahidw0770de62021-04-30 13:58:07 +0000166 ricData.E2Ts[key] = estObj
prabhukaliswamye110ee02019-12-23 09:51:01 +0000167 }
168 }
169 }
170
171 byteValue, err := json.Marshal(ricData)
wahidw0770de62021-04-30 13:58:07 +0000172 if err != nil {
173 return errors.New("cannot convert data due to: " + err.Error())
174 }
175 err = ioutil.WriteFile(file, byteValue, 0644)
176 if err != nil {
177 return errors.New("cannot write file due to: " + err.Error())
178 }
179 return nil
prabhukaliswamye110ee02019-12-23 09:51:01 +0000180}
181
182func (f *File) WriteDisAssRANFromE2TInstance(file string, disassranmap models.RanE2tMap) error {
wahidw2bff80d2021-10-15 02:34:33 +0000183 xapp.Logger.Debug("Invoked sdl.WriteDisAssRANFromE2TInstance: " + file + ",file.WriteDisAssRANFromE2TInstance writes data: %v", disassranmap)
prabhukaliswamye110ee02019-12-23 09:51:01 +0000184
wahidw0770de62021-04-30 13:58:07 +0000185 ricData, err := NewFile().ReadAll(file)
186 if err != nil {
wahidw2bff80d2021-10-15 02:34:33 +0000187 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
188 return errors.New("cannot read full ric data to modify xApps data due to: " + err.Error())
wahidw0770de62021-04-30 13:58:07 +0000189 }
wahidw51db0122020-01-12 04:24:29 +0000190
wahidw0770de62021-04-30 13:58:07 +0000191 var str, meiddel, meiddisdel string
192 //ricData.MeidMap = []string{}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000193 for _, element := range disassranmap {
wahidw2bff80d2021-10-15 02:34:33 +0000194 xapp.Logger.Info("Data received")
wahidw51db0122020-01-12 04:24:29 +0000195 for _, meid := range element.RanNamelist {
wahidw0770de62021-04-30 13:58:07 +0000196 meiddisdel += meid + " "
wahidw51db0122020-01-12 04:24:29 +0000197 }
wahidw0770de62021-04-30 13:58:07 +0000198 if len(element.RanNamelist) > 0 {
199 str = "mme_del|" + strings.TrimSuffix(meiddisdel, " ")
200 ricData.MeidMap = append(ricData.MeidMap, str)
201 }
prabhukaliswamye110ee02019-12-23 09:51:01 +0000202 e2taddress_key := *element.E2TAddress
wahidw0770de62021-04-30 13:58:07 +0000203 //Check whether the provided E2T Address is available in SDL as a key.
prabhukaliswamye110ee02019-12-23 09:51:01 +0000204 //If exist, proceed further to check RAN list, Otherwise move to next E2T Instance
205 if _, exist := ricData.E2Ts[e2taddress_key]; exist {
206 var estObj rtmgr.E2TInstance
207 estObj = ricData.E2Ts[e2taddress_key]
208 // If RAN list is empty, then routing manager assumes that all RANs attached associated to the particular E2T Instance to be removed.
209 if len(element.RanNamelist) == 0 {
210 xapp.Logger.Debug("RAN List is empty. So disassociating all RANs from the E2T Instance: %v ", *element.E2TAddress)
wahidw0770de62021-04-30 13:58:07 +0000211 for _, meid := range estObj.Ranlist {
212 meiddel += meid + " "
213 }
214 str = "mme_del|" + strings.TrimSuffix(meiddel, " ")
215 ricData.MeidMap = append(ricData.MeidMap, str)
wahidw51db0122020-01-12 04:24:29 +0000216
wahidw0770de62021-04-30 13:58:07 +0000217 estObj.Ranlist = []string{}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000218 } else {
219 xapp.Logger.Debug("Remove only selected rans from E2T Instance: %v and %v ", ricData.E2Ts[e2taddress_key].Ranlist, element.RanNamelist)
220 for _, disRanValue := range element.RanNamelist {
221 for ranIndex, ranValue := range ricData.E2Ts[e2taddress_key].Ranlist {
222 if disRanValue == ranValue {
223 estObj.Ranlist[ranIndex] = estObj.Ranlist[len(estObj.Ranlist)-1]
224 estObj.Ranlist[len(estObj.Ranlist)-1] = ""
225 estObj.Ranlist = estObj.Ranlist[:len(estObj.Ranlist)-1]
226 }
227 }
228 }
229 }
wahidw0770de62021-04-30 13:58:07 +0000230 ricData.E2Ts[e2taddress_key] = estObj
prabhukaliswamye110ee02019-12-23 09:51:01 +0000231 }
232 }
233
234 xapp.Logger.Debug("Final data after disassociate: %v", ricData)
235
236 byteValue, err := json.Marshal(ricData)
wahidw0770de62021-04-30 13:58:07 +0000237 if err != nil {
238 return errors.New("cannot convert data due to: " + err.Error())
239 }
240 err = ioutil.WriteFile(file, byteValue, 0644)
241 if err != nil {
242 return errors.New("cannot write file due to: " + err.Error())
243 }
prabhukaliswamye110ee02019-12-23 09:51:01 +0000244 return nil
245}
246
247func (f *File) WriteDeleteE2TInstance(file string, E2TInst *models.E2tDeleteData) error {
wahidw2bff80d2021-10-15 02:34:33 +0000248 xapp.Logger.Debug("Invoked sdl.WriteDeleteE2TInstance: " + file + ",file.WriteDeleteE2TInstance writes data: %v", *E2TInst)
prabhukaliswamye110ee02019-12-23 09:51:01 +0000249
250 ricData, err := NewFile().ReadAll(file)
251 if err != nil {
wahidw2bff80d2021-10-15 02:34:33 +0000252 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
253 return errors.New("cannot read full ric data to modify xApps data due to: " + err.Error())
prabhukaliswamye110ee02019-12-23 09:51:01 +0000254 }
255
wahidw0770de62021-04-30 13:58:07 +0000256 //ricData.MeidMap = []string{}
257 var delrow, meiddel string
258 if len(E2TInst.RanNamelistTobeDissociated) > 0 {
259 for _, meid := range E2TInst.RanNamelistTobeDissociated {
wahidw51db0122020-01-12 04:24:29 +0000260 meiddel += meid + " "
261 }
wahidw0770de62021-04-30 13:58:07 +0000262 delrow = "mme_del|" + strings.TrimSuffix(meiddel, " ")
263 ricData.MeidMap = append(ricData.MeidMap, delrow)
wahidw51db0122020-01-12 04:24:29 +0000264 } else {
wahidw0770de62021-04-30 13:58:07 +0000265 if len(ricData.E2Ts[*E2TInst.E2TAddress].Ranlist) > 0 {
266 for _, meid := range ricData.E2Ts[*E2TInst.E2TAddress].Ranlist {
267 meiddel += meid + " "
268 }
269 delrow = "mme_del|" + strings.TrimSuffix(meiddel, " ")
270 ricData.MeidMap = append(ricData.MeidMap, delrow)
271 }
wahidw51db0122020-01-12 04:24:29 +0000272 }
273
prabhukaliswamye110ee02019-12-23 09:51:01 +0000274 delete(ricData.E2Ts, *E2TInst.E2TAddress)
275
prabhukaliswamye110ee02019-12-23 09:51:01 +0000276 for _, element := range E2TInst.RanAssocList {
wahidw0770de62021-04-30 13:58:07 +0000277 var str, meidar string
wahidw2bff80d2021-10-15 02:34:33 +0000278 xapp.Logger.Info("Data received")
wahidw51db0122020-01-12 04:24:29 +0000279 for _, meid := range element.RanNamelist {
280 meidar = meid + " "
281 }
wahidw0770de62021-04-30 13:58:07 +0000282 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar, " ")
283 ricData.MeidMap = append(ricData.MeidMap, str)
prabhukaliswamye110ee02019-12-23 09:51:01 +0000284 key := *element.E2TAddress
285
286 if val, ok := ricData.E2Ts[key]; ok {
287 var estObj rtmgr.E2TInstance
288 estObj = val
289 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
wahidw0770de62021-04-30 13:58:07 +0000290 ricData.E2Ts[key] = estObj
prabhukaliswamye110ee02019-12-23 09:51:01 +0000291 } else {
292 xapp.Logger.Error("file.WriteDeleteE2TInstance E2T instance is not found for provided E2TAddress : %v", errors.New(key).Error())
293 }
294
295 }
296
297 byteValue, err := json.Marshal(ricData)
298 if err != nil {
wahidw0770de62021-04-30 13:58:07 +0000299 return errors.New("cannot convert data due to: " + err.Error())
prabhukaliswamye110ee02019-12-23 09:51:01 +0000300 }
301 err = ioutil.WriteFile(file, byteValue, 0644)
302 if err != nil {
wahidw0770de62021-04-30 13:58:07 +0000303 return errors.New("cannot write file due to: " + err.Error())
prabhukaliswamye110ee02019-12-23 09:51:01 +0000304 }
305 return nil
306}