2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal;
10 import java.math.BigInteger;
12 import java.util.concurrent.ConcurrentHashMap;
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.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.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * session and inventory listener implementation
42 public class SalRegistrationManager implements SessionListener, SwitchInventory {
44 private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
46 Map<InstanceIdentifier<Node>, ModelDrivenSwitch> salSwitches = new ConcurrentHashMap<>();
48 private ProviderContext providerContext;
50 private NotificationProviderService publishService;
52 private DataProviderService dataService;
54 public NotificationProviderService getPublishService() {
55 return publishService;
58 public void setPublishService(NotificationProviderService publishService) {
59 this.publishService = publishService;
62 public ProviderContext getProviderContext() {
63 return providerContext;
66 public void onSessionInitiated(ProviderContext session) {
67 this.providerContext = session;
68 this.publishService = session.getSALService(NotificationProviderService.class);
69 this.dataService = session.getSALService(DataProviderService.class);
70 // We register as listener for Session Manager
71 getSessionManager().registerSessionListener(this);
72 getSessionManager().setNotificationProviderService(publishService);
73 getSessionManager().setDataProviderService(dataService);
74 LOG.info("SalRegistrationManager initialized");
79 public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
80 GetFeaturesOutput features = context.getFeatures();
81 BigInteger datapathId = features.getDatapathId();
82 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
83 NodeRef nodeRef = new NodeRef(identifier);
84 NodeId nodeId = nodeIdFromDatapathId(datapathId);
85 ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
86 salSwitches.put(identifier, ofSwitch);
87 ofSwitch.register(providerContext);
89 LOG.info("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
91 publishService.publish(nodeAdded(ofSwitch, features,nodeRef));
95 public void onSessionRemoved(SessionContext context) {
96 GetFeaturesOutput features = context.getFeatures();
97 BigInteger datapathId = features.getDatapathId();
98 InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
99 NodeRef nodeRef = new NodeRef(identifier);
100 NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
102 LOG.info("ModelDrivenSwitch for {} unregistred from MD-SAL.", datapathId.toString());
103 publishService.publish(nodeRemoved);
106 private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
107 NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
108 builder.setId(sw.getNodeId());
109 builder.setNodeRef(nodeRef);
110 return builder.build();
113 private NodeRemoved nodeRemoved(NodeRef nodeRef) {
114 NodeRemovedBuilder builder = new NodeRemovedBuilder();
115 builder.setNodeRef(nodeRef);
116 return builder.build();
120 public ModelDrivenSwitch getSwitch(NodeRef node) {
121 return salSwitches.get(node.getValue());
124 public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
125 NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
126 InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class,nodeKey);
127 return builder.toInstance();
130 public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
131 return new NodeKey(nodeIdFromDatapathId(datapathId));
134 public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
135 // FIXME: Convert to textual representation of datapathID
136 String current = datapathId.toString();
137 return new NodeId("openflow:" + current);
140 public SessionManager getSessionManager() {
141 return OFSessionUtil.getSessionManager();