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