Fix netvirtsfc flows
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / openflow13 / NshUtils.java
1 /*
2  * Copyright (c) 2015 Dell, 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.ovsdb.openstack.netvirt.sfc.openflow13;
10
11 import java.math.BigInteger;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
13
14 import com.google.common.net.InetAddresses;
15
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIdCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxTunIpv4DstCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.group.buckets.bucket.action.action.NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionSetNshc1NodesNodeTableFlowApplyActionsCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionSetNshc2NodesNodeTableFlowApplyActionsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionSetNshc3NodesNodeTableFlowApplyActionsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionSetNshc4NodesNodeTableFlowApplyActionsCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionSetNsiNodesNodeTableFlowApplyActionsCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionSetNspNodesNodeTableFlowApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputReg;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.output.reg.grouping.NxOutputRegBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._1.grouping.NxSetNshc1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._1.grouping.NxSetNshc1Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._2.grouping.NxSetNshc2;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._2.grouping.NxSetNshc2Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._3.grouping.NxSetNshc3;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._3.grouping.NxSetNshc3Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._4.grouping.NxSetNshc4;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nshc._4.grouping.NxSetNshc4Builder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsi.grouping.NxSetNsi;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsi.grouping.NxSetNsiBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsp.grouping.NxSetNsp;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.set.nsp.grouping.NxSetNspBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.SrcChoice;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.src.choice.grouping.src.choice.SrcNxRegCaseBuilder;
51
52 /**
53  * Open Vswitch DB OpenFlow 1.3 Networking Provider for Netvirt SFC Utilities.
54  * @author Arun Yerra
55  */
56 public class NshUtils {
57     private Ipv4Address nshTunIpDst;
58     private PortNumber nshTunUdpPort;
59     private long nshNsp;
60     private short nshNsi;
61     private long nshMetaC1;
62     private long nshMetaC2;
63
64     public NshUtils() {
65         super();
66     }
67
68     /**
69      * {@link NshUtils} constructor.
70      * @param nshTunIpDst Tunnel Destination IP
71      * @param nshTunUdpPort Tunnel Transport Port
72      * @param nshNsp Service Path Id
73      * @param nshNsi Service Path Index
74      * @param nshMetaC1 End point ID
75      * @param nshMetaC2 Tunnel Id.
76      */
77     public NshUtils(Ipv4Address nshTunIpDst, PortNumber nshTunUdpPort,
78             long nshNsp, short nshNsi, long nshMetaC1,
79             long nshMetaC2) {
80         super();
81         this.nshTunIpDst = nshTunIpDst;
82         this.nshTunUdpPort = nshTunUdpPort;
83         this.nshNsp = nshNsp;
84         this.nshNsi = nshNsi;
85         this.nshMetaC1 = nshMetaC1;
86         this.nshMetaC2 = nshMetaC2;
87     }
88
89     /*
90      * @return the nshTunIpDst
91      */
92     public Ipv4Address getNshTunIpDst() {
93         return nshTunIpDst;
94     }
95
96     /*
97      * @param nshTunIpDst the nshTunIpDst to set
98      */
99     public void setNshTunIpDst(Ipv4Address nshTunIpDst) {
100         this.nshTunIpDst = nshTunIpDst;
101     }
102
103     /*
104      * @return the nshTunUdpPort
105      */
106     public PortNumber getNshTunUdpPort() {
107         return nshTunUdpPort;
108     }
109
110     /*
111      * @param nshTunUdpPort the nshTunUdpPort to set
112      */
113     public void setNshTunUdpPort(PortNumber nshTunUdpPort) {
114         this.nshTunUdpPort = nshTunUdpPort;
115     }
116
117     /*
118      * @return the nshNsp
119      */
120     public long getNshNsp() {
121         return nshNsp;
122     }
123
124     /*
125      * @param nshNsp the nshNsp to set
126      */
127     public void setNshNsp(long nshNsp) {
128         this.nshNsp = nshNsp;
129     }
130
131     /*
132      * @return the nshNsi
133      */
134     public short getNshNsi() {
135         return nshNsi;
136     }
137
138     /*
139      * @param nshNsi the nshNsi to set
140      */
141     public void setNshNsi(short nshNsi) {
142         this.nshNsi = nshNsi;
143     }
144
145     /*
146      * @return the nshMetaC1
147      */
148     public long getNshMetaC1() {
149         return nshMetaC1;
150     }
151
152     /*
153      * @param nshMetaC1 the nshMetaC1 to set
154      */
155     public void setNshMetaC1(long nshMetaC1) {
156         this.nshMetaC1 = nshMetaC1;
157     }
158
159     /*
160      * @return the nshMetaC2
161      */
162     public long getNshMetaC2() {
163         return nshMetaC2;
164     }
165
166     /*
167      * @param nshMetaC2 the nshMetaC2 to set
168      */
169     public void setNshMetaC2(long nshMetaC2) {
170         this.nshMetaC2 = nshMetaC2;
171     }
172
173     /* (non-Javadoc)
174      * @see java.lang.Object#toString()
175      */
176     @Override
177     public String toString() {
178         return "NshUtils [nshTunIpDst=" + nshTunIpDst + ", nshTunUdpPort=" + nshTunUdpPort + ", nshNsp=" + nshNsp
179                 + ", nshNsi=" + nshNsi + ", nshMetaC1=" + nshMetaC1 + ", nshMetaC2=" + nshMetaC2 + "]";
180     }
181
182     /**
183      * This method loads the action into NX register.
184      *{@link NshUtils} Loading Register
185      * @param dstChoice destination
186      * @param value value
187      * @param endOffset Offset
188      * @param groupBucket Identifies the group
189      */
190     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value, int endOffset, boolean groupBucket) {
191         NxRegLoad reg = new NxRegLoadBuilder().setDst(
192                 new DstBuilder().setDstChoice(dstChoice)
193                     .setStart(Integer.valueOf(0))
194                     .setEnd(Integer.valueOf(endOffset))
195                     .build())
196             .setValue(value)
197             .build();
198         if (groupBucket) {
199             return new NxActionRegLoadNodesNodeGroupBucketsBucketActionsCaseBuilder().setNxRegLoad(reg).build();
200         } else {
201             return new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(reg).build();
202         }
203     }
204
205     public static Action nxLoadRegAction(DstChoice dstChoice, BigInteger value) {
206         return nxLoadRegAction(dstChoice, value, 31, false);
207     }
208
209     public static Action nxLoadRegAction(Class<? extends NxmNxReg> reg, BigInteger value) {
210         return nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(reg).build(), value);
211     }
212
213     public static Action nxSetNsiAction(Short nsi) {
214         NxSetNsi newNsi = new NxSetNsiBuilder().setNsi(nsi).build();
215         return new NxActionSetNsiNodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNsi(newNsi).build();
216     }
217
218     public static Action nxSetNspAction(Long nsp) {
219         NxSetNsp newNsp = new NxSetNspBuilder().setNsp(nsp).build();
220         return new NxActionSetNspNodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNsp(newNsp).build();
221     }
222
223     public static Action nxLoadNshc1RegAction(Long value) {
224         NxSetNshc1 newNshc1 = new NxSetNshc1Builder().setNshc(value).build();
225         return new NxActionSetNshc1NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc1(newNshc1).build();
226     }
227
228     public static Action nxLoadNshc2RegAction(Long value) {
229         NxSetNshc2 newNshc2 = new NxSetNshc2Builder().setNshc(value).build();
230         return new NxActionSetNshc2NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc2(newNshc2).build();
231     }
232
233     public static Action nxLoadNshc3RegAction(Long value) {
234         NxSetNshc3 newNshc3 = new NxSetNshc3Builder().setNshc(value).build();
235         return new NxActionSetNshc3NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc3(newNshc3).build();
236     }
237
238     public static Action nxLoadNshc4RegAction(Long value) {
239         NxSetNshc4 newNshc4 = new NxSetNshc4Builder().setNshc(value).build();
240         return new NxActionSetNshc4NodesNodeTableFlowApplyActionsCaseBuilder().setNxSetNshc4(newNshc4).build();
241     }
242
243     /**
244      * This method loads Destination IPv4 address of Tunnel.
245      */
246     public static Action nxLoadTunIPv4Action(String ipAddress, boolean groupBucket) {
247         int ip = InetAddresses.coerceToInteger(InetAddresses.forString(ipAddress));
248         long ipl = ip & 0xffffffffL;
249         return nxLoadRegAction(new DstNxTunIpv4DstCaseBuilder().setNxTunIpv4Dst(Boolean.TRUE).build(),
250                 BigInteger.valueOf(ipl), 31, groupBucket);
251     }
252
253     public static Action nxLoadTunIdAction(BigInteger tunnelId, boolean groupBucket) {
254         return nxLoadRegAction(new DstNxTunIdCaseBuilder().setNxTunId(Boolean.TRUE).build(), tunnelId, 31, groupBucket);
255     }
256
257     /**
258      * This method loads output port.
259      */
260     public static Action nxOutputRegAction(SrcChoice srcChoice) {
261         NxOutputReg reg = new NxOutputRegBuilder().setSrc(
262                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension
263                     .nicira.action.rev140714.nx.action.output.reg.grouping.nx.output.reg
264                     .SrcBuilder().setSrcChoice(srcChoice)
265                     .setOfsNbits(Integer.valueOf(31))
266                     .build())
267             .setMaxLen(Integer.valueOf(0xffff))
268             .build();
269         return new NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder().setNxOutputReg(reg).build();
270     }
271
272     public static Action nxOutputRegAction(Class<? extends NxmNxReg> reg) {
273         return nxOutputRegAction(new SrcNxRegCaseBuilder().setNxReg(reg).build());
274     }
275 }