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 InstanceIdentifier<?> instanceIdent;
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) == false) {
56 NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(instanceIdent, node,
57 netconfEventSourceManager);
59 LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
63 private static boolean isEventSource(final Node node) {
64 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
65 if (netconfNode == null) {
68 if (netconfNode.getAvailableCapabilities() == null) {
71 final List<String> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
72 if (capabilities == null || capabilities.isEmpty()) {
75 for (final String capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
76 if (capability.startsWith(NotificationCapabilityPrefix)) {
84 private NetconfEventSourceRegistration(final InstanceIdentifier<?> instanceIdent, final Node node,
85 final NetconfEventSourceManager netconfEventSourceManager) {
86 this.instanceIdent = instanceIdent;
88 this.netconfEventSourceManager = netconfEventSourceManager;
89 this.eventSourceRegistration = null;
92 public Node getNode() {
96 Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
97 return Optional.fromNullable(eventSourceRegistration);
100 NetconfNode getNetconfNode() {
101 return node.getAugmentation(NetconfNode.class);
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)) {
110 changeStatus(netconfConnStatus);
113 private boolean checkConnectionStatusType(ConnectionStatus status) {
114 if (status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
115 || status == ConnectionStatus.UnableToConnect) {
121 private void changeStatus(ConnectionStatus newStatus) {
122 Preconditions.checkNotNull(newStatus);
123 if (checkConnectionStatusType(newStatus) == false) {
124 throw new IllegalStateException("Unknown new Netconf Connection Status");
126 if (this.currentNetconfConnStatus == null) {
127 if (newStatus == ConnectionStatus.Connected) {
128 registrationEventSource();
130 } else if (this.currentNetconfConnStatus == ConnectionStatus.Connecting) {
131 if (newStatus == ConnectionStatus.Connected) {
132 if (this.eventSourceRegistration == null) {
133 registrationEventSource();
135 // reactivate stream on registered event source (invoke publish notification about connection)
136 this.eventSourceRegistration.getInstance().reActivateStreams();
139 } else if (this.currentNetconfConnStatus == ConnectionStatus.Connected) {
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();
145 } else if (this.currentNetconfConnStatus == ConnectionStatus.UnableToConnect) {
146 if (newStatus == ConnectionStatus.Connected) {
147 if (this.eventSourceRegistration == null) {
148 registrationEventSource();
150 // reactivate stream on registered event source (invoke publish notification about connection)
151 this.eventSourceRegistration.getInstance().reActivateStreams();
155 throw new IllegalStateException("Unknown current Netconf Connection Status");
157 this.currentNetconfConnStatus = newStatus;
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());
173 this.eventSourceRegistration = registration;
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();
181 private void closeEventSourceRegistration() {
182 if (getEventSourceRegistration().isPresent()) {
183 getEventSourceRegistration().get().close();
187 @Override public void close() {
188 closeEventSourceRegistration();