46463f1c7098076e337d2c8c5b20a4c60687a147
[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.core.NotificationQueueWrapper;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionListener;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
25 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
41 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
45 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
46 import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
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         CompositeObjectRegistration<ModelDrivenSwitch> 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         } catch (Exception e) {
153             LOG.warn("IP address of the node {} cannot be obtained.", sw.getNodeId(), e);
154         }
155         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
156         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
157
158         return builder.build();
159     }
160
161     private static IpAddress getIpAddressOf(final ModelDrivenSwitch sw) {
162         SessionContext sessionContext = sw.getSessionContext();
163         Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
164                 "primary conductor must not be NULL -> " + sw.getNodeId());
165         Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
166                 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
167         InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
168                 .getRemoteAddress();
169         if (remoteAddress == null) {
170             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
171             return null;
172         }
173         return resolveIpAddress(remoteAddress.getAddress());
174     }
175
176     private static IpAddress resolveIpAddress(final InetAddress address) {
177         String hostAddress = address.getHostAddress();
178         if (address instanceof Inet4Address) {
179             return new IpAddress(new Ipv4Address(hostAddress));
180         }
181         if (address instanceof Inet6Address) {
182             return new IpAddress(new Ipv6Address(hostAddress));
183         }
184         throw new IllegalArgumentException("Unsupported IP address type!");
185     }
186
187     private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
188         NodeRemovedBuilder builder = new NodeRemovedBuilder();
189         builder.setNodeRef(nodeRef);
190         return builder.build();
191     }
192
193     public static InstanceIdentifier<Node> identifierFromDatapathId(final BigInteger datapathId) {
194         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
195         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
196         return builder.build();
197     }
198
199     public static NodeKey nodeKeyFromDatapathId(final BigInteger datapathId) {
200         return new NodeKey(nodeIdFromDatapathId(datapathId));
201     }
202
203     public static NodeId nodeIdFromDatapathId(final BigInteger datapathId) {
204         // FIXME: Convert to textual representation of datapathID
205         String current = String.valueOf(datapathId);
206         return new NodeId("openflow:" + current);
207     }
208
209     public SessionManager getSessionManager() {
210         return OFSessionUtil.getSessionManager();
211     }
212
213     @Override
214     public void close() {
215         dataService = null;
216         rpcProviderRegistry = null;
217         publishService = null;
218         if (sessionListenerRegistration != null) {
219             sessionListenerRegistration.close();
220         }
221     }
222
223     private void reqOpenflowEntityOwnership(ModelDrivenSwitch ofSwitch,
224                                             SessionContext context,
225                                             NotificationQueueWrapper wrappedNotification,
226                                             RpcProviderRegistry rpcProviderRegistry) {
227         context.setValid(true);
228         entManager.requestOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
229     }
230
231     private void unregOpenflowEntityOwnership(NodeId nodeId) {
232         entManager.unregisterEntityOwnershipRequest(nodeId);
233     }
234
235 }