6838bc3b2f1d95c0df5cb96581bed0874e04ce1b
[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.netconfnodestateservice.NetconfNodeStateService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class MountpointStateProviderImpl implements AutoCloseable {
33
34     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateProviderImpl.class);
35     private static final String APPLICATION_NAME = "mountpoint-state-provider";
36
37     private NetconfNodeStateService netconfNodeStateService;
38     private NetconfNetworkElementService netconfNetworkElementService;
39
40     private MountpointNodeConnectListenerImpl nodeConnectListener;
41     private MountpointNodeStateListenerImpl nodeStateListener;
42     private MountpointStatePublisher mountpointStatePublisher;
43
44     public MountpointStateProviderImpl() {
45         LOG.info("Creating provider class for {}", APPLICATION_NAME);
46         nodeConnectListener = null;
47         nodeStateListener = null;
48     }
49
50     public void setNetconfNodeStateService(NetconfNodeStateService netconfNodeStateService) {
51         this.netconfNodeStateService = netconfNodeStateService;
52     }
53
54     public void setNetconfNetworkElementService(NetconfNetworkElementService netconfNetworkElementService) {
55         this.netconfNetworkElementService = netconfNetworkElementService;
56     }
57
58     public void init() {
59         LOG.info("Init call for {}", APPLICATION_NAME);
60
61         nodeConnectListener = new MountpointNodeConnectListenerImpl(netconfNodeStateService);
62         nodeStateListener = new MountpointNodeStateListenerImpl(netconfNodeStateService);
63
64         startPublishing();
65     }
66
67     /**
68      * Reflect status for Unit Tests
69      *
70      * @return Text with status
71      */
72     public String isInitializationOk() {
73         return "No implemented";
74     }
75
76     public void startPublishing() {
77         mountpointStatePublisher = new MountpointStatePublisher(netconfNetworkElementService.getServiceProvider().getVESCollectorService());
78         Thread t = new Thread(mountpointStatePublisher);
79         t.start();
80
81         nodeConnectListener.start(mountpointStatePublisher);
82         nodeStateListener.start(mountpointStatePublisher);
83     }
84
85     @Override
86     public void close() throws Exception {
87         LOG.info("{} closing ...", this.getClass().getName());
88         mountpointStatePublisher.stop();
89         close(nodeConnectListener, nodeStateListener);
90         LOG.info("{} closing done", APPLICATION_NAME);
91     }
92
93     /**
94      * Used to close all Services, that should support AutoCloseable Pattern
95      *
96      * @param toClose
97      * @throws Exception
98      */
99     private void close(AutoCloseable... toCloseList) throws Exception {
100         for (AutoCloseable element : toCloseList) {
101             if (element != null) {
102                 element.close();
103             }
104         }
105     }
106 }