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