Merge "Bug-835:Reserve ports should be logical ports"
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / NodeMapping.java
1 /**
2  * Copyright (c) 2014 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.controller.sal.compatibility;
9
10 import com.google.common.base.Objects;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.sal.common.util.Arguments;
13 import org.opendaylight.controller.sal.core.AdvertisedBandwidth;
14 import org.opendaylight.controller.sal.core.Bandwidth;
15 import org.opendaylight.controller.sal.core.Buffers;
16 import org.opendaylight.controller.sal.core.Capabilities;
17 import org.opendaylight.controller.sal.core.Config;
18 import org.opendaylight.controller.sal.core.ConstructionException;
19 import org.opendaylight.controller.sal.core.MacAddress;
20 import org.opendaylight.controller.sal.core.Name;
21 import org.opendaylight.controller.sal.core.Node.NodeIDType;
22 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
23 import org.opendaylight.controller.sal.core.PeerBandwidth;
24 import org.opendaylight.controller.sal.core.Property;
25 import org.opendaylight.controller.sal.core.SupportedBandwidth;
26 import org.opendaylight.controller.sal.core.Tables;
27 import org.opendaylight.controller.sal.core.TimeStamp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityArpMatchIp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityIpReasm;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityStp;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNodeConnector;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58
59 import java.math.BigInteger;
60 import java.util.Date;
61 import java.util.HashSet;
62 import java.util.List;
63
64 public final class NodeMapping {
65
66     /** openflow id prefix */
67     public static final String OPENFLOW_ID_PREFIX = "openflow:";
68
69     public final static String MD_SAL_TYPE = "MD_SAL_DEPRECATED";
70
71     private final static Class<Node> NODE_CLASS = Node.class;
72
73     private final static Class<NodeConnector> NODECONNECTOR_CLASS = NodeConnector.class;
74
75     private NodeMapping() {
76         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
77     }
78
79     public static org.opendaylight.controller.sal.core.Node toADNode(final InstanceIdentifier<? extends Object> node) throws ConstructionException {
80         NodeId nodeId = NodeMapping.toNodeId(node);
81         return NodeMapping.toADNode(nodeId);
82     }
83
84     public static org.opendaylight.controller.sal.core.Node toADNode(final NodeId id) throws ConstructionException {
85         Long aDNodeId = openflowFullNodeIdToLong(NodeMapping.toADNodeId(id));
86         return new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, aDNodeId);
87     }
88
89     /**
90      * @param adNodeId
91      * @return
92      */
93     private static Long openflowFullNodeIdToLong(String adNodeId) {
94         if (adNodeId == null) {
95             return null;
96         }
97         return Long.valueOf(adNodeId.replaceFirst("^.*:", ""));
98     }
99
100     public static NodeId toNodeId(final InstanceIdentifier<?> id) {
101         final NodeKey key = id.firstKeyOf(Node.class, NodeKey.class);
102         Preconditions.checkArgument(key != null, "No node identifier found in %s", id);
103         return key.getId();
104     }
105
106     public static String toADNodeId(final NodeId nodeId) {
107         if (nodeId == null) {
108             return null;
109         }
110         return nodeId.getValue();
111     }
112
113     public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorRef source) throws ConstructionException {
114         final InstanceIdentifier<?> id = Preconditions.checkNotNull(source.getValue());
115         final NodeConnectorKey key = id.firstKeyOf(NodeConnector.class, NodeConnectorKey.class);
116         return NodeMapping.toADNodeConnector(key.getId(), NodeMapping.toNodeId(id));
117     }
118
119     public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorId ncid, final NodeId nid) throws ConstructionException {
120         String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid);
121         Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid);
122         org.opendaylight.controller.sal.core.Node aDNode = NodeMapping.toADNode(nid);
123         return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode);
124     }
125
126     public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) {
127         if (ncId.equals(toLocalNodeConnectorId(nodeId))) {
128             return NodeConnectorIDType.SWSTACK;
129         } else if (ncId.equals(toNormalNodeConnectorId(nodeId))) {
130             return NodeConnectorIDType.HWPATH;
131         } else if (ncId.equals(toControllerNodeConnectorId(nodeId))) {
132             return NodeConnectorIDType.CONTROLLER;
133         }
134         return NodeConnectorIDType.OPENFLOW;
135     }
136
137     public static Object toADNodeConnectorId(final NodeConnectorId nodeConnectorId, final NodeId nodeId) {
138         if (nodeConnectorId.equals(toLocalNodeConnectorId(nodeId)) ||
139                 nodeConnectorId.equals(toNormalNodeConnectorId(nodeId)) ||
140                 nodeConnectorId.equals(toControllerNodeConnectorId(nodeId))) {
141             return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID;
142         }
143
144         return (short) Long.valueOf(nodeConnectorId.getValue().replaceFirst("^.*:", "")).longValue();
145     }
146
147     public static NodeId toAdNodeId(final NodeConnectorId nodeConnectorId) {
148         NodeId nodeId = null;
149         if (nodeConnectorId != null) {
150             nodeId = new NodeId(nodeConnectorId.getValue().replaceFirst(":[0-9]+$", ""));
151         }
152         return nodeId;
153     }
154
155     public static NodeConnectorId toControllerNodeConnectorId(final NodeId node) {
156         return new NodeConnectorId(node.getValue() + ":" + OutputPortValues.CONTROLLER.toString());
157     }
158
159     public static NodeConnectorId toLocalNodeConnectorId(final NodeId node) {
160         return new NodeConnectorId(node.getValue() + ":" + OutputPortValues.LOCAL.toString());
161     }
162
163     public static NodeConnectorId toNormalNodeConnectorId(final NodeId node) {
164         return new NodeConnectorId(node.getValue() + ":" + OutputPortValues.NORMAL.toString());
165     }
166
167     public static NodeRef toNodeRef(final org.opendaylight.controller.sal.core.Node node) {
168         Preconditions.checkArgument(NodeIDType.OPENFLOW.equals(node.getType()));
169         final Long nodeId = Arguments.<Long>checkInstanceOf(node.getID(), Long.class);
170         final NodeKey nodeKey = new NodeKey(new NodeId(OPENFLOW_ID_PREFIX+nodeId));
171         final InstanceIdentifier<Node> nodePath = InstanceIdentifier.builder(Nodes.class).child(NODE_CLASS, nodeKey).toInstance();
172         return new NodeRef(nodePath);
173     }
174
175     public static NodeConnectorRef toNodeConnectorRef(final org.opendaylight.controller.sal.core.NodeConnector nodeConnector) {
176
177         final NodeRef node = NodeMapping.toNodeRef(nodeConnector.getNode());
178         @SuppressWarnings("unchecked")
179         final InstanceIdentifier<Node> nodePath = ((InstanceIdentifier<Node>) node.getValue());
180         NodeConnectorId nodeConnectorId = null;
181
182         if (nodeConnector.getID().equals(org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID)) {
183             final NodeId nodeId = toNodeId(nodePath);
184             final String nodeConnectorType = nodeConnector.getType();
185             if (nodeConnectorType.equals(NodeConnectorIDType.SWSTACK)) {
186                 nodeConnectorId = toLocalNodeConnectorId(nodeId);
187             } else if (nodeConnectorType.equals(NodeConnectorIDType.HWPATH)) {
188                 nodeConnectorId = toNormalNodeConnectorId(nodeId);
189             } else if (nodeConnectorType.equals(NodeConnectorIDType.CONTROLLER)) {
190                 nodeConnectorId = toControllerNodeConnectorId(nodeId);
191             }
192         } else {
193             nodeConnectorId = new NodeConnectorId(OPENFLOW_ID_PREFIX
194                     + Arguments.<Short>checkInstanceOf(nodeConnector.getID(), Short.class));
195         }
196         final NodeConnectorKey connectorKey = new NodeConnectorKey(nodeConnectorId);
197         final InstanceIdentifier<NodeConnector> path = nodePath.child(NODECONNECTOR_CLASS, connectorKey);
198         return new NodeConnectorRef(path);
199     }
200
201     public static org.opendaylight.controller.sal.core.Node toADNode(final NodeRef node) throws ConstructionException {
202         return NodeMapping.toADNode(node.getValue());
203     }
204
205     public static HashSet<Property> toADNodeConnectorProperties(final NodeConnectorUpdated nc) {
206         final FlowCapableNodeConnectorUpdated fcncu = nc.<FlowCapableNodeConnectorUpdated>getAugmentation(FlowCapableNodeConnectorUpdated.class);
207         if (!Objects.equal(fcncu, null)) {
208             return NodeMapping.toADNodeConnectorProperties(fcncu);
209         }
210         return new HashSet<Property>();
211     }
212
213     public static HashSet<Property> toADNodeConnectorProperties(final NodeConnector nc) {
214         final FlowCapableNodeConnector fcnc = nc.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
215         if (!Objects.equal(fcnc, null)) {
216             return NodeMapping.toADNodeConnectorProperties(fcnc);
217         }
218         return new HashSet<Property>();
219     }
220
221     public static HashSet<Property> toADNodeConnectorProperties(final FlowNodeConnector fcncu) {
222
223         final HashSet<org.opendaylight.controller.sal.core.Property> props = new HashSet<>();
224         if (fcncu != null) {
225             if (fcncu.getCurrentFeature() != null && toAdBandwidth(fcncu.getCurrentFeature()) != null) {
226                 props.add(toAdBandwidth(fcncu.getCurrentFeature()));
227             }
228             if (fcncu.getAdvertisedFeatures() != null && toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()) != null) {
229                 props.add(toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()));
230             }
231             if (fcncu.getSupported() != null && toAdSupportedBandwidth(fcncu.getSupported()) != null) {
232                 props.add(toAdSupportedBandwidth(fcncu.getSupported()));
233             }
234             if (fcncu.getPeerFeatures() != null && toAdPeerBandwidth(fcncu.getPeerFeatures()) != null) {
235                 props.add(toAdPeerBandwidth(fcncu.getPeerFeatures()));
236             }
237             if (fcncu.getName() != null && toAdName(fcncu.getName()) != null) {
238                 props.add(toAdName(fcncu.getName()));
239             }
240             if (fcncu.getConfiguration() != null && toAdConfig(fcncu.getConfiguration()) != null) {
241                 props.add(toAdConfig(fcncu.getConfiguration()));
242             }
243             if (fcncu.getState() != null && toAdState(fcncu.getState()) != null) {
244                 props.add(toAdState(fcncu.getState()));
245             }
246         }
247         return props;
248     }
249
250     public static Name toAdName(final String name) {
251         return new Name(name);
252     }
253
254     public static Config toAdConfig(final PortConfig pc) {
255         Config config = null;
256         if (pc.isPORTDOWN()) {
257             config = new Config(Config.ADMIN_DOWN);
258         } else {
259             config = new Config(Config.ADMIN_UP);
260         }
261         return config;
262     }
263
264     public static org.opendaylight.controller.sal.core.State toAdState(final State s) {
265
266         org.opendaylight.controller.sal.core.State state = null;
267         if (s.isLinkDown()) {
268             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN);
269         } else {
270             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP);
271         }
272         return state;
273     }
274
275     public static Bandwidth toAdBandwidth(final PortFeatures pf) {
276         Bandwidth bw = null;
277         if (pf.isTenMbHd() || pf.isTenMbFd()) {
278             bw = new Bandwidth(Bandwidth.BW10Mbps);
279         } else if (pf.isHundredMbHd() || pf.isHundredMbFd()) {
280             bw = new Bandwidth(Bandwidth.BW100Mbps);
281         } else if (pf.isOneGbHd() || pf.isOneGbFd()) {
282             bw = new Bandwidth(Bandwidth.BW1Gbps);
283         } else if (pf.isOneGbFd()) {
284             bw = new Bandwidth(Bandwidth.BW10Gbps);
285         } else if (pf.isTenGbFd()) {
286             bw = new Bandwidth(Bandwidth.BW10Gbps);
287         } else if (pf.isFortyGbFd()) {
288             bw = new Bandwidth(Bandwidth.BW40Gbps);
289         } else if (pf.isHundredGbFd()) {
290             bw = new Bandwidth(Bandwidth.BW100Gbps);
291         } else if (pf.isOneTbFd()) {
292             bw = new Bandwidth(Bandwidth.BW1Tbps);
293         }
294         return bw;
295     }
296
297     public static AdvertisedBandwidth toAdAdvertizedBandwidth(final PortFeatures pf) {
298         AdvertisedBandwidth abw = null;
299         final Bandwidth bw = toAdBandwidth(pf);
300         if (bw != null) {
301             abw = new AdvertisedBandwidth(bw.getValue());
302         }
303         return abw;
304     }
305
306     public static SupportedBandwidth toAdSupportedBandwidth(final PortFeatures pf) {
307         SupportedBandwidth sbw = null;
308         final Bandwidth bw = toAdBandwidth(pf);
309         if (bw != null) {
310             sbw = new SupportedBandwidth(bw.getValue());
311         }
312         return sbw;
313     }
314
315     public static PeerBandwidth toAdPeerBandwidth(final PortFeatures pf) {
316         PeerBandwidth pbw = null;
317         final Bandwidth bw = toAdBandwidth(pf);
318         if (bw != null) {
319             pbw = new PeerBandwidth(bw.getValue());
320         }
321         return pbw;
322     }
323
324     public static HashSet<Property> toADNodeProperties(final NodeUpdated nu) {
325         final FlowCapableNodeUpdated fcnu = nu.getAugmentation(FlowCapableNodeUpdated.class);
326         if (fcnu != null) {
327             return toADNodeProperties(fcnu, nu.getId());
328         }
329         return new HashSet<org.opendaylight.controller.sal.core.Property>();
330     }
331
332     public static HashSet<Property> toADNodeProperties(final FlowNode fcnu, final NodeId id) {
333
334         final HashSet<org.opendaylight.controller.sal.core.Property> props = new HashSet<>();
335
336         if (fcnu != null) {
337             props.add(toADTimestamp());
338
339             // props.add(fcnu.supportedActions.toADActions) - TODO
340             if (id != null) {
341                 props.add(toADMacAddress(id));
342             }
343             SwitchFeatures switchFeatures = fcnu.getSwitchFeatures();
344             if (switchFeatures != null) {
345                 if (switchFeatures.getMaxTables() != null) {
346                     props.add(toADTables(switchFeatures.getMaxTables()));
347                 }
348                 if (switchFeatures.getCapabilities() != null) {
349                     props.add(toADCapabiliities(switchFeatures.getCapabilities()));
350                 }
351                 if (switchFeatures.getMaxBuffers() != null) {
352                     props.add(toADBuffers(switchFeatures.getMaxBuffers()));
353                 }
354             }
355         }
356         return props;
357     }
358
359     public static TimeStamp toADTimestamp() {
360         final Date date = new Date();
361         final TimeStamp timestamp = new TimeStamp(date.getTime(), "connectedSince");
362         return timestamp;
363     }
364
365     public static MacAddress toADMacAddress(final NodeId id) {
366         final String nodeId = id.getValue().replaceAll(OPENFLOW_ID_PREFIX, "");
367         BigInteger nodeIdRaw = new BigInteger(nodeId);
368         long lNodeId = nodeIdRaw.longValue();
369         byte[] bytesFromDpid = ToSalConversionsUtils.bytesFromDpid(lNodeId);
370         return new MacAddress(bytesFromDpid);
371     }
372
373     public static Tables toADTables(final Short tables) {
374         return new Tables(tables.byteValue());
375     }
376
377     public static Capabilities toADCapabiliities(final List<Class<? extends FeatureCapability>> capabilities) {
378
379         int b = 0;
380         for (Class<? extends FeatureCapability> capability : capabilities) {
381             if (capability.equals(FlowFeatureCapabilityFlowStats.class)) {
382                 b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.getValue() | b;
383             } else if (capability.equals(FlowFeatureCapabilityTableStats.class)) {
384                 b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.getValue() | b;
385             } else if (capability.equals(FlowFeatureCapabilityPortStats.class)) {
386                 b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.getValue() | b;
387             } else if (capability.equals(FlowFeatureCapabilityStp.class)) {
388                 b = Capabilities.CapabilitiesType.STP_CAPABILITY.getValue() | b;
389             } else if (capability.equals(FlowFeatureCapabilityIpReasm.class)) {
390                 b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.getValue() | b;
391             } else if (capability.equals(FlowFeatureCapabilityQueueStats.class)) {
392                 b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.getValue() | b;
393             } else if (capability.equals(FlowFeatureCapabilityArpMatchIp.class)) {
394                 b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.getValue() | b;
395             }
396         }
397         return new Capabilities(b);
398     }
399
400     public static Buffers toADBuffers(final Long buffers) {
401         return new Buffers(buffers.intValue());
402     }
403 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.