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