2 * Copyright (c) 2013 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.openflowplugin.openflow.md.core.sal;
10 import com.google.common.base.Preconditions;
11 import java.math.BigInteger;
12 import java.net.Inet4Address;
13 import java.net.Inet6Address;
14 import java.net.InetAddress;
15 import java.net.InetSocketAddress;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
19 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
20 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitchRegistration;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionListener;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
26 import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
27 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * session and inventory listener implementation
53 public class SalRegistrationManager implements SessionListener, AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
57 private NotificationProviderService publishService;
59 private DataBroker dataService;
61 private RpcProviderRegistry rpcProviderRegistry;
63 private final SwitchFeaturesUtil swFeaturesUtil;
65 private ListenerRegistration<SessionListener> sessionListenerRegistration;
67 private OfEntityManager entManager;
69 public SalRegistrationManager() {
70 swFeaturesUtil = SwitchFeaturesUtil.getInstance();
73 public NotificationProviderService getPublishService() {
74 return publishService;
77 public void setPublishService(final NotificationProviderService publishService) {
78 this.publishService = publishService;
81 public void setDataService(final DataBroker dataService) {
82 this.dataService = dataService;
85 public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
86 this.rpcProviderRegistry = rpcProviderRegistry;
89 public void setOfEntityManager(OfEntityManager entManager) {
90 this.entManager = entManager;
95 sessionListenerRegistration = getSessionManager().registerSessionListener(this);
96 getSessionManager().setNotificationProviderService(publishService);
97 getSessionManager().setDataBroker(dataService);
98 LOG.debug("SalRegistrationManager initialized");
102 public void onSessionAdded(final SwitchSessionKeyOF sessionKey, final SessionContext context) {
103 GetFeaturesOutput features = context.getFeatures();
104 BigInteger datapathId = features.getDatapathId();
105 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
106 NodeRef nodeRef = new NodeRef(identifier);
107 NodeId nodeId = nodeIdFromDatapathId(datapathId);
108 ModelDrivenSwitch ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier,context);
110 NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
111 nodeAdded(ofSwitch, features, nodeRef),
112 context.getFeatures().getVersion());
114 reqOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
118 public void setRole (SessionContext context) {
119 entManager.setSlaveRole(context);
123 public void onSessionRemoved(final SessionContext context) {
124 GetFeaturesOutput features = context.getFeatures();
125 BigInteger datapathId = features.getDatapathId();
126 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
127 NodeRef nodeRef = new NodeRef(identifier);
128 NodeId nodeId = nodeIdFromDatapathId(datapathId);
129 unregOpenflowEntityOwnership(nodeId);
130 NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
132 ModelDrivenSwitchRegistration registration = context.getProviderRegistration();
133 if (null != registration) {
134 registration.close();
135 context.setProviderRegistration(null);
137 LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId);
139 NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
140 nodeRemoved, context.getFeatures().getVersion());
141 context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
144 private NodeUpdated nodeAdded(final ModelDrivenSwitch sw, final GetFeaturesOutput features, final NodeRef nodeRef) {
145 NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
146 builder.setId(sw.getNodeId());
147 builder.setNodeRef(nodeRef);
149 FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
151 builder2.setIpAddress(getIpAddressOf(sw));
152 builder2.setPortNumber(getPortNumberOf(sw));
153 } catch (Exception e) {
154 LOG.warn("IP address/Port Number of the node {} cannot be obtained.", sw.getNodeId(), e);
156 builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
157 builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
159 return builder.build();
162 private static IpAddress getIpAddressOf(final ModelDrivenSwitch sw) {
163 SessionContext sessionContext = sw.getSessionContext();
164 Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
165 "primary conductor must not be NULL -> " + sw.getNodeId());
166 Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
167 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
168 InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
170 if (remoteAddress == null) {
171 LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
174 return resolveIpAddress(remoteAddress.getAddress());
177 private static IpAddress resolveIpAddress(final InetAddress address) {
178 String hostAddress = address.getHostAddress();
179 if (address instanceof Inet4Address) {
180 return new IpAddress(new Ipv4Address(hostAddress));
182 if (address instanceof Inet6Address) {
183 return new IpAddress(new Ipv6Address(hostAddress));
185 throw new IllegalArgumentException("Unsupported IP address type!");
188 private static PortNumber getPortNumberOf(ModelDrivenSwitch sw) {
189 SessionContext sessionContext = sw.getSessionContext();
191 Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
192 "primary conductor must not be NULL -> " + sw.getNodeId());
193 Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
194 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
195 InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
197 if (remoteAddress == null) {
198 LOG.warn("Port Number of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
201 return resolvePortNumber(remoteAddress.getPort());
204 private static PortNumber resolvePortNumber(int port) {
205 PortNumber portNo = new PortNumber(port);
209 private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
210 NodeRemovedBuilder builder = new NodeRemovedBuilder();
211 builder.setNodeRef(nodeRef);
212 return builder.build();
215 public static InstanceIdentifier<Node> identifierFromDatapathId(final BigInteger datapathId) {
216 NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
217 InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
218 return builder.build();
221 public static NodeKey nodeKeyFromDatapathId(final BigInteger datapathId) {
222 return new NodeKey(nodeIdFromDatapathId(datapathId));
225 public static NodeId nodeIdFromDatapathId(final BigInteger datapathId) {
226 // FIXME: Convert to textual representation of datapathID
227 String current = String.valueOf(datapathId);
228 return new NodeId("openflow:" + current);
231 public SessionManager getSessionManager() {
232 return OFSessionUtil.getSessionManager();
236 public void close() {
238 rpcProviderRegistry = null;
239 publishService = null;
240 if (sessionListenerRegistration != null) {
241 sessionListenerRegistration.close();
245 private void reqOpenflowEntityOwnership(ModelDrivenSwitch ofSwitch,
246 SessionContext context,
247 NotificationQueueWrapper wrappedNotification,
248 RpcProviderRegistry rpcProviderRegistry) {
249 context.setValid(true);
250 entManager.requestOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
253 private void unregOpenflowEntityOwnership(NodeId nodeId) {
254 entManager.unregisterEntityOwnershipRequest(nodeId);