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