2671a8cff3a67c5d643a088790a218ddc7e556cb
[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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Set;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
19 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
29 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;
30 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;
31 import org.opendaylight.yangtools.yang.binding.DataContainer;
32
33 /**
34  * Converts port mod, port status and port description MD-SAL messages to OF library data
35  *
36  * Example usage:
37  * <pre>
38  * {@code
39  * VersionConvertorData data = new VersionConvertorData(version);
40  * Optional<PortModInput> ofPort = ConvertorManager.getInstance().convert(salPort, data);
41  * }
42  * </pre>
43  */
44 public class PortConvertor implements Convertor<Port, PortModInput, VersionConvertorData> {
45
46     private static final Set<Class<? extends DataContainer>> TYPES = Collections.singleton(Port.class);
47
48     /**
49      * Create default empty port mod input
50      * Use this method, if result from convertor is empty.
51      *
52      * @param version Openflow version
53      * @return default empty port mod input
54      */
55     public static PortModInput defaultResult(short version) {
56         return new PortModInputBuilder()
57                 .setVersion(version)
58                 .build();
59     }
60
61     private static PortConfig maskPortConfigFields(
62             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
63         Boolean portDown = configData.isPORTDOWN();
64         Boolean noRecv = configData.isNORECV();
65         Boolean noFwd = configData.isNOFWD();
66         Boolean noPacketIn = configData.isNOPACKETIN();
67
68         return new PortConfig(noFwd, noPacketIn, noRecv, portDown);
69
70     }
71
72     private static PortConfigV10 maskPortConfigV10Fields(
73             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
74         Boolean portDown = configData.isPORTDOWN();
75         Boolean noRecv = configData.isNORECV();
76         Boolean noFwd = configData.isNOFWD();
77         Boolean noPacketIn = configData.isNOPACKETIN();
78
79         return new PortConfigV10(false, noFwd, noPacketIn, noRecv, true, true, portDown);
80
81     }
82
83     private static PortFeatures getPortFeatures(
84             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
85
86         return new PortFeatures(salPortFeatures.isHundredGbFd(), salPortFeatures.isHundredMbFd(),
87                 salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(),
88                 salPortFeatures.isTenMbHd(), salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(),
89                 salPortFeatures.isOneTbFd(), salPortFeatures.isFortyGbFd(), salPortFeatures.isAutoeng(),
90                 salPortFeatures.isCopper(), salPortFeatures.isFiber(), salPortFeatures.isOther(),
91                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
92     }
93
94     private static PortFeaturesV10 getPortFeaturesV10(
95             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
96
97         return new PortFeaturesV10(salPortFeatures.isHundredMbFd(), salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(), salPortFeatures.isTenMbHd(),
98                 salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(), salPortFeatures.isAutoeng(), salPortFeatures.isCopper(), salPortFeatures.isFiber(),
99                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
100     }
101
102     /**
103      * This method is called as a reply to OFPMP_PORT_DESCRIPTION
104      * message(OF1.3.1)
105      *
106      * @param source  FlowCapablePort
107      * @param version openflow version
108      * @return OF:Ports
109      */
110     @VisibleForTesting
111     static Ports toPortDesc(
112             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
113             short version) {
114
115         PortsBuilder oFPortDescDataBuilder = new PortsBuilder();
116
117         oFPortDescDataBuilder.setPortNo(
118                 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber())); // portNO
119
120         oFPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
121         oFPortDescDataBuilder.setName(source.getName());
122
123         PortConfig config = maskPortConfigFields(source.getConfiguration());
124
125         oFPortDescDataBuilder.setConfig(config);
126
127         PortState portState = getPortState(source.getState());
128
129         oFPortDescDataBuilder.setState(portState);
130         oFPortDescDataBuilder.setCurrentFeatures(getPortFeatures(source.getCurrentFeature()));
131         oFPortDescDataBuilder.setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()));
132         oFPortDescDataBuilder.setSupportedFeatures(getPortFeatures(source.getSupported()));
133         oFPortDescDataBuilder.setPeerFeatures(getPortFeatures(source.getPeerFeatures()));
134         oFPortDescDataBuilder.setCurrSpeed(source.getCurrentSpeed());
135         oFPortDescDataBuilder.setMaxSpeed(source.getMaximumSpeed());
136
137         return oFPortDescDataBuilder.build();
138
139     }
140
141     private static PortState getPortState(
142             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
143
144         boolean isLinkDown = state.isLinkDown();
145         boolean isBlocked = state.isBlocked();
146         boolean isLive = state.isLive();
147
148         return new PortState(isLinkDown, isBlocked, isLive);
149
150     }
151
152     @Override
153     public Collection<Class<? extends DataContainer>> getTypes() {
154         return TYPES;
155     }
156
157     @Override
158     public PortModInput convert(Port source, VersionConvertorData data) {
159         PortConfig config = maskPortConfigFields(source.getConfiguration());
160         PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
161
162         PortModInputBuilder portModInputBuilder = new PortModInputBuilder();
163         portModInputBuilder.setAdvertise(getPortFeatures(source.getAdvertisedFeatures()));
164         portModInputBuilder.setPortNo(new PortNumber(
165                 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(data.getVersion()), source.getPortNumber())));
166
167         portModInputBuilder.setConfig(config);
168         portModInputBuilder.setMask(config);
169
170         portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
171
172         portModInputBuilder.setVersion(data.getVersion());
173
174         portModInputBuilder.setConfigV10(configV10);
175         portModInputBuilder.setMaskV10(configV10);
176         portModInputBuilder.setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()));
177         return portModInputBuilder.build();
178     }
179 }