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