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