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