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

©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.