Merge "Mask Support for TCP and UDP ports in nicira extension "
[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.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;
50
51 /**
52  * session and inventory listener implementation
53  */
54 public class SalRegistrationManager implements SessionListener, AutoCloseable {
55
56     private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
57     private final ConvertorExecutor convertorExecutor;
58
59     private NotificationProviderService publishService;
60
61     private DataBroker dataService;
62
63     private RpcProviderRegistry rpcProviderRegistry;
64
65     private final SwitchFeaturesUtil swFeaturesUtil;
66
67     private ListenerRegistration<SessionListener> sessionListenerRegistration;
68
69     private OfEntityManager entManager;
70
71     public SalRegistrationManager(ConvertorExecutor convertorExecutor) {
72         this.convertorExecutor = convertorExecutor;
73         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
74     }
75
76     public NotificationProviderService getPublishService() {
77         return publishService;
78     }
79
80     public void setPublishService(final NotificationProviderService publishService) {
81         this.publishService = publishService;
82     }
83
84     public void setDataService(final DataBroker dataService) {
85         this.dataService = dataService;
86     }
87
88     public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
89         this.rpcProviderRegistry = rpcProviderRegistry;
90     }
91
92     public void setOfEntityManager(OfEntityManager entManager) {
93         this.entManager = entManager;
94     }
95
96     public void init() {
97         LOG.debug("init..");
98         sessionListenerRegistration = getSessionManager().registerSessionListener(this);
99         getSessionManager().setNotificationProviderService(publishService);
100         getSessionManager().setDataBroker(dataService);
101         LOG.debug("SalRegistrationManager initialized");
102     }
103
104     @Override
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);
112
113         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
114                 nodeAdded(ofSwitch, features, nodeRef),
115                 context.getFeatures().getVersion());
116
117         reqOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
118     }
119
120     @Override
121     public void setRole (SessionContext context) {
122         entManager.setSlaveRole(context);
123     }
124
125     @Override
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);
134
135         ModelDrivenSwitchRegistration registration = context.getProviderRegistration();
136         if (null != registration) {
137             registration.close();
138             context.setProviderRegistration(null);
139         }
140         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId);
141
142         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
143                 nodeRemoved, context.getFeatures().getVersion());
144         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
145     }
146
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);
151
152         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
153         try {
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);
158         }
159         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
160         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
161
162         return builder.build();
163     }
164
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()
172                 .getRemoteAddress();
173         if (remoteAddress == null) {
174             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
175             return null;
176         }
177         return resolveIpAddress(remoteAddress.getAddress());
178     }
179
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));
184         }
185         if (address instanceof Inet6Address) {
186             return new IpAddress(new Ipv6Address(hostAddress));
187         }
188         throw new IllegalArgumentException("Unsupported IP address type!");
189     }
190
191     private static PortNumber getPortNumberOf(ModelDrivenSwitch sw) {
192         SessionContext sessionContext = sw.getSessionContext();
193
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()
199                 .getRemoteAddress();
200         if (remoteAddress == null) {
201             LOG.warn("Port Number of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
202             return null;
203         }
204         return resolvePortNumber(remoteAddress.getPort());
205     }
206
207     private static PortNumber resolvePortNumber(int port) {
208         PortNumber portNo = new PortNumber(port);
209         return portNo;
210     }
211
212     private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
213         NodeRemovedBuilder builder = new NodeRemovedBuilder();
214         builder.setNodeRef(nodeRef);
215         return builder.build();
216     }
217
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();
222     }
223
224     public static NodeKey nodeKeyFromDatapathId(final BigInteger datapathId) {
225         return new NodeKey(nodeIdFromDatapathId(datapathId));
226     }
227
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);
232     }
233
234     public SessionManager getSessionManager() {
235         return OFSessionUtil.getSessionManager();
236     }
237
238     @Override
239     public void close() {
240         dataService = null;
241         rpcProviderRegistry = null;
242         publishService = null;
243         if (sessionListenerRegistration != null) {
244             sessionListenerRegistration.close();
245         }
246     }
247
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);
254     }
255
256     private void unregOpenflowEntityOwnership(NodeId nodeId) {
257         entManager.unregisterEntityOwnershipRequest(nodeId);
258     }
259
260 }