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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.MoreObjects;
12 import java.util.Collection;
13 import java.util.Collections;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
18 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
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;
33 * 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.convert(salPort, data);
44 public class PortConvertor extends Convertor<Port, PortModInput, VersionConvertorData> {
46 private static final Set<Class<?>> 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) {
64 if (configData == null) {
68 Boolean portDown = configData.isPORTDOWN();
69 Boolean noRecv = configData.isNORECV();
70 Boolean noFwd = configData.isNOFWD();
71 Boolean noPacketIn = configData.isNOPACKETIN();
73 return new PortConfig(noFwd, noPacketIn, noRecv, portDown);
77 private static PortConfigV10 maskPortConfigV10Fields(
78 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
80 if (configData == null) {
84 Boolean portDown = configData.isPORTDOWN();
85 Boolean noRecv = configData.isNORECV();
86 Boolean noFwd = configData.isNOFWD();
87 Boolean noPacketIn = configData.isNOPACKETIN();
89 return new PortConfigV10(false, noFwd, noPacketIn, noRecv, true, true, portDown);
93 private static PortFeatures getPortFeatures(
94 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
96 return new PortFeatures(salPortFeatures.isHundredGbFd(), salPortFeatures.isHundredMbFd(),
97 salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(),
98 salPortFeatures.isTenMbHd(), salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(),
99 salPortFeatures.isOneTbFd(), salPortFeatures.isFortyGbFd(), salPortFeatures.isAutoeng(),
100 salPortFeatures.isCopper(), salPortFeatures.isFiber(), salPortFeatures.isOther(),
101 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
104 private static PortFeaturesV10 getPortFeaturesV10(
105 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
107 return new PortFeaturesV10(salPortFeatures.isHundredMbFd(), salPortFeatures.isHundredMbHd(),
108 salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(), salPortFeatures.isTenMbHd(),
109 salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(), salPortFeatures.isAutoeng(),
110 salPortFeatures.isCopper(), salPortFeatures.isFiber(), salPortFeatures.isPause(),
111 salPortFeatures.isPauseAsym());
115 * This method is called as a reply to OFPMP_PORT_DESCRIPTION message(OF1.3.1).
117 * @param source FlowCapablePort
118 * @param version openflow version
122 static Ports toPortDesc(
123 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
126 PortsBuilder ofPortDescDataBuilder = new PortsBuilder();
128 ofPortDescDataBuilder.setPortNo(OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version),
129 source.getPortNumber())); // portNO
131 ofPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
132 ofPortDescDataBuilder.setName(source.getName());
134 PortConfig config = maskPortConfigFields(source.getConfiguration());
136 ofPortDescDataBuilder.setConfig(config);
138 PortState portState = getPortState(source.getState());
140 ofPortDescDataBuilder.setState(portState);
141 ofPortDescDataBuilder.setCurrentFeatures(getPortFeatures(source.getCurrentFeature()));
142 ofPortDescDataBuilder.setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()));
143 ofPortDescDataBuilder.setSupportedFeatures(getPortFeatures(source.getSupported()));
144 ofPortDescDataBuilder.setPeerFeatures(getPortFeatures(source.getPeerFeatures()));
145 ofPortDescDataBuilder.setCurrSpeed(source.getCurrentSpeed());
146 ofPortDescDataBuilder.setMaxSpeed(source.getMaximumSpeed());
148 return ofPortDescDataBuilder.build();
152 private static PortState getPortState(
153 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
155 boolean isLinkDown = state.isLinkDown();
156 boolean isBlocked = state.isBlocked();
157 boolean isLive = state.isLive();
159 return new PortState(isLinkDown, isBlocked, isLive);
164 public Collection<Class<?>> getTypes() {
169 public PortModInput convert(Port source, VersionConvertorData data) {
170 PortConfig config = maskPortConfigFields(source.getConfiguration());
171 PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
173 PortModInputBuilder portModInputBuilder = new PortModInputBuilder();
174 portModInputBuilder.setAdvertise(getPortFeatures(source.getAdvertisedFeatures()));
175 portModInputBuilder.setPortNo(new PortNumber(OpenflowPortsUtil.getProtocolPortNumber(
176 OpenflowVersion.get(data.getVersion()), source.getPortNumber())));
178 portModInputBuilder.setConfig(config);
179 portModInputBuilder.setMask(MoreObjects.firstNonNull(maskPortConfigFields(source.getMask()),
180 new PortConfig(true, true, true, true)));
182 portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
184 portModInputBuilder.setVersion(data.getVersion());
186 portModInputBuilder.setConfigV10(configV10);
187 portModInputBuilder.setMaskV10(MoreObjects.firstNonNull(maskPortConfigV10Fields(source.getMask()),
188 new PortConfigV10(true, true, true, true, true, true, true)));
189 portModInputBuilder.setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()));
190 return portModInputBuilder.build();