Merge "Add test for generated code checking list of dependencies."
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / NodeMapping.xtend
1 package org.opendaylight.controller.sal.compatibility
2
3 import org.opendaylight.controller.sal.core.Node
4 import org.opendaylight.controller.sal.core.NodeConnector
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
6 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem
7
8 import static com.google.common.base.Preconditions.*;
9 import static extension org.opendaylight.controller.sal.common.util.Arguments.*;
10 import static extension org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils.*;
11
12 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
16 import org.opendaylight.controller.sal.core.ConstructionException
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
23 import org.opendaylight.controller.sal.core.Bandwidth
24 import org.opendaylight.controller.sal.core.AdvertisedBandwidth
25 import org.opendaylight.controller.sal.core.SupportedBandwidth
26 import org.opendaylight.controller.sal.core.PeerBandwidth
27 import org.opendaylight.controller.sal.core.Name
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
29 import org.opendaylight.controller.sal.core.Config
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated
32 import java.util.HashSet
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated
34 import org.opendaylight.controller.sal.core.Tables
35 import java.util.List
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability
37 import org.opendaylight.controller.sal.core.Buffers
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats
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.FlowFeatureCapabilityIpReasm
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityStp
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityArpMatchIp
45 import org.opendaylight.controller.sal.core.Capabilities
46 import org.opendaylight.controller.sal.core.MacAddress
47 import java.util.Date
48 import org.opendaylight.controller.sal.core.TimeStamp
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNodeConnector
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNode
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector
52
53 public class NodeMapping {
54
55     public static val MD_SAL_TYPE = "MD_SAL";
56     private static val NODE_CLASS = org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
57     private static val NODECONNECTOR_CLASS = org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.
58         NodeConnector;
59
60     private new() {
61         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
62     }
63
64     public static def toADNode(InstanceIdentifier<?> node) throws ConstructionException {
65         return node.toNodeId.toADNode
66     }
67
68     public static def toADNode(NodeId id) {
69         return new Node(MD_SAL_TYPE, id.toADNodeId);
70     }
71
72     public static def toNodeId(InstanceIdentifier<?> node) {
73         checkNotNull(node);
74         checkNotNull(node.getPath());
75         checkArgument(node.getPath().size() >= 2);
76         val arg = node.getPath().get(1);
77         val item = arg.checkInstanceOf(IdentifiableItem);
78         val nodeKey = item.getKey().checkInstanceOf(NodeKey);
79         return nodeKey.id
80     }
81
82     public static def toADNodeId(NodeId nodeId) {
83         checkNotNull(nodeId);
84         return nodeId.value
85     }
86
87     public static def toADNodeConnector(NodeConnectorRef source) throws ConstructionException {
88         checkNotNull(source);
89         val InstanceIdentifier<?> path = checkNotNull(source.getValue());
90         checkArgument(path.path.size() >= 3);
91         val arg = path.getPath().get(2);
92         val item = arg.checkInstanceOf(IdentifiableItem);
93         val connectorKey = item.getKey().checkInstanceOf(NodeConnectorKey);
94         return connectorKey.id.toADNodeConnector(path.toNodeId)
95     }
96     
97     public static def toADNodeConnector(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId ncid,
98         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nid) {
99             return new NodeConnector(ncid.toNodeConnectorType(nid),
100             ncid.toADNodeConnectorId(nid), nid.toADNode);
101      }
102
103     public static def toNodeConnectorType(NodeConnectorId ncId, NodeId nodeId) {
104         if (ncId.equals(nodeId.toLocalNodeConnectorId)) {
105             return NodeConnector.NodeConnectorIDType.SWSTACK
106         } else if (ncId.equals(nodeId.toNormalNodeConnectorId)) {
107             return NodeConnector.NodeConnectorIDType.HWPATH
108         } else if (ncId.equals(nodeId.toControllerNodeConnectorId)) {
109             return NodeConnector.NodeConnectorIDType.CONTROLLER
110         }
111         return MD_SAL_TYPE
112     }
113
114     public static def toADNodeConnectorId(NodeConnectorId nodeConnectorId, NodeId nodeId) {
115         if (nodeConnectorId.equals(nodeId.toLocalNodeConnectorId) ||
116             nodeConnectorId.equals(nodeId.toNormalNodeConnectorId) ||
117             nodeConnectorId.equals(nodeId.toControllerNodeConnectorId)) {
118             return NodeConnector.SPECIALNODECONNECTORID
119         }
120         return nodeConnectorId.value
121     }
122
123     public static def toControllerNodeConnectorId(NodeId node) {
124         return new NodeConnectorId(node.value + ":" + 4294967293L)
125     }
126
127     public static def toLocalNodeConnectorId(NodeId node) {
128         return new NodeConnectorId(node.value + ":" + 4294967294L)
129     }
130
131     public static def toNormalNodeConnectorId(NodeId node) {
132         return new NodeConnectorId(node.value + ":" + 4294967290L)
133     }
134
135     public static def toNodeRef(Node node) {
136         checkArgument(MD_SAL_TYPE.equals(node.getType()));
137         var nodeId = node.ID.checkInstanceOf(String)
138         val nodeKey = new NodeKey(new NodeId(nodeId));
139         val nodePath = InstanceIdentifier.builder().node(Nodes).child(NODE_CLASS, nodeKey).toInstance();
140         return new NodeRef(nodePath);
141     }
142
143     public static def toNodeConnectorRef(NodeConnector nodeConnector) {
144         val node = nodeConnector.node.toNodeRef();
145         val nodePath = node.getValue() as InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node>
146         var NodeConnectorId nodeConnectorId
147         if (nodeConnector.ID.equals(NodeConnector.SPECIALNODECONNECTORID)) {
148             if (nodeConnector.type.equals(NodeConnector.NodeConnectorIDType.SWSTACK)) {
149                 nodeConnectorId = nodePath.toNodeId.toLocalNodeConnectorId
150             } else if (nodeConnector.type.equals(NodeConnector.NodeConnectorIDType.HWPATH)) {
151                 nodeConnectorId = nodePath.toNodeId.toNormalNodeConnectorId
152             } else if (nodeConnector.type.equals(NodeConnector.NodeConnectorIDType.CONTROLLER)) {
153                 nodeConnectorId = nodePath.toNodeId.toControllerNodeConnectorId
154             }
155         } else {
156             nodeConnectorId = new NodeConnectorId(nodeConnector.ID.checkInstanceOf(String))
157         }
158         val connectorKey = new NodeConnectorKey(nodeConnectorId);
159         val path = InstanceIdentifier.builder(nodePath).child(NODECONNECTOR_CLASS, connectorKey).toInstance();
160         return new NodeConnectorRef(path);
161     }
162
163     public static def toADNode(NodeRef node) throws ConstructionException {
164         return toADNode(node.getValue());
165     }
166
167     public static def toADNodeConnectorProperties(NodeConnectorUpdated nc) {
168         val fcncu = nc.getAugmentation(FlowCapableNodeConnectorUpdated)
169         if (fcncu != null) {
170             return fcncu.toADNodeConnectorProperties
171         }
172         return new HashSet<org.opendaylight.controller.sal.core.Property>();
173     }
174
175     public static def toADNodeConnectorProperties(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc) {
176         val fcnc = nc.getAugmentation(FlowCapableNodeConnector)
177         if (fcnc != null) {
178             return fcnc.toADNodeConnectorProperties
179         }
180         return new HashSet<org.opendaylight.controller.sal.core.Property>();
181     }
182
183     public static def toADNodeConnectorProperties(FlowNodeConnector fcncu) {
184         val props = new HashSet<org.opendaylight.controller.sal.core.Property>();
185         if (fcncu != null) {
186             if (fcncu.currentFeature != null && fcncu.currentFeature.toAdBandwidth != null) {
187                 props.add(fcncu.currentFeature.toAdBandwidth)
188             }
189             if (fcncu.advertisedFeatures != null && fcncu.advertisedFeatures.toAdAdvertizedBandwidth != null) {
190                 props.add(fcncu.advertisedFeatures.toAdAdvertizedBandwidth)
191             }
192             if (fcncu.supported != null && fcncu.supported.toAdSupportedBandwidth != null) {
193                 props.add(fcncu.supported.toAdSupportedBandwidth)
194             }
195             if (fcncu.peerFeatures != null && fcncu.peerFeatures.toAdPeerBandwidth != null) {
196                 props.add(fcncu.peerFeatures.toAdPeerBandwidth)
197             }
198             if (fcncu.name != null && fcncu.name.toAdName != null) {
199                 props.add(fcncu.name.toAdName)
200             }
201             if (fcncu.configuration != null && fcncu.configuration.toAdConfig != null) {
202                 props.add(fcncu.configuration.toAdConfig)
203             }
204             if (fcncu.state != null && fcncu.state.toAdState != null) {
205                 props.add(fcncu.state.toAdState)
206             }
207         }
208         return props
209     }
210
211     public static def toAdName(String name) {
212         return new Name(name)
213     }
214
215     public static def toAdConfig(PortConfig pc) {
216         var Config config;
217         if (pc.PORTDOWN) {
218             config = new Config(Config.ADMIN_DOWN)
219         } else {
220             config = new Config(Config.ADMIN_UP)
221         }
222         return config
223     }
224
225     public static def toAdState(State s) {
226         var org.opendaylight.controller.sal.core.State state
227         if (s.linkDown) {
228             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN)
229         } else {
230             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP)
231         }
232         return state
233     }
234
235     public static def toAdBandwidth(PortFeatures pf) {
236         var Bandwidth bw = null
237         if (pf.is_10mbHd || pf.is_10mbFd) {
238             bw = new Bandwidth(Bandwidth.BW10Mbps)
239         } else if (pf.is_100mbHd || pf.is_100mbFd) {
240             bw = new Bandwidth(Bandwidth.BW100Mbps)
241         } else if (pf.is_1gbHd || pf.is_1gbFd) {
242             bw = new Bandwidth(Bandwidth.BW1Gbps)
243         } else if (pf.is_1gbFd) {
244             bw = new Bandwidth(Bandwidth.BW10Gbps)
245         } else if (pf.is_10gbFd) {
246             bw = new Bandwidth(Bandwidth.BW10Gbps)
247         } else if (pf.is_40gbFd) {
248             bw = new Bandwidth(Bandwidth.BW40Gbps)
249         } else if (pf.is_100gbFd) {
250             bw = new Bandwidth(Bandwidth.BW100Gbps)
251         } else if (pf.is_1tbFd) {
252             bw = new Bandwidth(Bandwidth.BW1Tbps)
253         }
254         return bw;
255     }
256
257     public static def toAdAdvertizedBandwidth(PortFeatures pf) {
258         var AdvertisedBandwidth abw
259         val bw = pf.toAdBandwidth
260         if (bw != null) {
261             abw = new AdvertisedBandwidth(bw.value)
262         }
263         return abw
264     }
265
266     public static def toAdSupportedBandwidth(PortFeatures pf) {
267         var SupportedBandwidth sbw
268         val bw = pf.toAdBandwidth
269         if (bw != null) {
270             sbw = new SupportedBandwidth(bw.value)
271         }
272         return sbw
273     }
274
275     public static def toAdPeerBandwidth(PortFeatures pf) {
276         var PeerBandwidth pbw
277         val bw = pf.toAdBandwidth
278         if (bw != null) {
279             pbw = new PeerBandwidth(bw.value)
280         }
281         return pbw
282     }
283
284     public static def toADNodeProperties(NodeUpdated nu) {
285         val fcnu = nu.getAugmentation(FlowCapableNodeUpdated)
286         if (fcnu != null) {
287             return fcnu.toADNodeProperties(nu.id)
288         }
289         return new HashSet<org.opendaylight.controller.sal.core.Property>();
290
291     }
292
293     public static def toADNodeProperties(FlowNode fcnu, NodeId id) {
294         val props = new HashSet<org.opendaylight.controller.sal.core.Property>();
295         if (fcnu != null) {
296             props.add(toADTimestamp)
297
298             // props.add(fcnu.supportedActions.toADActions) - TODO
299             if (id != null) {
300                 props.add(id.toADMacAddress)
301             }
302             if (fcnu.switchFeatures != null) {
303                 if (fcnu.switchFeatures.maxTables != null) {
304                     props.add(fcnu.switchFeatures.maxTables.toADTables)
305                 }
306                 if (fcnu.switchFeatures.capabilities != null) {
307                     props.add(fcnu.switchFeatures.capabilities.toADCapabiliities)
308                 }
309                 if (fcnu.switchFeatures.maxBuffers != null) {
310                     props.add(fcnu.switchFeatures.maxBuffers.toADBuffers)
311                 }
312             }
313         }
314         return props;
315     }
316
317     public static def toADTimestamp() {
318         val date = new Date();
319         val timestamp = new TimeStamp(date.time, "connectedSince")
320         return timestamp;
321     }
322
323     public static def toADMacAddress(NodeId id) {
324         return new MacAddress(Long.parseLong(id.value.replaceAll("openflow:", "")).longValue.bytesFromDpid)
325     }
326
327     public static def toADTables(Short tables) {
328         return new Tables(tables.byteValue)
329     }
330
331     public static def toADCapabiliities(List<Class<? extends FeatureCapability>> capabilities) {
332         var int b
333         for (capability : capabilities) {
334             if (capability.equals(FlowFeatureCapabilityFlowStats)) {
335                 b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.value.bitwiseOr(b)
336             } else if (capability.equals(FlowFeatureCapabilityTableStats)) {
337                 b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.value.bitwiseOr(b)
338             } else if (capability.equals(FlowFeatureCapabilityPortStats)) {
339                 b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.value.bitwiseOr(b)
340             } else if (capability.equals(FlowFeatureCapabilityStp)) {
341                 b = Capabilities.CapabilitiesType.STP_CAPABILITY.value.bitwiseOr(b)
342             } else if (capability.equals(FlowFeatureCapabilityIpReasm)) {
343                 b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.value.bitwiseOr(b)
344             } else if (capability.equals(FlowFeatureCapabilityQueueStats)) {
345                 b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.value.bitwiseOr(b)
346             } else if (capability.equals(FlowFeatureCapabilityArpMatchIp)) {
347                 b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.value.bitwiseOr(b)
348             }
349         }
350         return new Capabilities(b)
351     }
352
353     public static def toADBuffers(Long buffers) {
354         return new Buffers(buffers.intValue)
355     }
356
357 }

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