Move adsal into its own subdirectory.
[controller.git] / opendaylight / adsal / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / InventoryServiceHelper.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.opendaylight.controller.sal.core.Bandwidth;
18 import org.opendaylight.controller.sal.core.AdvertisedBandwidth;
19 import org.opendaylight.controller.sal.core.SupportedBandwidth;
20 import org.opendaylight.controller.sal.core.PeerBandwidth;
21 import org.opendaylight.controller.sal.core.Config;
22 import org.opendaylight.controller.sal.core.Name;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.core.Property;
26 import org.opendaylight.controller.sal.core.State;
27
28 import org.opendaylight.controller.sal.utils.NodeCreator;
29
30 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
31 import org.openflow.protocol.OFPhysicalPort;
32 import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
33 import org.openflow.protocol.OFPhysicalPort.OFPortFeatures;
34 import org.openflow.protocol.OFPhysicalPort.OFPortState;
35
36 /**
37  * The class provides helper functions to retrieve inventory properties from
38  * OpenFlow messages
39  */
40 public class InventoryServiceHelper {
41     /*
42      * Returns BandWidth property from OpenFlow OFPhysicalPort features
43      */
44     public static Bandwidth OFPortToBandWidth(int portFeatures) {
45         Bandwidth bw = null;
46         int value = portFeatures
47                 & (OFPortFeatures.OFPPF_10MB_FD.getValue()
48                         | OFPortFeatures.OFPPF_10MB_HD.getValue()
49                         | OFPortFeatures.OFPPF_100MB_FD.getValue()
50                         | OFPortFeatures.OFPPF_100MB_HD.getValue()
51                         | OFPortFeatures.OFPPF_1GB_FD.getValue()
52                         | OFPortFeatures.OFPPF_1GB_HD.getValue() | OFPortFeatures.OFPPF_10GB_FD
53                         .getValue());
54
55         switch (value) {
56         case 1:
57         case 2:
58             bw = new Bandwidth(Bandwidth.BW10Mbps);
59             break;
60         case 4:
61         case 8:
62             bw = new Bandwidth(Bandwidth.BW100Mbps);
63             break;
64         case 16:
65         case 32:
66             bw = new Bandwidth(Bandwidth.BW1Gbps);
67             break;
68         case 64:
69             bw = new Bandwidth(Bandwidth.BW10Gbps);
70             break;
71         default:
72             break;
73         }
74         return bw;
75     }
76
77     /*
78      * Returns Config property from OpenFLow OFPhysicalPort config
79      */
80     public static Config OFPortToConfig(int portConfig) {
81         Config config;
82         if ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & portConfig) != 0)
83             config = new Config(Config.ADMIN_DOWN);
84         else
85             config = new Config(Config.ADMIN_UP);
86         return config;
87     }
88
89     /*
90      * Returns State property from OpenFLow OFPhysicalPort state
91      */
92     public static State OFPortToState(int portState) {
93         State state;
94         if ((OFPortState.OFPPS_LINK_DOWN.getValue() & portState) != 0)
95             state = new State(State.EDGE_DOWN);
96         else
97             state = new State(State.EDGE_UP);
98         return state;
99     }
100
101     /*
102      * Returns set of properties from OpenFLow OFPhysicalPort
103      */
104     public static Set<Property> OFPortToProps(OFPhysicalPort port) {
105         Set<Property> props = new HashSet<Property>();
106         Bandwidth bw = InventoryServiceHelper.OFPortToBandWidth(port
107                 .getCurrentFeatures());
108         if (bw != null) {
109             props.add(bw);
110         }
111
112         Bandwidth abw = InventoryServiceHelper.OFPortToBandWidth(port.getAdvertisedFeatures());
113         if (abw != null) {
114                 AdvertisedBandwidth a = new AdvertisedBandwidth(abw.getValue());
115                 if (a != null) {
116                         props.add(a);
117                 }
118         }
119         Bandwidth sbw = InventoryServiceHelper.OFPortToBandWidth(port.getSupportedFeatures());
120         if (sbw != null) {
121                 SupportedBandwidth s = new SupportedBandwidth(sbw.getValue());
122                 if (s != null) {
123                         props.add(s);
124                 }
125         }
126         Bandwidth pbw = InventoryServiceHelper.OFPortToBandWidth(port.getPeerFeatures());
127         if (pbw != null) {
128                 PeerBandwidth p = new PeerBandwidth(pbw.getValue());
129                 if (p != null) {
130                         props.add(p);
131                 }
132         }
133         props.add(new Name(port.getName()));
134         props.add(InventoryServiceHelper.OFPortToConfig(port.getConfig()));
135         props.add(InventoryServiceHelper.OFPortToState(port.getState()));
136         return props;
137     }
138
139     /*
140      * Returns set of properties for each nodeConnector in an OpenFLow switch
141      */
142     public static Map<NodeConnector, Set<Property>> OFSwitchToProps(ISwitch sw) {
143         Map<NodeConnector, Set<Property>> ncProps = new HashMap<NodeConnector, Set<Property>>();
144
145         if (sw == null) {
146             return ncProps;
147         }
148
149         Node node = NodeCreator.createOFNode(sw.getId());
150         if (node == null) {
151             return ncProps;
152         }
153
154         Set<Property> props;
155         NodeConnector nodeConnector;
156         OFPhysicalPort port;
157         Map<Short, OFPhysicalPort> ports = sw.getPhysicalPorts();
158         for (Map.Entry<Short, OFPhysicalPort> entry : ports.entrySet()) {
159             nodeConnector = PortConverter.toNodeConnector(entry.getKey(), node);
160             port = entry.getValue();
161             props = InventoryServiceHelper.OFPortToProps(port);
162             ncProps.put(nodeConnector, props);
163         }
164
165         return ncProps;
166     }
167 }