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