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

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