41819504390c6ab06648405f0024abb092e641da
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.net.flow.instructions;
17
18 import static com.google.common.base.MoreObjects.toStringHelper;
19
20 import java.util.Objects;
21
22 import org.onlab.packet.IpAddress;
23
24 /**
25  * Abstraction of a single traffic treatment step.
26  */
27 public abstract class L3ModificationInstruction implements Instruction {
28
29     /**
30      * Represents the type of traffic treatment.
31      */
32     public enum L3SubType {
33         /**
34          * IPv4 src modification.
35          */
36         IPV4_SRC,
37
38         /**
39          * IPv4 dst modification.
40          */
41         IPV4_DST,
42
43         /**
44          * IPv6 src modification.
45          */
46         IPV6_SRC,
47
48         /**
49          * IPv6 dst modification.
50          */
51         IPV6_DST,
52
53         /**
54          * IPv6 flow label modification.
55          */
56         IPV6_FLABEL,
57
58         /**
59          * Decrement TTL.
60          */
61         DEC_TTL,
62
63         /**
64          * Copy TTL out.
65          */
66         TTL_OUT,
67
68         /**
69          * Copy TTL in.
70          */
71         TTL_IN
72
73         //TODO: remaining types
74     }
75
76     /**
77      * Returns the subtype of the modification instruction.
78      * @return type of instruction
79      */
80     public abstract L3SubType subtype();
81
82     @Override
83     public final Type type() {
84         return Type.L3MODIFICATION;
85     }
86
87     /**
88      * Represents a L3 src/dst modification instruction.
89      */
90     public static final class ModIPInstruction extends L3ModificationInstruction {
91
92         private final L3SubType subtype;
93         private final IpAddress ip;
94
95         ModIPInstruction(L3SubType subType, IpAddress addr) {
96
97             this.subtype = subType;
98             this.ip = addr;
99         }
100
101         @Override
102         public L3SubType subtype() {
103             return this.subtype;
104         }
105
106         public IpAddress ip() {
107             return this.ip;
108         }
109
110         @Override
111         public String toString() {
112             return toStringHelper(subtype().toString())
113                     .add("ip", ip).toString();
114         }
115
116         @Override
117         public int hashCode() {
118             return Objects.hash(type(), subtype(), ip);
119         }
120
121         @Override
122         public boolean equals(Object obj) {
123             if (this == obj) {
124                 return true;
125             }
126             if (obj instanceof ModIPInstruction) {
127                 ModIPInstruction that = (ModIPInstruction) obj;
128                 return  Objects.equals(ip, that.ip) &&
129                         Objects.equals(this.subtype(), that.subtype());
130             }
131             return false;
132         }
133     }
134
135     /**
136      * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction
137      * (20 bits unsigned integer).
138      */
139     public static final class ModIPv6FlowLabelInstruction
140         extends L3ModificationInstruction {
141         private static final int MASK = 0xfffff;
142         private final int flowLabel;            // IPv6 flow label: 20 bits
143
144         /**
145          * Creates a new flow mod instruction.
146          *
147          * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
148          */
149         ModIPv6FlowLabelInstruction(int flowLabel) {
150             this.flowLabel = flowLabel & MASK;
151         }
152
153         @Override
154         public L3SubType subtype() {
155             return L3SubType.IPV6_FLABEL;
156         }
157
158         /**
159          * Gets the IPv6 flow label to set in the treatment.
160          *
161          * @return the IPv6 flow label to set in the treatment (20 bits)
162          */
163         public int flowLabel() {
164             return this.flowLabel;
165         }
166
167         @Override
168         public String toString() {
169             return toStringHelper(subtype().toString())
170                 .add("flowLabel", Long.toHexString(flowLabel)).toString();
171         }
172
173         @Override
174         public int hashCode() {
175             return Objects.hash(type(), subtype(), flowLabel);
176         }
177
178         @Override
179         public boolean equals(Object obj) {
180             if (this == obj) {
181                 return true;
182             }
183             if (obj instanceof ModIPv6FlowLabelInstruction) {
184                 ModIPv6FlowLabelInstruction that =
185                     (ModIPv6FlowLabelInstruction) obj;
186                 return  Objects.equals(flowLabel, that.flowLabel);
187             }
188             return false;
189         }
190     }
191
192     /**
193      * Represents a L3 TTL modification instruction.
194      */
195     public static final class ModTtlInstruction extends L3ModificationInstruction {
196
197         private final L3SubType subtype;
198
199         ModTtlInstruction(L3SubType subtype) {
200             this.subtype = subtype;
201         }
202
203         @Override
204         public L3SubType subtype() {
205             return this.subtype;
206         }
207
208         @Override
209         public String toString() {
210             return toStringHelper(subtype().toString())
211                     .toString();
212         }
213
214         @Override
215         public int hashCode() {
216             return Objects.hash(type(), subtype());
217         }
218
219         @Override
220         public boolean equals(Object obj) {
221             if (this == obj) {
222                 return true;
223             }
224             if (obj instanceof ModTtlInstruction) {
225                 ModTtlInstruction that = (ModTtlInstruction) obj;
226                 return  Objects.equals(this.subtype(), that.subtype());
227             }
228             return false;
229         }
230     }
231 }