Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / vendorextension / v6extension / V6FlowMod.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension;
11
12 import java.nio.ByteBuffer;
13 import java.util.LinkedList;
14 import java.util.List;
15
16 import org.openflow.protocol.OFPacketOut;
17 import org.openflow.protocol.OFPort;
18 import org.openflow.protocol.OFVendor;
19 import org.openflow.protocol.action.OFAction;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * This class is used to create IPv6 Vendor Extension messages. Specfically, It
25  * defines the methods used in creation of Vendor specific IPv6 Flow Mod message.
26  *
27  *
28  */
29 public class V6FlowMod extends OFVendor implements Cloneable {
30     private static final Logger logger = LoggerFactory
31             .getLogger(V6FlowMod.class);
32     private static final long serialVersionUID = 1L;
33     protected V6Match match;
34     protected long cookie;
35     protected short command;
36     protected short idleTimeout;
37     protected short hardTimeout;
38     protected short priority;
39     protected int bufferId;
40     protected short outPort;
41     protected short flags;
42     protected List<OFAction> actions;
43     short match_len;
44     short actions_len;
45     short pad_size;
46
47     private static int IPV6EXT_ADD_FLOW_MSG_TYPE = 13;
48     private static int IPV6_EXT_MIN_HDR_LEN = 36;
49
50     /**
51      * Constructor for the V6FlowMod class. Initializes OFVendor (parent class)
52      * fields by calling the parent class' constructor.
53      */
54     public V6FlowMod() {
55         super();
56     }
57
58     /**
59      * This method sets the match fields of V6FlowMod object
60      * @param match     V6Match object for this V6FlowMod message
61      */
62     public void setMatch(V6Match match) {
63         this.match = match;
64     }
65
66     /**
67      * Sets the list of actions V6FlowMod message
68      * @param actions   a list of ordered OFAction objects
69      */
70     public void setActions(List<OFAction> actions) {
71         this.actions = actions;
72     }
73
74     /**
75      * Sets the priority field of V6FlowMod message
76      * @param priority  Priority of the message
77      */
78     public void setPriority(short priority) {
79         this.priority = priority;
80     }
81
82     /**
83      * Sets the cookie field of V6FlowMod message
84      * @param cookie    Cookie of the message
85      */
86     public void setCookie(long cookie) {
87         this.cookie = cookie;
88     }
89
90     /**
91      * Sets the command field of V6FlowMod message
92      * @param command   Command type of the message (ADD or DELETE)
93      */
94     public V6FlowMod setCommand(short command) {
95         this.command = command;
96         return this;
97     }
98
99     /**
100      * Sets the outPort field of V6FlowMod message
101      * @param outPort   outPort of the message
102      */
103     public V6FlowMod setOutPort(OFPort port) {
104         this.outPort = port.getValue();
105         return this;
106     }
107
108     /**
109      * Sets the idle_timeout of V6FlowMod message
110      * @param idleTimeout   idle timeout for this message
111      */
112     public void setIdleTimeout(short idleTimeout) {
113         this.idleTimeout = idleTimeout;
114     }
115
116     /**
117      * Sets the hardTimeout field of V6FlowMod message
118      * @param hardTimeout       hard timeout of the message
119      */
120     public void setHardTimeout(short hardTimeout) {
121         this.hardTimeout = hardTimeout;
122     }
123
124     /**
125      * Returns the Flow Mod message subtype for V6FlowMod message
126      * @return          message subtype
127      */
128     private int getIPv6ExtensionFlowModAddSubType() {
129         return IPV6EXT_ADD_FLOW_MSG_TYPE;
130     }
131
132     /**
133      * Returns the minimum header size for V6Flow Message type
134      * @return      minimum header size
135      */
136
137     public int getV6FlowModMinHdrSize() {
138         return IPV6_EXT_MIN_HDR_LEN;
139     }
140
141     /**
142      * Sets the Vendor type in OFVendor message
143      */
144
145     public void setVendor() {
146         super.setVendor(V6StatsRequest.NICIRA_VENDOR_ID);
147     }
148
149     /**
150      * Get flags
151      * @return
152      */
153     public short getFlags() {
154         return flags;
155     }
156
157     /**
158      * Set flags
159      * @param flags
160      */
161     public void setFlags(short flags) {
162         this.flags = flags;
163     }
164
165     /**
166      * This method forms the Vendor extension IPv6 Flow Mod message.It uses the
167      * fields in V6FlowMod class, and writes the data according to vendor
168      * extension format. The fields include flow properties (cookie, timeout,
169      * priority, etc), flow match, and action list. It also takes care of
170      * required padding.
171      */
172
173     @Override
174     public void writeTo(ByteBuffer data) {
175         super.writeTo(data);
176         data.putInt(getIPv6ExtensionFlowModAddSubType());
177         data.putLong(this.cookie);
178         data.putShort(command); /* should be OFPFC_ADD, OFPFC_DELETE_STRICT, etc*/
179         data.putShort(this.idleTimeout);
180         data.putShort(this.hardTimeout);
181         data.putShort(this.priority);
182         data.putInt(OFPacketOut.BUFFER_ID_NONE);
183         data.putShort(outPort); /* output_port */
184         data.putShort(flags); /* flags */
185         match_len = this.match.getIPv6MatchLen();
186         data.putShort(match_len);
187         byte[] pad = new byte[6];
188         data.put(pad);
189         this.match.writeTo(data);
190
191         pad_size = (short) (((match_len + 7) / 8) * 8 - match_len);
192
193         /*
194          * action list should be preceded by a padding of 0 to 7 bytes based upon
195          * above formula.
196          */
197
198         byte[] pad2 = new byte[pad_size];
199         data.put(pad2);
200         if (actions != null) {
201             for (OFAction action : actions) {
202                 actions_len += action.getLength();
203                 action.writeTo(data);
204             }
205         }
206         logger.trace("{}", this);
207     }
208
209     /**
210      * Forms the clone of V6FlowMod Object. If Object is returned
211      * successfully, then returns the cloned object. Throws an
212      * exception if cloning is not supported.
213      */
214     @Override
215     public V6FlowMod clone() {
216         try {
217             V6Match neoMatch = match.clone();
218             V6FlowMod v6flowMod = (V6FlowMod) super.clone();
219             v6flowMod.setMatch(neoMatch);
220             List<OFAction> neoActions = new LinkedList<OFAction>();
221             for (OFAction action : this.actions) {
222                 neoActions.add((OFAction) action.clone());
223             }
224             v6flowMod.setActions(neoActions);
225             return v6flowMod;
226         } catch (CloneNotSupportedException e) {
227             // Won't happen
228             throw new RuntimeException(e);
229         }
230     }
231
232     @Override
233     public String toString() {
234         return "V6FlowMod [match=" + match + ", cookie=" + cookie
235                 + ", command=" + command + ", idleTimeout=" + idleTimeout
236                 + ", hardTimeout=" + hardTimeout + ", priority=" + priority
237                 + ", bufferId=" + bufferId + ", outPort=" + outPort
238                 + ", flags=" + flags + ", actions=" + actions + ", match_len="
239                 + match_len + ", actions_len=" + actions_len + ", pad_size="
240                 + pad_size + "]";
241     }
242
243 }