Support for symptomdata collection
[ric-plt/e2mgr.git] / E2Manager / controllers / symptomdata_controller.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
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 package controllers
22
23 import (
24         "e2mgr/logger"
25         "e2mgr/managers"
26         "e2mgr/models"
27         "e2mgr/providers/httpmsghandlerprovider"
28         "e2mgr/services"
29         "encoding/json"
30         "net/http"
31
32         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
33 )
34
35 type ISymptomdataController interface {
36         GetSymptomData(writer http.ResponseWriter, r *http.Request)
37 }
38
39 type SymptomdataController struct {
40         logger          *logger.Logger
41         handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider
42         rNibDataService services.RNibDataService
43         ranListManager  managers.RanListManager
44 }
45
46 func NewSymptomdataController(l *logger.Logger, hp *httpmsghandlerprovider.IncomingRequestHandlerProvider, rnib services.RNibDataService, rl managers.RanListManager) *SymptomdataController {
47         return &SymptomdataController{
48                 logger:          l,
49                 handlerProvider: hp,
50                 rNibDataService: rnib,
51                 ranListManager:  rl,
52         }
53 }
54
55 func (s *SymptomdataController) GetSymptomData(w http.ResponseWriter, r *http.Request) {
56         e2TList := s.handleRequest(httpmsghandlerprovider.GetE2TInstancesRequest)
57         nodeBList := s.ranListManager.GetNbIdentityList()
58
59         s.logger.Infof("nodeBList=%+v, e2TList=%+v", nodeBList, e2TList)
60
61         var NodebInfoList []entities.NodebInfo
62         for _, v := range nodeBList {
63                 if n, err := s.rNibDataService.GetNodeb(v.InventoryName); err == nil {
64                         NodebInfoList = append(NodebInfoList, *n)
65                 }
66         }
67         s.logger.Infof("NodebInfoList=%+v", NodebInfoList)
68
69         symptomdata := struct {
70                 E2TList       models.IResponse       `json:"e2TList"`
71                 NodeBList     []*entities.NbIdentity `json:"nodeBList"`
72                 NodebInfoList []entities.NodebInfo   `json:"nodeBInfo"`
73         }{
74                 e2TList,
75                 nodeBList,
76                 NodebInfoList,
77         }
78
79         w.Header().Set("Content-Type", "application/json")
80         w.Header().Set("Content-Disposition", "attachment; filename=platform/e2_info.json")
81         w.WriteHeader(http.StatusOK)
82         resp, _ := json.MarshalIndent(symptomdata, "", "    ")
83         w.Write(resp)
84 }
85
86 func (s *SymptomdataController) handleRequest(requestName httpmsghandlerprovider.IncomingRequest) models.IResponse {
87         handler, err := s.handlerProvider.GetHandler(requestName)
88         if err != nil {
89                 return nil
90         }
91
92         resp, err := handler.Handle(nil)
93         return resp
94 }