6b1163f9e5453be1b74711211cadaf528195e09d
[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             vesCollectorEnabledCV = true;
74             startPublishing();
75         }
76     }
77
78     /**
79      * Reflect status for Unit Tests
80      *
81      * @return Text with status
82      */
83     public String isInitializationOk() {
84         return "No implemented";
85     }
86
87     public void startPublishing() {
88         mountpointStatePublisher = new MountpointStatePublisher(
89                 netconfNetworkElementService.getServiceProvider().getVESCollectorService());
90         Thread t = new Thread(mountpointStatePublisher);
91         t.start();
92
93         nodeConnectListener.start(mountpointStatePublisher);
94         nodeStateListener.start(mountpointStatePublisher);
95     }
96
97     public void stopPublishing() throws Exception {
98         mountpointStatePublisher.stop();
99         close(nodeConnectListener, nodeStateListener);
100     }
101
102     @Override
103     public void close() throws Exception {
104         LOG.info("{} closing ...", this.getClass().getName());
105         mountpointStatePublisher.stop();
106         vesCollectorService.deregister(this);
107         close(nodeConnectListener, nodeStateListener);
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     private void close(AutoCloseable... toCloseList) throws Exception {
118         for (AutoCloseable element : toCloseList) {
119             if (element != null) {
120                 element.close();
121             }
122         }
123     }
124
125     @Override
126     public void notify(VESCollectorCfgService cfg) {
127         boolean vesCollectorEnabledPV = cfg.isVESCollectorEnabled(); // Pending value a.k.a new value
128         if (vesCollectorEnabledPV != vesCollectorEnabledCV) {
129             vesCollectorEnabledCV = vesCollectorEnabledPV;
130             if (vesCollectorEnabledPV) {
131                 startPublishing();
132             } else {
133                 try {
134                     stopPublishing();
135                 } catch (Exception e) {
136                     LOG.debug("{}", e);
137                 }
138             }
139         }
140     }
141
142 }