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