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