Merge "Getting node for which e2 reset has to be applied"
[ric-plt/e2mgr.git] / E2Manager / app / main.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package main
21
22 import (
23         "e2mgr/clients"
24         "e2mgr/configuration"
25         "e2mgr/controllers"
26         "e2mgr/httpserver"
27         "e2mgr/logger"
28         "e2mgr/managers"
29         "e2mgr/managers/notificationmanager"
30         "e2mgr/providers/httpmsghandlerprovider"
31         "e2mgr/providers/rmrmsghandlerprovider"
32         "e2mgr/rNibWriter"
33         "e2mgr/rmrCgo"
34         "e2mgr/services"
35         "e2mgr/services/rmrreceiver"
36         "e2mgr/services/rmrsender"
37         //"fmt"
38         "flag"
39         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
40         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
41         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
42         "github.com/spf13/viper"
43         "github.com/fsnotify/fsnotify"
44         "os"
45         "strconv"
46 )
47
48 const GeneralKeyDefaultValue = "{\"enableRic\":true}"
49 const DEFAULT_CONFIG_FILE = "../resources/configuration.yaml"
50 const DEFAULT_PORT = "8080"
51 var Log *logger.Logger
52
53 func initKeys(logger *logger.Logger, sdl *sdlgo.SyncStorage) error {
54         ok, err := sdl.SetIfNotExists(common.GetRNibNamespace(), common.BuildGeneralConfigurationKey(), GeneralKeyDefaultValue)
55
56         if err != nil {
57                 logger.Errorf("#app.main - Failed setting GENERAL key")
58                 return err
59         }
60
61         if ok {
62                 logger.Infof("#app.main - Successfully set GENERAL key")
63         } else {
64                 logger.Infof("#app.main - GENERAL key exists, no need to set")
65         }
66
67         return nil
68
69 }
70 /**Dynamic log-level changes **/
71
72 func loadConfig() {
73         viper.SetConfigFile(parseCmd())
74         if err := viper.ReadInConfig(); err != nil {
75                 Log.Errorf("Error reading config file, %s", err)
76         }
77         Log.Infof("Using config file: %s\n", viper.ConfigFileUsed())
78         // Watch for config file changes and re-read data ...
79         watch()
80 }
81 func parseCmd() string {
82         var fileName *string
83         fileName = flag.String("f", DEFAULT_CONFIG_FILE, "Specify the configuration file.")
84         flag.String("port", DEFAULT_PORT, "Specify the port file.")
85         flag.Parse()
86
87         return *fileName
88 }
89
90 func watch() {
91         viper.WatchConfig()
92         viper.OnConfigChange(func(e fsnotify.Event) {
93                 Log.Infof("config file changed  %s", e.Name)
94                 setLoglevel()
95         })
96 }
97
98 /*********MDC LOG CHNAGES ********/
99 func setLoglevel() {
100     var loglevel int
101     if err := viper.UnmarshalKey("loglevel", &loglevel); err != nil {
102         Log.Errorf("Unmarshalling failed while reading %d", loglevel)
103     }
104
105     switch loglevel {
106     case 1:
107         Log.Infof("LOGLEVEL is set to ERROR\n")
108     case 2:
109         Log.Infof("LOGLEVEL is set to WARNING\n")
110     case 3:
111         Log.Infof("LOGLEVEL is set to INFO\n")
112     case 4:
113         Log.Infof("LOGLEVEL is set to DEBUG\n")
114     }
115     Log.SetLevel(loglevel)
116 }
117
118
119 func main() {
120         config := configuration.ParseConfiguration()
121         level := int8(4)
122         Log, _ = logger.InitLogger(level)
123         Log.SetFormat(0)
124         Log.SetMdc("e2mgr", "0.2.2")
125         /*if err != nil {
126                 fmt.Printf("#app.main - failed to initialize logger, error: %s", err)
127                 os.Exit(1)
128         }*/
129         Log.Infof("#app.main - Configuration %s", config)
130         loadConfig()
131         
132         setLoglevel()
133         sdl := sdlgo.NewSyncStorage()
134         err := initKeys(Log, sdl)
135
136         if err != nil {
137                 os.Exit(1)
138         }
139
140         defer sdl.Close()
141         rnibDataService := services.NewRnibDataService(Log, config, reader.GetNewRNibReader(sdl), rNibWriter.GetRNibWriter(sdl, config.RnibWriter))
142
143         ranListManager := managers.NewRanListManager(Log, rnibDataService)
144
145         err = ranListManager.InitNbIdentityMap()
146
147         if err != nil {
148                 Log.Errorf("#app.main - quit")
149                 os.Exit(1)
150         }
151
152         var msgImpl *rmrCgo.Context
153         rmrMessenger := msgImpl.Init("tcp:"+strconv.Itoa(config.Rmr.Port), config.Rmr.MaxMsgSize, 0, Log)
154         rmrSender := rmrsender.NewRmrSender(Log, rmrMessenger)
155         e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, Log)
156         routingManagerClient := clients.NewRoutingManagerClient(Log, config, clients.NewHttpClient())
157         ranAlarmService := services.NewRanAlarmService(Log, config)
158         ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(Log, rnibDataService, ranListManager, ranAlarmService)
159         e2tAssociationManager := managers.NewE2TAssociationManager(Log, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
160         e2tShutdownManager := managers.NewE2TShutdownManager(Log, config, rnibDataService, e2tInstancesManager, e2tAssociationManager, ranConnectStatusChangeManager)
161         e2tKeepAliveWorker := managers.NewE2TKeepAliveWorker(Log, rmrSender, e2tInstancesManager, e2tShutdownManager, config)
162         rmrNotificationHandlerProvider := rmrmsghandlerprovider.NewNotificationHandlerProvider()
163         rmrNotificationHandlerProvider.Init(Log, config, rnibDataService, rmrSender, e2tInstancesManager, routingManagerClient, e2tAssociationManager, ranConnectStatusChangeManager, ranListManager)
164
165         notificationManager := notificationmanager.NewNotificationManager(Log, rmrNotificationHandlerProvider)
166         rmrReceiver := rmrreceiver.NewRmrReceiver(Log, rmrMessenger, notificationManager)
167         nodebValidator := managers.NewNodebValidator()
168         updateEnbManager := managers.NewUpdateEnbManager(Log, rnibDataService, nodebValidator)
169         updateGnbManager := managers.NewUpdateGnbManager(Log, rnibDataService, nodebValidator)
170
171         e2tInstancesManager.ResetKeepAliveTimestampsForAllE2TInstances()
172
173         defer rmrMessenger.Close()
174
175         go rmrReceiver.ListenAndHandle()
176         go e2tKeepAliveWorker.Execute()
177
178         httpMsgHandlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(Log, rmrSender, config, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager, nodebValidator, updateEnbManager, updateGnbManager, ranListManager)
179         rootController := controllers.NewRootController(rnibDataService)
180         nodebController := controllers.NewNodebController(Log, httpMsgHandlerProvider)
181         e2tController := controllers.NewE2TController(Log, httpMsgHandlerProvider)
182         symptomController := controllers.NewSymptomdataController(Log, httpMsgHandlerProvider, rnibDataService, ranListManager)
183         //fmt.Println("loadconfig called at last")
184         //loadConfig()
185         _ = httpserver.Run(Log, config.Http.Port, rootController, nodebController, e2tController, symptomController)
186         //fmt.Println("loadconfig called at last")
187         //loadConfig()
188 }