Merge "fixed license issue"
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / DeviceManagerImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 /*
23  * ============LICENSE_START========================================================================
24  * ONAP : ccsdk feature sdnr wt
25  * =================================================================================================
26  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
27  * =================================================================================================
28  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
29  * in compliance with the License. You may obtain a copy of the License at
30  *
31  * http://www.apache.org/licenses/LICENSE-2.0
32  *
33  * Unless required by applicable law or agreed to in writing, software distributed under the License
34  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
35  * or implied. See the License for the specific language governing permissions and limitations under
36  * the License.
37  * ============LICENSE_END==========================================================================
38  */
39 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl;
40
41 import java.util.List;
42 import java.util.concurrent.CopyOnWriteArrayList;
43 import org.eclipse.jdt.annotation.NonNull;
44 import org.eclipse.jdt.annotation.Nullable;
45 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
46 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
47 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEntityDataProvider;
48 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEsConfig;
49 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
50 import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanService;
51 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeForwarderImpl;
52 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeProviderClient;
53 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitor;
54 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitorImpl;
55 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.DeviceManagerDatabaseNotificationService;
56 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.ODLEventListenerHandler;
57 import org.onap.ccsdk.features.sdnr.wt.devicemanager.eventdatahandler.RpcPushNotificationsHandler;
58 import org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping.ConnectionStatusHousekeepingService;
59 import org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping.ResyncNetworkElementHouskeepingService;
60 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClientImpl2;
61 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClientInternal;
62 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl.MaintenanceServiceImpl;
63 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.factory.FactoryRegistration;
64 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.factory.NetworkElementFactory;
65 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
66 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.PerformanceManagerImpl;
67 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.AaiService;
68 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
69 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.EquipmentService;
70 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.EventHandlingService;
71 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
72 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.MaintenanceService;
73 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NetconfNetworkElementService;
74 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
75 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.PerformanceManager;
76 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
77 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.DevicemanagerNotificationDelayService;
78 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.VESCollectorServiceImpl;
79 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService;
81 import org.opendaylight.mdsal.binding.api.DataBroker;
82 import org.opendaylight.mdsal.binding.api.MountPointService;
83 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
84 import org.opendaylight.mdsal.binding.api.RpcProviderService;
85 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
86 import org.slf4j.Logger;
87 import org.slf4j.LoggerFactory;
88
89 /**
90  * Devicemanager - Handles startup and closedown of network element handlers for netconf session - Provide common
91  * services for network element specific components
92  */
93 public class DeviceManagerImpl implements NetconfNetworkElementService, DeviceManagerServiceProvider, AutoCloseable {
94
95     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
96     private static final String APPLICATION_NAME = "DeviceManager";
97     private static final String MYDBKEYNAMEBASE = "SDN-Controller";
98     private static final String CONFIGURATIONFILE = "etc/devicemanager.properties";
99     public static final long DATABASE_TIMEOUT_MS = 120 * 1000L;
100
101     @SuppressWarnings("unused")
102     private static final String STARTUPLOG_FILENAME = "etc/devicemanager.startup.log";
103
104     // MDSAL Services
105     private DataBroker dataBroker;
106     private MountPointService mountPointService;
107     private RpcProviderService rpcProviderRegistry;
108     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
109     private WebsocketmanagerService websocketmanagerService;
110     private IEntityDataProvider iEntityDataProvider;
111
112     // Devicemanager common services for network element handler
113     private WebSocketServiceClientInternal webSocketService;
114     private ODLEventListenerHandler odlEventListenerHandler; //EventHandlingService
115     private DeviceManagerApiServiceImpl rpcApiService;
116     private PerformanceManagerImpl performanceManager;
117     private DcaeProviderClient dcaeProviderClient;
118     private AaiProviderClient aaiProviderClient;
119     private DcaeForwarderImpl aotsDcaeForwarder;
120     private DeviceMonitor deviceMonitor;
121     private MaintenanceServiceImpl maintenanceService;
122     private DevicemanagerNotificationDelayService notificationDelayService;
123     private ResyncNetworkElementHouskeepingService resyncNetworkElementHouskeepingService;
124     private ArchiveCleanService archiveCleanService;
125     private ConnectionStatusHousekeepingService housekeepingService;
126     private NetconfNodeStateService netconfNodeStateService;
127     private DataProvider dataProvider;
128     private VESCollectorServiceImpl vesCollectorServiceImpl;
129
130     // Handler
131     private DeviceManagerNetconfConnectHandler deviceManagerNetconfConnectHandler;
132
133     // Attributes
134     private final List<NetworkElementFactory> factoryList;
135
136     private DeviceManagerDatabaseNotificationService deviceManagerDatabaseAndNotificationService;
137
138     ConfigurationFileRepresentation config;
139     private Boolean devicemanagerInitializationOk;
140
141     // Blueprint 1
142     public DeviceManagerImpl() {
143         LOG.info("Creating provider for {}", APPLICATION_NAME);
144         this.devicemanagerInitializationOk = false;
145         this.factoryList = new CopyOnWriteArrayList<>();
146
147         this.dataBroker = null;
148         this.mountPointService = null;
149         this.rpcProviderRegistry = null;
150         this.clusterSingletonServiceProvider = null;
151         this.websocketmanagerService = null;
152         this.iEntityDataProvider = null;
153
154         this.webSocketService = null;
155     }
156
157     public void setDataBroker(DataBroker dataBroker) {
158         this.dataBroker = dataBroker;
159     }
160
161     public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
162         this.rpcProviderRegistry = rpcProviderRegistry;
163     }
164
165     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {}
166
167     public void setMountPointService(MountPointService mountPointService) {
168         this.mountPointService = mountPointService;
169     }
170
171     public void setClusterSingletonService(ClusterSingletonServiceProvider clusterSingletonService) {
172         this.clusterSingletonServiceProvider = clusterSingletonService;
173     }
174
175     public void setNetconfNodeStateService(NetconfNodeStateService netconfNodeStateService) {
176         this.netconfNodeStateService = netconfNodeStateService;
177     }
178
179     public void setWebsocketmanagerService(WebsocketmanagerService websocketmanagerService) {
180         this.websocketmanagerService = websocketmanagerService;
181     }
182
183     public void setEntityDataProvider(IEntityDataProvider iEntityDataProvider) {
184         this.iEntityDataProvider = iEntityDataProvider;
185     }
186
187     public void init() {
188
189         LOG.info("Session Initiated start {}", APPLICATION_NAME);
190
191         this.iEntityDataProvider.setReadyStatus(false);
192
193         this.dataProvider = iEntityDataProvider.getDataProvider();
194
195         // Get configuration
196         this.config = new ConfigurationFileRepresentation(CONFIGURATIONFILE);
197
198         this.notificationDelayService = new DevicemanagerNotificationDelayService(config);
199
200         // start service for device maintenance service
201         this.maintenanceService = new MaintenanceServiceImpl(iEntityDataProvider.getHtDatabaseMaintenance());
202
203         // Websockets
204         this.webSocketService = new WebSocketServiceClientImpl2(websocketmanagerService);
205
206         IEsConfig esConfig = iEntityDataProvider.getEsConfig();
207         // DCAE
208         this.dcaeProviderClient = new DcaeProviderClient(config, esConfig.getCluster(), this);
209
210         this.aaiProviderClient = new AaiProviderClient(config, this);
211
212         this.vesCollectorServiceImpl = new VESCollectorServiceImpl(config);
213         // EM
214         String myDbKeyNameExtended = MYDBKEYNAMEBASE + "-" + esConfig.getCluster();
215
216         this.aotsDcaeForwarder = new DcaeForwarderImpl(null, dcaeProviderClient, maintenanceService);
217
218         this.deviceManagerDatabaseAndNotificationService = new DeviceManagerDatabaseNotificationService(dataProvider,
219                 maintenanceService, webSocketService, notificationDelayService, aotsDcaeForwarder);
220
221         RpcPushNotificationsHandler rpcPushNotificationsHandler =
222                 new RpcPushNotificationsHandler(webSocketService, dataProvider, aotsDcaeForwarder);
223         this.odlEventListenerHandler =
224                 new ODLEventListenerHandler(myDbKeyNameExtended, webSocketService, dataProvider, aotsDcaeForwarder);
225         this.archiveCleanService = new ArchiveCleanService(iEntityDataProvider.getEsConfig(),
226                 clusterSingletonServiceProvider, dataProvider);
227         this.housekeepingService = new ConnectionStatusHousekeepingService(config, clusterSingletonServiceProvider,
228                 this.dataBroker, dataProvider);
229         // PM
230         this.performanceManager = new PerformanceManagerImpl(60, this, dataProvider, config);
231         // DM
232         // DeviceMonitor has to be available before netconfSubscriptionManager is
233         // configured
234         LOG.debug("start DeviceMonitor Service");
235         this.deviceMonitor = new DeviceMonitorImpl(dataBroker, odlEventListenerHandler, config);
236
237         // ResyncNetworkElementHouskeepingService
238         this.resyncNetworkElementHouskeepingService = new ResyncNetworkElementHouskeepingService(this,
239                 mountPointService, odlEventListenerHandler, dataProvider, deviceMonitor);
240
241         // RPC Service for specific services
242         // Start RPC Service
243         LOG.debug("start rpc service");
244         this.rpcApiService = new DeviceManagerApiServiceImpl(rpcProviderRegistry, maintenanceService,
245                 resyncNetworkElementHouskeepingService, rpcPushNotificationsHandler);
246
247         // netconfSubscriptionManager should be the last one because this is a callback
248
249         // service
250         LOG.debug("start NetconfSubscriptionManager Service");
251         this.deviceManagerNetconfConnectHandler = new DeviceManagerNetconfConnectHandler(netconfNodeStateService,
252                 clusterSingletonServiceProvider, odlEventListenerHandler, deviceMonitor, this, factoryList);
253
254         writeToEventLog(APPLICATION_NAME, "startup", "done");
255         this.devicemanagerInitializationOk = true;
256
257         LOG.info("Session Initiated end. Initialization done {}", devicemanagerInitializationOk);
258         this.iEntityDataProvider.setReadyStatus(true);
259
260     }
261
262     @Override
263     public void close() {
264         LOG.info("DeviceManagerImpl closing ...");
265         close(performanceManager);
266         close(dcaeProviderClient);
267         close(aotsDcaeForwarder);
268         close(aaiProviderClient);
269         close(deviceMonitor);
270         close(maintenanceService);
271         close(rpcApiService);
272         close(notificationDelayService);
273         close(archiveCleanService);
274         close(housekeepingService);
275         close(deviceManagerNetconfConnectHandler);
276         close(vesCollectorServiceImpl);
277         LOG.info("DeviceManagerImpl closing done");
278     }
279
280     @Override
281     public @NonNull <L extends NetworkElementFactory> FactoryRegistration<L> registerBindingNetworkElementFactory(
282             @NonNull final L factory) {
283         LOG.info("Factory registration {}", factory.getClass().getName());
284
285         factoryList.add(factory);
286         return new FactoryRegistration<L>() {
287
288             @Override
289             public @NonNull L getInstance() {
290                 return factory;
291             }
292
293             @Override
294             public void close() {
295                 factoryList.remove(factory);
296             }
297
298         };
299     }
300
301     @SuppressWarnings("null")
302     @Override
303     public @NonNull DataProvider getDataProvider() {
304         return this.dataProvider;
305     }
306
307     @SuppressWarnings("null")
308     @Override
309     public @NonNull NotificationService getNotificationService() {
310         return this.deviceManagerDatabaseAndNotificationService;
311     }
312
313     @SuppressWarnings("null")
314     @Override
315     public @NonNull FaultService getFaultService() {
316         return this.deviceManagerDatabaseAndNotificationService;
317     }
318
319     @SuppressWarnings("null")
320     @Override
321     public @NonNull EquipmentService getEquipmentService() {
322         return this.deviceManagerDatabaseAndNotificationService;
323     }
324
325     @SuppressWarnings("null")
326     @Override
327     public @NonNull AaiService getAaiService() {
328         return this.aaiProviderClient;
329     }
330
331     @SuppressWarnings("null")
332     @Override
333     public @NonNull MaintenanceService getMaintenanceService() {
334         return this.maintenanceService;
335     }
336
337     @SuppressWarnings("null")
338     @Override
339     public @NonNull PerformanceManager getPerformanceManagerService() {
340         return this.performanceManager;
341     }
342
343     @SuppressWarnings("null")
344     @Override
345     public @NonNull EventHandlingService getEventHandlingService() {
346         return this.odlEventListenerHandler;
347     }
348
349     @SuppressWarnings("null")
350     @Override
351     public @NonNull ConfigurationFileRepresentation getConfigurationFileRepresentation() {
352         return this.config;
353     }
354
355     // Deviceinitialization
356
357     /**
358      * Used to close all Services, that should support AutoCloseable Pattern
359      *
360      * @param toClose
361      */
362     private void close(AutoCloseable... toCloseList) {
363         for (AutoCloseable element : toCloseList) {
364             if (element != null) {
365                 try {
366                     element.close();
367                 } catch (Exception e) {
368                     LOG.warn("Problem during close {}", e);
369                 }
370             }
371         }
372     }
373
374     /*-------------------------------------------------------------------------------------------
375      * Functions
376      */
377
378     public ArchiveCleanService getArchiveCleanService() {
379         return this.archiveCleanService;
380     }
381
382     public DataProvider getDatabaseClientEvents() {
383         return dataProvider;
384     }
385
386     @Override
387     public DeviceManagerServiceProvider getServiceProvider() {
388         return this;
389     }
390
391     /**
392      * Indication if init() of devicemanager successfully done.
393      *
394      * @return true if init() was sucessfull. False if not done or not successfull.
395      */
396     public boolean isDevicemanagerInitializationOk() {
397         return this.devicemanagerInitializationOk;
398     }
399
400     /**
401      * Get NE object. Used by DCAE Service
402      *
403      * @param mountpoint mount point name
404      * @return null or NE specific data
405      */
406     public @Nullable NetworkElement getConnectedNeByMountpoint(String mountpoint) {
407
408         return this.deviceManagerNetconfConnectHandler.getConnectedNeByMountpoint(mountpoint);
409
410     }
411
412     @Override
413     public void writeToEventLog(String objectId, String msg, String value) {
414         this.odlEventListenerHandler.writeEventLog(objectId, msg, value);
415     }
416
417     @Override
418     public @NonNull VESCollectorService getVESCollectorService() {
419         return this.vesCollectorServiceImpl;
420     }
421
422 }