BUG-1254: fix concurrent add/remove session test
[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 java.math.BigInteger;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
15
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.openflowplugin.openflow.md.lldp.LLDPSpeaker;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
42 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * session and inventory listener implementation
51  */
52 public class SalRegistrationManager implements SessionListener, AutoCloseable {
53
54     private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
55
56     private ProviderContext providerContext;
57
58     private NotificationProviderService publishService;
59
60     private DataBroker dataService;
61
62     private SwitchFeaturesUtil swFeaturesUtil;
63
64     private ListenerRegistration<SessionListener> sessionListenerRegistration;
65
66     public SalRegistrationManager() {
67         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
68     }
69
70     public NotificationProviderService getPublishService() {
71         return publishService;
72     }
73
74     public void setPublishService(NotificationProviderService publishService) {
75         this.publishService = publishService;
76     }
77
78     public ProviderContext getProviderContext() {
79         return providerContext;
80     }
81
82     public void onSessionInitiated(ProviderContext session) {
83         LOG.debug("onSessionInitiated");
84         this.providerContext = session;
85         this.publishService = session.getSALService(NotificationProviderService.class);
86         this.dataService = session.getSALService(DataBroker.class);
87         // We register as listener for Session Manager
88         sessionListenerRegistration = getSessionManager().registerSessionListener(this);
89         getSessionManager().setNotificationProviderService(publishService);
90         getSessionManager().setDataBroker(dataService);
91         LOG.debug("SalRegistrationManager initialized");
92     }
93
94     @Override
95     public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
96         GetFeaturesOutput features = context.getFeatures();
97         BigInteger datapathId = features.getDatapathId();
98         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
99         NodeRef nodeRef = new NodeRef(identifier);
100         NodeId nodeId = nodeIdFromDatapathId(datapathId);
101         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
102         LLDPSpeaker.getInstance().addModelDrivenSwitch(identifier, ofSwitch);
103         CompositeObjectRegistration<ModelDrivenSwitch> registration = ofSwitch.register(providerContext);
104         context.setProviderRegistration(registration);
105
106         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
107
108         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
109                 nodeAdded(ofSwitch, features, nodeRef), 
110                 context.getFeatures().getVersion());
111         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
112     }
113
114     @Override
115     public void onSessionRemoved(SessionContext context) {
116         GetFeaturesOutput features = context.getFeatures();
117         BigInteger datapathId = features.getDatapathId();
118         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
119         NodeRef nodeRef = new NodeRef(identifier);
120         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
121         LLDPSpeaker.getInstance().removeModelDrivenSwitch(identifier);
122         if (context.isValid()) {
123             CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
124             registration.close();
125         }
126
127         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId.toString());
128         
129         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
130                 nodeRemoved, context.getFeatures().getVersion());
131         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
132     }
133
134     private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
135         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
136         builder.setId(sw.getNodeId());
137         builder.setNodeRef(nodeRef);
138
139         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
140         builder2.setIpAddress(getIpAddressOf(sw));
141         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
142         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
143
144         return builder.build();
145     }
146
147     private IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
148         SessionContext sessionContext = sw.getSessionContext();
149         if (!sessionContext.isValid()) {
150             LOG.warn("IP address of the node {} cannot be obtained. Session is not valid.", sw.getNodeId());
151             return null;
152         }
153         InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
154                 .getRemoteAddress();
155         if (remoteAddress == null) {
156             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
157             return null;
158         }
159         return resolveIpAddress(remoteAddress.getAddress());
160     }
161
162     private static IpAddress resolveIpAddress(InetAddress address) {
163         String hostAddress = address.getHostAddress();
164         if (address instanceof Inet4Address) {
165             return new IpAddress(new Ipv4Address(hostAddress));
166         }
167         if (address instanceof Inet6Address) {
168             return new IpAddress(new Ipv6Address(hostAddress));
169         }
170         throw new IllegalArgumentException("Unsupported IP address type!");
171     }
172
173     private NodeRemoved nodeRemoved(NodeRef nodeRef) {
174         NodeRemovedBuilder builder = new NodeRemovedBuilder();
175         builder.setNodeRef(nodeRef);
176         return builder.build();
177     }
178
179     public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
180         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
181         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
182         return builder.toInstance();
183     }
184
185     public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
186         return new NodeKey(nodeIdFromDatapathId(datapathId));
187     }
188
189     public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
190         // FIXME: Convert to textual representation of datapathID
191         String current = datapathId.toString();
192         return new NodeId("openflow:" + current);
193     }
194
195     public SessionManager getSessionManager() {
196         return OFSessionUtil.getSessionManager();
197     }
198
199     @Override
200     public void close() {
201         LOG.debug("close");
202         dataService = null;
203         providerContext = null;
204         publishService = null;
205         if (sessionListenerRegistration != null) {
206             sessionListenerRegistration.close();
207         }
208     }
209 }