2f4e2043fc104e9eb3baefcb01e12804aeb11843
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / PortConvertor.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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.openflow.md.core.sal.convertor;
9
10 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
11 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
21 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;
22 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;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * @author esssuuu This convertor class is used for Port Mod,port status and
28  *         port description messages,decodes SAL and encodes to OF Data
29  *
30  */
31 public final class PortConvertor {
32     private static final Logger LOG = LoggerFactory.getLogger(PortConvertor.class);
33
34     private PortConvertor() {
35
36     }
37
38     /**
39      * This method is used by PORT_MOD_MESSAGE
40      *
41      * @param source source port
42      * @param version openflow version
43      * @return port mod input
44      */
45
46     public static PortModInput toPortModInput(
47             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port source,
48             short version) {
49
50
51         PortConfig config = maskPortConfigFields(source.getConfiguration());
52         PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
53
54         PortModInputBuilder portModInputBuilder = new PortModInputBuilder();
55         portModInputBuilder.setAdvertise(getPortFeatures(source.getAdvertisedFeatures()));
56         portModInputBuilder.setPortNo(new PortNumber(
57                 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber())));
58
59         portModInputBuilder.setConfig(config);
60         portModInputBuilder.setMask(config);
61
62         portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
63
64         portModInputBuilder.setVersion(version);
65
66         portModInputBuilder.setConfigV10(configV10);
67         portModInputBuilder.setMaskV10(configV10);
68         portModInputBuilder.setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()));
69         return portModInputBuilder.build();
70
71     }
72
73     private static PortConfig maskPortConfigFields(
74             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
75         Boolean portDown = configData.isPORTDOWN();
76         Boolean noRecv = configData.isNORECV();
77         Boolean noFwd = configData.isNOFWD();
78         Boolean noPacketIn = configData.isNOPACKETIN();
79
80         return new PortConfig(noFwd, noPacketIn, noRecv, portDown);
81
82     }
83
84     private static PortConfigV10 maskPortConfigV10Fields(
85             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
86         Boolean portDown = configData.isPORTDOWN();
87         Boolean noRecv = configData.isNORECV();
88         Boolean noFwd = configData.isNOFWD();
89         Boolean noPacketIn = configData.isNOPACKETIN();
90
91         return new PortConfigV10(false, noFwd, noPacketIn, noRecv, true, true, portDown);
92
93     }
94     private static PortFeatures getPortFeatures(
95             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
96
97         return new PortFeatures(salPortFeatures.isHundredGbFd(), salPortFeatures.isHundredMbFd(),
98                 salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(),
99                 salPortFeatures.isTenMbHd(), salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(),
100                 salPortFeatures.isOneTbFd(), salPortFeatures.isFortyGbFd(), salPortFeatures.isAutoeng(),
101                 salPortFeatures.isCopper(), salPortFeatures.isFiber(), salPortFeatures.isOther(),
102                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
103     }
104
105     private static PortFeaturesV10 getPortFeaturesV10(
106             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
107
108         return new PortFeaturesV10(salPortFeatures.isHundredMbFd(), salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(), salPortFeatures.isTenMbHd(),
109                 salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(), salPortFeatures.isAutoeng(), salPortFeatures.isCopper(), salPortFeatures.isFiber(),
110                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
111     }
112
113
114     /*
115      * This method is called as a reply to OFPMP_PORT_DESCRIPTION
116      * message(OF1.3.1)
117      */
118     /**
119      * @param source FlowCapablePort
120      * @param version openflow version
121      * @return OF:Ports
122      */
123     public static Ports toPortDesc(
124             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
125             short version) {
126
127         PortConfig config = null;
128         PortState portState = null;
129
130         PortsBuilder oFPortDescDataBuilder = new PortsBuilder();
131
132         oFPortDescDataBuilder.setPortNo(
133                 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber())); // portNO
134
135         oFPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
136         oFPortDescDataBuilder.setName(source.getName());
137
138         config = maskPortConfigFields(source.getConfiguration());
139
140         oFPortDescDataBuilder.setConfig(config);
141
142         portState = getPortState(source.getState());
143
144         oFPortDescDataBuilder.setState(portState);
145         oFPortDescDataBuilder.setCurrentFeatures(getPortFeatures(source.getCurrentFeature()));
146         oFPortDescDataBuilder.setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()));
147         oFPortDescDataBuilder.setSupportedFeatures(getPortFeatures(source.getSupported()));
148         oFPortDescDataBuilder.setPeerFeatures(getPortFeatures(source.getPeerFeatures()));
149         oFPortDescDataBuilder.setCurrSpeed(source.getCurrentSpeed());
150         oFPortDescDataBuilder.setMaxSpeed(source.getMaximumSpeed());
151
152         return oFPortDescDataBuilder.build();
153
154     }
155
156     private static PortState getPortState(
157             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
158
159         boolean isLinkDown = state.isLinkDown();
160         boolean isBlocked = state.isBlocked();
161         boolean isLive = state.isLive();
162
163         return new PortState(isLinkDown, isBlocked, isLive);
164
165     }
166
167
168
169 }