2 * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11 import com.google.common.annotations.VisibleForTesting;
12 import java.util.Collection;
13 import java.util.Collections;
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;
34 * Converts port mod, port status and port description MD-SAL messages to OF library data
39 * VersionConvertorData data = new VersionConvertorData(version);
40 * Optional<PortModInput> ofPort = ConvertorManager.getInstance().convert(salPort, data);
44 public class PortConvertor implements Convertor<Port, PortModInput, VersionConvertorData> {
46 private static final Set<Class<? extends DataContainer>> TYPES = Collections.singleton(Port.class);
49 * Create default empty port mod input
50 * Use this method, if result from convertor is empty.
52 * @param version Openflow version
53 * @return default empty port mod input
55 public static PortModInput defaultResult(short version) {
56 return new PortModInputBuilder()
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();
68 return new PortConfig(noFwd, noPacketIn, noRecv, portDown);
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();
79 return new PortConfigV10(false, noFwd, noPacketIn, noRecv, true, true, portDown);
83 private static PortFeatures getPortFeatures(
84 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
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());
94 private static PortFeaturesV10 getPortFeaturesV10(
95 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
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());
103 * This method is called as a reply to OFPMP_PORT_DESCRIPTION
106 * @param source FlowCapablePort
107 * @param version openflow version
111 static Ports toPortDesc(
112 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
115 PortsBuilder oFPortDescDataBuilder = new PortsBuilder();
117 oFPortDescDataBuilder.setPortNo(
118 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber())); // portNO
120 oFPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
121 oFPortDescDataBuilder.setName(source.getName());
123 PortConfig config = maskPortConfigFields(source.getConfiguration());
125 oFPortDescDataBuilder.setConfig(config);
127 PortState portState = getPortState(source.getState());
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());
137 return oFPortDescDataBuilder.build();
141 private static PortState getPortState(
142 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
144 boolean isLinkDown = state.isLinkDown();
145 boolean isBlocked = state.isBlocked();
146 boolean isLive = state.isLive();
148 return new PortState(isLinkDown, isBlocked, isLive);
153 public Collection<Class<? extends DataContainer>> getTypes() {
158 public PortModInput convert(Port source, VersionConvertorData data) {
159 PortConfig config = maskPortConfigFields(source.getConfiguration());
160 PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
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())));
167 portModInputBuilder.setConfig(config);
168 portModInputBuilder.setMask(config);
170 portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
172 portModInputBuilder.setVersion(data.getVersion());
174 portModInputBuilder.setConfigV10(configV10);
175 portModInputBuilder.setMaskV10(configV10);
176 portModInputBuilder.setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()));
177 return portModInputBuilder.build();