cc26f78b94cdd146726966d4d8a27b102c27450a
[oam/oam-controller.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.util.Optional;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NetconfNetworkElementService;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorConfigChangeListener;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeConnectListener;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev240911.netconf.node.augment.NetconfNode;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class MountpointStateProviderImpl implements VESCollectorConfigChangeListener, NetconfNodeConnectListener,
45         NetconfNodeStateListener, AutoCloseable {
46
47     private static final Logger LOG = LoggerFactory.getLogger(MountpointStateProviderImpl.class);
48     private static final String APPLICATION_NAME = "mountpoint-state-provider";
49
50     private NetconfNodeStateService netconfNodeStateService;
51     private NetconfNetworkElementService netconfNetworkElementService;
52     private VESCollectorService vesCollectorService;
53     private boolean vesCollectorEnabled = false; //Current value
54     private MountpointStateVESMessageFormatter vesMessageFormatter;
55
56     public MountpointStateProviderImpl() {
57         LOG.info("Creating provider class for {}", APPLICATION_NAME);
58
59     }
60
61     public void setNetconfNodeStateService(NetconfNodeStateService netconfNodeStateService) {
62         this.netconfNodeStateService = netconfNodeStateService;
63     }
64
65     public void setNetconfNetworkElementService(NetconfNetworkElementService netconfNetworkElementService) {
66         this.netconfNetworkElementService = netconfNetworkElementService;
67     }
68
69     public void init() {
70         LOG.info("Init call for {}", APPLICATION_NAME);
71         this.vesCollectorService = netconfNetworkElementService.getServiceProvider().getVESCollectorService();
72         this.vesCollectorEnabled = vesCollectorService.getConfig().isVESCollectorEnabled();
73         this.vesMessageFormatter = new MountpointStateVESMessageFormatter(vesCollectorService);
74         // regsiter for live configuration changes
75         vesCollectorService.registerForChanges(this);
76         // register for node changes
77         netconfNodeStateService.registerNetconfNodeConnectListener(this);
78         netconfNodeStateService.registerNetconfNodeStateListener(this);
79
80     }
81
82     /**
83      * Reflect status for Unit Tests
84      *
85      * @return Text with status
86      */
87     public String isInitializationOk() {
88         return "No implemented";
89     }
90
91     @Override
92     public void close() throws Exception {
93         LOG.info("{} closing ...", this.getClass().getName());
94         vesCollectorService.deregister(this);
95         LOG.info("{} closing done", APPLICATION_NAME);
96     }
97
98
99     private void publishStateChange(String nodeId, String connectionStatus) {
100         this.vesCollectorService.publishVESMessage(
101                 vesMessageFormatter.createVESMessage(nodeId, connectionStatus, java.time.Clock.systemUTC().instant()));
102     }
103
104     @Override
105     public void notify(VESCollectorCfgService cfg) {
106         boolean vesCollectorEnabledPV = cfg.isVESCollectorEnabled(); // Pending value a.k.a new value
107         if (vesCollectorEnabledPV != vesCollectorEnabled) {
108             this.vesCollectorEnabled = vesCollectorEnabledPV;
109         }
110     }
111
112     @Override
113     public void onEnterConnected(@NonNull NetconfAccessor accessor) {
114         if (!this.vesCollectorEnabled) {
115             return;
116         }
117         NodeId nNodeId = accessor.getNodeId();
118         NetconfNode netconfNode = accessor.getNetconfNode();
119
120         Ipv4Address ipv4Address = netconfNode.getHost().getIpAddress().getIpv4Address();
121         Ipv6Address ipv6Address = netconfNode.getHost().getIpAddress().getIpv6Address();
122         LOG.debug("In onEnterConnected of MountpointNodeConnectListenerImpl - nNodeId = {}, IP Address = {}",
123                 nNodeId.getValue()
124                 , ipv4Address != null ? ipv4Address.getValue() : ipv6Address.getValue());
125         this.publishStateChange(nNodeId.getValue(), netconfNode.getConnectionStatus().toString());
126     }
127
128     @Override
129     public void onLeaveConnected(NodeId nNodeId, Optional<NetconfNode> optionalNetconfNode) {
130         if (!this.vesCollectorEnabled) {
131             return;
132         }
133         LOG.debug("In onLeaveConnected of MountpointNodeConnectListenerImpl - nNodeId = {}", nNodeId);
134         this.publishStateChange(nNodeId.getValue(), "Unmounted");
135     }
136
137     @Override
138     public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
139         if (!this.vesCollectorEnabled) {
140             return;
141         }
142         Ipv4Address ipv4Address = netconfNode.getHost().getIpAddress().getIpv4Address();
143         Ipv6Address ipv6Address = netconfNode.getHost().getIpAddress().getIpv6Address();
144         LOG.debug("In onCreated of MountpointNodeStateListenerImpl - nNodeId = {}, IP Address = {}", nNodeId.getValue(),
145                 ipv4Address != null ? ipv4Address.getValue() : ipv6Address.getValue());
146         this.publishStateChange(nNodeId.getValue(), netconfNode.getConnectionStatus().toString());
147
148
149     }
150
151     @Override
152     public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
153         if (!this.vesCollectorEnabled) {
154             return;
155         }
156         Ipv4Address ipv4Address = netconfNode.getHost().getIpAddress().getIpv4Address();
157         Ipv6Address ipv6Address = netconfNode.getHost().getIpAddress().getIpv6Address();
158         LOG.debug("In onStateChange of MountpointNodeStateListenerImpl - nNodeId = {}, IP Address = {}",
159                 nNodeId.getValue(),
160                 ipv4Address != null ? ipv4Address.getValue() : ipv6Address.getValue());
161         this.publishStateChange(nNodeId.getValue(), netconfNode.getConnectionStatus().toString());
162
163
164     }
165
166     @Override
167     public void onRemoved(NodeId nNodeId) {
168         if (!this.vesCollectorEnabled) {
169             return;
170         }
171         LOG.debug("In onRemoved of MountpointNodeStateListenerImpl - nNodeId = {}", nNodeId);
172         this.publishStateChange(nNodeId.getValue(), "Removed");
173
174     }
175
176 }