e00b656f2668b7e4f4801a4c69046abd533aa197
[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 org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NetconfNetworkElementService;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorConfigChangeListener;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
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 VESCollectorConfigChangeListener, AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateProviderImpl.class);
38     private static final String APPLICATION_NAME = "mountpoint-state-provider";
39
40     private NetconfNodeStateService netconfNodeStateService;
41     private NetconfNetworkElementService netconfNetworkElementService;
42
43     private MountpointNodeConnectListenerImpl nodeConnectListener;
44     private MountpointNodeStateListenerImpl nodeStateListener;
45     private MountpointStatePublisher mountpointStatePublisher;
46     private VESCollectorService vesCollectorService;
47     private boolean vesCollectorEnabledCV = false; //Current value
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 setNetconfNetworkElementService(NetconfNetworkElementService netconfNetworkElementService) {
60         this.netconfNetworkElementService = netconfNetworkElementService;
61     }
62
63     public void init() {
64         LOG.info("Init call for {}", APPLICATION_NAME);
65
66         nodeConnectListener = new MountpointNodeConnectListenerImpl(netconfNodeStateService);
67         nodeStateListener = new MountpointNodeStateListenerImpl(netconfNodeStateService);
68         vesCollectorService = netconfNetworkElementService.getServiceProvider().getVESCollectorService();
69         vesCollectorService.registerForChanges(this);
70         boolean vesCollectorEnabled = vesCollectorService.getConfig().isVESCollectorEnabled();
71
72         if (vesCollectorEnabled) {
73             startPublishing();
74         }
75     }
76
77     /**
78      * Reflect status for Unit Tests
79      *
80      * @return Text with status
81      */
82     public String isInitializationOk() {
83         return "No implemented";
84     }
85
86     public void startPublishing() {
87         mountpointStatePublisher = new MountpointStatePublisher(
88                 netconfNetworkElementService.getServiceProvider().getVESCollectorService());
89         Thread t = new Thread(mountpointStatePublisher);
90         t.start();
91
92         nodeConnectListener.start(mountpointStatePublisher);
93         nodeStateListener.start(mountpointStatePublisher);
94     }
95
96     public void stopPublishing() throws Exception {
97         mountpointStatePublisher.stop();
98         close(nodeConnectListener, nodeStateListener);
99     }
100
101     @Override
102     public void close() throws Exception {
103         LOG.info("{} closing ...", this.getClass().getName());
104         mountpointStatePublisher.stop();
105         vesCollectorService.deregister(this);
106         close(nodeConnectListener, nodeStateListener);
107         LOG.info("{} closing done", APPLICATION_NAME);
108     }
109
110     /**
111      * Used to close all Services, that should support AutoCloseable Pattern
112      *
113      * @param toClose
114      * @throws Exception
115      */
116     private void close(AutoCloseable... toCloseList) throws Exception {
117         for (AutoCloseable element : toCloseList) {
118             if (element != null) {
119                 element.close();
120             }
121         }
122     }
123
124     @Override
125     public void notify(VESCollectorCfgService cfg) {
126         boolean vesCollectorEnabledPV = cfg.isVESCollectorEnabled(); // Pending value a.k.a new value
127         if (vesCollectorEnabledPV != vesCollectorEnabledCV) {
128             vesCollectorEnabledCV = vesCollectorEnabledPV;
129             if (vesCollectorEnabledPV) {
130                 startPublishing();
131             } else {
132                 try {
133                     stopPublishing();
134                 } catch (Exception e) {
135                     LOG.debug("{}", e);
136                 }
137             }
138         }
139     }
140
141 }