5ffae29f46bf77438a5bb0af49216e8a691aa630
[ccsdk/features.git] /
1 package org.onap.ccsdk.features.sdnr.wt.mountpointstateprovider.impl;
2 /*******************************************************************************
3  * ============LICENSE_START========================================================================
4  * ONAP : ccsdk feature sdnr wt
5  * =================================================================================================
6  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
7  * =================================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  * ============LICENSE_END==========================================================================
18  ******************************************************************************/
19
20 import java.io.IOException;
21
22 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
23 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
24 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @SuppressWarnings("deprecation")
29 public class MountpointStateProviderImpl implements AutoCloseable, IConfigChangedListener {
30
31     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateProviderImpl.class);
32     private static final String APPLICATION_NAME = "mountpoint-state-provider";
33     private static final String CONFIGURATIONFILE = "etc/mountpoint-state-provider.properties";
34
35     private NetconfNodeStateService netconfNodeStateService;
36
37     private GeneralConfig generalConfig;
38     private boolean dmaapEnabled = false;
39     private Thread mountpointStatePublisher = null;
40
41     MountpointNodeConnectListenerImpl nodeConnectListener = new MountpointNodeConnectListenerImpl();
42     MountpointNodeStateListenerImpl nodeStateListener = new MountpointNodeStateListenerImpl();
43
44     public MountpointStateProviderImpl() {
45         LOG.info("Creating provider class for {}", APPLICATION_NAME);
46     }
47
48     public void setNetconfNodeStateService(NetconfNodeStateService netconfNodeStateService) {
49         this.netconfNodeStateService = netconfNodeStateService;
50     }
51
52     public void init() {
53         LOG.info("Init call for {}", APPLICATION_NAME);
54         ConfigurationFileRepresentation configFileRepresentation = new ConfigurationFileRepresentation(CONFIGURATIONFILE);
55         configFileRepresentation.registerConfigChangedListener(this);
56
57         generalConfig = new GeneralConfig(configFileRepresentation);
58         if (generalConfig.getEnabled()) { //dmaapEnabled
59             mountpointStatePublisher = new Thread(new MountpointStatePublisher(generalConfig));
60             mountpointStatePublisher.start();
61             netconfNodeStateService.registerNetconfNodeConnectListener(nodeConnectListener);
62             netconfNodeStateService.registerNetconfNodeStateListener(nodeStateListener);
63         }
64     }
65
66     /**
67      * Reflect status for Unit Tests
68      * @return Text with status
69      */
70     public String isInitializationOk() {
71         return "No implemented";
72     }
73
74     @Override
75     public void onConfigChanged() {
76         LOG.info("Service configuration state changed. Enabled: {}", generalConfig.getEnabled());
77         boolean dmaapEnabledNewVal = generalConfig.getEnabled();
78
79         // DMaap disabled earlier (or during bundle startup) but enabled later, start Consumer(s)
80         if (!dmaapEnabled && dmaapEnabledNewVal) {
81             LOG.info("DMaaP is enabled, starting Publisher");
82             mountpointStatePublisher = new Thread(new MountpointStatePublisher(generalConfig));
83             mountpointStatePublisher.start();
84             netconfNodeStateService.registerNetconfNodeConnectListener(nodeConnectListener);
85             netconfNodeStateService.registerNetconfNodeStateListener(nodeStateListener);
86         } else if (dmaapEnabled && !dmaapEnabledNewVal) {
87                 // DMaap enabled earlier (or during bundle startup) but disabled later, stop consumer(s)
88             LOG.info("DMaaP is disabled, stop publisher");
89             try {
90                                 MountpointStatePublisher.stopPublisher();
91                         } catch (IOException | InterruptedException e) {
92                                 LOG.error("Exception while stopping publisher ", e);
93                         }
94         }
95         dmaapEnabled = dmaapEnabledNewVal;
96     }
97
98     @Override
99     public void close() throws Exception {
100         LOG.info("{} closing ...", this.getClass().getName());
101         //close(updateService, configService, mwtnService); issue#1
102         try {
103                         MountpointStatePublisher.stopPublisher();
104                 } catch (IOException | InterruptedException e) {
105                         LOG.error("Exception while stopping publisher ", e);
106                 }
107         //close(updateService, mwtnService);
108         LOG.info("{} closing done",APPLICATION_NAME);
109     }
110
111     /**
112      * Used to close all Services, that should support AutoCloseable Pattern
113      *
114      * @param toClose
115      * @throws Exception
116      */
117     @SuppressWarnings("unused")
118     private void close(AutoCloseable... toCloseList) throws Exception {
119         for (AutoCloseable element : toCloseList) {
120             if (element != null) {
121                 element.close();
122             }
123         }
124     }
125
126 }