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