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.Preconditions;
11 import java.util.List;
12 import java.util.Optional;
13 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
14 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
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;
30 * Helper class to keep connection status of netconf node and event source registration object.
32 public final 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
39 .node(Node.QNAME).build();
40 private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id");
41 private static final String NOTIFICATION_CAPABILITY_PREFIX = "(urn:ietf:params:xml:ns:netconf:notification";
43 private final Node node;
44 private final NetconfEventSourceManager netconfEventSourceManager;
45 private ConnectionStatus currentNetconfConnStatus;
46 private EventSourceRegistration<NetconfEventSource> eventSourceRegistration;
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)) {
56 NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(node, netconfEventSourceManager);
58 LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
62 private static boolean isEventSource(final Node node) {
63 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
64 if (netconfNode == null) {
67 if (netconfNode.getAvailableCapabilities() == null) {
70 final List<AvailableCapability> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
71 if (capabilities == null || capabilities.isEmpty()) {
74 for (final AvailableCapability capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
75 if (capability.getCapability().startsWith(NOTIFICATION_CAPABILITY_PREFIX)) {
83 private NetconfEventSourceRegistration(final Node node, final NetconfEventSourceManager netconfEventSourceManager) {
85 this.netconfEventSourceManager = netconfEventSourceManager;
86 this.eventSourceRegistration = null;
87 this.currentNetconfConnStatus = ConnectionStatus.Connecting;
90 Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
91 return Optional.ofNullable(eventSourceRegistration);
94 NetconfNode getNetconfNode() {
95 return node.augmentation(NetconfNode.class);
99 ConnectionStatus netconfConnStatus = getNetconfNode().getConnectionStatus();
100 LOG.info("Change status on node {}, new status is {}", this.node.getNodeId().getValue(), netconfConnStatus);
101 if (netconfConnStatus.equals(currentNetconfConnStatus)) {
104 changeStatus(netconfConnStatus);
107 private static boolean checkConnectionStatusType(final ConnectionStatus status) {
108 return status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
109 || status == ConnectionStatus.UnableToConnect;
112 private void changeStatus(final ConnectionStatus newStatus) {
113 Preconditions.checkNotNull(newStatus);
114 Preconditions.checkState(this.currentNetconfConnStatus != null);
115 if (!checkConnectionStatusType(newStatus)) {
116 throw new IllegalStateException("Unknown new Netconf Connection Status");
118 switch (this.currentNetconfConnStatus) {
120 case UnableToConnect:
121 if (newStatus == ConnectionStatus.Connected) {
122 if (this.eventSourceRegistration == null) {
123 registrationEventSource();
125 // reactivate stream on registered event source (invoke publish notification about connection)
126 this.eventSourceRegistration.getInstance().reActivateStreams();
131 if (newStatus == ConnectionStatus.Connecting || newStatus == ConnectionStatus.UnableToConnect) {
132 // deactivate streams on registered event source (invoke publish notification about connection)
133 this.eventSourceRegistration.getInstance().deActivateStreams();
137 throw new IllegalStateException("Unknown current Netconf Connection Status");
139 this.currentNetconfConnStatus = newStatus;
142 private void registrationEventSource() {
143 final Optional<DOMMountPoint> domMountPoint = netconfEventSourceManager.getDomMounts()
144 .getMountPoint(domMountPath(node.getNodeId()));
145 EventSourceRegistration<NetconfEventSource> registration = null;
146 if (domMountPoint.isPresent()/* && mountPoint.isPresent()*/) {
147 NetconfEventSourceMount mount = new NetconfEventSourceMount(node, domMountPoint.get());
148 final NetconfEventSource netconfEventSource = new NetconfEventSource(
149 netconfEventSourceManager.getStreamMap(),
151 netconfEventSourceManager.getPublishService());
152 registration = netconfEventSourceManager.getEventSourceRegistry().registerEventSource(netconfEventSource);
153 LOG.info("Event source {} has been registered", node.getNodeId().getValue());
155 this.eventSourceRegistration = registration;
158 private static YangInstanceIdentifier domMountPath(final NodeId nodeId) {
159 return YangInstanceIdentifier.builder(NETCONF_DEVICE_DOM_PATH)
160 .nodeWithKey(Node.QNAME, NODE_ID_QNAME, nodeId.getValue()).build();
163 private void closeEventSourceRegistration() {
164 if (getEventSourceRegistration().isPresent()) {
165 getEventSourceRegistration().get().close();
170 public void close() {
171 closeEventSourceRegistration();