Decouple message bus from netconf connector
[controller.git] / opendaylight / netconf / messagebus-netconf / src / main / java / org / opendaylight / controller / messagebus / eventsources / netconf / NetconfEventSourceRegistration.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.messagebus.eventsources.netconf;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeFields.ConnectionStatus;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Helper class to keep connection status of netconf node  and event source registration object
31  */
32 public class NetconfEventSourceRegistration implements AutoCloseable {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceRegistration.class);
35     private static final YangInstanceIdentifier NETCONF_DEVICE_DOM_PATH = YangInstanceIdentifier.builder()
36         .node(NetworkTopology.QNAME).node(Topology.QNAME)
37         .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), TopologyNetconf.QNAME.getLocalName())
38         .node(Node.QNAME).build();
39     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id");
40     private static final String NotificationCapabilityPrefix = "(urn:ietf:params:xml:ns:netconf:notification";
41
42     private final Node node;
43     private final InstanceIdentifier<?> instanceIdent;
44     private final NetconfEventSourceManager netconfEventSourceManager;
45     private ConnectionStatus currentNetconfConnStatus;
46     private EventSourceRegistration<NetconfEventSource> eventSourceRegistration;
47
48     public static NetconfEventSourceRegistration create(final InstanceIdentifier<?> instanceIdent, final Node node,
49         final NetconfEventSourceManager netconfEventSourceManager) {
50         Preconditions.checkNotNull(instanceIdent);
51         Preconditions.checkNotNull(node);
52         Preconditions.checkNotNull(netconfEventSourceManager);
53         if (isEventSource(node) == false) {
54             return null;
55         }
56         NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(instanceIdent, node,
57             netconfEventSourceManager);
58         nesr.updateStatus();
59         LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
60         return nesr;
61     }
62
63     private static boolean isEventSource(final Node node) {
64         final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
65         if (netconfNode == null) {
66             return false;
67         }
68         if (netconfNode.getAvailableCapabilities() == null) {
69             return false;
70         }
71         final List<String> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
72         if (capabilities == null || capabilities.isEmpty()) {
73             return false;
74         }
75         for (final String capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
76             if (capability.startsWith(NotificationCapabilityPrefix)) {
77                 return true;
78             }
79         }
80
81         return false;
82     }
83
84     private NetconfEventSourceRegistration(final InstanceIdentifier<?> instanceIdent, final Node node,
85         final NetconfEventSourceManager netconfEventSourceManager) {
86         this.instanceIdent = instanceIdent;
87         this.node = node;
88         this.netconfEventSourceManager = netconfEventSourceManager;
89         this.eventSourceRegistration = null;
90     }
91
92     public Node getNode() {
93         return node;
94     }
95
96     Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
97         return Optional.fromNullable(eventSourceRegistration);
98     }
99
100     NetconfNode getNetconfNode() {
101         return node.getAugmentation(NetconfNode.class);
102     }
103
104     void updateStatus() {
105         ConnectionStatus netconfConnStatus = getNetconfNode().getConnectionStatus();
106         LOG.info("Change status on node {}, new status is {}", this.node.getNodeId().getValue(), netconfConnStatus);
107         if (netconfConnStatus.equals(currentNetconfConnStatus)) {
108             return;
109         }
110         changeStatus(netconfConnStatus);
111     }
112
113     private boolean checkConnectionStatusType(ConnectionStatus status) {
114         if (status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
115             || status == ConnectionStatus.UnableToConnect) {
116             return true;
117         }
118         return false;
119     }
120
121     private void changeStatus(ConnectionStatus newStatus) {
122         Preconditions.checkNotNull(newStatus);
123         if (checkConnectionStatusType(newStatus) == false) {
124             throw new IllegalStateException("Unknown new Netconf Connection Status");
125         }
126         if (this.currentNetconfConnStatus == null) {
127             if (newStatus == ConnectionStatus.Connected) {
128                 registrationEventSource();
129             }
130         } else if (this.currentNetconfConnStatus == ConnectionStatus.Connecting) {
131             if (newStatus == ConnectionStatus.Connected) {
132                 if (this.eventSourceRegistration == null) {
133                     registrationEventSource();
134                 } else {
135                     // reactivate stream on registered event source (invoke publish notification about connection)
136                     this.eventSourceRegistration.getInstance().reActivateStreams();
137                 }
138             }
139         } else if (this.currentNetconfConnStatus == ConnectionStatus.Connected) {
140
141             if (newStatus == ConnectionStatus.Connecting || newStatus == ConnectionStatus.UnableToConnect) {
142                 // deactivate streams on registered event source (invoke publish notification about connection)
143                 this.eventSourceRegistration.getInstance().deActivateStreams();
144             }
145         } else if (this.currentNetconfConnStatus == ConnectionStatus.UnableToConnect) {
146             if (newStatus == ConnectionStatus.Connected) {
147                 if (this.eventSourceRegistration == null) {
148                     registrationEventSource();
149                 } else {
150                     // reactivate stream on registered event source (invoke publish notification about connection)
151                     this.eventSourceRegistration.getInstance().reActivateStreams();
152                 }
153             }
154         } else {
155             throw new IllegalStateException("Unknown current Netconf Connection Status");
156         }
157         this.currentNetconfConnStatus = newStatus;
158     }
159
160     private void registrationEventSource() {
161         final Optional<MountPoint> mountPoint = netconfEventSourceManager.getMountPointService()
162             .getMountPoint(instanceIdent);
163         final Optional<DOMMountPoint> domMountPoint = netconfEventSourceManager.getDomMounts()
164             .getMountPoint(domMountPath(node.getNodeId()));
165         EventSourceRegistration<NetconfEventSource> registration = null;
166         if (domMountPoint.isPresent() && mountPoint.isPresent()) {
167             final NetconfEventSource netconfEventSource = new NetconfEventSource(node,
168                 netconfEventSourceManager.getStreamMap(), domMountPoint.get(), mountPoint.get(),
169                 netconfEventSourceManager.getPublishService());
170             registration = netconfEventSourceManager.getEventSourceRegistry().registerEventSource(netconfEventSource);
171             LOG.info("Event source {} has been registered", node.getNodeId().getValue());
172         }
173         this.eventSourceRegistration = registration;
174     }
175
176     private YangInstanceIdentifier domMountPath(final NodeId nodeId) {
177         return YangInstanceIdentifier.builder(NETCONF_DEVICE_DOM_PATH)
178             .nodeWithKey(Node.QNAME, NODE_ID_QNAME, nodeId.getValue()).build();
179     }
180
181     private void closeEventSourceRegistration() {
182         if (getEventSourceRegistration().isPresent()) {
183             getEventSourceRegistration().get().close();
184         }
185     }
186
187     @Override public void close() {
188         closeEventSourceRegistration();
189     }
190
191 }