blob: 7a5e8a0533d0514c90fa5611b4aace80e9a20f24 [file] [log] [blame]
Carmelo Cascone5899c132016-04-06 22:09:08 -07001/*
Carmelo Casconeaa8b6292016-04-13 14:27:06 -07002 * Copyright 2016-present Open Networking Laboratory
Carmelo Cascone5899c132016-04-06 22:09:08 -07003 *
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
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070017package org.onosproject.bmv2.api.model;
Carmelo Cascone5899c132016-04-06 22:09:08 -070018
19import com.eclipsesource.json.JsonArray;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.base.MoreObjects;
22import com.google.common.base.Objects;
23import com.google.common.collect.ImmutableList;
24import com.google.common.collect.Lists;
25import com.google.common.collect.Maps;
26import com.google.common.collect.Sets;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070027import org.onosproject.bmv2.api.runtime.Bmv2MatchParam;
Carmelo Cascone5899c132016-04-06 22:09:08 -070028
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32import java.util.SortedMap;
33
34import static com.google.common.base.Preconditions.checkArgument;
35
36/**
37 * Partial representation of a packet processing model for BMv2. Such a model is
38 * used to define the way BMv2 should process packets (i.e. it defines the
39 * device ingress/egress pipelines, parser, tables, actions, etc.) and can be
40 * generated (i.e. JSON) by compiling a P4 program using p4c-bm.
41 * <p>
42 * It must be noted that this class exposes only a subset of the full model
43 * properties (only those that are needed for the purpose of mapping ONOS types
44 * to BMv2 types.
45 *
46 * @see <a href="https://github.com/p4lang/p4c-bm">
47 * P4 front-end compiler for BMv2 (p4c-bm)</a>
48 */
49public final class Bmv2Model {
50
51 private final JsonObject json;
52 private final DualKeySortedMap<Bmv2ModelHeaderType> headerTypes = new DualKeySortedMap<>();
53 private final DualKeySortedMap<Bmv2ModelHeader> headers = new DualKeySortedMap<>();
54 private final DualKeySortedMap<Bmv2ModelAction> actions = new DualKeySortedMap<>();
55 private final DualKeySortedMap<Bmv2ModelTable> tables = new DualKeySortedMap<>();
56
57 private Bmv2Model(JsonObject json) {
58 this.json = JsonObject.unmodifiableObject(json);
59 }
60
61 /**
62 * Returns a new BMv2 model object by parsing the passed JSON.
63 *
64 * @param json json
65 * @return a new BMv2 configuration object
66 * @see <a href="https://github.com/p4lang/behavioral-model/blob/master/docs/JSON_format.md">
67 * BMv2 JSON specification</a>
68 */
69 public static Bmv2Model parse(JsonObject json) {
70 checkArgument(json != null, "json cannot be null");
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070071 // TODO: implement caching, no need to parse a json if we already have the model
Carmelo Cascone5899c132016-04-06 22:09:08 -070072 Bmv2Model model = new Bmv2Model(json);
73 model.doParse();
74 return model;
75 }
76
77 /**
78 * Returns the header type associated with the passed numeric id,
79 * null if there's no such an id in the model.
80 *
81 * @param id integer value
82 * @return header type object or null
83 */
84 public Bmv2ModelHeaderType headerType(int id) {
85 return headerTypes.get(id);
86 }
87
88 /**
89 * Returns the header type associated with the passed name,
90 * null if there's no such a name in the model.
91 *
92 * @param name string value
93 * @return header type object or null
94 */
95 public Bmv2ModelHeaderType headerType(String name) {
96 return headerTypes.get(name);
97 }
98
99 /**
100 * Returns the list of all the header types defined by in this model.
101 * Values returned are sorted in ascending order based on the numeric id.
102 *
103 * @return list of header types
104 */
105 public List<Bmv2ModelHeaderType> headerTypes() {
106 return ImmutableList.copyOf(headerTypes.sortedMap().values());
107 }
108
109 /**
110 * Returns the header associated with the passed numeric id,
111 * null if there's no such an id in the model.
112 *
113 * @param id integer value
114 * @return header object or null
115 */
116 public Bmv2ModelHeader header(int id) {
117 return headers.get(id);
118 }
119
120 /**
121 * Returns the header associated with the passed name,
122 * null if there's no such a name in the model.
123 *
124 * @param name string value
125 * @return header object or null
126 */
127 public Bmv2ModelHeader header(String name) {
128 return headers.get(name);
129 }
130
131 /**
132 * Returns the list of all the header instances defined in this model.
133 * Values returned are sorted in ascending order based on the numeric id.
134 *
135 * @return list of header types
136 */
137 public List<Bmv2ModelHeader> headers() {
138 return ImmutableList.copyOf(headers.sortedMap().values());
139 }
140
141 /**
142 * Returns the action associated with the passed numeric id,
143 * null if there's no such an id in the model.
144 *
145 * @param id integer value
146 * @return action object or null
147 */
148 public Bmv2ModelAction action(int id) {
149 return actions.get(id);
150 }
151
152 /**
153 * Returns the action associated with the passed name,
154 * null if there's no such a name in the model.
155 *
156 * @param name string value
157 * @return action object or null
158 */
159 public Bmv2ModelAction action(String name) {
160 return actions.get(name);
161 }
162
163 /**
164 * Returns the list of all the actions defined by in this model.
165 * Values returned are sorted in ascending order based on the numeric id.
166 *
167 * @return list of actions
168 */
169 public List<Bmv2ModelAction> actions() {
170 return ImmutableList.copyOf(actions.sortedMap().values());
171 }
172
173 /**
174 * Returns the table associated with the passed numeric id,
175 * null if there's no such an id in the model.
176 *
177 * @param id integer value
178 * @return table object or null
179 */
180 public Bmv2ModelTable table(int id) {
181 return tables.get(id);
182 }
183
184 /**
185 * Returns the table associated with the passed name,
186 * null if there's no such a name in the model.
187 *
188 * @param name string value
189 * @return table object or null
190 */
191 public Bmv2ModelTable table(String name) {
192 return tables.get(name);
193 }
194
195 /**
196 * Returns the list of all the tables defined by in this model.
197 * Values returned are sorted in ascending order based on the numeric id.
198 *
199 * @return list of actions
200 */
201 public List<Bmv2ModelTable> tables() {
202 return ImmutableList.copyOf(tables.sortedMap().values());
203 }
204
205 /**
206 * Return an unmodifiable view of the low-level JSON representation of this
207 * model.
208 *
209 * @return a JSON object.
210 */
211 public JsonObject json() {
212 return this.json;
213 }
214
215 /**
216 * Generates a hash code for this BMv2 model. The hash function is based
217 * solely on the low-level JSON representation.
218 */
219 @Override
220 public int hashCode() {
221 return json.hashCode();
222 }
223
224 /**
225 * Indicates whether some other BMv2 model is equal to this one.
226 * Equality is based solely on the low-level JSON representation.
227 *
228 * @param obj other object
229 * @return true if equals, false elsewhere
230 */
231 @Override
232 public boolean equals(Object obj) {
233 if (this == obj) {
234 return true;
235 }
236 if (obj == null || getClass() != obj.getClass()) {
237 return false;
238 }
239 final Bmv2Model other = (Bmv2Model) obj;
240 return Objects.equal(this.json, other.json);
241 }
242
243 @Override
244 public String toString() {
245 return MoreObjects.toStringHelper(this)
246 .add("jsonHash", json.hashCode())
247 .toString();
248 }
249
250 /**
251 * Parse the JSON object and build the corresponding objects.
252 */
253 private void doParse() {
254 // parse header types
255 json.get("header_types").asArray().forEach(val -> {
256
257 JsonObject jHeaderType = val.asObject();
258
259 // populate fields list
260 List<Bmv2ModelFieldType> fieldTypes = Lists.newArrayList();
261
262 jHeaderType.get("fields").asArray().forEach(x -> fieldTypes.add(
263 new Bmv2ModelFieldType(
264 x.asArray().get(0).asString(),
265 x.asArray().get(1).asInt())));
266
267 // add header type instance
268 String name = jHeaderType.get("name").asString();
269 int id = jHeaderType.get("id").asInt();
270
271 Bmv2ModelHeaderType headerType = new Bmv2ModelHeaderType(name,
272 id,
273 fieldTypes);
274
275 headerTypes.put(name, id, headerType);
276 });
277
278 // parse headers
279 json.get("headers").asArray().forEach(val -> {
280
281 JsonObject jHeader = val.asObject();
282
283 String name = jHeader.get("name").asString();
284 int id = jHeader.get("id").asInt();
285 String typeName = jHeader.get("header_type").asString();
286
287 Bmv2ModelHeader header = new Bmv2ModelHeader(name,
288 id,
289 headerTypes.get(typeName),
290 jHeader.get("metadata").asBoolean());
291
292 // add instance
293 headers.put(name, id, header);
294 });
295
296 // parse actions
297 json.get("actions").asArray().forEach(val -> {
298
299 JsonObject jAction = val.asObject();
300
301 // populate runtime data list
302 List<Bmv2ModelRuntimeData> runtimeDatas = Lists.newArrayList();
303
304 jAction.get("runtime_data").asArray().forEach(jData -> runtimeDatas.add(
305 new Bmv2ModelRuntimeData(
306 jData.asObject().get("name").asString(),
307 jData.asObject().get("bitwidth").asInt()
308 )));
309
310 // add action instance
311 String name = jAction.get("name").asString();
312 int id = jAction.get("id").asInt();
313
314 Bmv2ModelAction action = new Bmv2ModelAction(name,
315 id,
316 runtimeDatas);
317
318 actions.put(name, id, action);
319 });
320
321 // parse tables
322 json.get("pipelines").asArray().forEach(pipeline -> {
323
324 pipeline.asObject().get("tables").asArray().forEach(val -> {
325
326 JsonObject jTable = val.asObject();
327
328 // populate keys
329 List<Bmv2ModelTableKey> keys = Lists.newArrayList();
330
331 jTable.get("key").asArray().forEach(jKey -> {
332 JsonArray target = jKey.asObject().get("target").asArray();
333
334 Bmv2ModelHeader header = header(target.get(0).asString());
335 String typeName = target.get(1).asString();
336
337 Bmv2ModelField field = new Bmv2ModelField(
338 header, header.type().field(typeName));
339
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700340 String matchTypeStr = jKey.asObject().get("match_type").asString();
341
342 Bmv2MatchParam.Type matchType;
343
344 switch (matchTypeStr) {
345 case "ternary":
346 matchType = Bmv2MatchParam.Type.TERNARY;
347 break;
348 case "exact":
349 matchType = Bmv2MatchParam.Type.EXACT;
350 break;
351 case "lpm":
352 matchType = Bmv2MatchParam.Type.LPM;
353 break;
354 case "valid":
355 matchType = Bmv2MatchParam.Type.VALID;
356 break;
357 default:
358 throw new RuntimeException(
359 "Unable to parse match type: " + matchTypeStr);
360 }
Carmelo Cascone5899c132016-04-06 22:09:08 -0700361
362 keys.add(new Bmv2ModelTableKey(matchType, field));
363 });
364
365 // populate actions set
366 Set<Bmv2ModelAction> actionzz = Sets.newHashSet();
367 jTable.get("actions").asArray().forEach(
368 jAction -> actionzz.add(action(jAction.asString())));
369
370 // add table instance
371 String name = jTable.get("name").asString();
372 int id = jTable.get("id").asInt();
373
374 Bmv2ModelTable table = new Bmv2ModelTable(name,
375 id,
376 jTable.get("match_type").asString(),
377 jTable.get("type").asString(),
378 jTable.get("max_size").asInt(),
379 jTable.get("with_counters").asBoolean(),
380 jTable.get("support_timeout").asBoolean(),
381 keys,
382 actionzz);
383
384 tables.put(name, id, table);
385 });
386 });
387 }
388
389 /**
390 * Handy class for a map indexed by two keys, a string and an integer.
391 *
392 * @param <T> type of value stored by the map
393 */
394 private class DualKeySortedMap<T> {
395 private final SortedMap<Integer, T> intMap = Maps.newTreeMap();
396 private final Map<String, Integer> strToIntMap = Maps.newHashMap();
397
398 private void put(String name, int id, T object) {
399 strToIntMap.put(name, id);
400 intMap.put(id, object);
401 }
402
403 private T get(int id) {
404 return intMap.get(id);
405 }
406
407 private T get(String name) {
408 return get(strToIntMap.get(name));
409 }
410
411 private SortedMap<Integer, T> sortedMap() {
412 return intMap;
413 }
414
415 @Override
416 public int hashCode() {
417 return Objects.hashCode(intMap, strToIntMap);
418 }
419
420 @Override
421 public boolean equals(Object obj) {
422 if (this == obj) {
423 return true;
424 }
425 if (obj == null || getClass() != obj.getClass()) {
426 return false;
427 }
428 final DualKeySortedMap other = (DualKeySortedMap) obj;
429 return Objects.equal(this.intMap, other.intMap)
430 && Objects.equal(this.strToIntMap, other.strToIntMap);
431 }
432 }
433}