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 java.math.BigInteger;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
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.lldp.LLDPSpeaker;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
42 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * session and inventory listener implementation
52 public class SalRegistrationManager implements SessionListener, AutoCloseable {
54 private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
56 private ProviderContext providerContext;
58 private NotificationProviderService publishService;
60 private DataBroker dataService;
62 private SwitchFeaturesUtil swFeaturesUtil;
64 private ListenerRegistration<SessionListener> sessionListenerRegistration;
66 public SalRegistrationManager() {
67 swFeaturesUtil = SwitchFeaturesUtil.getInstance();
70 public NotificationProviderService getPublishService() {
71 return publishService;
74 public void setPublishService(NotificationProviderService publishService) {
75 this.publishService = publishService;
78 public ProviderContext getProviderContext() {
79 return providerContext;
82 public void onSessionInitiated(ProviderContext session) {
83 LOG.debug("onSessionInitiated");
84 this.providerContext = session;
85 this.publishService = session.getSALService(NotificationProviderService.class);
86 this.dataService = session.getSALService(DataBroker.class);
87 // We register as listener for Session Manager
88 sessionListenerRegistration = getSessionManager().registerSessionListener(this);
89 getSessionManager().setNotificationProviderService(publishService);
90 getSessionManager().setDataBroker(dataService);
91 LOG.debug("SalRegistrationManager initialized");
95 public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
96 GetFeaturesOutput features = context.getFeatures();
97 BigInteger datapathId = features.getDatapathId();
98 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
99 NodeRef nodeRef = new NodeRef(identifier);
100 NodeId nodeId = nodeIdFromDatapathId(datapathId);
101 ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
102 LLDPSpeaker.getInstance().addModelDrivenSwitch(identifier, ofSwitch);
103 CompositeObjectRegistration<ModelDrivenSwitch> registration = ofSwitch.register(providerContext);
104 context.setProviderRegistration(registration);
106 LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
108 NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
109 nodeAdded(ofSwitch, features, nodeRef),
110 context.getFeatures().getVersion());
111 context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
115 public void onSessionRemoved(SessionContext context) {
116 GetFeaturesOutput features = context.getFeatures();
117 BigInteger datapathId = features.getDatapathId();
118 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
119 NodeRef nodeRef = new NodeRef(identifier);
120 NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
121 LLDPSpeaker.getInstance().removeModelDrivenSwitch(identifier);
122 if (context.isValid()) {
123 CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
124 registration.close();
127 LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId.toString());
129 NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
130 nodeRemoved, context.getFeatures().getVersion());
131 context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
134 private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
135 NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
136 builder.setId(sw.getNodeId());
137 builder.setNodeRef(nodeRef);
139 FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
140 builder2.setIpAddress(getIpAddressOf(sw));
141 builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
142 builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
144 return builder.build();
147 private IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
148 SessionContext sessionContext = sw.getSessionContext();
149 if (!sessionContext.isValid()) {
150 LOG.warn("IP address of the node {} cannot be obtained. Session is not valid.", sw.getNodeId());
153 InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
155 if (remoteAddress == null) {
156 LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
159 return resolveIpAddress(remoteAddress.getAddress());
162 private static IpAddress resolveIpAddress(InetAddress address) {
163 String hostAddress = address.getHostAddress();
164 if (address instanceof Inet4Address) {
165 return new IpAddress(new Ipv4Address(hostAddress));
167 if (address instanceof Inet6Address) {
168 return new IpAddress(new Ipv6Address(hostAddress));
170 throw new IllegalArgumentException("Unsupported IP address type!");
173 private NodeRemoved nodeRemoved(NodeRef nodeRef) {
174 NodeRemovedBuilder builder = new NodeRemovedBuilder();
175 builder.setNodeRef(nodeRef);
176 return builder.build();
179 public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
180 NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
181 InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
182 return builder.toInstance();
185 public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
186 return new NodeKey(nodeIdFromDatapathId(datapathId));
189 public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
190 // FIXME: Convert to textual representation of datapathID
191 String current = datapathId.toString();
192 return new NodeId("openflow:" + current);
195 public SessionManager getSessionManager() {
196 return OFSessionUtil.getSessionManager();
200 public void close() {
203 providerContext = null;
204 publishService = null;
205 if (sessionListenerRegistration != null) {
206 sessionListenerRegistration.close();