Fix nullPointerException
[ccsdk/features.git] / sdnr / wt / mountpoint-state-provider / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / mountpointstateprovider / impl / MountpointNodeStateListenerImpl.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 package org.onap.ccsdk.features.sdnr.wt.mountpointstateprovider.impl;
20
21 import org.json.JSONObject;
22 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
23 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateService;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33 public class MountpointNodeStateListenerImpl implements NetconfNodeStateListener, AutoCloseable {
34     private static final Logger LOG = LoggerFactory.getLogger(MountpointNodeStateListenerImpl.class);
35     private NetconfNodeStateService netconfNodeStateService;
36     private MountpointStatePublisher mountpointStatePublisher;
37     private ListenerRegistration<MountpointNodeStateListenerImpl> registeredNodeStateListener;
38
39     public MountpointNodeStateListenerImpl(NetconfNodeStateService netconfNodeStateService) {
40         this.netconfNodeStateService = netconfNodeStateService;
41     }
42
43     public void start(MountpointStatePublisher mountpointStatePublisher) {
44         this.mountpointStatePublisher = mountpointStatePublisher;
45         registeredNodeStateListener = netconfNodeStateService.registerNetconfNodeStateListener(this);
46     }
47
48     @Override
49     public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
50         Ipv4Address ipv4Address = netconfNode.getHost().getIpAddress().getIpv4Address();
51         Ipv6Address ipv6Address = netconfNode.getHost().getIpAddress().getIpv6Address();
52         LOG.info("In onCreated of MountpointNodeStateListenerImpl - nNodeId = {}, IP Address = {}",nNodeId.getValue(),
53                 ipv4Address != null?ipv4Address.getValue():ipv6Address.getValue());
54         JSONObject obj = new JSONObject();
55         obj.put(Constants.NODEID, nNodeId.getValue());
56         obj.put(Constants.NETCONFNODESTATE, netconfNode.getConnectionStatus().toString());
57         obj.put(Constants.TIMESTAMP, java.time.Clock.systemUTC().instant());
58
59         mountpointStatePublisher.addToPublish(obj);
60     }
61
62     @Override
63     public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
64         Ipv4Address ipv4Address = netconfNode.getHost().getIpAddress().getIpv4Address();
65         Ipv6Address ipv6Address = netconfNode.getHost().getIpAddress().getIpv6Address();
66         LOG.info("In onStateChange of MountpointNodeStateListenerImpl - nNodeId = {}, IP Address = {}",nNodeId.getValue(),
67                 ipv4Address != null?ipv4Address.getValue():ipv6Address.getValue());
68         JSONObject obj = new JSONObject();
69         obj.put(Constants.NODEID, nNodeId.getValue());
70         obj.put(Constants.NETCONFNODESTATE, netconfNode.getConnectionStatus().toString());
71         obj.put(Constants.TIMESTAMP, java.time.Clock.systemUTC().instant());
72
73         mountpointStatePublisher.addToPublish(obj);
74     }
75
76     @Override
77     public void onRemoved(NodeId nNodeId) {
78
79         LOG.info("In onRemoved of MountpointNodeStateListenerImpl - nNodeId = {}",nNodeId);
80         JSONObject obj = new JSONObject();
81         obj.put(Constants.NODEID, nNodeId.getValue());
82         obj.put(Constants.NETCONFNODESTATE, "Removed");
83         obj.put(Constants.TIMESTAMP, java.time.Clock.systemUTC().instant());
84
85         mountpointStatePublisher.addToPublish(obj);
86     }
87
88     public void stop() throws Exception {
89         this.close();
90     }
91
92     @Override
93     public void close() throws Exception {
94         registeredNodeStateListener.close();
95     }
96
97 }