Bug 4957 Clean unnecessary code
[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.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * session and inventory listener implementation
50  */
51 public class SalRegistrationManager implements SessionListener, AutoCloseable {
52
53     private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
54
55     private NotificationProviderService publishService;
56
57     private DataBroker dataService;
58
59     private RpcProviderRegistry rpcProviderRegistry;
60
61     private final SwitchFeaturesUtil swFeaturesUtil;
62
63     private ListenerRegistration<SessionListener> sessionListenerRegistration;
64
65     public SalRegistrationManager() {
66         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
67     }
68
69     public NotificationProviderService getPublishService() {
70         return publishService;
71     }
72
73     public void setPublishService(final NotificationProviderService publishService) {
74         this.publishService = publishService;
75     }
76
77     public void setDataService(final DataBroker dataService) {
78         this.dataService = dataService;
79     }
80
81     public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
82         this.rpcProviderRegistry = rpcProviderRegistry;
83     }
84
85     public void init() {
86         LOG.debug("init..");
87         sessionListenerRegistration = getSessionManager().registerSessionListener(this);
88         getSessionManager().setNotificationProviderService(publishService);
89         getSessionManager().setDataBroker(dataService);
90         LOG.debug("SalRegistrationManager initialized");
91     }
92
93     @Override
94     public void onSessionAdded(final SwitchSessionKeyOF sessionKey, final SessionContext context) {
95         GetFeaturesOutput features = context.getFeatures();
96         BigInteger datapathId = features.getDatapathId();
97         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
98         NodeRef nodeRef = new NodeRef(identifier);
99         NodeId nodeId = nodeIdFromDatapathId(datapathId);
100         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
101         CompositeObjectRegistration<ModelDrivenSwitch> registration =
102                 ofSwitch.register(rpcProviderRegistry);
103         context.setProviderRegistration(registration);
104
105         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId);
106
107         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
108                 nodeAdded(ofSwitch, features, nodeRef),
109                 context.getFeatures().getVersion());
110         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
111     }
112
113     @Override
114     public void onSessionRemoved(final SessionContext context) {
115         GetFeaturesOutput features = context.getFeatures();
116         BigInteger datapathId = features.getDatapathId();
117         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
118         NodeRef nodeRef = new NodeRef(identifier);
119         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
120
121         CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
122         if (null != registration) {
123             registration.close();
124             context.setProviderRegistration(null);
125         }
126         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId);
127
128         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
129                 nodeRemoved, context.getFeatures().getVersion());
130         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
131     }
132
133     private NodeUpdated nodeAdded(final ModelDrivenSwitch sw, final GetFeaturesOutput features, final NodeRef nodeRef) {
134         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
135         builder.setId(sw.getNodeId());
136         builder.setNodeRef(nodeRef);
137
138         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
139         try {
140             builder2.setIpAddress(getIpAddressOf(sw));
141         } catch (Exception e) {
142             LOG.warn("IP address of the node {} cannot be obtained.", sw.getNodeId(), e);
143         }
144         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
145         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
146
147         return builder.build();
148     }
149
150     private static IpAddress getIpAddressOf(final ModelDrivenSwitch sw) {
151         SessionContext sessionContext = sw.getSessionContext();
152         Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
153                 "primary conductor must not be NULL -> " + sw.getNodeId());
154         Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
155                 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
156         InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
157                 .getRemoteAddress();
158         if (remoteAddress == null) {
159             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
160             return null;
161         }
162         return resolveIpAddress(remoteAddress.getAddress());
163     }
164
165     private static IpAddress resolveIpAddress(final InetAddress address) {
166         String hostAddress = address.getHostAddress();
167         if (address instanceof Inet4Address) {
168             return new IpAddress(new Ipv4Address(hostAddress));
169         }
170         if (address instanceof Inet6Address) {
171             return new IpAddress(new Ipv6Address(hostAddress));
172         }
173         throw new IllegalArgumentException("Unsupported IP address type!");
174     }
175
176     private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
177         NodeRemovedBuilder builder = new NodeRemovedBuilder();
178         builder.setNodeRef(nodeRef);
179         return builder.build();
180     }
181
182     public static InstanceIdentifier<Node> identifierFromDatapathId(final BigInteger datapathId) {
183         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
184         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
185         return builder.build();
186     }
187
188     public static NodeKey nodeKeyFromDatapathId(final BigInteger datapathId) {
189         return new NodeKey(nodeIdFromDatapathId(datapathId));
190     }
191
192     public static NodeId nodeIdFromDatapathId(final BigInteger datapathId) {
193         // FIXME: Convert to textual representation of datapathID
194         String current = String.valueOf(datapathId);
195         return new NodeId("openflow:" + current);
196     }
197
198     public SessionManager getSessionManager() {
199         return OFSessionUtil.getSessionManager();
200     }
201
202     @Override
203     public void close() {
204         LOG.debug("close");
205         dataService = null;
206         rpcProviderRegistry = null;
207         publishService = null;
208         if (sessionListenerRegistration != null) {
209             sessionListenerRegistration.close();
210         }
211     }
212 }