Remove Objects.{is,non}Null abuse
[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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.MoreObjects;
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.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;
31
32 /**
33  * Converts port mod, port status and port description MD-SAL messages to OF library data.
34  *
35  * <p>
36  * Example usage:
37  * <pre>
38  * {@code
39  * VersionConvertorData data = new VersionConvertorData(version);
40  * Optional<PortModInput> ofPort = convertorManager.convert(salPort, data);
41  * }
42  * </pre>
43  */
44 public class PortConvertor extends Convertor<Port, PortModInput, VersionConvertorData> {
45
46     private static final Set<Class<?>> 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
64         if (configData == null) {
65             return null;
66         }
67
68         Boolean portDown = configData.isPORTDOWN();
69         Boolean noRecv = configData.isNORECV();
70         Boolean noFwd = configData.isNOFWD();
71         Boolean noPacketIn = configData.isNOPACKETIN();
72
73         return new PortConfig(noFwd, noPacketIn, noRecv, portDown);
74
75     }
76
77     private static PortConfigV10 maskPortConfigV10Fields(
78             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
79
80         if (configData == null) {
81             return null;
82         }
83
84         Boolean portDown = configData.isPORTDOWN();
85         Boolean noRecv = configData.isNORECV();
86         Boolean noFwd = configData.isNOFWD();
87         Boolean noPacketIn = configData.isNOPACKETIN();
88
89         return new PortConfigV10(false, noFwd, noPacketIn, noRecv, true, true, portDown);
90
91     }
92
93     private static PortFeatures getPortFeatures(
94             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
95
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());
102     }
103
104     private static PortFeaturesV10 getPortFeaturesV10(
105             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
106
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());
112     }
113
114     /**
115      * This method is called as a reply to OFPMP_PORT_DESCRIPTION message(OF1.3.1).
116      *
117      * @param source  FlowCapablePort
118      * @param version openflow version
119      * @return OF:Ports
120      */
121     @VisibleForTesting
122     static Ports toPortDesc(
123             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
124             short version) {
125
126         PortsBuilder ofPortDescDataBuilder = new PortsBuilder();
127
128         ofPortDescDataBuilder.setPortNo(OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version),
129                 source.getPortNumber())); // portNO
130
131         ofPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
132         ofPortDescDataBuilder.setName(source.getName());
133
134         PortConfig config = maskPortConfigFields(source.getConfiguration());
135
136         ofPortDescDataBuilder.setConfig(config);
137
138         PortState portState = getPortState(source.getState());
139
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());
147
148         return ofPortDescDataBuilder.build();
149
150     }
151
152     private static PortState getPortState(
153             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
154
155         boolean isLinkDown = state.isLinkDown();
156         boolean isBlocked = state.isBlocked();
157         boolean isLive = state.isLive();
158
159         return new PortState(isLinkDown, isBlocked, isLive);
160
161     }
162
163     @Override
164     public Collection<Class<?>> getTypes() {
165         return TYPES;
166     }
167
168     @Override
169     public PortModInput convert(Port source, VersionConvertorData data) {
170         PortConfig config = maskPortConfigFields(source.getConfiguration());
171         PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
172
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())));
177
178         portModInputBuilder.setConfig(config);
179         portModInputBuilder.setMask(MoreObjects.firstNonNull(maskPortConfigFields(source.getMask()),
180                 new PortConfig(true, true, true, true)));
181
182         portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
183
184         portModInputBuilder.setVersion(data.getVersion());
185
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();
191     }
192 }