BUG-2382: OutputActionBuilder.setOutputNodeConnector
[openflowplugin.git] / legacy / sal-compatibility / src / main / java / org / opendaylight / openflowplugin / legacy / 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.openflowplugin.legacy.sal.compatibility;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Objects;
12 import com.google.common.base.Preconditions;
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.Description;
22 import org.opendaylight.controller.sal.core.MacAddress;
23 import org.opendaylight.controller.sal.core.Name;
24 import org.opendaylight.controller.sal.core.Node.NodeIDType;
25 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
26 import org.opendaylight.controller.sal.core.PeerBandwidth;
27 import org.opendaylight.controller.sal.core.Property;
28 import org.opendaylight.controller.sal.core.SupportedBandwidth;
29 import org.opendaylight.controller.sal.core.Tables;
30 import org.opendaylight.controller.sal.core.TimeStamp;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityArpMatchIp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityIpReasm;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityStp;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNodeConnector;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 import java.math.BigInteger;
65 import java.util.Date;
66 import java.util.HashSet;
67 import java.util.List;
68 import java.util.regex.Pattern;
69
70 public final class NodeMapping {
71
72     private static final Logger LOG = LoggerFactory
73             .getLogger(NodeMapping.class);
74
75     /**
76      * openflow id prefix
77      */
78     public static final String OPENFLOW_ID_PREFIX = "openflow:";
79
80     public final static String MD_SAL_TYPE = "MD_SAL_DEPRECATED";
81
82     private final static Class<Node> NODE_CLASS = Node.class;
83
84     private final static Class<NodeConnector> NODECONNECTOR_CLASS = NodeConnector.class;
85
86     private final static Pattern COLON_NUMBERS_EOL = Pattern.compile(":[0-9]+$");
87
88     private final static Pattern NUMBERS_ONLY = Pattern.compile("[0-9]+");
89
90     private final static Pattern ALL_CHARS_TO_COLON = Pattern.compile("^.*:");
91
92     private NodeMapping() {
93         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
94     }
95
96     public static org.opendaylight.controller.sal.core.Node toADNode(final InstanceIdentifier<? extends Object> node) throws ConstructionException {
97         NodeId nodeId = NodeMapping.toNodeId(node);
98         return NodeMapping.toADNode(nodeId);
99     }
100
101     public static org.opendaylight.controller.sal.core.Node toADNode(final NodeId id) throws ConstructionException {
102         String nodeId = NodeMapping.toADNodeId(id);
103         String nodeIdasNumber = nodeId.replaceFirst("^.*:", "");
104         if (isInteger(nodeIdasNumber)) {
105             Long aDNodeId = openflowFullNodeIdToLong(nodeIdasNumber);
106             return new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, aDNodeId);
107         } else {
108             return new org.opendaylight.controller.sal.core.Node(NodeIDType.PRODUCTION, nodeId);
109         }
110     }
111
112     /**
113      * @param adNodeId
114      * @return nodeId as long
115      */
116     @VisibleForTesting
117     public static Long openflowFullNodeIdToLong(String adNodeId) {
118         if (adNodeId == null) {
119             return null;
120         }
121         return new BigInteger(adNodeId).longValue();
122     }
123
124     public static NodeId toNodeId(final InstanceIdentifier<?> id) {
125         final NodeKey key = id.firstKeyOf(Node.class, NodeKey.class);
126         Preconditions.checkArgument(key != null, "No node identifier found in %s", id);
127         return key.getId();
128     }
129
130     /**
131      * @param nodeId containing "&lt;NodeTypeString&gt;:&lt;plainIntegerId&gt;"
132      * @return adNodeId form
133      */
134     public static String toADNodeId(final NodeId nodeId) {
135         if (nodeId == null) {
136             return null;
137         }
138         return nodeId.getValue();
139     }
140
141     public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorRef source) throws ConstructionException {
142         final InstanceIdentifier<?> id = Preconditions.checkNotNull(source.getValue());
143         final NodeConnectorKey key = id.firstKeyOf(NodeConnector.class, NodeConnectorKey.class);
144         return NodeMapping.toADNodeConnector(key.getId(), NodeMapping.toNodeId(id));
145     }
146
147     public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorId ncid, final NodeId nid) throws ConstructionException {
148         String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid);
149         Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid);
150         org.opendaylight.controller.sal.core.Node aDNode = NodeMapping.toADNode(nid);
151         return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode);
152     }
153
154     /**
155      * @param ncid   nodeConnector identifier, e.g.: OF:21 or CTRL
156      * @param aDNode
157      * @return nodeConnector attached to given node
158      * @throws ConstructionException
159      */
160     public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(
161             final NodeConnectorId ncid, final org.opendaylight.controller.sal.core.Node aDNode) throws ConstructionException {
162         NodeId nid = NodeMapping.toNodeId(aDNode);
163         String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid);
164         Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid);
165         return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode);
166     }
167
168     /**
169      * @param aDNode
170      * @return
171      */
172     public static NodeId toNodeId(org.opendaylight.controller.sal.core.Node aDNode) {
173         String targetPrefix = null;
174         if (NodeIDType.OPENFLOW.equals(aDNode.getType())) {
175                 targetPrefix = OPENFLOW_ID_PREFIX;
176         } else {
177             targetPrefix = aDNode.getType() + ":";
178         }
179
180         return new NodeId(targetPrefix + String.valueOf(aDNode.getID()));
181     }
182
183     /**
184      * @param aDNode
185      * @return md-sal {@link NodeKey}
186      */
187     public static NodeKey toNodeKey(org.opendaylight.controller.sal.core.Node aDNode) {
188         return new NodeKey(toNodeId(aDNode));
189     }
190
191     public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) {
192         if (ncId.equals(toLocalNodeConnectorId(nodeId))) {
193             return NodeConnectorIDType.SWSTACK;
194         } else if (ncId.equals(toNormalNodeConnectorId(nodeId))) {
195             return NodeConnectorIDType.HWPATH;
196         } else if (ncId.equals(toControllerNodeConnectorId(nodeId))) {
197             return NodeConnectorIDType.CONTROLLER;
198         }
199         return NodeConnectorIDType.OPENFLOW;
200     }
201
202     public static Object toADNodeConnectorId(final NodeConnectorId nodeConnectorId, final NodeId nodeId) {
203         if (nodeConnectorId.equals(toLocalNodeConnectorId(nodeId)) ||
204                 nodeConnectorId.equals(toNormalNodeConnectorId(nodeId)) ||
205                 nodeConnectorId.equals(toControllerNodeConnectorId(nodeId))) {
206             return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID;
207         }
208
209         String nodeConnectorIdStripped = stripToColon(nodeConnectorId.getValue());
210
211         if (NUMBERS_ONLY.matcher(nodeConnectorIdStripped).matches()) {
212             Short nodeConnectorIdVal = null;
213             try {
214                 nodeConnectorIdVal = Short.valueOf(nodeConnectorIdStripped);
215                 return nodeConnectorIdVal;
216             } catch (NumberFormatException e) {
217                 LOG.warn("nodeConnectorId not supported (long): {}", nodeConnectorIdStripped, e);
218             }
219         }
220         return nodeConnectorIdStripped;
221     }
222
223     public static String stripToColon(final String elementId) {
224         return ALL_CHARS_TO_COLON.matcher(elementId).replaceFirst("");
225     }
226
227     public static NodeId toAdNodeId(final NodeConnectorId nodeConnectorId) {
228         NodeId nodeId = null;
229         if (nodeConnectorId != null) {
230             nodeId = new NodeId(COLON_NUMBERS_EOL.matcher(nodeConnectorId.getValue()).replaceFirst(""));
231         }
232         return nodeId;
233     }
234
235     public static NodeConnectorId toControllerNodeConnectorId(final NodeId node) {
236         return new NodeConnectorId(node.getValue() + ":" + OutputPortValues.CONTROLLER.toString());
237     }
238
239     public static NodeConnectorId toLocalNodeConnectorId(final NodeId node) {
240         return new NodeConnectorId(node.getValue() + ":" + OutputPortValues.LOCAL.toString());
241     }
242
243     public static NodeConnectorId toNormalNodeConnectorId(final NodeId node) {
244         return new NodeConnectorId(node.getValue() + ":" + OutputPortValues.NORMAL.toString());
245     }
246
247     public static NodeRef toNodeRef(final org.opendaylight.controller.sal.core.Node node) {
248         Preconditions.checkArgument(NodeIDType.OPENFLOW.equals(node.getType()));
249         final Long nodeId = Arguments.<Long>checkInstanceOf(node.getID(), Long.class);
250         final NodeKey nodeKey = new NodeKey(new NodeId(OPENFLOW_ID_PREFIX + nodeId));
251         final InstanceIdentifier<Node> nodePath = InstanceIdentifier.builder(Nodes.class).child(NODE_CLASS, nodeKey).toInstance();
252         return new NodeRef(nodePath);
253     }
254
255     public static NodeConnectorRef toNodeConnectorRef(final org.opendaylight.controller.sal.core.NodeConnector nodeConnector) {
256
257         final NodeRef node = NodeMapping.toNodeRef(nodeConnector.getNode());
258         @SuppressWarnings("unchecked")
259         final InstanceIdentifier<Node> nodePath = ((InstanceIdentifier<Node>) node.getValue());
260         NodeConnectorId nodeConnectorId = null;
261
262         if (nodeConnector.getID().equals(org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID)) {
263             final NodeId nodeId = toNodeId(nodePath);
264             final String nodeConnectorType = nodeConnector.getType();
265             if (nodeConnectorType.equals(NodeConnectorIDType.SWSTACK)) {
266                 nodeConnectorId = toLocalNodeConnectorId(nodeId);
267             } else if (nodeConnectorType.equals(NodeConnectorIDType.HWPATH)) {
268                 nodeConnectorId = toNormalNodeConnectorId(nodeId);
269             } else if (nodeConnectorType.equals(NodeConnectorIDType.CONTROLLER)) {
270                 nodeConnectorId = toControllerNodeConnectorId(nodeId);
271             }
272         } else {
273             nodeConnectorId = new NodeConnectorId(OPENFLOW_ID_PREFIX
274                     + Arguments.<Short>checkInstanceOf(nodeConnector.getID(), Short.class));
275         }
276         final NodeConnectorKey connectorKey = new NodeConnectorKey(nodeConnectorId);
277         final InstanceIdentifier<NodeConnector> path = nodePath.child(NODECONNECTOR_CLASS, connectorKey);
278         return new NodeConnectorRef(path);
279     }
280
281     public static org.opendaylight.controller.sal.core.Node toADNode(final NodeRef node) throws ConstructionException {
282         return NodeMapping.toADNode(node.getValue());
283     }
284
285     public static HashSet<Property> toADNodeConnectorProperties(final NodeConnectorUpdated nc) {
286         final FlowCapableNodeConnectorUpdated fcncu = nc.<FlowCapableNodeConnectorUpdated>getAugmentation(FlowCapableNodeConnectorUpdated.class);
287         if (!Objects.equal(fcncu, null)) {
288             HashSet<Property> adNodeConnectorProperties = NodeMapping.toADNodeConnectorProperties(fcncu);
289             return adNodeConnectorProperties;
290         }
291         return new HashSet<Property>();
292     }
293
294     /**
295      * @param nodeRef
296      * @return node description in AD form, e.g.: OF|00:00:00:...:01
297      */
298     private static Description toADDescription(NodeRef nodeRef) {
299         Description desc;
300         try {
301             desc = new Description(toADNode(nodeRef).toString());
302         } catch (ConstructionException e) {
303             desc = new Description("none");
304             LOG.warn("node description extraction failed: {}", nodeRef);
305         }
306         return desc;
307     }
308
309     public static HashSet<Property> toADNodeConnectorProperties(final NodeConnector nc) {
310         final FlowCapableNodeConnector fcnc = nc.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
311         if (!Objects.equal(fcnc, null)) {
312             return NodeMapping.toADNodeConnectorProperties(fcnc);
313         }
314         return new HashSet<Property>();
315     }
316
317     public static HashSet<Property> toADNodeConnectorProperties(final FlowNodeConnector fcncu) {
318
319         final HashSet<org.opendaylight.controller.sal.core.Property> props = new HashSet<>();
320         if (fcncu != null) {
321             if (fcncu.getCurrentFeature() != null && toAdBandwidth(fcncu.getCurrentFeature()) != null) {
322                 props.add(toAdBandwidth(fcncu.getCurrentFeature()));
323             }
324             if (fcncu.getAdvertisedFeatures() != null && toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()) != null) {
325                 props.add(toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()));
326             }
327             if (fcncu.getSupported() != null && toAdSupportedBandwidth(fcncu.getSupported()) != null) {
328                 props.add(toAdSupportedBandwidth(fcncu.getSupported()));
329             }
330             if (fcncu.getPeerFeatures() != null && toAdPeerBandwidth(fcncu.getPeerFeatures()) != null) {
331                 props.add(toAdPeerBandwidth(fcncu.getPeerFeatures()));
332             }
333             if (fcncu.getName() != null && toAdName(fcncu.getName()) != null) {
334                 props.add(toAdName(fcncu.getName()));
335             }
336             if (fcncu.getConfiguration() != null && toAdConfig(fcncu.getConfiguration()) != null) {
337                 props.add(toAdConfig(fcncu.getConfiguration()));
338             }
339             if (fcncu.getState() != null && toAdState(fcncu.getState()) != null) {
340                 props.add(toAdState(fcncu.getState()));
341             }
342         }
343         return props;
344     }
345
346     public static Name toAdName(final String name) {
347         return new Name(name);
348     }
349
350     public static Config toAdConfig(final PortConfig pc) {
351         Config config = null;
352         if (pc.isPORTDOWN()) {
353             config = new Config(Config.ADMIN_DOWN);
354         } else {
355             config = new Config(Config.ADMIN_UP);
356         }
357         return config;
358     }
359
360     public static org.opendaylight.controller.sal.core.State toAdState(final State s) {
361
362         org.opendaylight.controller.sal.core.State state = null;
363         if (s.isLinkDown()) {
364             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN);
365         } else {
366             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP);
367         }
368         return state;
369     }
370
371     public static Bandwidth toAdBandwidth(final PortFeatures pf) {
372         Bandwidth bw = null;
373         if (pf.isTenMbHd() || pf.isTenMbFd()) {
374             bw = new Bandwidth(Bandwidth.BW10Mbps);
375         } else if (pf.isHundredMbHd() || pf.isHundredMbFd()) {
376             bw = new Bandwidth(Bandwidth.BW100Mbps);
377         } else if (pf.isOneGbHd() || pf.isOneGbFd()) {
378             bw = new Bandwidth(Bandwidth.BW1Gbps);
379         } else if (pf.isOneGbFd()) {
380             bw = new Bandwidth(Bandwidth.BW10Gbps);
381         } else if (pf.isTenGbFd()) {
382             bw = new Bandwidth(Bandwidth.BW10Gbps);
383         } else if (pf.isFortyGbFd()) {
384             bw = new Bandwidth(Bandwidth.BW40Gbps);
385         } else if (pf.isHundredGbFd()) {
386             bw = new Bandwidth(Bandwidth.BW100Gbps);
387         } else if (pf.isOneTbFd()) {
388             bw = new Bandwidth(Bandwidth.BW1Tbps);
389         }
390         return bw;
391     }
392
393     public static AdvertisedBandwidth toAdAdvertizedBandwidth(final PortFeatures pf) {
394         AdvertisedBandwidth abw = null;
395         final Bandwidth bw = toAdBandwidth(pf);
396         if (bw != null) {
397             abw = new AdvertisedBandwidth(bw.getValue());
398         }
399         return abw;
400     }
401
402     public static SupportedBandwidth toAdSupportedBandwidth(final PortFeatures pf) {
403         SupportedBandwidth sbw = null;
404         final Bandwidth bw = toAdBandwidth(pf);
405         if (bw != null) {
406             sbw = new SupportedBandwidth(bw.getValue());
407         }
408         return sbw;
409     }
410
411     public static PeerBandwidth toAdPeerBandwidth(final PortFeatures pf) {
412         PeerBandwidth pbw = null;
413         final Bandwidth bw = toAdBandwidth(pf);
414         if (bw != null) {
415             pbw = new PeerBandwidth(bw.getValue());
416         }
417         return pbw;
418     }
419
420     public static HashSet<Property> toADNodeProperties(final NodeUpdated nu) {
421         final FlowCapableNodeUpdated fcnu = nu.getAugmentation(FlowCapableNodeUpdated.class);
422         if (fcnu != null) {
423             HashSet<Property> adNodeProperties = toADNodeProperties(fcnu, nu.getId());
424             adNodeProperties.add(toADDescription(nu.getNodeRef()));
425             return adNodeProperties;
426         }
427         return new HashSet<org.opendaylight.controller.sal.core.Property>();
428     }
429
430     public static HashSet<Property> toADNodeProperties(final FlowNode fcnu, final NodeId id) {
431
432         final HashSet<org.opendaylight.controller.sal.core.Property> props = new HashSet<>();
433
434         if (fcnu != null) {
435             props.add(toADTimestamp());
436
437             // props.add(fcnu.supportedActions.toADActions) - TODO
438             if (id != null) {
439                 props.add(toADMacAddress(id));
440             }
441             SwitchFeatures switchFeatures = fcnu.getSwitchFeatures();
442             if (switchFeatures != null) {
443                 if (switchFeatures.getMaxTables() != null) {
444                     props.add(toADTables(switchFeatures.getMaxTables()));
445                 }
446                 if (switchFeatures.getCapabilities() != null) {
447                     props.add(toADCapabiliities(switchFeatures.getCapabilities()));
448                 }
449                 if (switchFeatures.getMaxBuffers() != null) {
450                     props.add(toADBuffers(switchFeatures.getMaxBuffers()));
451                 }
452             }
453         }
454         return props;
455     }
456
457     public static TimeStamp toADTimestamp() {
458         final Date date = new Date();
459         final TimeStamp timestamp = new TimeStamp(date.getTime(), "connectedSince");
460         return timestamp;
461     }
462
463     public static MacAddress toADMacAddress(final NodeId id) {
464         final String nodeId = id.getValue().replaceAll(OPENFLOW_ID_PREFIX, "");
465         BigInteger nodeIdRaw = new BigInteger(nodeId);
466         long lNodeId = nodeIdRaw.longValue();
467         byte[] bytesFromDpid = ToSalConversionsUtils.bytesFromDpid(lNodeId);
468         return new MacAddress(bytesFromDpid);
469     }
470
471     public static Tables toADTables(final Short tables) {
472         return new Tables(tables.byteValue());
473     }
474
475     public static Capabilities toADCapabiliities(final List<Class<? extends FeatureCapability>> capabilities) {
476
477         int b = 0;
478         for (Class<? extends FeatureCapability> capability : capabilities) {
479             if (capability.equals(FlowFeatureCapabilityFlowStats.class)) {
480                 b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.getValue() | b;
481             } else if (capability.equals(FlowFeatureCapabilityTableStats.class)) {
482                 b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.getValue() | b;
483             } else if (capability.equals(FlowFeatureCapabilityPortStats.class)) {
484                 b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.getValue() | b;
485             } else if (capability.equals(FlowFeatureCapabilityStp.class)) {
486                 b = Capabilities.CapabilitiesType.STP_CAPABILITY.getValue() | b;
487             } else if (capability.equals(FlowFeatureCapabilityIpReasm.class)) {
488                 b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.getValue() | b;
489             } else if (capability.equals(FlowFeatureCapabilityQueueStats.class)) {
490                 b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.getValue() | b;
491             } else if (capability.equals(FlowFeatureCapabilityArpMatchIp.class)) {
492                 b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.getValue() | b;
493             }
494         }
495         return new Capabilities(b);
496     }
497
498     public static Buffers toADBuffers(final Long buffers) {
499         return new Buffers(buffers.intValue());
500     }
501
502
503     private static final boolean isInteger(String value) {
504         if (value.isEmpty()) return false;
505         for (int i = 0; i < value.length(); i++) {
506             if (i == 0 && value.charAt(i) == '-') {
507                 if (value.length() == 1) return false;
508                 else continue;
509             }
510             if (Character.digit(value.charAt(i), 10) < 0) return false;
511         }
512         return true;
513     }
514 }