7b48c623faf9e9ff6c4aa3885491268846c2397c
[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.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.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 final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
54
55     private ProviderContext providerContext;
56
57     private NotificationProviderService publishService;
58
59     private DataBroker dataService;
60
61     private 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(NotificationProviderService publishService) {
74         this.publishService = publishService;
75     }
76
77     public ProviderContext getProviderContext() {
78         return providerContext;
79     }
80
81     public void onSessionInitiated(ProviderContext session) {
82         LOG.debug("onSessionInitiated");
83         this.providerContext = session;
84         this.publishService = session.getSALService(NotificationProviderService.class);
85         this.dataService = session.getSALService(DataBroker.class);
86         // We register as listener for Session Manager
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(SwitchSessionKeyOF sessionKey, 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 = ofSwitch.register(providerContext);
102         context.setProviderRegistration(registration);
103
104         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
105
106         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
107                 nodeAdded(ofSwitch, features, nodeRef),
108                 context.getFeatures().getVersion());
109         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
110     }
111
112     @Override
113     public void onSessionRemoved(SessionContext context) {
114         GetFeaturesOutput features = context.getFeatures();
115         BigInteger datapathId = features.getDatapathId();
116         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
117         NodeRef nodeRef = new NodeRef(identifier);
118         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
119
120         CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
121         if (null != registration) {
122             registration.close();
123             context.setProviderRegistration(null);
124         }
125         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId.toString());
126
127         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
128                 nodeRemoved, context.getFeatures().getVersion());
129         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
130     }
131
132     private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
133         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
134         builder.setId(sw.getNodeId());
135         builder.setNodeRef(nodeRef);
136
137         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
138         try {
139             builder2.setIpAddress(getIpAddressOf(sw));
140         } catch (Exception e) {
141             LOG.warn("IP address of the node {} cannot be obtained: {}", sw.getNodeId(), e.getMessage());
142         }
143         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
144         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
145
146         return builder.build();
147     }
148
149     private static IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
150         SessionContext sessionContext = sw.getSessionContext();
151 //        if (!sessionContext.isValid()) {
152 //            LOG.warn("IP address of the node {} cannot be obtained. Session is not valid.", sw.getNodeId());
153 //            return null;
154 //        }
155         Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
156                 "primary conductor must not be NULL -> " + sw.getNodeId());
157         Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
158                 "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
159         InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
160                 .getRemoteAddress();
161         if (remoteAddress == null) {
162             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
163             return null;
164         }
165         return resolveIpAddress(remoteAddress.getAddress());
166     }
167
168     private static IpAddress resolveIpAddress(InetAddress address) {
169         String hostAddress = address.getHostAddress();
170         if (address instanceof Inet4Address) {
171             return new IpAddress(new Ipv4Address(hostAddress));
172         }
173         if (address instanceof Inet6Address) {
174             return new IpAddress(new Ipv6Address(hostAddress));
175         }
176         throw new IllegalArgumentException("Unsupported IP address type!");
177     }
178
179     private NodeRemoved nodeRemoved(NodeRef nodeRef) {
180         NodeRemovedBuilder builder = new NodeRemovedBuilder();
181         builder.setNodeRef(nodeRef);
182         return builder.build();
183     }
184
185     public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
186         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
187         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
188         return builder.build();
189     }
190
191     public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
192         return new NodeKey(nodeIdFromDatapathId(datapathId));
193     }
194
195     public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
196         // FIXME: Convert to textual representation of datapathID
197         String current = datapathId.toString();
198         return new NodeId("openflow:" + current);
199     }
200
201     public SessionManager getSessionManager() {
202         return OFSessionUtil.getSessionManager();
203     }
204
205     @Override
206     public void close() {
207         LOG.debug("close");
208         dataService = null;
209         providerContext = null;
210         publishService = null;
211         if (sessionListenerRegistration != null) {
212             sessionListenerRegistration.close();
213         }
214     }
215
216     /**
217      * @param providerContext the providerContext to set
218      */
219     public void setProviderContext(ProviderContext providerContext) {
220         this.providerContext = providerContext;
221     }
222 }