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