ec72db0329f9effd5e26fd371b09747e901016c8
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManager.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core.sal;
9
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.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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;
49
50 /**
51  * session and inventory listener implementation
52  */
53 public class SalRegistrationManager implements SessionListener, AutoCloseable {
54
55     private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
56
57     private NotificationProviderService publishService;
58
59     private DataBroker dataService;
60
61     private RpcProviderRegistry rpcProviderRegistry;
62
63     private final SwitchFeaturesUtil swFeaturesUtil;
64
65     private ListenerRegistration<SessionListener> sessionListenerRegistration;
66
67     private OfEntityManager entManager;
68
69     public SalRegistrationManager() {
70         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
71     }
72
73     public NotificationProviderService getPublishService() {
74         return publishService;
75     }
76
77     public void setPublishService(final NotificationProviderService publishService) {
78         this.publishService = publishService;
79     }
80
81     public void setDataService(final DataBroker dataService) {
82         this.dataService = dataService;
83     }
84
85     public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
86         this.rpcProviderRegistry = rpcProviderRegistry;
87     }
88
89     public void setOfEntityManager(OfEntityManager entManager) {
90         this.entManager = entManager;
91     }
92
93     public void init() {
94         LOG.debug("init..");
95         sessionListenerRegistration = getSessionManager().registerSessionListener(this);
96         getSessionManager().setNotificationProviderService(publishService);
97         getSessionManager().setDataBroker(dataService);
98         LOG.debug("SalRegistrationManager initialized");
99     }
100
101     @Override
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);
109
110         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
111                 nodeAdded(ofSwitch, features, nodeRef),
112                 context.getFeatures().getVersion());
113
114         reqOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
115     }
116
117     @Override
118     public void setRole (SessionContext context) {
119         entManager.setSlaveRole(context);
120     }
121
122     @Override
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);
131
132         ModelDrivenSwitchRegistration registration = context.getProviderRegistration();
133         if (null != registration) {
134             registration.close();
135             context.setProviderRegistration(null);
136         }
137         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId);
138
139         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
140                 nodeRemoved, context.getFeatures().getVersion());
141         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
142     }
143
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);
148
149         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
150         try {
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);
155         }
156         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
157         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
158
159         return builder.build();
160     }
161
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()
169                 .getRemoteAddress();
170         if (remoteAddress == null) {
171             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
172             return null;
173         }
174         return resolveIpAddress(remoteAddress.getAddress());
175     }
176
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));
181         }
182         if (address instanceof Inet6Address) {
183             return new IpAddress(new Ipv6Address(hostAddress));
184         }
185         throw new IllegalArgumentException("Unsupported IP address type!");
186     }
187
188     private static PortNumber getPortNumberOf(ModelDrivenSwitch sw) {
189         SessionContext sessionContext = sw.getSessionContext();
190
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()
196                 .getRemoteAddress();
197         if (remoteAddress == null) {
198             LOG.warn("Port Number of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
199             return null;
200         }
201         return resolvePortNumber(remoteAddress.getPort());
202     }
203
204     private static PortNumber resolvePortNumber(int port) {
205         PortNumber portNo = new PortNumber(port);
206         return portNo;
207     }
208
209     private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
210         NodeRemovedBuilder builder = new NodeRemovedBuilder();
211         builder.setNodeRef(nodeRef);
212         return builder.build();
213     }
214
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();
219     }
220
221     public static NodeKey nodeKeyFromDatapathId(final BigInteger datapathId) {
222         return new NodeKey(nodeIdFromDatapathId(datapathId));
223     }
224
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);
229     }
230
231     public SessionManager getSessionManager() {
232         return OFSessionUtil.getSessionManager();
233     }
234
235     @Override
236     public void close() {
237         dataService = null;
238         rpcProviderRegistry = null;
239         publishService = null;
240         if (sessionListenerRegistration != null) {
241             sessionListenerRegistration.close();
242         }
243     }
244
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);
251     }
252
253     private void unregOpenflowEntityOwnership(NodeId nodeId) {
254         entManager.unregisterEntityOwnershipRequest(nodeId);
255     }
256
257 }