RICAPP-227:- dynamically adding fields to influxdb and changed the UE KPI
[ric-app/kpimon-go.git] / control / control.go
1 package control\r
2 /*\r
3 #include <e2sm/wrapper.h>\r
4 #cgo LDFLAGS: -lm  -le2smwrapper\r
5 #cgo CFLAGS:  -I/usr/local/include/e2sm\r
6 */\r
7 import "C"\r
8 import (\r
9         "unsafe"\r
10 )\r
11 import (\r
12         "context"\r
13         "encoding/json"\r
14         "log"\r
15         "net/http"\r
16         "time"\r
17 //      "bytes"\r
18 //      "encoding/binary"\r
19         "strconv"\r
20         //"encoding/base64"\r
21         //"strings"\r
22         "fmt"\r
23         "reflect"\r
24         "errors"\r
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"\r
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"\r
27         influxdb2 "github.com/influxdata/influxdb-client-go"\r
28 )\r
29 \r
30 type Control struct {\r
31         RMR    chan *xapp.RMRParams //channel for receiving rmr message\r
32         client influxdb2.Client     //client for influxdb\r
33 }\r
34 \r
35 var (\r
36         timeToWait           = "w10ms"\r
37         subsequentActionType = "continue"\r
38         actionType           = "report"\r
39         actionId             = int64(1)\r
40         seqId                = int64(1)\r
41         funcId               = int64(2)\r
42         hPort                = int64(8080)\r
43         rPort                = int64(4560)\r
44         clientEndpoint       = clientmodel.SubscriptionParamsClientEndpoint{Host: "service-ricxapp-kpimon-go-http.ricxapp", HTTPPort: &hPort, RMRPort: &rPort}\r
45 )\r
46 var Glob_cell = make(map[string]bool)\r
47 var Glob_Ran_cell = make(map[string][]string)\r
48 var Glob_cell_Plmn = make(map[string]string)\r
49 var ranUeKpi = make(map[string][]string)\r
50 var ranCellKpi =  make(map[string][]string)\r
51 func (c Control) Consume(msg *xapp.RMRParams) error {\r
52         id := xapp.Rmr.GetRicMessageName(msg.Mtype)\r
53         xapp.Logger.Info(\r
54                 "Message received: name=%s meid=%s subId=%d txid=%s len=%d",\r
55                 id,\r
56                 msg.Meid.RanName,\r
57                 msg.SubId,\r
58                 msg.Xid,\r
59                 msg.PayloadLen,\r
60         )\r
61         c.RMR <- msg\r
62         return nil\r
63 }\r
64 \r
65 func NewControl() Control {\r
66         xapp.Logger.Info("In new control\n")\r
67         create_db()\r
68         xapp.Logger.Info("returning control\n")\r
69         return Control{\r
70                 make(chan *xapp.RMRParams),\r
71                 influxdb2.NewClient("http://ricplt-influxdb.ricplt:8086", "client"),\r
72         }\r
73 }\r
74 func create_db() {\r
75         //Create a database named kpimon in influxDB\r
76         xapp.Logger.Info("In create_db\n")\r
77         _, err := http.Post("http://ricplt-influxdb.ricplt:8086/query?q=create%20database%20kpimon", "", nil)\r
78         if err != nil {\r
79                 xapp.Logger.Error("Create database failed!")\r
80         }\r
81         xapp.Logger.Info("exiting create_db\n")\r
82 }\r
83 \r
84 func (c Control) getEnbList() ([]*xapp.RNIBNbIdentity, error) {\r
85         enbs, err := xapp.Rnib.GetListEnbIds()\r
86         if err != nil {\r
87                 xapp.Logger.Error("err: %s", err)\r
88                 return nil, err\r
89         }\r
90 \r
91         xapp.Logger.Info("List for connected eNBs :")\r
92         for index, enb := range enbs {\r
93                 xapp.Logger.Info("%d. enbid: %s", index+1, enb.InventoryName)\r
94         }\r
95         return enbs, nil\r
96 }\r
97 \r
98 func (c *Control) getGnbList() ([]*xapp.RNIBNbIdentity, error) {\r
99         gnbs, err := xapp.Rnib.GetListGnbIds()\r
100 \r
101         if err != nil {\r
102                 xapp.Logger.Error("err: %s", err)\r
103                 return nil, err\r
104         }\r
105         xapp.Logger.Info("List of connected gNBs :")\r
106         for index, gnb := range gnbs {\r
107                 xapp.Logger.Info("%d. gnbid : %s", index+1, gnb.InventoryName)\r
108         }\r
109         return gnbs, nil\r
110 }\r
111 \r
112 func (c *Control) getnbList() []*xapp.RNIBNbIdentity {\r
113         //Get all GnodeB and EnodeB connected to RIC\r
114         var nbs []*xapp.RNIBNbIdentity\r
115 \r
116         if enbs, err := c.getEnbList(); err == nil {\r
117                 nbs = append(nbs, enbs...)\r
118         }\r
119 \r
120         if gnbs, err := c.getGnbList(); err == nil {\r
121                 nbs = append(nbs, gnbs...)\r
122         }\r
123         return nbs\r
124 }\r
125 \r
126 func cellid_to_list_of_int(str string) []int64 {\r
127         l := len(str)\r
128         var ans []int64\r
129         for i := 0; i < l; i += 2 {\r
130                 output, err := strconv.ParseInt(str[i:i+2], 16, 64)\r
131                 if err != nil {\r
132                         fmt.Println(err)\r
133                         return ans\r
134                 }\r
135                 ans = append(ans, output)\r
136         }\r
137         return ans\r
138 }\r
139 func plmnid_to_list_of_int(str string) []int64 {\r
140         l := len(str)\r
141         var ans []int64\r
142         for i := 0; i < l; i += 2 {\r
143                 output, err := strconv.ParseInt(str[i:i+2], 16, 64)\r
144                 if err != nil {\r
145                         fmt.Println(err)\r
146                         return ans\r
147                 }\r
148                 ans = append(ans, output)\r
149         }\r
150         return ans\r
151 }\r
152 func encode_action_format1(plmn string, cellid string, meid string) clientmodel.ActionDefinition {\r
153         lol1 := plmnid_to_list_of_int(plmn)\r
154         lol2 := cellid_to_list_of_int(cellid)\r
155         var format1 []int64\r
156         //format1=[]int64{0,1,1,8,0,19,32,0,3,1,32,0,0,32,0,4,1,32,0,0,32,0,78,1,32,0,0,32,0,79,1,32,0,0,32,0,8,1,32,0,0,32,0,7,1,32,0,0,32,0,11,1,32,0,0,32,0,12,1,32,0,0,32,0,80,1,32,0,0,32,0,81,1,32,0,0,32,0,13,1,32,0,0,32,0,14,1,32,0,0,32,0,40,1,32,0,0,32,0,41,1,32,0,0,32,0,42,1,32,0,0,32,0,82,1,32,0,0,32,0,83,1,32,0,0,32,0,84,1,32,0,0,32,0,85,1,32,0,0,32,0,86,1,32,0,0,64,39,15,0,0,31,1,18,52,92,0,16}\r
157         //0(nr) 1(eutra)  0,31,1(plmn)18,52,92,0,16(cellid)\r
158         link:="http://service-ricplt-e2mgr-http.ricplt.svc.cluster.local:3800/v1/nodeb/"\r
159         \r
160         link=link+meid\r
161         tmpr,err := http.Get(link)\r
162         if err != nil {\r
163                 log.Fatalln(err)\r
164                 l:=[]int64{0}\r
165                 return l\r
166         }\r
167         defer tmpr.Body.Close()\r
168         var resp E2mgrResponse\r
169 \r
170         err=json.NewDecoder(tmpr.Body).Decode(&resp)\r
171         if err != nil {\r
172                 log.Fatalln(err)\r
173                 l:=[]int64{0}\r
174                 return l\r
175 \r
176         }\r
177         counter:=0\r
178         //RanFunctionId=2 for kpm in viavi\r
179         for i := 0; i < len(resp.Gnb.RanFunctions); i++ {\r
180                 if resp.Gnb.RanFunctions[i].RanFunctionId  == 2 {\r
181                         counter = i\r
182                         break\r
183                 }\r
184         }\r
185         cString := C.CString(resp.Gnb.RanFunctions[counter].RanFunctionDefinition)\r
186         defer C.free(unsafe.Pointer(cString)) // Free the allocated C string when done\r
187         // Call the C function\r
188         determine := 2 //2 for format1 by name\r
189         result:=C.encode_action_Definition(cString, C.int(determine))\r
190         \r
191         for i := 0; i < int(result.length); i++ {\r
192                 value := int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(result.array)) + uintptr(i*4))))\r
193                 format1 = append(format1, value)\r
194         }\r
195         format1 = append(format1, lol1...) //appending plmn\r
196         format1 = append(format1, lol2...) //appending cellid\r
197         return format1\r
198 }\r
199 \r
200 func encode_action_format2() clientmodel.ActionDefinition{\r
201         var format2 []int64\r
202         format2 = []int64{0, 1, 0, 0, 0, 20, 0, 160, 68, 82, 66, 46, 85, 69, 84, 104, 112, 68, 108, 1, 0, 0, 0, 1, 64, 68, 82, 66, 46, 85, 69, 84, 104, 112, 85, 108, 1, 0, 0, 0, 1, 0, 71, 78, 66, 45, 68, 85, 45, 73, 68, 1, 0, 0, 0, 0, 160, 78, 82, 45, 67, 71, 73, 1, 0, 0, 0, 0, 160, 78, 82, 45, 80, 67, 73, 1, 0, 0, 0, 2, 192, 81, 111, 115, 70, 108, 111, 119, 46, 80, 100, 99, 112, 80, 100, 117, 86, 111, 108, 117, 109, 101, 68, 108, 1, 0, 0, 0, 2, 192, 81, 111, 115, 70, 108, 111, 119, 46, 80, 100, 99, 112, 80, 100, 117, 86, 111, 108, 117, 109, 101, 85, 108, 1, 0, 0, 0, 1, 64, 82, 82, 67, 46, 67, 111, 110, 110, 77, 97, 120, 1, 0, 0, 0, 1, 96, 82, 82, 67, 46, 67, 111, 110, 110, 77, 101, 97, 110, 1, 0, 0, 0, 1, 160, 82, 82, 85, 46, 80, 114, 98, 65, 118, 97, 105, 108, 68, 108, 1, 0, 0, 0, 1, 160, 82, 82, 85, 46, 80, 114, 98, 65, 118, 97, 105, 108, 85, 108, 1, 0, 0, 0, 1, 32, 82, 82, 85, 46, 80, 114, 98, 84, 111, 116, 1, 0, 0, 0, 1, 96, 82, 82, 85, 46, 80, 114, 98, 84, 111, 116, 68, 108, 1, 0, 0, 0, 1, 96, 82, 82, 85, 46, 80, 114, 98, 84, 111, 116, 85, 108, 1, 0, 0, 0, 1, 128, 82, 82, 85, 46, 80, 114, 98, 85, 115, 101, 100, 68, 108, 1, 0, 0, 0, 1, 128, 82, 82, 85, 46, 80, 114, 98, 85, 115, 101, 100, 85, 108, 1, 0, 0, 0, 1, 64, 86, 105, 97, 118, 105, 46, 71, 101, 111, 46, 120, 1, 0, 0, 0, 1, 64, 86, 105, 97, 118, 105, 46, 71, 101, 111, 46, 121, 1, 0, 0, 0, 1, 64, 86, 105, 97, 118, 105, 46, 71, 101, 111, 46, 122, 1, 0, 0, 0, 2, 0, 86, 105, 97, 118, 105, 46, 82, 97, 100, 105, 111, 46, 112, 111, 119, 101, 114, 1, 0, 0, 0, 2, 64, 86, 105, 97, 118, 105, 46, 82, 97, 100, 105, 111, 46, 115, 101, 99, 116, 111, 114, 115, 1, 0, 0, 0, 0, 0}\r
203         //encode the variable part and append it to our array.\r
204         format2 = append(format2, 89) //appending variable part if necessory\r
205         return format2\r
206 }\r
207 \r
208 func encode_action_format3(meid string) clientmodel.ActionDefinition {\r
209         var format3 []int64\r
210 \r
211         //for simulation-by measId(not supported in Viavi 1.4)\r
212         //format3=[]int64{0,1,3,64,0,29,32,0,90,0,0,16,0,0,16,0,91,0,0,16,0,0,16,0,3,0,0,16,0,0,16,0,4,0,0,16,0,0,16,0,80,0,0,16,0,0,16,0,13,0,0,16,0,0,16,0,14,0,0,16,0,0,16,0,92,0,0,16,0,0,16,0,93,0,0,16,0,0,16,0,44,0,0,16,0,0,16,0,40,0,0,16,0,0,16,0,41,0,0,16,0,0,16,0,42,0,0,16,0,0,16,0,94,0,0,16,0,0,16,0,95,0,0,16,0,0,16,0,96,0,0,16,0,0,16,0,97,0,0,16,0,0,16,0,45,0,0,16,0,0,16,0,98,0,0,16,0,0,16,0,99,0,0,16,0,0,16,0,100,0,0,16,0,0,16,0,101,0,0,16,0,0,16,0,102,0,0,16,0,0,16,0,103,0,0,16,0,0,16,0,104,0,0,16,0,0,16,0,43,0,0,16,0,0,16,0,105,0,0,16,0,0,16,0,106,0,0,16,0,0,16,0,107,0,0,16,0,0,16,0,108,0,0,16,0,0,32,39,15}\r
213         //variable part is not presetnt in action def format 3\r
214         \r
215         link:="http://service-ricplt-e2mgr-http.ricplt.svc.cluster.local:3800/v1/nodeb/"\r
216         link=link+meid\r
217         tmpr,err := http.Get(link)\r
218         if err != nil {\r
219                 log.Fatalln(err)\r
220                 l:=[]int64{0}\r
221                 return l\r
222 \r
223         }\r
224         defer tmpr.Body.Close()\r
225         var resp E2mgrResponse\r
226 \r
227         err=json.NewDecoder(tmpr.Body).Decode(&resp)\r
228         if err != nil {\r
229                 log.Fatalln(err)\r
230                 l:=[]int64{0}\r
231                 return l\r
232 \r
233         }\r
234         counter:=0\r
235         //RanFunctionId=2 for kpm in viavi\r
236         for i := 0; i < len(resp.Gnb.RanFunctions); i++ {\r
237                 if resp.Gnb.RanFunctions[i].RanFunctionId  == 2 {\r
238                         counter = i\r
239                         break\r
240                 }\r
241         }\r
242         cString := C.CString(resp.Gnb.RanFunctions[counter].RanFunctionDefinition)\r
243         defer C.free(unsafe.Pointer(cString)) // Free the allocated C string when done\r
244         // Call the C function\r
245         determine := 4 //2 for format1 by name\r
246         \r
247         result:=C.encode_action_Definition(cString, C.int(determine))\r
248         for i := 0; i < int(result.length); i++ {\r
249                 value := int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(result.array)) + uintptr(i*4))))\r
250                 format3 = append(format3, value)\r
251 \r
252         }\r
253 \r
254 \r
255         //variable part is not presetnt in action def format 3\r
256         return format3\r
257 }\r
258 func encode_actionsToBeSetup(meid string) clientmodel.ActionsToBeSetup {\r
259         var l clientmodel.ActionsToBeSetup\r
260         /*\r
261         link:="http://service-ricplt-e2mgr-http.ricplt.svc.cluster.local:3800/v1/nodeb/"\r
262         link=link+meid\r
263         tmpr,err := http.Get(link)\r
264         if err != nil {\r
265                 log.Fatalln(err)\r
266                 return l\r
267         }\r
268         defer tmpr.Body.Close()\r
269         var resp E2mgrResponse\r
270         \r
271         err=json.NewDecoder(tmpr.Body).Decode(&resp)\r
272         if err != nil {\r
273                 log.Fatalln(err)\r
274                 return l\r
275         }\r
276 \r
277         \r
278         counter := 0\r
279         for i := 0; i < len(resp.Gnb.NodeConfigs); i++ {\r
280                 if resp.Gnb.NodeConfigs[i].E2nodeComponentInterfaceType == "f1" {\r
281                         counter = i\r
282                         break\r
283                 }\r
284         }\r
285         tm := resp.Gnb.NodeConfigs[counter].E2nodeComponentRequestPart\r
286         base64Text := make([]byte, base64.StdEncoding.DecodedLen(len(tm)))\r
287         nl, _ := base64.StdEncoding.Decode(base64Text, []byte(tm))\r
288         message := string(base64Text[:nl])\r
289         \r
290         counter = 0\r
291         for i := 0; i < len(meid); i++ {\r
292                 if meid[i] == '_' {\r
293                         counter++\r
294                 }\r
295                 if counter == 3 {\r
296                         counter = i + 1\r
297                         break\r
298                 }\r
299         }\r
300         \r
301         ans := strings.ToUpper(meid[counter:len(meid)])\r
302         l1 := int64(len(message))\r
303         l2 := int64(len(ans))\r
304         var cells []string\r
305         for i := int64(0); i <= l1-l2; i++ {\r
306                 if strings.Contains(message[i:i+l2], ans) {\r
307                         Glob_cell[message[i:i+10]]=true\r
308                         cells = append(cells, message[i:i+10])\r
309                         fmt.Println(message[i : i+10])\r
310                 }\r
311         }\r
312         */\r
313 \r
314         var n int64 = 1\r
315         cells:=Glob_Ran_cell[meid]\r
316         //fmt.Println("len of Tonga cells= ",len(cells))\r
317         //fmt.Println("Tonga cells = ", cells)\r
318         //fmt.Println("Tonga  = ", Glob_Ran_cell)\r
319 \r
320         var tempCells []string\r
321         for _, ele:=range cells{\r
322                 if ele!=""{\r
323                         tempCells =append(tempCells,ele)\r
324                 }\r
325         }\r
326         //fmt.Println("len of Tonga tmep cells= ",len(tempCells))\r
327         //fmt.Println("Tonga temp cells = ", tempCells)\r
328 \r
329         //var ue int64 = 1 //get no of ue connected to du(if required)\r
330 \r
331         //for action def 1\r
332         for n <= int64(len(tempCells)) {\r
333                 //fix double length issue/empty cell sting  in Glob_Ran_cell map\r
334                 var tmp int64 = n\r
335                 var lol *int64 = &tmp\r
336                 s := clientmodel.ActionToBeSetup{\r
337                         ActionID:         lol,\r
338                         ActionType:       &actionType,\r
339                         ActionDefinition: encode_action_format1(Glob_cell_Plmn[tempCells[n-1]], tempCells[n-1],meid),\r
340                         SubsequentAction:  &clientmodel.SubsequentAction{\r
341                                 SubsequentActionType: &subsequentActionType,\r
342                                 TimeToWait:           &timeToWait,\r
343                         },\r
344                 }\r
345                 l = append(l, &s)\r
346 \r
347                 n = n + 1\r
348         }\r
349         var tmp_act_id int64 = n\r
350         /*\r
351         n = 1\r
352         // for action def 2\r
353         for n <= ue {\r
354                 var tmp int64 = tmp_act_id\r
355                 var lol *int64 = &tmp\r
356                 s := clientmodel.ActionToBeSetup{\r
357                         ActionID:         lol,\r
358                         ActionType:       &actionType,\r
359                         ActionDefinition: encode_action_format2(),\r
360                         SubsequentAction:  &clientmodel.SubsequentAction{\r
361                                 SubsequentActionType: &subsequentActionType,\r
362                                 TimeToWait:           &timeToWait,\r
363                         },\r
364                 }\r
365                 l = append(l, &s)\r
366                 tmp_act_id = tmp_act_id + 1\r
367                 n = n + 1\r
368         }\r
369         */\r
370 \r
371         //for action def 3\r
372         var tmp int64 = tmp_act_id\r
373         var lol *int64 = &tmp\r
374         s := clientmodel.ActionToBeSetup{\r
375                 ActionID:         lol,\r
376                 ActionType:       &actionType,\r
377                 ActionDefinition: encode_action_format3(meid),\r
378                 SubsequentAction:  &clientmodel.SubsequentAction{\r
379                         SubsequentActionType: &subsequentActionType,\r
380                         TimeToWait:           &timeToWait,\r
381                 },\r
382         }\r
383         l = append(l, &s)\r
384 \r
385         return l\r
386 \r
387 }\r
388 func (c Control) sendSubscription(meid string) {\r
389         //Create Subscription message and send it to RIC platform\r
390         xapp.Logger.Info("Sending subscription request for MEID: %v", meid)\r
391 /*\r
392         subscritionParams := clientmodel.SubscriptionParams{\r
393                 ClientEndpoint: &clientEndpoint,\r
394                 Meid:           &meid,\r
395                 RANFunctionID:  &funcId,\r
396                 SubscriptionDetails: clientmodel.SubscriptionDetailsList{\r
397                         &clientmodel.SubscriptionDetail{\r
398                                 EventTriggers: clientmodel.EventTriggerDefinition{\r
399                                         8,39,15,\r
400                                 },\r
401                                 XappEventInstanceID: &seqId,\r
402                                 ActionToBeSetupList: clientmodel.ActionsToBeSetup{\r
403                                         &clientmodel.ActionToBeSetup{\r
404                                                 ActionID:   &actionId,\r
405                                                 ActionType: &actionType,\r
406                                                 ActionDefinition: clientmodel.ActionDefinition{\r
407                                                         5678,\r
408                                                 },\r
409                                                 SubsequentAction: &clientmodel.SubsequentAction{\r
410                                                         SubsequentActionType: &subsequentActionType,\r
411                                                         TimeToWait:           &timeToWait,\r
412                                                 },\r
413                                         },\r
414                                 },\r
415                         },\r
416                 },\r
417         }\r
418         */\r
419         \r
420         //8,39,15, for 10000 ms reporting period\r
421         subscritionParams := clientmodel.SubscriptionParams{\r
422                 ClientEndpoint: &clientEndpoint,\r
423                 Meid:           &meid,\r
424                 RANFunctionID:  &funcId,\r
425                 SubscriptionDetails: clientmodel.SubscriptionDetailsList{\r
426                         &clientmodel.SubscriptionDetail{\r
427                                 EventTriggers: clientmodel.EventTriggerDefinition{\r
428                                         8,39,15,\r
429                                 },\r
430                                 XappEventInstanceID: &seqId,\r
431                                 ActionToBeSetupList: encode_actionsToBeSetup(meid),\r
432                         },\r
433                 },\r
434         }\r
435 \r
436         b, err := json.MarshalIndent(subscritionParams, "", " ")\r
437         if err != nil {\r
438                 xapp.Logger.Error("Json marshaling failed: %v", err)\r
439         }\r
440         xapp.Logger.Info("*****body: %s", string(b))\r
441 \r
442         resp, err := xapp.Subscription.Subscribe(&subscritionParams)\r
443         if err != nil {\r
444                 xapp.Logger.Error("Subscription (%s) failed  with error: %s", meid, err)\r
445                 return\r
446         }\r
447         xapp.Logger.Info("Successfully subscription done (%s), subscriptrion id: %s", meid, *resp.SubscriptionID)\r
448 }\r
449 \r
450 func Test() (err error) {\r
451         var e2ap *E2ap\r
452         //var e2sm *E2sm\r
453 \r
454         //indicationMsg, err := e2ap.GetIndicationMessage(params.Payload)\r
455         indicationMsg, err := e2ap.GetIndicationM()\r
456 \r
457         if err != nil {\r
458                 xapp.Logger.Error("Failed to decode RIC Indication message: %v", err)\r
459                 return\r
460         }\r
461 \r
462         //log.Printf("RIC Indication message from {%s} received", params.Meid.RanName)\r
463         /*\r
464                 indicationHdr, err := e2sm.GetIndicationHeader(indicationMsg.IndHeader)\r
465                 if err != nil {\r
466                         xapp.Logger.Error("Failed to decode RIC Indication Header: %v", err)\r
467                         return\r
468                 }\r
469         */\r
470 \r
471         //Decoding message and put information into log\r
472         log.Printf("-----------RIC Indication Header-----------")\r
473         log.Printf("indicationMsg.IndHeader= %x", indicationMsg.IndHeader)\r
474 /*      \r
475         buf := new(bytes.Buffer) //create my buffer\r
476         binary.Write(buf, binary.LittleEndian, indicationMsg.IndHeader)\r
477         log.Printf("binary Write buf= %x",buf )\r
478         b := buf.Bytes()\r
479         //str := buf.String()\r
480         //log.Printf(" buf Strin()= %s",str )\r
481         //cptr1:= unsafe.Pointer(C.CString(str))\r
482         cptr1:= unsafe.Pointer(&b[0])\r
483         defer C.free(cptr1)\r
484 */      \r
485         cptr1 := unsafe.Pointer(&indicationMsg.IndHeader[0])\r
486         decodedHdr := C.e2sm_decode_ric_indication_header(cptr1, C.size_t(len(indicationMsg.IndHeader)))\r
487         //decodedHdr := C.e2sm_decode_ric_indication_header(cptr1, C.size_t(len(str)))\r
488         //decodedHdr := C.e2sm_decode_ric_indication_header(cptr1, C.size_t(buf.Len()))\r
489         if decodedHdr == nil {\r
490                 return errors.New("e2sm wrapper is unable to get IndicationHeader due to wrong or invalid input")\r
491         }\r
492         defer C.e2sm_free_ric_indication_header(decodedHdr)\r
493         IndHdrType := int32(decodedHdr.indicationHeader_formats.present)\r
494         if IndHdrType==0{\r
495                 log.Printf("No Indication Header present")\r
496         }\r
497         if IndHdrType==1{\r
498                 log.Printf("Indication Header format = %d",IndHdrType)\r
499                 indHdrFormat1_C := *(**C.E2SM_KPM_IndicationHeader_Format1_t)(unsafe.Pointer(&decodedHdr.indicationHeader_formats.choice[0]))\r
500                 //senderName_C := (*C.PrintableString_t)(unsafe.Pointer(indHdrFormat1_C.senderName))\r
501                 senderName_C:=indHdrFormat1_C.senderName\r
502                 var senderName []byte\r
503                 senderName = C.GoBytes(unsafe.Pointer(senderName_C.buf), C.int(senderName_C.size))\r
504                 log.Printf("Sender Name = %x",senderName)\r
505 \r
506                 //senderType_C := (*C.PrintableString_t)(unsafe.Pointer(indHdrFormat1_C.senderType))\r
507                 senderType_C :=indHdrFormat1_C.senderType\r
508                 //senderType []byte\r
509                 senderType := C.GoBytes(unsafe.Pointer(senderType_C.buf), C.int(senderType_C.size))\r
510                 log.Printf("Sender Type = %x",senderType)\r
511 \r
512                 //vendorName_C := (*C.PrintableString_t)(unsafe.Pointer(indHdrFormat1_C.vendorName))\r
513                 vendorName_C :=indHdrFormat1_C.vendorName\r
514                 //vendorName  []byte\r
515                 vendorName := C.GoBytes(unsafe.Pointer(vendorName_C.buf), C.int(vendorName_C.size))\r
516                 log.Printf("Vendor Name = %x",vendorName)\r
517 \r
518 \r
519         }\r
520 \r
521         /*\r
522                 indMsg, err := e2sm.GetIndicationMessage(indicationMsg.IndMessage)\r
523                 if err != nil {\r
524                         xapp.Logger.Error("Failed to decode RIC Indication Message: %v", err)\r
525                         return\r
526                 }\r
527         */\r
528         log.Printf("-----------RIC Indication Message-----------")\r
529         log.Printf("indicationMsg.IndMessage= %x",indicationMsg.IndMessage)\r
530         cptr2 := unsafe.Pointer(&indicationMsg.IndMessage[0])\r
531         indicationmessage := C.e2sm_decode_ric_indication_message(cptr2, C.size_t(len(indicationMsg.IndMessage)))\r
532         if  indicationmessage == nil {\r
533                 return errors.New("e2sm wrapper is unable to get IndicationMessage due to wrong or invalid input")\r
534         }\r
535         defer C.e2sm_free_ric_indication_message(indicationmessage)\r
536         IndMsgType := int32(indicationmessage.indicationMessage_formats.present)\r
537         if IndMsgType==1  {//parsing cell metrics\r
538                 fmt.Printf(" parsing for cell metrics\n" )\r
539                 indMsgFormat1_C := *(**C.E2SM_KPM_IndicationMessage_Format1_t)(unsafe.Pointer(&indicationmessage.indicationMessage_formats.choice[0]))\r
540                 no_of_cell:=int32(indMsgFormat1_C .measData.list.count)\r
541                 fmt.Printf(" \n No of cell = %d\n",no_of_cell )\r
542                 //fmt.Println(no_of_cell)\r
543                 for n := int32(0); n < no_of_cell; n++ {\r
544                                 var sizeof_MeasurementDataItem_t  *C.MeasurementDataItem_t\r
545                                 MeasurementDataItem_C:=*(**C.MeasurementDataItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(indMsgFormat1_C.measData.list.array)) + (uintptr)(int(n))*unsafe.Sizeof(sizeof_MeasurementDataItem_t)))\r
546                                 no_of_cell_metrics:=int32(MeasurementDataItem_C.measRecord.list.count)\r
547                                 var CellM CellMetricsEntry\r
548                                 v := reflect.ValueOf(CellM)\r
549                                 fmt.Printf(" \n No of cell metrics = %d\n",no_of_cell_metrics)\r
550                                 values := make(map[string]interface{}, v.NumField())\r
551                                 //assert no_of_cell_metrics == v.NumField()   they both should be equal.\r
552                                 for i := int32(0); i < no_of_cell_metrics; i++ {\r
553                                         //fmt.Println(i)\r
554                                         if v.Field(int(i)).CanInterface() {\r
555                                                         var sizeof_MeasurementRecordItem_t *C.MeasurementRecordItem_t\r
556                                                         MeasurementRecordItem_C:=*(**C. MeasurementRecordItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(MeasurementDataItem_C.measRecord.list.array)) + (uintptr)(int(i))*unsafe.Sizeof(sizeof_MeasurementRecordItem_t)))\r
557                                                         type_var:=int(MeasurementRecordItem_C.present)\r
558                                                         if type_var==1{\r
559                                                                 var cast_integer *C.long = (*C.long)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
560                                                                 values[v.Type().Field(int(i)).Name]=int32(*cast_integer)\r
561                                                                 }else if type_var==2{\r
562                                 var cast_float *C.double = (*C.double)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
563                                 values[v.Type().Field(int(i)).Name]=float64(*cast_float)\r
564                                                         }else{\r
565                                                         fmt.Printf("Wrong Data Type")\r
566                                                 }\r
567 \r
568                                                 }else {\r
569                                                 fmt.Printf("sorry you have a unexported field (lower case) value you are trying to sneak past. Can not allow it: %v\n", v.Type().Field(int(i)).Name)\r
570                                                 }\r
571                                         }//end of inner for loop\r
572 \r
573 \r
574                                 fmt.Println(values)\r
575                                 fmt.Printf("Parsing Cell Metric Done")\r
576                                 //c.writeCellMetrics_db(&values)//push cellmetrics map entry to database.\r
577                         }//end of outer for loop\r
578                         //end of if IndMsgType==1 , parsing cell metrics done\r
579 \r
580         }  else if IndMsgType==2  { //parsing ue metrics\r
581 \r
582                 fmt.Printf(" parsing for UE metrics" )\r
583                 indMsgFormat2_C := *(**C.E2SM_KPM_IndicationMessage_Format2_t)(unsafe.Pointer(&indicationmessage.indicationMessage_formats.choice[0]))\r
584                 no_of_ue_metrics:=int32(indMsgFormat2_C .measData.list.count)\r
585                 fmt.Printf(" \n No of ue metrics = %d\n",no_of_ue_metrics)\r
586 \r
587                 var sizeof_MeasurementDataItem_t  *C.MeasurementDataItem_t\r
588                 MeasurementDataItem_C:=*(**C.MeasurementDataItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(indMsgFormat2_C.measData.list.array)) + (uintptr)(0)*unsafe.Sizeof(sizeof_MeasurementDataItem_t)))\r
589 \r
590                 no_of_ue:=int32(MeasurementDataItem_C.measRecord.list.count)\r
591                 fmt.Printf(" \n No of ue= %d\n",no_of_ue)\r
592                 for n := int32(0); n < no_of_ue; n++ {\r
593                                 var UeM UeMetricsEntry\r
594                                 v := reflect.ValueOf(UeM)\r
595                                 values := make(map[string]interface{}, v.NumField())\r
596                                 //assert no_of_ue_metrics == v.NumField()   they both should be equal.\r
597                                 for i := int32(0); i < no_of_ue_metrics; i++ {\r
598                                 //fmt.Println(i)\r
599                                 if v.Field(int(i)).CanInterface() {\r
600 \r
601                                         var sizeof_MeasurementDataItem_t  *C.MeasurementDataItem_t\r
602                                         MeasurementDataItem_C:=*(**C.MeasurementDataItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(indMsgFormat2_C.measData.list.array)) + (uintptr)(i)*unsafe.Sizeof(sizeof_MeasurementDataItem_t)))\r
603                                         var sizeof_MeasurementRecordItem_t *C.MeasurementRecordItem_t\r
604                                         MeasurementRecordItem_C:=*(**C.MeasurementRecordItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(MeasurementDataItem_C.measRecord.list.array)) + (uintptr)(n)*unsafe.Sizeof(sizeof_MeasurementRecordItem_t)))\r
605 \r
606                                         type_var:=int(MeasurementRecordItem_C.present)\r
607                                 if type_var==1{\r
608                                         var cast_integer *C.long = (*C.long)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
609                                         values[v.Type().Field(int(i)).Name]=int32(*cast_integer)\r
610                                 }else if type_var==2{\r
611                                         var cast_float *C.double = (*C.double)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
612                                         values[v.Type().Field(int(i)).Name]=float64(*cast_float)\r
613 \r
614                                         }else{\r
615                                         fmt.Printf("Wrong Data Type")\r
616                                 }\r
617 \r
618                         }else {\r
619                                 fmt.Printf("sorry you have a unexported field (lower case) value you are trying to sneak past. Can not allow it: %v\n", v.Type().Field(int(i)).Name)\r
620                                 }\r
621 \r
622 \r
623                                         }       //end of inner for loop\r
624                         fmt.Println(values)\r
625                          fmt.Printf("Parsing UE Metric Done")\r
626                          //c.writeUeMetrics_db(&values)//push UEmetrics map entry to database.\r
627 \r
628                         }// end of outer for loop\r
629         //parsing ue metrics done\r
630         }else{\r
631                 fmt.Printf(" Invalid Indication message format" )\r
632 \r
633         }\r
634 \r
635 \r
636         return nil\r
637 }\r
638 \r
639 func (c *Control) controlLoop() {\r
640         //Handle receiving message based on message type\r
641         for {\r
642                 msg := <-c.RMR\r
643                 xapp.Logger.Debug("Received message type: %d", msg.Mtype)\r
644                 switch msg.Mtype {\r
645                 case xapp.RIC_INDICATION:\r
646                         go c.handleIndication(msg)\r
647                 default:\r
648                         xapp.Logger.Error("Unknown Message Type '%d', discarding", msg.Mtype)\r
649                 }\r
650         }\r
651 }\r
652 func (c *Control) handleIndication(params *xapp.RMRParams) (err error) {\r
653         var e2ap *E2ap\r
654         //var e2sm *E2sm\r
655 \r
656         indicationMsg, err := e2ap.GetIndicationMessage(params.Payload)\r
657         if err != nil {\r
658                 xapp.Logger.Error("Failed to decode RIC Indication message: %v", err)\r
659                 return\r
660         }\r
661 \r
662         log.Printf("RIC Indication message from {%s} received", params.Meid.RanName)\r
663         /*\r
664                 indicationHdr, err := e2sm.GetIndicationHeader(indicationMsg.IndHeader)\r
665                 if err != nil {\r
666                         xapp.Logger.Error("Failed to decode RIC Indication Header: %v", err)\r
667                         return\r
668                 }\r
669         */\r
670 \r
671         //Decoding message and put information into log\r
672         //log.Printf("-----------RIC Indication Header-----------")\r
673         //log.Printf("indicationMsg.IndHeader= %x", indicationMsg.IndHeader)\r
674 /*\r
675         buf := new(bytes.Buffer) //create my buffer\r
676         binary.Write(buf, binary.LittleEndian, indicationMsg.IndHeader)\r
677         log.Printf("binary Write buf= %x",buf )\r
678         b := buf.Bytes()\r
679         //str := buf.String()\r
680         //log.Printf(" buf Strin()= %s",str )\r
681         //cptr1:= unsafe.Pointer(C.CString(str))\r
682         cptr1:= unsafe.Pointer(&b[0])\r
683         defer C.free(cptr1)\r
684 */\r
685         cptr1 := unsafe.Pointer(&indicationMsg.IndHeader[0])\r
686         decodedHdr := C.e2sm_decode_ric_indication_header(cptr1, C.size_t(len(indicationMsg.IndHeader)))\r
687         //decodedHdr := C.e2sm_decode_ric_indication_header(cptr1, C.size_t(len(str)))\r
688         //decodedHdr := C.e2sm_decode_ric_indication_header(cptr1, C.size_t(buf.Len()))\r
689         if decodedHdr == nil {\r
690                 return errors.New("e2sm wrapper is unable to get IndicationHeader due to wrong or invalid input")\r
691         }\r
692         defer C.e2sm_free_ric_indication_header(decodedHdr)\r
693         IndHdrType := int32(decodedHdr.indicationHeader_formats.present)\r
694         if IndHdrType==0{\r
695                 log.Printf("No Indication Header present")\r
696         }\r
697         if IndHdrType==1{\r
698                 log.Printf("Indication Header format = %d",IndHdrType)\r
699                 /*\r
700                 indHdrFormat1_C := *(**C.E2SM_KPM_IndicationHeader_Format1_t)(unsafe.Pointer(&decodedHdr.indicationHeader_formats.choice[0]))\r
701                 //senderName_C := (*C.PrintableString_t)(unsafe.Pointer(indHdrFormat1_C.senderName))\r
702                 senderName_C:=indHdrFormat1_C.senderName\r
703                 var senderName []byte\r
704                 senderName = C.GoBytes(unsafe.Pointer(senderName_C.buf), C.int(senderName_C.size))\r
705                 //log.Printf("Sender Name = %x",senderName)\r
706 \r
707                 //senderType_C := (*C.PrintableString_t)(unsafe.Pointer(indHdrFormat1_C.senderType))\r
708                 senderType_C :=indHdrFormat1_C.senderType\r
709                 //senderType []byte\r
710                 senderType := C.GoBytes(unsafe.Pointer(senderType_C.buf), C.int(senderType_C.size))\r
711                 //log.Printf("Sender Type = %x",senderType)\r
712 \r
713                 //vendorName_C := (*C.PrintableString_t)(unsafe.Pointer(indHdrFormat1_C.vendorName))\r
714                 vendorName_C :=indHdrFormat1_C.vendorName\r
715                 //vendorName  []byte\r
716                 vendorName := C.GoBytes(unsafe.Pointer(vendorName_C.buf), C.int(vendorName_C.size))\r
717                 //log.Printf("Vendor Name = %x",vendorName)\r
718                 */\r
719 \r
720         }\r
721 \r
722         /*\r
723                 indMsg, err := e2sm.GetIndicationMessage(indicationMsg.IndMessage)\r
724                 if err != nil {\r
725                         xapp.Logger.Error("Failed to decode RIC Indication Message: %v", err)\r
726                         return\r
727                 }\r
728         */\r
729         //log.Printf("-----------RIC Indication Message-----------")\r
730         //log.Printf("indicationMsg.IndMessage= %x",indicationMsg.IndMessage)\r
731         cptr2 := unsafe.Pointer(&indicationMsg.IndMessage[0])\r
732         indicationmessage := C.e2sm_decode_ric_indication_message(cptr2, C.size_t(len(indicationMsg.IndMessage)))\r
733         if  indicationmessage == nil {\r
734                 return errors.New("e2sm wrapper is unable to get IndicationMessage due to wrong or invalid input")\r
735         }\r
736         defer C.e2sm_free_ric_indication_message(indicationmessage)\r
737         IndMsgType := int32(indicationmessage.indicationMessage_formats.present)\r
738         if IndMsgType==1  {//parsing cell metrics\r
739                 fmt.Printf(" parsing for cell metrics\n" )\r
740                 indMsgFormat1_C := *(**C.E2SM_KPM_IndicationMessage_Format1_t)(unsafe.Pointer(&indicationmessage.indicationMessage_formats.choice[0]))\r
741                 no_of_cell:=int32(indMsgFormat1_C .measData.list.count)\r
742                 fmt.Printf(" \n No of cell = %d\n",no_of_cell )\r
743                 //fmt.Println(no_of_cell)\r
744                 for n := int32(0); n < no_of_cell; n++ {\r
745                                 var sizeof_MeasurementDataItem_t  *C.MeasurementDataItem_t\r
746                                 MeasurementDataItem_C:=*(**C.MeasurementDataItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(indMsgFormat1_C.measData.list.array)) + (uintptr)(int(n))*unsafe.Sizeof(sizeof_MeasurementDataItem_t)))\r
747                                 no_of_cell_metrics:=int32(MeasurementDataItem_C.measRecord.list.count)\r
748                                 //var CellM CellMetricsEntry\r
749                                 //v := reflect.ValueOf(CellM)\r
750                                 fmt.Printf(" \n No of cell metrics = %d\n",no_of_cell_metrics)\r
751                                 //values := make(map[string]interface{}, v.NumField())\r
752                                 values := make(map[string]interface{})\r
753                                 CellKpi:=ranCellKpi[params.Meid.RanName]\r
754                                 var tmpCellKpi []string\r
755                                 for _, ele:=range CellKpi{\r
756                                 if ele!=""{\r
757                                                 tmpCellKpi =append(tmpCellKpi,ele)\r
758                                         }\r
759                                 }\r
760 \r
761                                 //assert no_of_cell_metrics == v.NumField()   they both should be equal.\r
762                                 if (int(no_of_cell_metrics) != len(tmpCellKpi)){\r
763                          log.Printf("no_of_cell_metrics !=  len(tmpCellKpi)")\r
764                         return errors.New("no_of_cell_metrics  !=len(tmpCellKpi)")\r
765                                 }\r
766                                 for i := int32(0); i < no_of_cell_metrics; i++ {\r
767                                         //fmt.Println(i)\r
768                                         //if v.Field(int(i)).CanInterface() {\r
769                                                         var sizeof_MeasurementRecordItem_t *C.MeasurementRecordItem_t\r
770                                                         MeasurementRecordItem_C:=*(**C. MeasurementRecordItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(MeasurementDataItem_C.measRecord.list.array)) + (uintptr)(int(i))*unsafe.Sizeof(sizeof_MeasurementRecordItem_t)))\r
771                                                         type_var:=int(MeasurementRecordItem_C.present)\r
772                                                         if type_var==1{\r
773                                                                 var cast_integer *C.long = (*C.long)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
774                                                                 //values[v.Type().Field(int(i)).Name]=int32(*cast_integer)\r
775                                                                 values[tmpCellKpi[int(i)]]=int32(*cast_integer)\r
776                                                                 }else if type_var==2{\r
777                                 var cast_float *C.double = (*C.double)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
778                                 //values[v.Type().Field(int(i)).Name]=float64(*cast_float)\r
779                                 values[tmpCellKpi[int(i)]]=float64(*cast_float)\r
780                                                         }else{\r
781                                                         fmt.Printf("Wrong Data Type")\r
782                                                 }\r
783 \r
784                                                // }else {\r
785                                                 //fmt.Printf("sorry you have a unexported field (lower case) value you are trying to sneak past. Can not allow it: %v\n", v.Type().Field(int(i)).Name)\r
786                                                 //}\r
787                                         }//end of inner for loop\r
788 \r
789 \r
790                                 fmt.Println(values)\r
791                                 fmt.Printf("Parsing Cell Metric Done")\r
792                                 c.writeCellMetrics_db(&values)//push cellmetrics map entry to database.\r
793                         }//end of outer for loop\r
794                         //end of if IndMsgType==1 , parsing cell metrics done\r
795 \r
796         }  else if IndMsgType==2  { //parsing ue metrics\r
797 \r
798                 fmt.Printf(" parsing for UE metrics" )\r
799                 indMsgFormat2_C := *(**C.E2SM_KPM_IndicationMessage_Format2_t)(unsafe.Pointer(&indicationmessage.indicationMessage_formats.choice[0]))\r
800                 //no_of_ue_metrics:=int32(indMsgFormat2_C .measData.list.count)\r
801                 //fmt.Printf(" \n No of ue metrics = %d\n",no_of_ue_metrics)\r
802 \r
803                 var sizeof_MeasurementDataItem_t  *C.MeasurementDataItem_t\r
804                 MeasurementDataItem_C:=*(**C.MeasurementDataItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(indMsgFormat2_C.measData.list.array)) + (uintptr)(0)*unsafe.Sizeof(sizeof_MeasurementDataItem_t)))\r
805                 UeKpi:=ranUeKpi[params.Meid.RanName]\r
806                                 var tmpUeKpi []string\r
807                                 for _, ele:=range UeKpi{\r
808                                 if ele!=""{\r
809                                                 tmpUeKpi =append(tmpUeKpi,ele)\r
810                                         }\r
811                                 }\r
812 \r
813                 \r
814                 no_of_ue:=int32(MeasurementDataItem_C.measRecord.list.count)/int32(len(tmpUeKpi))\r
815                 no_of_ue_metrics:=int32(len(tmpUeKpi))\r
816                 fmt.Printf(" \n No of ue= %d\n",no_of_ue)\r
817 \r
818                 for n := int32(0); n < no_of_ue; n++ {\r
819                                 //var UeM UeMetricsEntry\r
820                                 //v := reflect.ValueOf(UeM)\r
821                                 //values := make(map[string]interface{}, v.NumField())\r
822                                 values := make(map[string]interface{})\r
823                                 //assert no_of_ue_metrics == v.NumField()   they both should be equal.\r
824                                 //if (int(no_of_ue_metrics) != v.NumField()){\r
825                          //log.Printf("no_of_ue_metrics != v.NumField()")\r
826                          //return errors.New("no_of_ue_metrics != v.NumField()")\r
827                                 //}\r
828                                 for i := int32(0); i < no_of_ue_metrics; i++ {\r
829                                 //fmt.Println(i)\r
830                                 //if v.Field(int(i)).CanInterface() {\r
831 \r
832                                         //var sizeof_MeasurementDataItem_t  *C.MeasurementDataItem_t\r
833                                         //MeasurementDataItem_C:=*(**C.MeasurementDataItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(indMsgFormat2_C.measData.list.array)) + (uintptr)(i)*unsafe.Sizeof(sizeof_MeasurementDataItem_t)))\r
834 \r
835                                         var sizeof_MeasurementRecordItem_t *C.MeasurementRecordItem_t\r
836                                         MeasurementRecordItem_C:=*(**C.MeasurementRecordItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(MeasurementDataItem_C.measRecord.list.array)) + (uintptr)(n+(i*no_of_ue))*unsafe.Sizeof(sizeof_MeasurementRecordItem_t)))\r
837                                         //var sizeof_MeasurementRecordItem_t *C.MeasurementRecordItem_t\r
838                                         //MeasurementRecordItem_C:=*(**C.MeasurementRecordItem_t)(unsafe.Pointer(uintptr(unsafe.Pointer(MeasurementDataItem_C.measRecord.list.array)) + (uintptr)(n)*unsafe.Sizeof(sizeof_MeasurementRecordItem_t)))\r
839 \r
840                                         type_var:=int(MeasurementRecordItem_C.present)\r
841                                 if type_var==1{\r
842                                         var cast_integer *C.long = (*C.long)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
843                                         values[tmpUeKpi[int(i)]]=int32(*cast_integer)\r
844                                 }else if type_var==2{\r
845                                         var cast_float *C.double = (*C.double)(unsafe.Pointer(&MeasurementRecordItem_C.choice[0]))\r
846                                         values[tmpUeKpi[int(i)]]=float64(*cast_float)\r
847 \r
848                                         }else{\r
849                                         fmt.Printf("Wrong Data Type")\r
850                                 }\r
851 \r
852                        // }else {\r
853                                // fmt.Printf("sorry you have a unexported field (lower case) value you are trying to sneak past. Can not allow it: %v\n", v.Type().Field(int(i)).Name)\r
854                                // }\r
855 \r
856 \r
857                                         }       //end of inner for loop\r
858                         fmt.Println(values)\r
859                          fmt.Printf("Parsing UE Metric Done")\r
860                          c.writeUeMetrics_db(&values)//push UEmetrics map entry to database.\r
861 \r
862                         }// end of outer for loop\r
863         //parsing ue metrics done\r
864         }else{\r
865                 fmt.Printf(" Invalid Indication message format" )\r
866 \r
867         }\r
868 \r
869 \r
870         return nil\r
871 \r
872 \r
873 }\r
874 \r
875 // func (c *Control) writeUeMetrics_db(ueMetrics UeMetricsEntry) {\r
876 //      //Write metric to InfluxDB using API\r
877 //      writeAPI := c.client.WriteAPIBlocking("my-org", "kpimon")\r
878 //      ueMetricsJSON, err := json.Marshal(ueMetrics)\r
879 //      if err != nil {\r
880 //              xapp.Logger.Error("Marshal UE Metrics failed!")\r
881 //      }\r
882 //      p := influxdb2.NewPointWithMeasurement("ricIndication_UeMetrics").\r
883 //              AddField("UE Metrics", ueMetricsJSON).\r
884 //              SetTime(time.Now())\r
885 //      writeAPI.WritePoint(context.Background(), p)\r
886 //      xapp.Logger.Info("Wrote UE Metrics to InfluxDB")\r
887 // }\r
888 \r
889 // func (c *Control) writeCellMetrics_db(cellMetrics CellMetricsEntry) {\r
890 //      writeAPI := c.client.WriteAPIBlocking("my-org", "kpimon")\r
891 //      cellMetricsJSON, er := json.Marshal(cellMetrics)\r
892 //      if er != nil {\r
893 //              xapp.Logger.Error("Marshal Cell Metrics failed!")\r
894 //      }\r
895 //      p := influxdb2.NewPointWithMeasurement("ricIndication_cellMetrics").\r
896 //              AddField("Cell Metrics", cellMetricsJSON).\r
897 //              SetTime(time.Now())\r
898 //      writeAPI.WritePoint(context.Background(), p)\r
899 //      xapp.Logger.Info("Wrote Cell Metrics to InfluxDB")\r
900 // }\r
901 \r
902 /*\r
903 func (c *Control) queryUEReports() {\r
904         log.Printf("query UE")\r
905         resp, err := http.Get("http://10.244.0.68/sba/influx/query?db=RIC-Test-static&q=select+*+from+UEReports")\r
906         if err != nil {\r
907                 log.Fatalln(err)\r
908         }\r
909         body, err := ioutil.ReadAll(resp.Body)\r
910         if err != nil {\r
911                 log.Fatalln(err)\r
912         }\r
913         var results ViaviMessages\r
914         er := json.Unmarshal(body, &results)\r
915         if er != nil {\r
916                 panic(er)\r
917         }\r
918         //      sb := string(body)\r
919         //    log.Printf(sb)\r
920         c.writeUeMetrics_db(results.results.series)\r
921 }\r
922 \r
923 func (c *Control) queryCellReports() {\r
924         log.Printf("query cell")\r
925         resp, err := http.Get("http://10.244.0.68/sba/influx/query?db=RIC-Test-static&q=select+*+from+CellReports")\r
926         if err != nil {\r
927                 log.Fatalln(err)\r
928         }\r
929         body, err := ioutil.ReadAll(resp.Body)\r
930         if err != nil {\r
931                 log.Fatalln(err)\r
932         }\r
933         var results ViaviMessages\r
934         er := json.Unmarshal(body, &results)\r
935         if er != nil {\r
936                 panic(er)\r
937         }\r
938         c.writeCellMetrics_db(results.results.series)\r
939 }\r
940 */\r
941 func (c *Control) writeUeMetrics_db(ueMetrics *map[string]interface{}) {\r
942         writeAPI := c.client.WriteAPIBlocking("my-org", "kpimon")\r
943         /*\r
944         ueMetricsJSON, err := json.Marshal(ueMetrics)\r
945         if err != nil {\r
946                 xapp.Logger.Error("Marshal UE Metrics failed!")\r
947         }\r
948         \r
949         p := influxdb2.NewPointWithMeasurement("UeMetrics").\r
950                 AddField("UE Metrics", ueMetricsJSON).\r
951                 SetTime(time.Now())\r
952         */\r
953         p := influxdb2.NewPointWithMeasurement("UeMetrics").SetTime(time.Now())\r
954         for key, value := range *ueMetrics {\r
955                 p = p.AddField(key, value)\r
956         }\r
957 \r
958         writeAPI.WritePoint(context.Background(), p)\r
959         xapp.Logger.Info("Wrote UE Metrics to InfluxDB")\r
960 }\r
961 \r
962 func (c *Control) writeCellMetrics_db(cellMetrics *map[string]interface{}) {\r
963         writeAPI := c.client.WriteAPIBlocking("my-org", "kpimon")\r
964         /*\r
965         cellMetricsJSON, er := json.Marshal(cellMetrics)\r
966         if er != nil {\r
967                 xapp.Logger.Error("Marshal Cell Metrics failed!")\r
968         }\r
969         \r
970         p := influxdb2.NewPointWithMeasurement("cellMetrics").\r
971                 AddField("Cell Metrics", cellMetricsJSON).\r
972                 SetTime(time.Now())\r
973         */\r
974         p := influxdb2.NewPointWithMeasurement("cellMetrics").SetTime(time.Now())\r
975         for key, value := range *cellMetrics {\r
976                 p = p.AddField(key, value)\r
977         }\r
978         writeAPI.WritePoint(context.Background(), p)\r
979         xapp.Logger.Info("Wrote Cell Metrics to InfluxDB")\r
980 }\r
981 \r
982 func (c Control) xAppStartCB(d interface{}) {\r
983                 xapp.Logger.Info("In callback KPI monitor xApp ...")\r
984 \r
985                 //ranUeKpi ranCellKpi   \r
986                 // Get eNodeB list\r
987                 nbList := c.getnbList()\r
988 \r
989                 for _, nb := range nbList {\r
990                 if nb.ConnectionStatus == 1 {\r
991                         xapp.Logger.Info("Building ranCellKp for %v", nb.InventoryName)\r
992                         link:="http://service-ricplt-e2mgr-http.ricplt.svc.cluster.local:3800/v1/nodeb/"\r
993                         link=link+nb.InventoryName\r
994                         tmpr,err := http.Get(link)\r
995                         if err != nil {\r
996                                 log.Fatalln(err)\r
997                                 panic("Unable to build ranCellKpi")\r
998                         }\r
999                         defer tmpr.Body.Close()\r
1000                         var resp E2mgrResponse\r
1001 \r
1002                         err=json.NewDecoder(tmpr.Body).Decode(&resp)\r
1003                         if err != nil {\r
1004                                 log.Fatalln(err)\r
1005                                 panic("Unable to build ranCellKpi")\r
1006 \r
1007                         }\r
1008                         counter:=0\r
1009                         //RanFunctionId=2 for kpm in viavi\r
1010                         for i := 0; i < len(resp.Gnb.RanFunctions); i++ {\r
1011                                 if resp.Gnb.RanFunctions[i].RanFunctionId  == 2 {\r
1012                                         counter = i\r
1013                                         break\r
1014                                 }\r
1015                         }\r
1016                         cString := C.CString(resp.Gnb.RanFunctions[counter].RanFunctionDefinition)\r
1017                         defer C.free(unsafe.Pointer(cString)) // Free the allocated C string when done\r
1018                         result:=C.buildRanCellUeKpi(cString)\r
1019                         \r
1020                         ueSlice := make([]string, result.ueKpiSize)\r
1021                         \r
1022                         for _, v := range unsafe.Slice(result.ueKpi, result.ueKpiSize) {\r
1023                                 ueSlice = append(ueSlice, C.GoString(v))\r
1024                         }\r
1025                         ranUeKpi[nb.InventoryName]=ueSlice\r
1026                         //fmt.Println("len of ranUeKpi= ",len(ranUeKpi))\r
1027                         //fmt.Println("ranUeKpi map = ", ranUeKpi)\r
1028 \r
1029 \r
1030                         cellSlice := make([]string, result.cellKpiSize)\r
1031 \r
1032                         for _, v := range unsafe.Slice(result.cellKpi, result.cellKpiSize) {\r
1033                                 cellSlice = append(cellSlice, C.GoString(v))\r
1034                         }\r
1035                         ranCellKpi[nb.InventoryName]=cellSlice\r
1036                         //fmt.Println("len of ranCellKpi= ",len(ranCellKpi))\r
1037                         //fmt.Println("ranCellKpi map = ", ranCellKpi)\r
1038                         /*\r
1039                         counter = 0\r
1040                         for i := 0; i < len(resp.Gnb.NodeConfigs); i++ {\r
1041                                 if resp.Gnb.NodeConfigs[i].E2nodeComponentInterfaceType == "f1" {\r
1042                                         counter = i\r
1043                                         break\r
1044                                 }\r
1045                         }\r
1046                         \r
1047         \r
1048                         tm := resp.Gnb.NodeConfigs[counter].E2nodeComponentRequestPart\r
1049                         base64Text := make([]byte, base64.StdEncoding.DecodedLen(len(tm)))\r
1050                         nl, _ := base64.StdEncoding.Decode(base64Text, []byte(tm))\r
1051                         message := string(base64Text[:nl])\r
1052                         */\r
1053                         /*\r
1054                         cString2 := C.CString(message)\r
1055                         fmt.Println("Ponga = ",tm)\r
1056                         fmt.Println("Ponga = ",message)\r
1057                         defer C.free(unsafe.Pointer(cString2)) // Free the allocated C string when done\r
1058                         result2:=C.decodeF1apGetCellIds(cString2)\r
1059                         cellList:=make([]string, result2.size)\r
1060                         for _, v := range unsafe.Slice(result2.cellids, result2.size) {\r
1061                                 cellList = append(cellList, C.GoString(v))\r
1062                         }\r
1063                         plmnList:=make([]string, result2.size)\r
1064                         for _, v := range unsafe.Slice(result2.plmn, result2.size) {\r
1065                                 plmnList = append(plmnList, C.GoString(v))\r
1066                         }\r
1067 \r
1068                         */\r
1069                         /*\r
1070                         var f1ap *F1ap\r
1071                         fmt.Println("Ponga = ",tm)\r
1072                         cellList,plmnList:=f1ap.F1apGetCellIds(message)\r
1073                         Glob_Ran_cell[nb.InventoryName]=cellList\r
1074                         for i := 0; i < len(cellList); i++ {\r
1075                                 Glob_cell_Plmn[cellList[i]]=plmnList[i]\r
1076                         }\r
1077                         */\r
1078                          response, err4 := http.Get("http://localhost:8090/getdata?gnbid="+nb.InventoryName)\r
1079    if err4 != nil {\r
1080        fmt.Println("Error:", err4)\r
1081        panic(err4)\r
1082    }\r
1083    defer response.Body.Close()\r
1084    \r
1085    var response2 APIResponse\r
1086    if err5 := json.NewDecoder(response.Body).Decode(&response2); err5 != nil {\r
1087        fmt.Println("Error decoding response:", err5)\r
1088         panic(err5)\r
1089    }\r
1090    \r
1091    fmt.Printf("f1apSever Response: %+v\n", response2)\r
1092    \r
1093                         Glob_Ran_cell[nb.InventoryName]=response2.Cellids\r
1094                         for i := 0; i < len(response2.Cellids); i++ {\r
1095                                 Glob_cell_Plmn[response2.Cellids[i]]=response2.PlmnIds[i]\r
1096                         }\r
1097 \r
1098 \r
1099                         //C.freeMemorydRanCellUeKpi(result)\r
1100 \r
1101 \r
1102                 }\r
1103 \r
1104         }\r
1105 \r
1106         for {\r
1107                 time.Sleep(5 * time.Second)\r
1108                 if xapp.IsRegistered() {\r
1109                         xapp.Logger.Info("App registration is done, ready to send subscription request.")\r
1110                         break\r
1111                 }\r
1112                 xapp.Logger.Debug("App registration is not done yet, sleep 5s and check again")\r
1113         }\r
1114         // Send subscription request to connected NodeB\r
1115         for _, nb := range nbList {\r
1116                 if nb.ConnectionStatus == 1 {\r
1117                         xapp.Logger.Info("Before send subscription request to %v", nb.InventoryName)\r
1118                         c.sendSubscription(nb.InventoryName)\r
1119                         xapp.Logger.Info("After send subscription request to %v", nb.InventoryName)\r
1120                 }\r
1121 \r
1122         }\r
1123 \r
1124                         fmt.Println("len of ranUeKpi= ",len(ranUeKpi))\r
1125                         fmt.Println("ranUeKpi map = ", ranUeKpi)\r
1126 \r
1127                          fmt.Println("len of ranCellKpi= ",len(ranCellKpi))\r
1128                         fmt.Println("ranCellKpi map = ", ranCellKpi)\r
1129 \r
1130                         fmt.Println("len of Glob_cell_Plmn= ",len(Glob_cell_Plmn))\r
1131                         fmt.Println("Glob_cell_Plmn map = ", Glob_cell_Plmn)\r
1132 \r
1133 \r
1134         go c.controlLoop()\r
1135         //go c.queryUEReports()\r
1136         //go c.queryCellReports()\r
1137         xapp.Logger.Info("End callback KPI monitor xApp ...")\r
1138 }\r
1139 \r
1140 func (c Control) Run() {\r
1141         // Setup level\r
1142         xapp.Logger.SetLevel(xapp.Config.GetInt("logger.level"))\r
1143         // Register callback\r
1144         xapp.Logger.Info("In Run() ...")\r
1145         xapp.SetReadyCB(c.xAppStartCB, true)\r
1146         // Start xApp\r
1147         xapp.Run(c)\r
1148 }\r