0223e732326601d1c068553fa87e886115683fa6
[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 = new ConfigurationFileRepresentation(CONFIGURATIONFILE);
62         configFileRepresentation.registerConfigChangedListener(this);
63
64         generalConfig = new GeneralConfig(configFileRepresentation);
65         if (generalConfig.getEnabled()) { //dmaapEnabled
66             mountpointStatePublisher = new Thread(new MountpointStatePublisher(generalConfig));
67             mountpointStatePublisher.start();
68             netconfNodeStateService.registerNetconfNodeConnectListener(nodeConnectListener);
69             netconfNodeStateService.registerNetconfNodeStateListener(nodeStateListener);
70         }
71     }
72
73     /**
74      * Reflect status for Unit Tests
75      * @return Text with status
76      */
77     public String isInitializationOk() {
78         return "No implemented";
79     }
80
81     @Override
82     public void onConfigChanged() {
83         LOG.info("Service configuration state changed. Enabled: {}", generalConfig.getEnabled());
84         boolean dmaapEnabledNewVal = generalConfig.getEnabled();
85
86         // DMaap disabled earlier (or during bundle startup) but enabled later, start Consumer(s)
87         if (!dmaapEnabled && dmaapEnabledNewVal) {
88             LOG.info("DMaaP is enabled, starting Publisher");
89             mountpointStatePublisher = new Thread(new MountpointStatePublisher(generalConfig));
90             mountpointStatePublisher.start();
91             netconfNodeStateService.registerNetconfNodeConnectListener(nodeConnectListener);
92             netconfNodeStateService.registerNetconfNodeStateListener(nodeStateListener);
93         } else if (dmaapEnabled && !dmaapEnabledNewVal) {
94                 // DMaap enabled earlier (or during bundle startup) but disabled later, stop consumer(s)
95             LOG.info("DMaaP is disabled, stop publisher");
96             try {
97                                 MountpointStatePublisher.stopPublisher();
98                         } catch (IOException | InterruptedException e) {
99                                 LOG.error("Exception while stopping publisher ", e);
100                         }
101         }
102         dmaapEnabled = dmaapEnabledNewVal;
103     }
104
105     @Override
106     public void close() throws Exception {
107         LOG.info("{} closing ...", this.getClass().getName());
108         //close(updateService, configService, mwtnService); issue#1
109         try {
110                         MountpointStatePublisher.stopPublisher();
111                 } catch (IOException | InterruptedException e) {
112                         LOG.error("Exception while stopping publisher ", e);
113                 }
114         //close(updateService, mwtnService);
115         LOG.info("{} closing done",APPLICATION_NAME);
116     }
117
118     /**
119      * Used to close all Services, that should support AutoCloseable Pattern
120      *
121      * @param toClose
122      * @throws Exception
123      */
124     @SuppressWarnings("unused")
125     private void close(AutoCloseable... toCloseList) throws Exception {
126         for (AutoCloseable element : toCloseList) {
127             if (element != null) {
128                 element.close();
129             }
130         }
131     }
132
133 }