Merge "Removed `which` dependency, now using proper shell builtin."
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / ToSalPropertyClassUtils.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.compability;
9
10 import org.opendaylight.controller.sal.core.*;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
14
15 public class ToSalPropertyClassUtils {
16     public static Bandwidth salAdvertisedBandwidthFrom(NodeConnector nodeConnector) {
17         FlowCapableNodeConnector flowCapNodeConn = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
18         PortFeatures portFeatures = flowCapNodeConn.getAdvertisedFeatures();
19         return new AdvertisedBandwidth(resolveBandwidth(portFeatures));
20     }
21
22     public static Bandwidth salPeerBandwidthFrom(NodeConnector nodeConnector) {
23         FlowCapableNodeConnector flowCapNodeConn = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
24         PortFeatures portFeatures = flowCapNodeConn.getPeerFeatures();
25         return new PeerBandwidth(resolveBandwidth(portFeatures));
26     }
27
28     public static Bandwidth salSupportedBandwidthFrom(NodeConnector nodeConnector) {
29         FlowCapableNodeConnector flowCapNodeConn = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
30         PortFeatures portFeatures = flowCapNodeConn.getSupported();
31         return new SupportedBandwidth(resolveBandwidth(portFeatures));
32     }
33
34     public static MacAddress salMacAddressFrom(NodeConnector nodeConnector) {
35         FlowCapableNodeConnector flowCapNodeConn = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
36         String hwAddress = flowCapNodeConn.getHardwareAddress().getValue();
37         return new MacAddress(bytesFrom(hwAddress));
38     }
39
40
41     public static Name salNameFrom(NodeConnector nodeConnector) {
42         FlowCapableNodeConnector flowCapNodeConn = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
43         return new Name(flowCapNodeConn.getName());
44     }
45
46
47
48     private static byte[] bytesFrom(String hwAddress) {
49         String[] mac = hwAddress.split(":");
50         byte[] macAddress = new byte[6]; // mac.length == 6 bytes
51         for (int i = 0; i < mac.length; i++) {
52             macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
53         }
54         return macAddress;
55     }
56
57     private static long resolveBandwidth(PortFeatures portFeatures) {
58         if (portFeatures.is_1tbFd()) {
59             return Bandwidth.BW1Tbps;
60         } else if (portFeatures.is_100gbFd()) {
61             return Bandwidth.BW100Gbps;
62         } else if (portFeatures.is_40gbFd()) {
63             return Bandwidth.BW40Gbps;
64         } else if (portFeatures.is_10gbFd()) {
65             return Bandwidth.BW10Gbps;
66         } else if (portFeatures.is_1gbHd() || portFeatures.is_1gbFd()) {
67             return Bandwidth.BW1Gbps;
68         } else if (portFeatures.is_100mbHd() || portFeatures.is_100mbFd()) {
69             return Bandwidth.BW100Mbps;
70         } else if (portFeatures.is_10mbHd() || portFeatures.is_10mbFd()) {
71             return Bandwidth.BW10Mbps;
72         } else {
73             return Bandwidth.BWUNK;
74         }
75     }
76
77 }