fix of BUG 434
[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.util.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
17 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
18 import org.opendaylight.openflowplugin.openflow.md.SwitchInventory;
19 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
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.lldp.LLDPSpeaker;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * session and inventory listener implementation
44  */
45 public class SalRegistrationManager implements SessionListener, AutoCloseable {
46
47     private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
48
49     private ProviderContext providerContext;
50
51     private NotificationProviderService publishService;
52
53     private DataProviderService dataService;
54     
55     private SwitchFeaturesUtil swFeaturesUtil;
56     
57     public SalRegistrationManager() {
58         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
59     }
60
61     public NotificationProviderService getPublishService() {
62         return publishService;
63     }
64
65     public void setPublishService(NotificationProviderService publishService) {
66         this.publishService = publishService;
67     }
68
69     public ProviderContext getProviderContext() {
70         return providerContext;
71     }
72
73     public void onSessionInitiated(ProviderContext session) {
74         LOG.debug("onSessionInitiated");
75         this.providerContext = session;
76         this.publishService = session.getSALService(NotificationProviderService.class);
77         this.dataService = session.getSALService(DataProviderService.class);
78         // We register as listener for Session Manager
79         getSessionManager().registerSessionListener(this);
80         getSessionManager().setNotificationProviderService(publishService);
81         getSessionManager().setDataProviderService(dataService);
82         LOG.debug("SalRegistrationManager initialized");
83     }
84
85     @Override
86     public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
87         GetFeaturesOutput features = context.getFeatures();
88         BigInteger datapathId = features.getDatapathId();
89         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
90         NodeRef nodeRef = new NodeRef(identifier);
91         NodeId nodeId = nodeIdFromDatapathId(datapathId);
92         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
93         LLDPSpeaker.getInstance().addModelDrivenSwitch(identifier, ofSwitch);
94         ofSwitch.register(providerContext);
95
96         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
97
98         publishService.publish(nodeAdded(ofSwitch, features,nodeRef));
99     }
100
101     @Override
102     public void onSessionRemoved(SessionContext context) {
103         GetFeaturesOutput features = context.getFeatures();
104         BigInteger datapathId = features.getDatapathId();
105         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
106         NodeRef nodeRef = new NodeRef(identifier);
107         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
108         LLDPSpeaker.getInstance().removeModelDrivenSwitch(identifier);
109         LOG.debug("ModelDrivenSwitch for {} unregistred from MD-SAL.", datapathId.toString());
110         publishService.publish(nodeRemoved);
111     }
112
113     private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
114         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
115         builder.setId(sw.getNodeId());
116         builder.setNodeRef(nodeRef);
117         
118         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
119         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
120         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
121         
122         return builder.build();
123     }
124
125     private NodeRemoved nodeRemoved(NodeRef nodeRef) {
126         NodeRemovedBuilder builder = new NodeRemovedBuilder();
127         builder.setNodeRef(nodeRef);
128         return builder.build();
129     }
130
131     public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
132         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
133         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class,nodeKey);
134         return builder.toInstance();
135     }
136
137     public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
138         return new NodeKey(nodeIdFromDatapathId(datapathId));
139     }
140
141     public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
142         // FIXME: Convert to textual representation of datapathID
143         String current = datapathId.toString();
144         return new NodeId("openflow:" + current);
145     }
146
147     public SessionManager getSessionManager() {
148         return OFSessionUtil.getSessionManager();
149     }
150     
151     @Override
152     public void close() {
153         LOG.debug("close");
154         dataService = null;
155         providerContext = null;
156         publishService = null;
157     }
158 }