cb5cbe3e25ce2600b1cb9c7085335c6c2ceb3cea
[ccsdk/features.git] /
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 @SuppressWarnings("deprecation")
36 public class MountpointStateProviderImpl implements AutoCloseable, IConfigChangedListener {
37
38     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateProviderImpl.class);
39     private static final String APPLICATION_NAME = "mountpoint-state-provider";
40     private static final String CONFIGURATIONFILE = "etc/mountpoint-state-provider.properties";
41
42     private NetconfNodeStateService netconfNodeStateService;
43
44     private GeneralConfig generalConfig;
45     private boolean dmaapEnabled = false;
46     private Thread mountpointStatePublisher = null;
47
48     MountpointNodeConnectListenerImpl nodeConnectListener = new MountpointNodeConnectListenerImpl();
49     MountpointNodeStateListenerImpl nodeStateListener = new MountpointNodeStateListenerImpl();
50
51     public MountpointStateProviderImpl() {
52         LOG.info("Creating provider class for {}", APPLICATION_NAME);
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         generalConfig = new GeneralConfig(configFileRepresentation);
66         if (generalConfig.getEnabled()) { //dmaapEnabled
67             mountpointStatePublisher = new Thread(new MountpointStatePublisher(generalConfig));
68             mountpointStatePublisher.start();
69             netconfNodeStateService.registerNetconfNodeConnectListener(nodeConnectListener);
70             netconfNodeStateService.registerNetconfNodeStateListener(nodeStateListener);
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 Consumer(s)
89         if (!dmaapEnabled && dmaapEnabledNewVal) {
90             LOG.info("DMaaP is enabled, starting Publisher");
91             mountpointStatePublisher = new Thread(new MountpointStatePublisher(generalConfig));
92             mountpointStatePublisher.start();
93             netconfNodeStateService.registerNetconfNodeConnectListener(nodeConnectListener);
94             netconfNodeStateService.registerNetconfNodeStateListener(nodeStateListener);
95         } else if (dmaapEnabled && !dmaapEnabledNewVal) {
96             // DMaap enabled earlier (or during bundle startup) but disabled later, stop consumer(s)
97             LOG.info("DMaaP is disabled, stop publisher");
98             try {
99                 MountpointStatePublisher.stopPublisher();
100             } catch (IOException | InterruptedException e) {
101                 LOG.error("Exception while stopping publisher ", e);
102             }
103         }
104         dmaapEnabled = dmaapEnabledNewVal;
105     }
106
107     @Override
108     public void close() throws Exception {
109         LOG.info("{} closing ...", this.getClass().getName());
110         //close(updateService, configService, mwtnService); issue#1
111         try {
112             MountpointStatePublisher.stopPublisher();
113         } catch (IOException | InterruptedException e) {
114             LOG.error("Exception while stopping publisher ", e);
115         }
116         //close(updateService, mwtnService);
117         LOG.info("{} closing done", APPLICATION_NAME);
118     }
119
120     /**
121      * Used to close all Services, that should support AutoCloseable Pattern
122      *
123      * @param toClose
124      * @throws Exception
125      */
126     @SuppressWarnings("unused")
127     private void close(AutoCloseable... toCloseList) throws Exception {
128         for (AutoCloseable element : toCloseList) {
129             if (element != null) {
130                 element.close();
131             }
132         }
133     }
134
135 }