Merge "Remove netconf-config-dispatcher as moved to netconf-client"
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / 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.netconf.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.dom.api.DOMMountPoint;
14 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
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 NetconfEventSourceManager netconfEventSourceManager;
44     private ConnectionStatus currentNetconfConnStatus;
45     private EventSourceRegistration<NetconfEventSource> eventSourceRegistration;
46
47     public static NetconfEventSourceRegistration create(final InstanceIdentifier<?> instanceIdent, final Node node,
48         final NetconfEventSourceManager netconfEventSourceManager) {
49         Preconditions.checkNotNull(instanceIdent);
50         Preconditions.checkNotNull(node);
51         Preconditions.checkNotNull(netconfEventSourceManager);
52         if (!isEventSource(node)) {
53             return null;
54         }
55         NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(node, netconfEventSourceManager);
56         nesr.updateStatus();
57         LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
58         return nesr;
59     }
60
61     private static boolean isEventSource(final Node node) {
62         final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
63         if (netconfNode == null) {
64             return false;
65         }
66         if (netconfNode.getAvailableCapabilities() == null) {
67             return false;
68         }
69         final List<AvailableCapability> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
70         if (capabilities == null || capabilities.isEmpty()) {
71             return false;
72         }
73         for (final AvailableCapability capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
74             if (capability.getCapability().startsWith(NotificationCapabilityPrefix)) {
75                 return true;
76             }
77         }
78
79         return false;
80     }
81
82     private NetconfEventSourceRegistration(final Node node, final NetconfEventSourceManager netconfEventSourceManager) {
83         this.node = node;
84         this.netconfEventSourceManager = netconfEventSourceManager;
85         this.eventSourceRegistration = null;
86         this.currentNetconfConnStatus = ConnectionStatus.Connecting;
87     }
88
89     Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
90         return Optional.fromNullable(eventSourceRegistration);
91     }
92
93     NetconfNode getNetconfNode() {
94         return node.getAugmentation(NetconfNode.class);
95     }
96
97     void updateStatus() {
98         ConnectionStatus netconfConnStatus = getNetconfNode().getConnectionStatus();
99         LOG.info("Change status on node {}, new status is {}", this.node.getNodeId().getValue(), netconfConnStatus);
100         if (netconfConnStatus.equals(currentNetconfConnStatus)) {
101             return;
102         }
103         changeStatus(netconfConnStatus);
104     }
105
106     private boolean checkConnectionStatusType(ConnectionStatus status) {
107         return status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
108                 || status == ConnectionStatus.UnableToConnect;
109     }
110
111     private void changeStatus(ConnectionStatus newStatus) {
112         Preconditions.checkNotNull(newStatus);
113         Preconditions.checkState(this.currentNetconfConnStatus != null);
114         if (!checkConnectionStatusType(newStatus)) {
115             throw new IllegalStateException("Unknown new Netconf Connection Status");
116         }
117         switch (this.currentNetconfConnStatus) {
118             case Connecting:
119             case UnableToConnect:
120                 if (newStatus == ConnectionStatus.Connected) {
121                     if (this.eventSourceRegistration == null) {
122                         registrationEventSource();
123                     } else {
124                         // reactivate stream on registered event source (invoke publish notification about connection)
125                         this.eventSourceRegistration.getInstance().reActivateStreams();
126                     }
127                 }
128                 break;
129             case Connected:
130                 if (newStatus == ConnectionStatus.Connecting || newStatus == ConnectionStatus.UnableToConnect) {
131                     // deactivate streams on registered event source (invoke publish notification about connection)
132                     this.eventSourceRegistration.getInstance().deActivateStreams();
133                 }
134                 break;
135             default:
136                 throw new IllegalStateException("Unknown current Netconf Connection Status");
137         }
138         this.currentNetconfConnStatus = newStatus;
139     }
140
141     private void registrationEventSource() {
142         final Optional<DOMMountPoint> domMountPoint = netconfEventSourceManager.getDomMounts()
143             .getMountPoint(domMountPath(node.getNodeId()));
144         EventSourceRegistration<NetconfEventSource> registration = null;
145         if (domMountPoint.isPresent()/* && mountPoint.isPresent()*/) {
146             NetconfEventSourceMount mount = new NetconfEventSourceMount(node, domMountPoint.get());
147             final NetconfEventSource netconfEventSource = new NetconfEventSource(
148                 netconfEventSourceManager.getStreamMap(),
149                     mount,
150                 netconfEventSourceManager.getPublishService());
151             registration = netconfEventSourceManager.getEventSourceRegistry().registerEventSource(netconfEventSource);
152             LOG.info("Event source {} has been registered", node.getNodeId().getValue());
153         }
154         this.eventSourceRegistration = registration;
155     }
156
157     private YangInstanceIdentifier domMountPath(final NodeId nodeId) {
158         return YangInstanceIdentifier.builder(NETCONF_DEVICE_DOM_PATH)
159             .nodeWithKey(Node.QNAME, NODE_ID_QNAME, nodeId.getValue()).build();
160     }
161
162     private void closeEventSourceRegistration() {
163         if (getEventSourceRegistration().isPresent()) {
164             getEventSourceRegistration().get().close();
165         }
166     }
167
168     @Override public void close() {
169         closeEventSourceRegistration();
170     }
171
172 }