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