blob: 19b30dbace372e84bda702def62d50fe8ce42ce3 [file] [log] [blame]
Frank Wang5733c382017-03-28 10:15:18 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wang5733c382017-03-28 10:15:18 +08003 *
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
17package org.onosproject.driver.extensions;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Maps;
21import org.onlab.util.KryoNamespace;
Jian Li99c2f2e2019-02-24 23:01:03 +090022import org.onosproject.driver.extensions.serializers.NiciraNatSerializer;
Frank Wang5733c382017-03-28 10:15:18 +080023import org.onosproject.net.flow.AbstractExtension;
24import org.onosproject.net.flow.instructions.ExtensionTreatment;
25import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
26
27import java.util.ArrayList;
Jian Li3df4a672018-10-27 20:32:24 +090028import java.util.HashMap;
Frank Wang5733c382017-03-28 10:15:18 +080029import java.util.List;
30import java.util.Map;
31import java.util.Objects;
32
33/**
34 * Nicira ct extension instruction.
35 */
36public class NiciraCt extends AbstractExtension implements ExtensionTreatment {
37
38 private int flags;
39 private long zoneSrc;
40 private int zone;
41 private short recircTable;
42 private int alg;
43 private List<ExtensionTreatment> nestedActions;
Jian Li3df4a672018-10-27 20:32:24 +090044 private final KryoNamespace appKryo = new KryoNamespace.Builder()
45 .register(HashMap.class)
46 .register(ArrayList.class)
47 .register(ExtensionTreatment.class)
Jian Li99c2f2e2019-02-24 23:01:03 +090048 .register(new NiciraNatSerializer(),
49 NiciraNat.class)
Jian Li3df4a672018-10-27 20:32:24 +090050 .build();
Frank Wang5733c382017-03-28 10:15:18 +080051
52 /**
53 * Creates a new nicicra ct instruction.
54 */
55 public NiciraCt() {
56 flags = 0;
57 zoneSrc = 0L;
58 zone = 0;
59 alg = 0;
60 recircTable = 0xFF;
61 nestedActions = new ArrayList<>();
62 }
63
64 /**
65 * Creates a new nicicra ct instruction.
66 * @param flags zero or commit(0x01)
67 * @param zoneSrc If 'zone_src' is nonzero, this specifies that the zone should be
68 * sourced from a field zone_src[ofs:ofs+nbits].
69 * @param zone this is the union of zone_imm and zone_ofs_nbits
70 * If 'zone_src' is zero, then the value of 'zone_imm'
71 * will be used as the connection tracking zone
72 * @param recircTable Recirculate to a specific table or 0xff for no recirculation
73 * @param alg Well-known port number for the protocol, 0 indicates no ALG is required
74 * @param actions a sequence of zero or more OpenFlow actions
75 */
76 public NiciraCt(int flags, long zoneSrc, int zone, short recircTable, int alg, List<ExtensionTreatment> actions) {
77 this.flags = flags;
78 this.zoneSrc = zoneSrc;
79 this.zone = zone;
80 this.recircTable = recircTable;
81 this.alg = alg;
82 this.nestedActions = actions;
83 }
84
85 @Override
86 public ExtensionTreatmentType type() {
87 return ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type();
88 }
89
90 /**
91 * Get Nicira Conntrack flags.
92 * @return flags
93 */
94 public int niciraCtFlags() {
95 return flags;
96 }
97
98 /**
99 * Get Nicira Conntrack zone.
100 * @return zone
101 */
102 public int niciraCtZone() {
103 return zone;
104 }
105
106 /**
107 * Get Nicira Conntrack zone src.
108 * @return zoneSrc
109 */
110 public long niciraCtZoneSrc() {
111 return zoneSrc;
112 }
113
114 /**
115 * Get Nicira Conntrack alg.
116 * @return alg
117 */
118 public int niciraCtAlg() {
119 return alg;
120 }
121
122 /**
123 * Get Nicira Conntrack Recirc table.
124 * @return recirc table
125 */
126 public short niciraCtRecircTable() {
127 return recircTable;
128 }
129
130 /**
131 * Get Nicira Conntrack Recirc table.
132 * @return list extension treatment
133 */
134 public List<ExtensionTreatment> niciraCtNestActions() {
135 return nestedActions;
136 }
137
138 @Override
139 public void deserialize(byte[] data) {
140 Map<String, Object> values = appKryo.deserialize(data);
141 flags = (int) values.get("flags");
142 zoneSrc = (long) values.get("zoneSrc");
143 zone = (int) values.get("zone");
144 recircTable = (short) values.get("recircTable");
145 alg = (int) values.get("alg");
146 nestedActions = (List) values.get("nestedActions");
147 }
148
149 @Override
150 public byte[] serialize() {
151 Map<String, Object> values = Maps.newHashMap();
152 values.put("flags", flags);
153 values.put("zoneSrc", zoneSrc);
154 values.put("zone", zone);
155 values.put("recircTable", recircTable);
156 values.put("alg", alg);
157 values.put("nestedActions", nestedActions);
158 return appKryo.serialize(values);
159 }
160
161 @Override
162 public int hashCode() {
163 return Objects.hash(type(), flags, zone, zoneSrc, alg, recircTable, nestedActions);
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (this == obj) {
169 return true;
170 }
171 if (obj instanceof NiciraCt) {
172 NiciraCt that = (NiciraCt) obj;
173 return Objects.equals(flags, that.flags) &&
174 Objects.equals(zone, that.zone) &&
175 Objects.equals(zoneSrc, that.zoneSrc) &&
176 Objects.equals(alg, that.alg) &&
177 Objects.equals(recircTable, that.recircTable) &&
178 Objects.equals(nestedActions, that.nestedActions) &&
179 Objects.equals(this.type(), that.type());
180 }
181 return false;
182 }
183
184 @Override
185 public String toString() {
186 return MoreObjects.toStringHelper(getClass())
187 .add("flags", flags)
188 .add("zoneSrc", zoneSrc)
189 .add("zone", zone)
190 .add("recircTable", recircTable)
191 .add("alg", alg)
192 .add("nestedActions", nestedActions)
193 .toString();
194 }
195}