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