Error/Experimenter converters
[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.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;
38
39 /**
40  * session and inventory listener implementation
41  */
42 public class SalRegistrationManager implements SessionListener, SwitchInventory {
43
44     private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
45
46     Map<InstanceIdentifier<Node>, ModelDrivenSwitch> salSwitches = new ConcurrentHashMap<>();
47
48     private ProviderContext providerContext;
49
50     private NotificationProviderService publishService;
51
52     private DataProviderService dataService;
53
54     public NotificationProviderService getPublishService() {
55         return publishService;
56     }
57
58     public void setPublishService(NotificationProviderService publishService) {
59         this.publishService = publishService;
60     }
61
62     public ProviderContext getProviderContext() {
63         return providerContext;
64     }
65
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
71         // We register as listener for Session Manager
72         getSessionManager().registerSessionListener(this);
73         getSessionManager().setNotificationProviderService(publishService);
74         getSessionManager().setDataProviderService(dataService);
75         LOG.info("SalRegistrationManager initialized");
76
77     }
78
79     @Override
80     public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
81         GetFeaturesOutput features = context.getFeatures();
82         BigInteger datapathId = features.getDatapathId();
83         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
84         NodeRef nodeRef = new NodeRef(identifier);
85         NodeId nodeId = nodeIdFromDatapathId(datapathId);
86         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
87         salSwitches.put(identifier, ofSwitch);
88         ofSwitch.register(providerContext);
89
90         LOG.info("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
91
92         publishService.publish(nodeAdded(ofSwitch, features,nodeRef));
93     }
94
95     @Override
96     public void onSessionRemoved(SessionContext context) {
97         GetFeaturesOutput features = context.getFeatures();
98         BigInteger datapathId = features.getDatapathId();
99         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
100         NodeRef nodeRef = new NodeRef(identifier);
101         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
102
103         LOG.info("ModelDrivenSwitch for {} unregistred from MD-SAL.", datapathId.toString());
104         publishService.publish(nodeRemoved);
105     }
106
107     private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
108         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
109         builder.setId(sw.getNodeId());
110         builder.setNodeRef(nodeRef);
111         return builder.build();
112     }
113
114     private NodeRemoved nodeRemoved(NodeRef nodeRef) {
115         NodeRemovedBuilder builder = new NodeRemovedBuilder();
116         builder.setNodeRef(nodeRef);
117         return builder.build();
118     }
119
120     @Override
121     public ModelDrivenSwitch getSwitch(NodeRef node) {
122         return salSwitches.get(node.getValue());
123     }
124
125     public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
126         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder().node(Nodes.class);
127
128         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
129         return builder.node(Node.class, nodeKey).toInstance();
130     }
131
132     public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
133         return new NodeKey(nodeIdFromDatapathId(datapathId));
134     }
135
136     public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
137         // FIXME: Convert to textual representation of datapathID
138         String current = datapathId.toString();
139         return new NodeId("openflow:" + current);
140     }
141
142     public SessionManager getSessionManager() {
143         return OFSessionUtil.getSessionManager();
144     }
145 }