Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / common / NodeStaticReplyTranslatorUtil.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.impl.common;
10
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import javax.annotation.CheckForNull;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.openflowplugin.impl.util.GroupUtil;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
21 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Chaining;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.ChainingChecks;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupAll;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupCapability;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupFf;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupIndirect;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupSelect;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.SelectLiveness;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.SelectWeight;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBand;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBandDrop;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBandDscpRemark;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBurst;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterCapability;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterKbps;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterPktps;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterStats;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
59
60 /**
61  * <p>
62  * openflowplugin-impl
63  * org.opendaylight.openflowplugin.impl.common
64  *
65  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
66  *         </p>
67  *         Created: Mar 31, 2015
68  */
69 public class NodeStaticReplyTranslatorUtil {
70
71     private NodeStaticReplyTranslatorUtil() {
72         throw new UnsupportedOperationException("Utility class");
73     }
74
75     /**
76      * Method transforms OFjava multipart reply model {@link MultipartReplyDesc} object
77      * to inventory data model {@link FlowCapableNode} object.
78      *
79      * @param reply
80      * @return
81      */
82     public static FlowCapableNodeBuilder nodeDescTranslator(@CheckForNull final MultipartReplyDesc reply, final IpAddress ipAddress) {
83         Preconditions.checkArgument(reply != null);
84         final FlowCapableNodeBuilder flowCapAugBuilder = new FlowCapableNodeBuilder();
85         flowCapAugBuilder.setDescription(reply.getDpDesc());
86         flowCapAugBuilder.setHardware(reply.getHwDesc());
87         flowCapAugBuilder.setManufacturer(reply.getMfrDesc());
88         flowCapAugBuilder.setSoftware(reply.getSwDesc());
89         flowCapAugBuilder.setSerialNumber(reply.getSerialNum());
90         flowCapAugBuilder.setTable(Collections.<Table>emptyList());
91         flowCapAugBuilder.setMeter(Collections.<Meter>emptyList());
92         flowCapAugBuilder.setGroup(Collections.<Group>emptyList());
93         if (ipAddress != null) {
94             flowCapAugBuilder.setIpAddress(ipAddress);
95         }
96         return flowCapAugBuilder;
97     }
98
99     /**
100      * Method transforms OFjava multipart reply model {@link MultipartReplyMeterFeatures} object
101      * to inventory data model {@link NodeMeterFeatures} object.
102      *
103      * @param reply
104      * @return
105      */
106     public static NodeMeterFeatures nodeMeterFeatureTranslator(@CheckForNull final MultipartReplyMeterFeatures reply) {
107         Preconditions.checkArgument(reply != null);
108         final MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
109         meterFeature.setMaxBands(reply.getMaxBands());
110         meterFeature.setMaxColor(reply.getMaxColor());
111         meterFeature.setMaxMeter(new Counter32(reply.getMaxMeter()));
112         final List<Class<? extends MeterBand>> meterBandTypes = new ArrayList<>();
113         if (reply.getBandTypes().isOFPMBTDROP()) {
114             meterBandTypes.add(MeterBandDrop.class);
115         }
116         if (reply.getBandTypes().isOFPMBTDSCPREMARK()) {
117             meterBandTypes.add(MeterBandDscpRemark.class);
118         }
119         meterFeature.setMeterBandSupported(Collections.unmodifiableList(meterBandTypes));
120
121         final List<java.lang.Class<? extends MeterCapability>> mCapability = new ArrayList<>();
122         if (reply.getCapabilities().isOFPMFBURST()) {
123             mCapability.add(MeterBurst.class);
124         }
125         if (reply.getCapabilities().isOFPMFKBPS()) {
126             mCapability.add(MeterKbps.class);
127
128         }
129         if (reply.getCapabilities().isOFPMFPKTPS()) {
130             mCapability.add(MeterPktps.class);
131
132         }
133         if (reply.getCapabilities().isOFPMFSTATS()) {
134             mCapability.add(MeterStats.class);
135
136         }
137         meterFeature.setMeterCapabilitiesSupported(Collections.unmodifiableList(mCapability));
138         return new NodeMeterFeaturesBuilder().setMeterFeatures(meterFeature.build()).build();
139     }
140
141     /**
142      * Method transforms OFjava reply model {@link MultipartReplyGroupFeatures} object
143      * to inventory data model {@link NodeGroupFeatures} object.
144      *
145      * @param reply
146      * @return
147      */
148     public static NodeGroupFeatures nodeGroupFeatureTranslator(@CheckForNull final MultipartReplyGroupFeatures reply) {
149         Preconditions.checkArgument(reply != null);
150         final GroupFeaturesBuilder groupFeature = new GroupFeaturesBuilder();
151         groupFeature.setMaxGroups(reply.getMaxGroups());
152
153         final List<Class<? extends GroupType>> supportedGroups = new ArrayList<>();
154         addSupportedGroups(reply, supportedGroups);
155         groupFeature.setGroupTypesSupported(supportedGroups);
156
157         final List<Class<? extends GroupCapability>> gCapability = new ArrayList<>();
158         addGroupCapabilities(reply, gCapability);
159         groupFeature.setGroupCapabilitiesSupported(gCapability);
160
161         groupFeature.setActions(GroupUtil.extractGroupActionsSupportBitmap(reply.getActionsBitmap()));
162         return new NodeGroupFeaturesBuilder().setGroupFeatures(groupFeature.build()).build();
163     }
164
165     private static void addGroupCapabilities(final MultipartReplyGroupFeatures reply, final List<Class<? extends GroupCapability>> gCapability) {
166         if (reply.getCapabilities().isOFPGFCCHAINING()) {
167             gCapability.add(Chaining.class);
168         }
169         if (reply.getCapabilities().isOFPGFCCHAININGCHECKS()) {
170             gCapability.add(ChainingChecks.class);
171         }
172         if (reply.getCapabilities().isOFPGFCSELECTLIVENESS()) {
173             gCapability.add(SelectLiveness.class);
174         }
175         if (reply.getCapabilities().isOFPGFCSELECTWEIGHT()) {
176             gCapability.add(SelectWeight.class);
177         }
178     }
179
180     private static void addSupportedGroups(final MultipartReplyGroupFeatures reply, final List<Class<? extends GroupType>> supportedGroups) {
181         if (reply.getTypes().isOFPGTALL()) {
182             supportedGroups.add(GroupAll.class);
183         }
184         if (reply.getTypes().isOFPGTSELECT()) {
185             supportedGroups.add(GroupSelect.class);
186         }
187         if (reply.getTypes().isOFPGTINDIRECT()) {
188             supportedGroups.add(GroupIndirect.class);
189         }
190         if (reply.getTypes().isOFPGTFF()) {
191             supportedGroups.add(GroupFf.class);
192         }
193     }
194
195     /**
196      * Method transform {@link MultipartReplyTableFeatures} to list of {@link TableFeatures}. Every
197      * table can have List of TableFeatures so add it directly to
198      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder}
199      *
200      * @param reply reply
201      * @param version Openflow version
202      * @param convertorExecutor convertor executor
203      * @return list of table features
204      */
205     public static List<TableFeatures> nodeTableFeatureTranslator(@CheckForNull final MultipartReplyTableFeatures reply, final short version, @CheckForNull final ConvertorExecutor convertorExecutor) {
206         Preconditions.checkArgument(reply != null);
207         Preconditions.checkArgument(convertorExecutor != null);
208         final Optional<List<TableFeatures>> tableFeaturesList = convertorExecutor.convert(reply, new VersionConvertorData(version));
209         return tableFeaturesList.orElse(Collections.emptyList());
210     }
211
212     /**
213      * Method build a ID Node Connector from version and port number.
214      *
215      * @param datapathId
216      * @param portNo
217      * @param version
218      * @return
219      */
220     public static NodeConnectorId nodeConnectorId(@CheckForNull final String datapathId, final long portNo, final short version) {
221         Preconditions.checkArgument(datapathId != null);
222         final String logicalName = OpenflowPortsUtil.getPortLogicalName(version, portNo);
223         return new NodeConnectorId(OFConstants.OF_URI_PREFIX + datapathId + ":" + (logicalName == null ? portNo : logicalName));
224     }
225 }