Merge "Reformat sdnr mountpoint-state-provider to ONAP code style"
[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 - 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
129     // Handler
130     private DeviceManagerNetconfConnectHandler forTest;
131
132     // Attributes
133     private final ConcurrentHashMap<String, NetworkElement> networkElementRepresentations;
134     private final List<NetworkElementFactory> factoryList;
135
136     private DeviceManagerDatabaseNotificationService deviceManagerDatabaseAndNotificationService;
137     private ClusterSingletonServiceRegistration cssRegistration;
138     private ClusterSingletonServiceRegistration cssRegistration2;
139
140     ConfigurationFileRepresentation config;
141     private Boolean devicemanagerInitializationOk;
142
143     // Blueprint 1
144     public DeviceManagerImpl() {
145         LOG.info("Creating provider for {}", APPLICATION_NAME);
146         this.devicemanagerInitializationOk = false;
147         this.factoryList = new CopyOnWriteArrayList<>();
148         this.networkElementRepresentations = new ConcurrentHashMap<>();
149
150         this.dataBroker = null;
151         this.mountPointService = null;
152         this.rpcProviderRegistry = null;
153         this.clusterSingletonServiceProvider = null;
154         this.websocketmanagerService = null;
155         this.iEntityDataProvider = null;
156
157         this.webSocketService = null;
158     }
159
160     public void setDataBroker(DataBroker dataBroker) {
161         this.dataBroker = dataBroker;
162     }
163
164     public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
165         this.rpcProviderRegistry = rpcProviderRegistry;
166     }
167
168     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {}
169
170     public void setMountPointService(MountPointService mountPointService) {
171         this.mountPointService = mountPointService;
172     }
173
174     public void setClusterSingletonService(ClusterSingletonServiceProvider clusterSingletonService) {
175         this.clusterSingletonServiceProvider = clusterSingletonService;
176     }
177
178     public void setNetconfNodeStateService(NetconfNodeStateService netconfNodeStateService) {
179         this.netconfNodeStateService = netconfNodeStateService;
180     }
181
182     public void setWebsocketmanagerService(WebsocketmanagerService websocketmanagerService) {
183         this.websocketmanagerService = websocketmanagerService;
184     }
185
186     public void setEntityDataProvider(IEntityDataProvider iEntityDataProvider) {
187         this.iEntityDataProvider = iEntityDataProvider;
188     }
189
190     public void init() {
191
192         LOG.info("Session Initiated start {}", APPLICATION_NAME);
193
194         this.iEntityDataProvider.setReadyStatus(false);
195
196         this.dataProvider = iEntityDataProvider.getDataProvider();
197
198         // Get configuration
199         this.config = new ConfigurationFileRepresentation(CONFIGURATIONFILE);
200
201         this.notificationDelayService = new DevicemanagerNotificationDelayService(config);
202
203         // start service for device maintenance service
204         this.maintenanceService = new MaintenanceServiceImpl(iEntityDataProvider.getHtDatabaseMaintenance());
205
206         // Websockets
207         this.webSocketService = new WebSocketServiceClientImpl2(websocketmanagerService);
208
209         IEsConfig esConfig = iEntityDataProvider.getEsConfig();
210         // DCAE
211         this.dcaeProviderClient = new DcaeProviderClient(config, esConfig.getCluster(), this);
212
213         this.aaiProviderClient = new AaiProviderClient(config, this);
214         // EM
215         String myDbKeyNameExtended = MYDBKEYNAMEBASE + "-" + esConfig.getCluster();
216
217         this.aotsDcaeForwarder = new DcaeForwarderImpl(null, dcaeProviderClient, maintenanceService);
218
219         this.deviceManagerDatabaseAndNotificationService = new DeviceManagerDatabaseNotificationService(dataProvider,
220                 maintenanceService, webSocketService, notificationDelayService, aotsDcaeForwarder);
221
222         RpcPushNotificationsHandler rpcPushNotificationsHandler =
223                 new RpcPushNotificationsHandler(webSocketService, dataProvider, aotsDcaeForwarder);
224         this.odlEventListenerHandler =
225                 new ODLEventListenerHandler(myDbKeyNameExtended, webSocketService, dataProvider, aotsDcaeForwarder);
226         this.archiveCleanService = new ArchiveCleanService(iEntityDataProvider.getEsConfig(), dataProvider);
227         this.housekeepingService = new ConnectionStatusHousekeepingService(this.dataBroker, dataProvider);
228         this.cssRegistration =
229                 this.clusterSingletonServiceProvider.registerClusterSingletonService(this.archiveCleanService);
230         this.cssRegistration2 =
231                 this.clusterSingletonServiceProvider.registerClusterSingletonService(this.housekeepingService);
232         // PM
233         this.performanceManager = new PerformanceManagerImpl(60, this, 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(this,
242                 mountPointService, odlEventListenerHandler, 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(
285             @NonNull L factory) {
286         LOG.info("Factory registration {}", factory.getClass().getName());
287
288         factoryList.add(factory);
289         return new FactoryRegistration<L>() {
290
291             @Override
292             public @NonNull L getInstance() {
293                 return factory;
294             }
295
296             @Override
297             public void close() {
298                 factoryList.remove(factory);
299             }
300
301         };
302     }
303
304     @SuppressWarnings("null")
305     @Override
306     public @NonNull DataProvider getDataProvider() {
307         return this.dataProvider;
308     }
309
310     @SuppressWarnings("null")
311     @Override
312     public @NonNull NotificationService getNotificationService() {
313         return this.deviceManagerDatabaseAndNotificationService;
314     }
315
316     @SuppressWarnings("null")
317     @Override
318     public @NonNull FaultService getFaultService() {
319         return this.deviceManagerDatabaseAndNotificationService;
320     }
321
322     @SuppressWarnings("null")
323     @Override
324     public @NonNull EquipmentService getEquipmentService() {
325         return this.deviceManagerDatabaseAndNotificationService;
326     }
327
328     @SuppressWarnings("null")
329     @Override
330     public @NonNull AaiService getAaiService() {
331         return this.aaiProviderClient;
332     }
333
334     @SuppressWarnings("null")
335     @Override
336     public @NonNull MaintenanceService getMaintenanceService() {
337         return this.maintenanceService;
338     }
339
340     @SuppressWarnings("null")
341     @Override
342     public @NonNull PerformanceManager getPerformanceManagerService() {
343         return this.performanceManager;
344     }
345
346     @SuppressWarnings("null")
347     @Override
348     public @NonNull EventHandlingService getEventHandlingService() {
349         return this.odlEventListenerHandler;
350     }
351
352     @SuppressWarnings("null")
353     @Override
354     public @NonNull ConfigurationFileRepresentation getConfigurationFileRepresentation() {
355         return this.config;
356     }
357
358     // Deviceinitialization
359
360     /**
361      * Used to close all Services, that should support AutoCloseable Pattern
362      * 
363      * @param toClose
364      */
365     private void close(AutoCloseable... toCloseList) {
366         for (AutoCloseable element : toCloseList) {
367             if (element != null) {
368                 try {
369                     element.close();
370                 } catch (Exception e) {
371                     LOG.warn("Problem during close {}", e);
372                 }
373             }
374         }
375     }
376
377     /*-------------------------------------------------------------------------------------------
378      * Functions
379      */
380
381     public ArchiveCleanService getArchiveCleanService() {
382         return this.archiveCleanService;
383     }
384
385     public DataProvider getDatabaseClientEvents() {
386         return dataProvider;
387     }
388
389     @Override
390     public DeviceManagerServiceProvider getServiceProvider() {
391         return this;
392     }
393
394     /**
395      * Indication if init() of devicemanager successfully done.
396      * 
397      * @return true if init() was sucessfull. False if not done or not successfull.
398      */
399     public boolean isDevicemanagerInitializationOk() {
400         return this.devicemanagerInitializationOk;
401     }
402
403     /**
404      * Get NE object. Used by DCAE Service
405      * 
406      * @param mountpoint mount point name
407      * @return null or NE specific data
408      */
409     public @Nullable NetworkElement getNeByMountpoint(String mountpoint) {
410
411         return networkElementRepresentations.get(mountpoint);
412
413     }
414
415     @Override
416     public void writeToEventLog(String objectId, String msg, String value) {
417         this.odlEventListenerHandler.writeEventLog(objectId, msg, value);
418     }
419
420
421 }