Merge "Fix for Bug 262 - goes with controller Gerrit3929"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / PortConvertor.java
1 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
2
3 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
4 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
6 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.PortsBuilder;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
16  * @author esssuuu This convertor class is used for Port Mod,port status and
17  *         port description messages,decodes SAL and encodes to OF Data
18  *
19  */
20 public final class PortConvertor {
21     private static final Logger log = LoggerFactory.getLogger(PortConvertor.class);
22
23     private PortConvertor() {
24
25     }
26
27     /**
28      * This method is used by PORT_MOD_MESSAGE
29      *
30      * @param source
31      * @return
32      */
33
34     public static PortModInput toPortModInput(
35             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port source,
36             short version) {
37
38
39         PortConfig config = null;
40
41         PortModInputBuilder portModInputBuilder = new PortModInputBuilder();
42         portModInputBuilder.setAdvertise(getPortFeatures(source.getAdvertisedFeatures()));
43         portModInputBuilder.setPortNo(new PortNumber(source.getPortNumber()));
44         maskPortConfigFields(source.getConfiguration(), config);
45         portModInputBuilder.setConfig(config);
46         portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
47         config = null;
48         maskPortConfigFields(source.getMask(), config);
49         portModInputBuilder.setMask(config);
50         portModInputBuilder.setVersion(version);
51         return portModInputBuilder.build();
52
53     }
54
55     private static void maskPortConfigFields(
56             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData,
57             PortConfig config) {
58         Boolean portDown = false;
59         Boolean noRecv = false;
60         Boolean noFwd = false;
61         Boolean noPacketIn = false;
62         if (configData.isNOFWD())
63             noFwd = true;
64         if (configData.isNOPACKETIN())
65             noPacketIn = true;
66         if (configData.isNORECV())
67             noRecv = true;
68         if (configData.isPORTDOWN())
69             portDown = true;
70
71         config = new PortConfig(noFwd, noPacketIn, noRecv, portDown);
72
73     }
74
75     private static PortFeatures getPortFeatures(
76             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
77
78         return new PortFeatures(salPortFeatures.is_100gbFd(), salPortFeatures.is_100mbFd(),
79                 salPortFeatures.is_100mbHd(), salPortFeatures.is_10gbFd(), salPortFeatures.is_10mbFd(),
80                 salPortFeatures.is_10mbHd(), salPortFeatures.is_1gbFd(), salPortFeatures.is_1gbHd(),
81                 salPortFeatures.is_1tbFd(), salPortFeatures.is_40gbFd(), salPortFeatures.isAutoeng(),
82                 salPortFeatures.isCopper(), salPortFeatures.isFiber(), salPortFeatures.isOther(),
83                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
84     }
85
86     /*
87      * This method is called as a reply to OFPMP_PORT_DESCRIPTION
88      * message(OF1.3.1)
89      */
90     /**
91      * @param source
92      *            :SAL FlowCapablePort
93      * @return OF:Ports
94      */
95     public static Ports toPortDesc(
96             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source) {
97
98         PortConfig config = null;
99         PortState portState = null;
100
101         PortsBuilder OFPortDescDataBuilder = new PortsBuilder();
102         OFPortDescDataBuilder.setPortNo(source.getPortNumber()); // portNO
103
104         OFPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
105         OFPortDescDataBuilder.setName(source.getName());
106
107         maskPortConfigFields(source.getConfiguration(), config);
108
109         OFPortDescDataBuilder.setConfig(config);
110
111         getPortState(source.getState(), portState);
112         OFPortDescDataBuilder.setState(portState);
113         OFPortDescDataBuilder.setCurrentFeatures(getPortFeatures(source.getCurrentFeature()));
114         OFPortDescDataBuilder.setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()));
115         OFPortDescDataBuilder.setSupportedFeatures(getPortFeatures(source.getSupported()));
116         OFPortDescDataBuilder.setPeerFeatures(getPortFeatures(source.getPeerFeatures()));
117         OFPortDescDataBuilder.setCurrSpeed(source.getCurrentSpeed());
118         OFPortDescDataBuilder.setMaxSpeed(source.getMaximumSpeed());
119
120         return OFPortDescDataBuilder.build();
121
122     }
123
124     private static void getPortState(
125             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state, PortState portState) {
126
127         boolean isLinkDown = false;// (0),
128         boolean isBlocked = false; // (1),
129         boolean isLive = false; // (2);
130
131         if (state.isLinkDown()) {
132             isLinkDown = true;
133         } else if (state.isBlocked()) {
134             isBlocked = true;
135         } else if (state.isLive()) {
136             isLive = true;
137         }
138         portState = new PortState(isLinkDown, isBlocked, isLive);
139
140     }
141
142
143
144 }