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