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