2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.messagebus.eventsources.netconf;
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.NetconfNodeConnectionStatus.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;
30 * Helper class to keep connection status of netconf node and event source registration object
32 public class NetconfEventSourceRegistration implements AutoCloseable {
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";
42 private final Node node;
43 private final NetconfEventSourceManager netconfEventSourceManager;
44 private ConnectionStatus currentNetconfConnStatus;
45 private EventSourceRegistration<NetconfEventSource> eventSourceRegistration;
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)) {
55 NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(node, netconfEventSourceManager);
57 LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
61 private static boolean isEventSource(final Node node) {
62 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
63 if (netconfNode == null) {
66 if (netconfNode.getAvailableCapabilities() == null) {
69 final List<String> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
70 if (capabilities == null || capabilities.isEmpty()) {
73 for (final String capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
74 if (capability.startsWith(NotificationCapabilityPrefix)) {
82 private NetconfEventSourceRegistration(final Node node, final NetconfEventSourceManager netconfEventSourceManager) {
84 this.netconfEventSourceManager = netconfEventSourceManager;
85 this.eventSourceRegistration = null;
86 this.currentNetconfConnStatus = ConnectionStatus.Connecting;
89 Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
90 return Optional.fromNullable(eventSourceRegistration);
93 NetconfNode getNetconfNode() {
94 return node.getAugmentation(NetconfNode.class);
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)) {
103 changeStatus(netconfConnStatus);
106 private boolean checkConnectionStatusType(ConnectionStatus status) {
107 return status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
108 || status == ConnectionStatus.UnableToConnect;
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");
117 switch (this.currentNetconfConnStatus) {
119 case UnableToConnect:
120 if (newStatus == ConnectionStatus.Connected) {
121 if (this.eventSourceRegistration == null) {
122 registrationEventSource();
124 // reactivate stream on registered event source (invoke publish notification about connection)
125 this.eventSourceRegistration.getInstance().reActivateStreams();
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();
136 throw new IllegalStateException("Unknown current Netconf Connection Status");
138 this.currentNetconfConnStatus = newStatus;
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(),
150 netconfEventSourceManager.getPublishService());
151 registration = netconfEventSourceManager.getEventSourceRegistry().registerEventSource(netconfEventSource);
152 LOG.info("Event source {} has been registered", node.getNodeId().getValue());
154 this.eventSourceRegistration = registration;
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();
162 private void closeEventSourceRegistration() {
163 if (getEventSourceRegistration().isPresent()) {
164 getEventSourceRegistration().get().close();
168 @Override public void close() {
169 closeEventSourceRegistration();