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