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