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.sal.convertor.ConvertorExecutor;
28 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * session and inventory listener implementation
54 public class SalRegistrationManager implements SessionListener, AutoCloseable {
56 private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
57 private final ConvertorExecutor convertorExecutor;
59 private NotificationProviderService publishService;
61 private DataBroker dataService;
63 private RpcProviderRegistry rpcProviderRegistry;
65 private final SwitchFeaturesUtil swFeaturesUtil;
67 private ListenerRegistration<SessionListener> sessionListenerRegistration;
69 private OfEntityManager entManager;
71 public SalRegistrationManager(ConvertorExecutor convertorExecutor) {
72 this.convertorExecutor = convertorExecutor;
73 swFeaturesUtil = SwitchFeaturesUtil.getInstance();
76 public NotificationProviderService getPublishService() {
77 return publishService;
80 public void setPublishService(final NotificationProviderService publishService) {
81 this.publishService = publishService;
84 public void setDataService(final DataBroker dataService) {
85 this.dataService = dataService;
88 public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
89 this.rpcProviderRegistry = rpcProviderRegistry;
92 public void setOfEntityManager(OfEntityManager entManager) {
93 this.entManager = entManager;
98 sessionListenerRegistration = getSessionManager().registerSessionListener(this);
99 getSessionManager().setNotificationProviderService(publishService);
100 getSessionManager().setDataBroker(dataService);
101 LOG.debug("SalRegistrationManager initialized");
105 public void onSessionAdded(final SwitchSessionKeyOF sessionKey, final SessionContext context) {
106 GetFeaturesOutput features = context.getFeatures();
107 BigInteger datapathId = features.getDatapathId();
108 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
109 NodeRef nodeRef = new NodeRef(identifier);
110 NodeId nodeId = nodeIdFromDatapathId(datapathId);
111 ModelDrivenSwitch ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context, convertorExecutor);
113 NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
114 nodeAdded(ofSwitch, features, nodeRef),
115 context.getFeatures().getVersion());
117 reqOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
121 public void setRole (SessionContext context) {
122 entManager.setSlaveRole(context);
126 public void onSessionRemoved(final SessionContext context) {
127 GetFeaturesOutput features = context.getFeatures();
128 BigInteger datapathId = features.getDatapathId();
129 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
130 NodeRef nodeRef = new NodeRef(identifier);
131 NodeId nodeId = nodeIdFromDatapathId(datapathId);
132 unregOpenflowEntityOwnership(nodeId);
133 NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
135 ModelDrivenSwitchRegistration registration = context.getProviderRegistration();
136 if (null != registration) {
137 registration.close();
138 context.setProviderRegistration(null);
140 LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId);
142 NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
143 nodeRemoved, context.getFeatures().getVersion());
144 context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
147 private NodeUpdated nodeAdded(final ModelDrivenSwitch sw, final GetFeaturesOutput features, final NodeRef nodeRef) {
148 NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
149 builder.setId(sw.getNodeId());
150 builder.setNodeRef(nodeRef);
152 FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
154 builder2.setIpAddress(getIpAddressOf(sw));
155 builder2.setPortNumber(getPortNumberOf(sw));
156 } catch (Exception e) {
157 LOG.warn("IP address/Port Number of the node {} cannot be obtained.", sw.getNodeId(), e);
159 builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
160 builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
162 return builder.build();
165 private static IpAddress getIpAddressOf(final ModelDrivenSwitch sw) {
166 SessionContext sessionContext = sw.getSessionContext();
167 Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
168 "primary conductor must not be NULL -> " + sw.getNodeId());
169 Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
170 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
171 InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
173 if (remoteAddress == null) {
174 LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
177 return resolveIpAddress(remoteAddress.getAddress());
180 private static IpAddress resolveIpAddress(final InetAddress address) {
181 String hostAddress = address.getHostAddress();
182 if (address instanceof Inet4Address) {
183 return new IpAddress(new Ipv4Address(hostAddress));
185 if (address instanceof Inet6Address) {
186 return new IpAddress(new Ipv6Address(hostAddress));
188 throw new IllegalArgumentException("Unsupported IP address type!");
191 private static PortNumber getPortNumberOf(ModelDrivenSwitch sw) {
192 SessionContext sessionContext = sw.getSessionContext();
194 Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
195 "primary conductor must not be NULL -> " + sw.getNodeId());
196 Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
197 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
198 InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
200 if (remoteAddress == null) {
201 LOG.warn("Port Number of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
204 return resolvePortNumber(remoteAddress.getPort());
207 private static PortNumber resolvePortNumber(int port) {
208 PortNumber portNo = new PortNumber(port);
212 private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
213 NodeRemovedBuilder builder = new NodeRemovedBuilder();
214 builder.setNodeRef(nodeRef);
215 return builder.build();
218 public static InstanceIdentifier<Node> identifierFromDatapathId(final BigInteger datapathId) {
219 NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
220 InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
221 return builder.build();
224 public static NodeKey nodeKeyFromDatapathId(final BigInteger datapathId) {
225 return new NodeKey(nodeIdFromDatapathId(datapathId));
228 public static NodeId nodeIdFromDatapathId(final BigInteger datapathId) {
229 // FIXME: Convert to textual representation of datapathID
230 String current = String.valueOf(datapathId);
231 return new NodeId("openflow:" + current);
234 public SessionManager getSessionManager() {
235 return OFSessionUtil.getSessionManager();
239 public void close() {
241 rpcProviderRegistry = null;
242 publishService = null;
243 if (sessionListenerRegistration != null) {
244 sessionListenerRegistration.close();
248 private void reqOpenflowEntityOwnership(ModelDrivenSwitch ofSwitch,
249 SessionContext context,
250 NotificationQueueWrapper wrappedNotification,
251 RpcProviderRegistry rpcProviderRegistry) {
252 context.setValid(true);
253 entManager.requestOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
256 private void unregOpenflowEntityOwnership(NodeId nodeId) {
257 entManager.unregisterEntityOwnershipRequest(nodeId);