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