Carmelo Cascone

Various Bmv2 protocol refactorings in preparation of the flow rule

translator (ONOS-4044)

- Added new classes for different match parameters (exact, ternary, lpm,
  valid)
- Divided api package in two sub-packages, model (previously under
  drivers) and runtime (old api package)
- Improved Bmv2ThriftClient caching and table entry handling

Change-Id: I23c174cf3e8f9f6ecddb99c2d09dc531e8f1c73f
Showing 30 changed files with 715 additions and 211 deletions
...@@ -20,10 +20,10 @@ import com.google.common.base.Preconditions; ...@@ -20,10 +20,10 @@ import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Maps; 21 import com.google.common.collect.Maps;
22 import com.google.common.collect.Sets; 22 import com.google.common.collect.Sets;
23 -import org.onosproject.bmv2.api.Bmv2ExtensionSelector; 23 +import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector;
24 -import org.onosproject.bmv2.api.Bmv2ExtensionTreatment; 24 +import org.onosproject.bmv2.api.runtime.Bmv2ExtensionTreatment;
25 -import org.onosproject.bmv2.api.Bmv2TableEntry; 25 +import org.onosproject.bmv2.api.runtime.Bmv2TableEntry;
26 -import org.onosproject.bmv2.api.Bmv2Exception; 26 +import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
27 import org.onosproject.bmv2.ctl.Bmv2ThriftClient; 27 import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
28 import org.onosproject.net.driver.AbstractHandlerBehaviour; 28 import org.onosproject.net.driver.AbstractHandlerBehaviour;
29 import org.onosproject.net.flow.DefaultFlowEntry; 29 import org.onosproject.net.flow.DefaultFlowEntry;
...@@ -70,7 +70,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour ...@@ -70,7 +70,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
70 Bmv2ThriftClient deviceClient; 70 Bmv2ThriftClient deviceClient;
71 try { 71 try {
72 deviceClient = getDeviceClient(); 72 deviceClient = getDeviceClient();
73 - } catch (Bmv2Exception e) { 73 + } catch (Bmv2RuntimeException e) {
74 return Collections.emptyList(); 74 return Collections.emptyList();
75 } 75 }
76 76
...@@ -107,7 +107,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour ...@@ -107,7 +107,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
107 tableEntryIdsMap.put(rule, entryId); 107 tableEntryIdsMap.put(rule, entryId);
108 deviceEntriesMap.put(rule, new DefaultFlowEntry( 108 deviceEntriesMap.put(rule, new DefaultFlowEntry(
109 rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0)); 109 rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
110 - } catch (Bmv2Exception e) { 110 + } catch (Bmv2RuntimeException e) {
111 log.error("Unable to update flow rule", e); 111 log.error("Unable to update flow rule", e);
112 continue; 112 continue;
113 } 113 }
...@@ -121,7 +121,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour ...@@ -121,7 +121,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
121 tableEntryIdsMap.put(rule, entryId); 121 tableEntryIdsMap.put(rule, entryId);
122 deviceEntriesMap.put(rule, new DefaultFlowEntry( 122 deviceEntriesMap.put(rule, new DefaultFlowEntry(
123 rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0)); 123 rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
124 - } catch (Bmv2Exception e) { 124 + } catch (Bmv2RuntimeException e) {
125 log.error("Unable to add flow rule", e); 125 log.error("Unable to add flow rule", e);
126 continue; 126 continue;
127 } 127 }
...@@ -138,7 +138,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour ...@@ -138,7 +138,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
138 Bmv2ThriftClient deviceClient; 138 Bmv2ThriftClient deviceClient;
139 try { 139 try {
140 deviceClient = getDeviceClient(); 140 deviceClient = getDeviceClient();
141 - } catch (Bmv2Exception e) { 141 + } catch (Bmv2RuntimeException e) {
142 return Collections.emptyList(); 142 return Collections.emptyList();
143 } 143 }
144 144
...@@ -152,7 +152,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour ...@@ -152,7 +152,7 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
152 152
153 try { 153 try {
154 deviceClient.deleteTableEntry(tableName, entryId); 154 deviceClient.deleteTableEntry(tableName, entryId);
155 - } catch (Bmv2Exception e) { 155 + } catch (Bmv2RuntimeException e) {
156 log.error("Unable to delete flow rule", e); 156 log.error("Unable to delete flow rule", e);
157 continue; 157 continue;
158 } 158 }
...@@ -225,10 +225,10 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour ...@@ -225,10 +225,10 @@ public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
225 return "table" + String.valueOf(tableId); 225 return "table" + String.valueOf(tableId);
226 } 226 }
227 227
228 - private Bmv2ThriftClient getDeviceClient() throws Bmv2Exception { 228 + private Bmv2ThriftClient getDeviceClient() throws Bmv2RuntimeException {
229 try { 229 try {
230 return Bmv2ThriftClient.of(handler().data().deviceId()); 230 return Bmv2ThriftClient.of(handler().data().deviceId());
231 - } catch (Bmv2Exception e) { 231 + } catch (Bmv2RuntimeException e) {
232 log.error("Failed to connect to Bmv2 device", e); 232 log.error("Failed to connect to Bmv2 device", e);
233 throw e; 233 throw e;
234 } 234 }
......
...@@ -18,7 +18,7 @@ package org.onosproject.drivers.bmv2; ...@@ -18,7 +18,7 @@ package org.onosproject.drivers.bmv2;
18 18
19 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 -import org.onosproject.bmv2.api.Bmv2Exception; 21 +import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
22 import org.onosproject.bmv2.ctl.Bmv2ThriftClient; 22 import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
23 import org.onosproject.net.DefaultAnnotations; 23 import org.onosproject.net.DefaultAnnotations;
24 import org.onosproject.net.PortNumber; 24 import org.onosproject.net.PortNumber;
...@@ -44,7 +44,7 @@ public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour ...@@ -44,7 +44,7 @@ public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour
44 Bmv2ThriftClient deviceClient; 44 Bmv2ThriftClient deviceClient;
45 try { 45 try {
46 deviceClient = Bmv2ThriftClient.of(handler().data().deviceId()); 46 deviceClient = Bmv2ThriftClient.of(handler().data().deviceId());
47 - } catch (Bmv2Exception e) { 47 + } catch (Bmv2RuntimeException e) {
48 log.error("Failed to connect to Bmv2 device", e); 48 log.error("Failed to connect to Bmv2 device", e);
49 return Collections.emptyList(); 49 return Collections.emptyList();
50 } 50 }
...@@ -68,7 +68,7 @@ public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour ...@@ -68,7 +68,7 @@ public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour
68 annotations 68 annotations
69 )); 69 ));
70 }); 70 });
71 - } catch (Bmv2Exception e) { 71 + } catch (Bmv2RuntimeException e) {
72 log.error("Unable to get port description from Bmv2 device", e); 72 log.error("Unable to get port description from Bmv2 device", e);
73 } 73 }
74 74
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.eclipsesource.json.JsonArray; 19 import com.eclipsesource.json.JsonArray;
20 import com.eclipsesource.json.JsonObject; 20 import com.eclipsesource.json.JsonObject;
...@@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableList; ...@@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.Lists; 24 import com.google.common.collect.Lists;
25 import com.google.common.collect.Maps; 25 import com.google.common.collect.Maps;
26 import com.google.common.collect.Sets; 26 import com.google.common.collect.Sets;
27 +import org.onosproject.bmv2.api.runtime.Bmv2MatchParam;
27 28
28 import java.util.List; 29 import java.util.List;
29 import java.util.Map; 30 import java.util.Map;
...@@ -67,6 +68,7 @@ public final class Bmv2Model { ...@@ -67,6 +68,7 @@ public final class Bmv2Model {
67 */ 68 */
68 public static Bmv2Model parse(JsonObject json) { 69 public static Bmv2Model parse(JsonObject json) {
69 checkArgument(json != null, "json cannot be null"); 70 checkArgument(json != null, "json cannot be null");
71 + // TODO: implement caching, no need to parse a json if we already have the model
70 Bmv2Model model = new Bmv2Model(json); 72 Bmv2Model model = new Bmv2Model(json);
71 model.doParse(); 73 model.doParse();
72 return model; 74 return model;
...@@ -335,7 +337,27 @@ public final class Bmv2Model { ...@@ -335,7 +337,27 @@ public final class Bmv2Model {
335 Bmv2ModelField field = new Bmv2ModelField( 337 Bmv2ModelField field = new Bmv2ModelField(
336 header, header.type().field(typeName)); 338 header, header.type().field(typeName));
337 339
338 - String matchType = jKey.asObject().get("match_type").asString(); 340 + 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 + }
339 361
340 keys.add(new Bmv2ModelTableKey(matchType, field)); 362 keys.add(new Bmv2ModelTableKey(matchType, field));
341 }); 363 });
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Maps; 20 import com.google.common.collect.Maps;
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
20 20
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
20 20
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
20 20
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
20 import com.google.common.collect.ImmutableList; 20 import com.google.common.collect.ImmutableList;
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import java.util.Objects; 19 import java.util.Objects;
20 20
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
20 20
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,9 +14,10 @@ ...@@ -14,9 +14,10 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2.model; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.google.common.base.Objects; 19 import com.google.common.base.Objects;
20 +import org.onosproject.bmv2.api.runtime.Bmv2MatchParam;
20 21
21 import static com.google.common.base.MoreObjects.toStringHelper; 22 import static com.google.common.base.MoreObjects.toStringHelper;
22 23
...@@ -25,7 +26,7 @@ import static com.google.common.base.MoreObjects.toStringHelper; ...@@ -25,7 +26,7 @@ import static com.google.common.base.MoreObjects.toStringHelper;
25 */ 26 */
26 public final class Bmv2ModelTableKey { 27 public final class Bmv2ModelTableKey {
27 28
28 - private final String matchType; 29 + private final Bmv2MatchParam.Type matchType;
29 private final Bmv2ModelField field; 30 private final Bmv2ModelField field;
30 31
31 /** 32 /**
...@@ -34,7 +35,7 @@ public final class Bmv2ModelTableKey { ...@@ -34,7 +35,7 @@ public final class Bmv2ModelTableKey {
34 * @param matchType match type 35 * @param matchType match type
35 * @param field field instance 36 * @param field field instance
36 */ 37 */
37 - protected Bmv2ModelTableKey(String matchType, Bmv2ModelField field) { 38 + protected Bmv2ModelTableKey(Bmv2MatchParam.Type matchType, Bmv2ModelField field) {
38 this.matchType = matchType; 39 this.matchType = matchType;
39 this.field = field; 40 this.field = field;
40 } 41 }
...@@ -45,7 +46,7 @@ public final class Bmv2ModelTableKey { ...@@ -45,7 +46,7 @@ public final class Bmv2ModelTableKey {
45 * @return a string value 46 * @return a string value
46 * TODO returns enum of match type 47 * TODO returns enum of match type
47 */ 48 */
48 - public String matchType() { 49 + public Bmv2MatchParam.Type matchType() {
49 return matchType; 50 return matchType;
50 } 51 }
51 52
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -17,4 +17,4 @@ ...@@ -17,4 +17,4 @@
17 /** 17 /**
18 * BMv2 configuration model classes. 18 * BMv2 configuration model classes.
19 */ 19 */
20 -package org.onosproject.drivers.bmv2.model;
...\ No newline at end of file ...\ No newline at end of file
20 +package org.onosproject.bmv2.api.model;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,5 +16,10 @@ ...@@ -16,5 +16,10 @@
16 16
17 /** 17 /**
18 * Bmv2 API abstractions. 18 * Bmv2 API abstractions.
19 + * <p>
20 + * Bmv2 APIs are divided in two sub-packages, runtime and model.
21 + * Runtime APIs are used to represent operations that can be performed at runtime
22 + * on a Bmv2 device, while model APIs are used to describe the Bmv2 packet
23 + * processing model.
19 */ 24 */
20 package org.onosproject.bmv2.api; 25 package org.onosproject.bmv2.api;
...\ No newline at end of file ...\ No newline at end of file
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,27 +14,28 @@ ...@@ -14,27 +14,28 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 18
19 import com.google.common.base.MoreObjects; 19 import com.google.common.base.MoreObjects;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 +import org.onlab.util.ImmutableByteSequence;
21 22
22 -import java.nio.ByteBuffer;
23 import java.util.Collections; 23 import java.util.Collections;
24 import java.util.List; 24 import java.util.List;
25 import java.util.Objects; 25 import java.util.Objects;
26 26
27 import static com.google.common.base.Preconditions.checkNotNull; 27 import static com.google.common.base.Preconditions.checkNotNull;
28 +import static com.google.common.base.Preconditions.checkState;
28 29
29 /** 30 /**
30 - * Bmv2 Action representation. 31 + * Bmv2 action representation.
31 */ 32 */
32 public final class Bmv2Action { 33 public final class Bmv2Action {
33 34
34 private final String name; 35 private final String name;
35 - private final List<ByteBuffer> parameters; 36 + private final List<ImmutableByteSequence> parameters;
36 37
37 - private Bmv2Action(String name, List<ByteBuffer> parameters) { 38 + private Bmv2Action(String name, List<ImmutableByteSequence> parameters) {
38 // hide constructor 39 // hide constructor
39 this.name = name; 40 this.name = name;
40 this.parameters = parameters; 41 this.parameters = parameters;
...@@ -48,7 +49,7 @@ public final class Bmv2Action { ...@@ -48,7 +49,7 @@ public final class Bmv2Action {
48 } 49 }
49 50
50 /** 51 /**
51 - * Get action name. 52 + * Return the name of this action.
52 * 53 *
53 * @return action name 54 * @return action name
54 */ 55 */
...@@ -57,11 +58,12 @@ public final class Bmv2Action { ...@@ -57,11 +58,12 @@ public final class Bmv2Action {
57 } 58 }
58 59
59 /** 60 /**
60 - * Get list of action parameters ordered as per P4 action definition. 61 + * Returns an immutable view of the ordered list of parameters of this
62 + * action.
61 * 63 *
62 - * @return List of action parameters 64 + * @return list of byte sequence
63 */ 65 */
64 - public final List<ByteBuffer> parameters() { 66 + public final List<ImmutableByteSequence> parameters() {
65 return Collections.unmodifiableList(parameters); 67 return Collections.unmodifiableList(parameters);
66 } 68 }
67 69
...@@ -96,8 +98,8 @@ public final class Bmv2Action { ...@@ -96,8 +98,8 @@ public final class Bmv2Action {
96 */ 98 */
97 public static final class Builder { 99 public static final class Builder {
98 100
99 - private String name; 101 + private String name = null;
100 - private List<ByteBuffer> parameters; 102 + private List<ImmutableByteSequence> parameters;
101 103
102 private Builder() { 104 private Builder() {
103 this.parameters = Lists.newArrayList(); 105 this.parameters = Lists.newArrayList();
...@@ -110,7 +112,7 @@ public final class Bmv2Action { ...@@ -110,7 +112,7 @@ public final class Bmv2Action {
110 * @return this 112 * @return this
111 */ 113 */
112 public Builder withName(String actionName) { 114 public Builder withName(String actionName) {
113 - this.name = actionName; 115 + this.name = checkNotNull(actionName);
114 return this; 116 return this;
115 } 117 }
116 118
...@@ -120,8 +122,8 @@ public final class Bmv2Action { ...@@ -120,8 +122,8 @@ public final class Bmv2Action {
120 * @param parameter a ByteBuffer value 122 * @param parameter a ByteBuffer value
121 * @return this 123 * @return this
122 */ 124 */
123 - public Builder addParameter(ByteBuffer parameter) { 125 + public Builder addParameter(ImmutableByteSequence parameter) {
124 - parameters.add(parameter); 126 + parameters.add(checkNotNull(parameter));
125 return this; 127 return this;
126 } 128 }
127 129
...@@ -131,7 +133,7 @@ public final class Bmv2Action { ...@@ -131,7 +133,7 @@ public final class Bmv2Action {
131 * @return a Bmv2 action 133 * @return a Bmv2 action
132 */ 134 */
133 public Bmv2Action build() { 135 public Bmv2Action build() {
134 - checkNotNull(name, "Action name not set"); 136 + checkState(name != null, "action name not set");
135 return new Bmv2Action(name, parameters); 137 return new Bmv2Action(name, parameters);
136 } 138 }
137 } 139 }
......
1 +/*
2 + * Copyright 2016-present 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 +
17 +package org.onosproject.bmv2.api.runtime;
18 +
19 +import com.google.common.base.MoreObjects;
20 +import com.google.common.base.Objects;
21 +import org.onlab.util.ImmutableByteSequence;
22 +
23 +import static com.google.common.base.Preconditions.checkNotNull;
24 +
25 +/**
26 + * Representation of a Bmv2 exact match parameter.
27 + */
28 +public class Bmv2ExactMatchParam implements Bmv2MatchParam {
29 +
30 + private final ImmutableByteSequence value;
31 +
32 + /**
33 + * Creates a new match parameter object that matches exactly on the
34 + * given byte sequence.
35 + *
36 + * @param value a byte sequence value
37 + */
38 + public Bmv2ExactMatchParam(ImmutableByteSequence value) {
39 + this.value = checkNotNull(value, "value cannot be null");
40 + }
41 +
42 + @Override
43 + public Type type() {
44 + return Type.EXACT;
45 + }
46 +
47 + /**
48 + * Return the byte sequence value matched by this parameter.
49 + *
50 + * @return an immutable byte buffer value
51 + */
52 + public ImmutableByteSequence value() {
53 + return this.value;
54 + }
55 +
56 + @Override
57 + public int hashCode() {
58 + return Objects.hashCode(value);
59 + }
60 +
61 + @Override
62 + public boolean equals(Object obj) {
63 + if (this == obj) {
64 + return true;
65 + }
66 + if (obj == null || getClass() != obj.getClass()) {
67 + return false;
68 + }
69 + final Bmv2ExactMatchParam other = (Bmv2ExactMatchParam) obj;
70 + return Objects.equal(this.value, other.value);
71 + }
72 +
73 + @Override
74 + public String toString() {
75 + return MoreObjects.toStringHelper(this)
76 + .add("value", value)
77 + .toString();
78 + }
79 +}
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,13 +14,16 @@ ...@@ -14,13 +14,16 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 18
19 import org.onlab.util.KryoNamespace; 19 import org.onlab.util.KryoNamespace;
20 import org.onosproject.net.flow.AbstractExtension; 20 import org.onosproject.net.flow.AbstractExtension;
21 import org.onosproject.net.flow.criteria.ExtensionSelector; 21 import org.onosproject.net.flow.criteria.ExtensionSelector;
22 import org.onosproject.net.flow.criteria.ExtensionSelectorType; 22 import org.onosproject.net.flow.criteria.ExtensionSelectorType;
23 23
24 +/**
25 + * Extension selector for Bmv2 used as a wrapper for a {@link Bmv2MatchKey}.
26 + */
24 public class Bmv2ExtensionSelector extends AbstractExtension implements ExtensionSelector { 27 public class Bmv2ExtensionSelector extends AbstractExtension implements ExtensionSelector {
25 28
26 private final KryoNamespace appKryo = new KryoNamespace.Builder().build(); 29 private final KryoNamespace appKryo = new KryoNamespace.Builder().build();
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,13 +14,16 @@ ...@@ -14,13 +14,16 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 18
19 import org.onlab.util.KryoNamespace; 19 import org.onlab.util.KryoNamespace;
20 import org.onosproject.net.flow.AbstractExtension; 20 import org.onosproject.net.flow.AbstractExtension;
21 import org.onosproject.net.flow.instructions.ExtensionTreatment; 21 import org.onosproject.net.flow.instructions.ExtensionTreatment;
22 import org.onosproject.net.flow.instructions.ExtensionTreatmentType; 22 import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
23 23
24 +/**
25 + * Extension treatment for Bmv2 used as a wrapper for a {@link Bmv2Action}.
26 + */
24 public class Bmv2ExtensionTreatment extends AbstractExtension implements ExtensionTreatment { 27 public class Bmv2ExtensionTreatment extends AbstractExtension implements ExtensionTreatment {
25 28
26 private final KryoNamespace appKryo = new KryoNamespace.Builder().build(); 29 private final KryoNamespace appKryo = new KryoNamespace.Builder().build();
......
1 +/*
2 + * Copyright 2016-present 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 +
17 +package org.onosproject.bmv2.api.runtime;
18 +
19 +import com.google.common.base.MoreObjects;
20 +import com.google.common.base.Objects;
21 +import org.onlab.util.ImmutableByteSequence;
22 +
23 +import static com.google.common.base.Preconditions.checkArgument;
24 +import static com.google.common.base.Preconditions.checkNotNull;
25 +
26 +/**
27 + * Representation of a Bmv2 longest prefix match (LPM) parameter.
28 + */
29 +public class Bmv2LpmMatchParam implements Bmv2MatchParam {
30 +
31 + private final ImmutableByteSequence value;
32 + private final int prefixLength;
33 +
34 + /**
35 + * Creates a new LPM parameter using the given byte sequence value and
36 + * prefix length.
37 + *
38 + * @param value a byte sequence value
39 + * @param prefixLength an integer value
40 + */
41 + public Bmv2LpmMatchParam(ImmutableByteSequence value, int prefixLength) {
42 + checkArgument(prefixLength >= 0, "prefix length cannot be negative");
43 + this.value = checkNotNull(value);
44 + this.prefixLength = prefixLength;
45 + }
46 +
47 + @Override
48 + public Bmv2MatchParam.Type type() {
49 + return Type.LPM;
50 + }
51 +
52 + /**
53 + * Returns the byte sequence value of this parameter.
54 + *
55 + * @return a byte sequence value
56 + */
57 + public ImmutableByteSequence value() {
58 + return this.value;
59 + }
60 +
61 + /**
62 + * Returns the prefix length of this parameter.
63 + *
64 + * @return an integer value
65 + */
66 + public int prefixLength() {
67 + return this.prefixLength;
68 + }
69 +
70 + @Override
71 + public int hashCode() {
72 + return Objects.hashCode(value, prefixLength);
73 + }
74 +
75 + @Override
76 + public boolean equals(Object obj) {
77 + if (this == obj) {
78 + return true;
79 + }
80 + if (obj == null || getClass() != obj.getClass()) {
81 + return false;
82 + }
83 + final Bmv2LpmMatchParam other = (Bmv2LpmMatchParam) obj;
84 + return Objects.equal(this.value, other.value)
85 + && Objects.equal(this.prefixLength, other.prefixLength);
86 + }
87 +
88 + @Override
89 + public String toString() {
90 + return MoreObjects.toStringHelper(this)
91 + .add("value", value)
92 + .add("prefixLength", prefixLength)
93 + .toString();
94 + }
95 +}
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,36 +14,28 @@ ...@@ -14,36 +14,28 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 18
19 import com.google.common.base.MoreObjects; 19 import com.google.common.base.MoreObjects;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 -import org.p4.bmv2.thrift.BmMatchParam; 21 +import org.onlab.util.ImmutableByteSequence;
22 -import org.p4.bmv2.thrift.BmMatchParamExact; 22 +
23 -import org.p4.bmv2.thrift.BmMatchParamLPM;
24 -import org.p4.bmv2.thrift.BmMatchParamTernary;
25 -import org.p4.bmv2.thrift.BmMatchParamType;
26 -import org.p4.bmv2.thrift.BmMatchParamValid;
27 -
28 -import java.nio.ByteBuffer;
29 -import java.util.Arrays;
30 import java.util.Collections; 23 import java.util.Collections;
31 import java.util.List; 24 import java.util.List;
32 import java.util.Objects; 25 import java.util.Objects;
33 26
27 +import static com.google.common.base.Preconditions.checkArgument;
28 +import static com.google.common.base.Preconditions.checkNotNull;
29 +
34 /** 30 /**
35 * Bmv2 match key representation. 31 * Bmv2 match key representation.
36 */ 32 */
37 public final class Bmv2MatchKey { 33 public final class Bmv2MatchKey {
38 34
39 - private final List<BmMatchParam> matchParams; 35 + private final List<Bmv2MatchParam> matchParams;
40 36
41 - /** 37 + private Bmv2MatchKey(List<Bmv2MatchParam> matchParams) {
42 - * Creates a new match key. 38 + // ban constructor
43 - *
44 - * @param matchParams The ordered list of match parameters
45 - */
46 - private Bmv2MatchKey(List<BmMatchParam> matchParams) {
47 this.matchParams = matchParams; 39 this.matchParams = matchParams;
48 } 40 }
49 41
...@@ -52,11 +44,12 @@ public final class Bmv2MatchKey { ...@@ -52,11 +44,12 @@ public final class Bmv2MatchKey {
52 } 44 }
53 45
54 /** 46 /**
55 - * Returns the match parameters defined for this match key (read-only). 47 + * Returns an immutable view of the ordered list of match parameters of this
48 + * match key.
56 * 49 *
57 - * @return match parameters 50 + * @return list match parameters
58 */ 51 */
59 - public final List<BmMatchParam> bmMatchParams() { 52 + public final List<Bmv2MatchParam> matchParams() {
60 return Collections.unmodifiableList(matchParams); 53 return Collections.unmodifiableList(matchParams);
61 } 54 }
62 55
...@@ -90,77 +83,28 @@ public final class Bmv2MatchKey { ...@@ -90,77 +83,28 @@ public final class Bmv2MatchKey {
90 */ 83 */
91 public static final class Builder { 84 public static final class Builder {
92 85
93 - private List<BmMatchParam> matchParams; 86 + private List<Bmv2MatchParam> matchParams;
94 87
95 private Builder() { 88 private Builder() {
96 this.matchParams = Lists.newArrayList(); 89 this.matchParams = Lists.newArrayList();
97 } 90 }
98 91
99 - /** 92 + public Builder add(Bmv2MatchParam param) {
100 - * Adds an exact match parameter. 93 + this.matchParams.add(checkNotNull(param));
101 - *
102 - * @param key a ByteBuffer value
103 - * @return this
104 - */
105 - public Builder withExact(ByteBuffer key) {
106 - this.matchParams.add(
107 - new BmMatchParam(BmMatchParamType.EXACT)
108 - .setExact(new BmMatchParamExact(key)));
109 - return this;
110 - }
111 -
112 -
113 - /**
114 - * Adds a longest prefix match parameter.
115 - *
116 - * @param key a ByteBuffer value
117 - * @param prefixLength an integer value
118 - * @return this
119 - */
120 - public Builder withLpm(ByteBuffer key, int prefixLength) {
121 - this.matchParams.add(
122 - new BmMatchParam(BmMatchParamType.LPM)
123 - .setLpm(new BmMatchParamLPM(key, prefixLength)));
124 - return this;
125 - }
126 -
127 - /**
128 - * Adds a ternary match parameter.
129 - *
130 - * @param key a ByteBuffer value
131 - * @param mask an ByteBuffer value
132 - * @return this
133 - */
134 - public Builder withTernary(ByteBuffer key, ByteBuffer mask) {
135 - this.matchParams.add(
136 - new BmMatchParam(BmMatchParamType.TERNARY).
137 - setTernary(new BmMatchParamTernary(key, mask)));
138 return this; 94 return this;
139 } 95 }
140 96
141 /** 97 /**
142 * Adds a ternary match parameter where all bits are don't-care. 98 * Adds a ternary match parameter where all bits are don't-care.
143 * 99 *
144 - * @param byteLength an integer value representing the length in byte of the parameter 100 + * @param byteLength length in bytes of the parameter
145 * @return this 101 * @return this
146 */ 102 */
147 public Builder withWildcard(int byteLength) { 103 public Builder withWildcard(int byteLength) {
148 - byte[] zeros = new byte[byteLength]; 104 + checkArgument(byteLength > 0, "length must be a positive integer");
149 - Arrays.fill(zeros, (byte) 0); 105 + return add(new Bmv2TernaryMatchParam(
150 - return this.withTernary(ByteBuffer.wrap(zeros), ByteBuffer.wrap(zeros)); 106 + ImmutableByteSequence.ofZeros(byteLength),
151 - } 107 + ImmutableByteSequence.ofZeros(byteLength)));
152 -
153 - /**
154 - * Adds a valid match parameter.
155 - *
156 - * @param key a boolean value
157 - * @return this
158 - */
159 - public Builder withValid(boolean key) {
160 - this.matchParams.add(
161 - new BmMatchParam(BmMatchParamType.VALID)
162 - .setValid(new BmMatchParamValid(key)));
163 - return this;
164 } 108 }
165 109
166 /** 110 /**
......
1 +/*
2 + * Copyright 2016-present 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 +
17 +package org.onosproject.bmv2.api.runtime;
18 +
19 +/**
20 + * Representation of a Bmv2 match parameter.
21 + */
22 +public interface Bmv2MatchParam {
23 +
24 + /**
25 + * Returns the match type of this parameter.
26 + *
27 + * @return a match type value
28 + */
29 + Type type();
30 +
31 + /**
32 + * Bmv2 match types.
33 + */
34 + enum Type {
35 + /**
36 + * Exact match type.
37 + */
38 + EXACT,
39 + /**
40 + * Ternary match type.
41 + */
42 + TERNARY,
43 + /**
44 + * Longest-prefix match type.
45 + */
46 + LPM,
47 + /**
48 + * Valid match type.
49 + */
50 + VALID;
51 + }
52 +}
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 18
19 import com.google.common.base.MoreObjects; 19 import com.google.common.base.MoreObjects;
20 import org.p4.bmv2.thrift.DevMgrPortInfo; 20 import org.p4.bmv2.thrift.DevMgrPortInfo;
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,14 +14,18 @@ ...@@ -14,14 +14,18 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 18
19 /** 19 /**
20 - * General Bmv2 exception. 20 + * General exception of the Bmv2 runtime APIs.
21 */ 21 */
22 -public class Bmv2Exception extends Exception { 22 +public class Bmv2RuntimeException extends Exception {
23 23
24 - public Bmv2Exception(String message, Throwable cause) { 24 + public Bmv2RuntimeException(String message, Throwable cause) {
25 super(message, cause); 25 super(message, cause);
26 } 26 }
27 +
28 + public Bmv2RuntimeException(String message) {
29 + super(message);
30 + }
27 } 31 }
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,12 +14,13 @@ ...@@ -14,12 +14,13 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.bmv2.api; 17 +package org.onosproject.bmv2.api.runtime;
18 -
19 -import com.google.common.base.Preconditions;
20 18
21 import java.util.Objects; 19 import java.util.Objects;
22 20
21 +import static com.google.common.base.Preconditions.checkArgument;
22 +import static com.google.common.base.Preconditions.checkNotNull;
23 +
23 /** 24 /**
24 * Bmv2 representation of a table entry. 25 * Bmv2 representation of a table entry.
25 */ 26 */
...@@ -43,6 +44,11 @@ public final class Bmv2TableEntry { ...@@ -43,6 +44,11 @@ public final class Bmv2TableEntry {
43 this.timeout = timeout; 44 this.timeout = timeout;
44 } 45 }
45 46
47 + /**
48 + * Returns a new Bmv2 table entry builder.
49 + *
50 + * @return a new builder.
51 + */
46 public static Builder builder() { 52 public static Builder builder() {
47 return new Builder(); 53 return new Builder();
48 } 54 }
...@@ -159,7 +165,7 @@ public final class Bmv2TableEntry { ...@@ -159,7 +165,7 @@ public final class Bmv2TableEntry {
159 * @return this 165 * @return this
160 */ 166 */
161 public Builder withTableName(String tableName) { 167 public Builder withTableName(String tableName) {
162 - this.tableName = tableName; 168 + this.tableName = checkNotNull(tableName, "table name cannot be null");
163 return this; 169 return this;
164 } 170 }
165 171
...@@ -170,7 +176,7 @@ public final class Bmv2TableEntry { ...@@ -170,7 +176,7 @@ public final class Bmv2TableEntry {
170 * @return this 176 * @return this
171 */ 177 */
172 public Builder withMatchKey(Bmv2MatchKey matchKey) { 178 public Builder withMatchKey(Bmv2MatchKey matchKey) {
173 - this.matchKey = matchKey; 179 + this.matchKey = checkNotNull(matchKey, "match key cannot be null");
174 return this; 180 return this;
175 } 181 }
176 182
...@@ -181,11 +187,12 @@ public final class Bmv2TableEntry { ...@@ -181,11 +187,12 @@ public final class Bmv2TableEntry {
181 * @return this 187 * @return this
182 */ 188 */
183 public Builder withAction(Bmv2Action action) { 189 public Builder withAction(Bmv2Action action) {
184 - this.action = action; 190 + this.action = checkNotNull(action, "action cannot be null");
185 return this; 191 return this;
186 } 192 }
187 193
188 public Builder withPriority(int priority) { 194 public Builder withPriority(int priority) {
195 + checkArgument(priority >= 0, "priority cannot be negative");
189 this.priority = priority; 196 this.priority = priority;
190 return this; 197 return this;
191 } 198 }
...@@ -197,6 +204,7 @@ public final class Bmv2TableEntry { ...@@ -197,6 +204,7 @@ public final class Bmv2TableEntry {
197 * @return this 204 * @return this
198 */ 205 */
199 public Builder withTimeout(double timeout) { 206 public Builder withTimeout(double timeout) {
207 + checkArgument(timeout > 0, "timeout must be a positive non-zero value");
200 this.timeout = timeout; 208 this.timeout = timeout;
201 return this; 209 return this;
202 } 210 }
...@@ -207,10 +215,6 @@ public final class Bmv2TableEntry { ...@@ -207,10 +215,6 @@ public final class Bmv2TableEntry {
207 * @return a new table entry object 215 * @return a new table entry object
208 */ 216 */
209 public Bmv2TableEntry build() { 217 public Bmv2TableEntry build() {
210 - Preconditions.checkNotNull(tableName);
211 - Preconditions.checkNotNull(matchKey);
212 - Preconditions.checkNotNull(action);
213 -
214 return new Bmv2TableEntry(tableName, matchKey, action, priority, 218 return new Bmv2TableEntry(tableName, matchKey, action, priority,
215 timeout); 219 timeout);
216 220
......
1 +/*
2 + * Copyright 2016-present 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 +
17 +package org.onosproject.bmv2.api.runtime;
18 +
19 +import com.google.common.base.MoreObjects;
20 +import com.google.common.base.Objects;
21 +import org.onlab.util.ImmutableByteSequence;
22 +
23 +import static com.google.common.base.Preconditions.checkNotNull;
24 +import static com.google.common.base.Preconditions.checkState;
25 +
26 +/**
27 + * Representation of a Bmv2 ternary match parameter.
28 + */
29 +public class Bmv2TernaryMatchParam implements Bmv2MatchParam {
30 +
31 + private final ImmutableByteSequence value;
32 + private final ImmutableByteSequence mask;
33 +
34 + /**
35 + * Creates a new ternary match parameter using the given byte sequences of
36 + * value and mask.
37 + *
38 + * @param value a byte sequence value
39 + * @param mask a byte sequence value
40 + */
41 + public Bmv2TernaryMatchParam(ImmutableByteSequence value,
42 + ImmutableByteSequence mask) {
43 + this.value = checkNotNull(value, "value cannot be null");
44 + this.mask = checkNotNull(mask, "value cannot be null");
45 + checkState(value.size() == mask.size(),
46 + "value and mask must have equal size");
47 + }
48 +
49 + @Override
50 + public Type type() {
51 + return Type.TERNARY;
52 + }
53 +
54 + /**
55 + * Returns the byte sequence value of by this parameter.
56 + *
57 + * @return a byte sequence value
58 + */
59 + public ImmutableByteSequence value() {
60 + return this.value;
61 + }
62 +
63 + /**
64 + * Returns the byte sequence mask of by this parameter.
65 + *
66 + * @return a byte sequence value
67 + */
68 + public ImmutableByteSequence mask() {
69 + return this.mask;
70 + }
71 +
72 + @Override
73 + public int hashCode() {
74 + return Objects.hashCode(value, mask);
75 + }
76 +
77 + @Override
78 + public boolean equals(Object obj) {
79 + if (this == obj) {
80 + return true;
81 + }
82 + if (obj == null || getClass() != obj.getClass()) {
83 + return false;
84 + }
85 + final Bmv2TernaryMatchParam other = (Bmv2TernaryMatchParam) obj;
86 + return Objects.equal(this.value, other.value)
87 + && Objects.equal(this.mask, other.mask);
88 + }
89 +
90 + @Override
91 + public String toString() {
92 + return MoreObjects.toStringHelper(this)
93 + .add("value", value)
94 + .add("mask", mask)
95 + .toString();
96 + }
97 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016-present 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 +
17 +package org.onosproject.bmv2.api.runtime;
18 +
19 +
20 +import com.google.common.base.MoreObjects;
21 +
22 +import java.util.Objects;
23 +
24 +/**
25 + * Representation of a Bmv2 valid match parameter.
26 + */
27 +public class Bmv2ValidMatchParam implements Bmv2MatchParam {
28 +
29 + private final boolean flag;
30 +
31 + /**
32 + * Creates a new valid match parameter using the given boolean flag.
33 + *
34 + * @param flag a boolean value
35 + */
36 + public Bmv2ValidMatchParam(boolean flag) {
37 + this.flag = flag;
38 + }
39 +
40 + @Override
41 + public Type type() {
42 + return Type.VALID;
43 + }
44 +
45 + /**
46 + * Returns the boolean flag of this parameter.
47 + *
48 + * @return a boolean value
49 + */
50 + public boolean flag() {
51 + return flag;
52 + }
53 +
54 + @Override
55 + public int hashCode() {
56 + return Objects.hashCode(flag);
57 + }
58 +
59 + @Override
60 + public boolean equals(Object obj) {
61 + if (this == obj) {
62 + return true;
63 + }
64 + if (obj == null || getClass() != obj.getClass()) {
65 + return false;
66 + }
67 + final Bmv2ValidMatchParam other = (Bmv2ValidMatchParam) obj;
68 + return this.flag == other.flag;
69 + }
70 +
71 + @Override
72 + public String toString() {
73 + return MoreObjects.toStringHelper(this)
74 + .add("flag", flag)
75 + .toString();
76 + }
77 +}
1 +/*
2 + * Copyright 2016-present 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 +
17 +/**
18 + * Bmv2 runtime APIs.
19 + */
20 +package org.onosproject.bmv2.api.runtime;
...\ No newline at end of file ...\ No newline at end of file
...@@ -31,15 +31,28 @@ import org.apache.thrift.protocol.TProtocol; ...@@ -31,15 +31,28 @@ import org.apache.thrift.protocol.TProtocol;
31 import org.apache.thrift.transport.TSocket; 31 import org.apache.thrift.transport.TSocket;
32 import org.apache.thrift.transport.TTransport; 32 import org.apache.thrift.transport.TTransport;
33 import org.apache.thrift.transport.TTransportException; 33 import org.apache.thrift.transport.TTransportException;
34 -import org.onosproject.bmv2.api.Bmv2Action; 34 +import org.onlab.util.ImmutableByteSequence;
35 -import org.onosproject.bmv2.api.Bmv2Exception; 35 +import org.onosproject.bmv2.api.runtime.Bmv2Action;
36 -import org.onosproject.bmv2.api.Bmv2PortInfo; 36 +import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
37 -import org.onosproject.bmv2.api.Bmv2TableEntry; 37 +import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
38 +import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam;
39 +import org.onosproject.bmv2.api.runtime.Bmv2MatchKey;
40 +import org.onosproject.bmv2.api.runtime.Bmv2PortInfo;
41 +import org.onosproject.bmv2.api.runtime.Bmv2TableEntry;
42 +import org.onosproject.bmv2.api.runtime.Bmv2TernaryMatchParam;
43 +import org.onosproject.bmv2.api.runtime.Bmv2ValidMatchParam;
38 import org.onosproject.net.DeviceId; 44 import org.onosproject.net.DeviceId;
39 import org.p4.bmv2.thrift.BmAddEntryOptions; 45 import org.p4.bmv2.thrift.BmAddEntryOptions;
46 +import org.p4.bmv2.thrift.BmMatchParam;
47 +import org.p4.bmv2.thrift.BmMatchParamExact;
48 +import org.p4.bmv2.thrift.BmMatchParamLPM;
49 +import org.p4.bmv2.thrift.BmMatchParamTernary;
50 +import org.p4.bmv2.thrift.BmMatchParamType;
51 +import org.p4.bmv2.thrift.BmMatchParamValid;
40 import org.p4.bmv2.thrift.DevMgrPortInfo; 52 import org.p4.bmv2.thrift.DevMgrPortInfo;
41 import org.p4.bmv2.thrift.Standard; 53 import org.p4.bmv2.thrift.Standard;
42 54
55 +import java.nio.ByteBuffer;
43 import java.util.Collection; 56 import java.util.Collection;
44 import java.util.List; 57 import java.util.List;
45 import java.util.concurrent.ExecutionException; 58 import java.util.concurrent.ExecutionException;
...@@ -63,31 +76,37 @@ public final class Bmv2ThriftClient { ...@@ -63,31 +76,37 @@ public final class Bmv2ThriftClient {
63 - avoids opening a new transport session when there's one already open 76 - avoids opening a new transport session when there's one already open
64 - close the connection after a predefined timeout of 5 seconds 77 - close the connection after a predefined timeout of 5 seconds
65 */ 78 */
66 - private static LoadingCache<DeviceId, Pair<TTransport, Standard.Iface>> 79 + private static LoadingCache<DeviceId, Bmv2ThriftClient>
67 clientCache = CacheBuilder.newBuilder() 80 clientCache = CacheBuilder.newBuilder()
68 .expireAfterAccess(5, TimeUnit.SECONDS) 81 .expireAfterAccess(5, TimeUnit.SECONDS)
69 .removalListener(new ClientRemovalListener()) 82 .removalListener(new ClientRemovalListener())
70 .build(new ClientLoader()); 83 .build(new ClientLoader());
71 private final Standard.Iface stdClient; 84 private final Standard.Iface stdClient;
85 + private final TTransport transport;
72 86
73 // ban constructor 87 // ban constructor
74 - private Bmv2ThriftClient(Standard.Iface stdClient) { 88 + private Bmv2ThriftClient(TTransport transport, Standard.Iface stdClient) {
89 + this.transport = transport;
75 this.stdClient = stdClient; 90 this.stdClient = stdClient;
76 } 91 }
77 92
93 + private void closeTransport() {
94 + this.transport.close();
95 + }
96 +
78 /** 97 /**
79 * Returns a client object to control the passed device. 98 * Returns a client object to control the passed device.
80 * 99 *
81 * @param deviceId device id 100 * @param deviceId device id
82 * @return bmv2 client object 101 * @return bmv2 client object
83 - * @throws Bmv2Exception if a connection to the device cannot be established 102 + * @throws Bmv2RuntimeException if a connection to the device cannot be established
84 */ 103 */
85 - public static Bmv2ThriftClient of(DeviceId deviceId) throws Bmv2Exception { 104 + public static Bmv2ThriftClient of(DeviceId deviceId) throws Bmv2RuntimeException {
86 try { 105 try {
87 checkNotNull(deviceId, "deviceId cannot be null"); 106 checkNotNull(deviceId, "deviceId cannot be null");
88 - return new Bmv2ThriftClient(clientCache.get(deviceId).getValue()); 107 + return clientCache.get(deviceId);
89 } catch (ExecutionException e) { 108 } catch (ExecutionException e) {
90 - throw new Bmv2Exception(e.getMessage(), e.getCause()); 109 + throw new Bmv2RuntimeException(e.getMessage(), e.getCause());
91 } 110 }
92 } 111 }
93 112
...@@ -103,7 +122,7 @@ public final class Bmv2ThriftClient { ...@@ -103,7 +122,7 @@ public final class Bmv2ThriftClient {
103 try { 122 try {
104 of(deviceId).stdClient.bm_dev_mgr_show_ports(); 123 of(deviceId).stdClient.bm_dev_mgr_show_ports();
105 return true; 124 return true;
106 - } catch (TException | Bmv2Exception e) { 125 + } catch (TException | Bmv2RuntimeException e) {
107 return false; 126 return false;
108 } 127 }
109 } 128 }
...@@ -129,13 +148,70 @@ public final class Bmv2ThriftClient { ...@@ -129,13 +148,70 @@ public final class Bmv2ThriftClient {
129 } 148 }
130 149
131 /** 150 /**
151 + * Builds a list of Bmv2/Thrift compatible match parameters.
152 + *
153 + * @param matchKey a bmv2 matchKey
154 + * @return list of thrift-compatible bm match parameters
155 + */
156 + private static List<BmMatchParam> buildMatchParamsList(Bmv2MatchKey matchKey) {
157 + List<BmMatchParam> paramsList = Lists.newArrayList();
158 + matchKey.matchParams().forEach(x -> {
159 + switch (x.type()) {
160 + case EXACT:
161 + paramsList.add(
162 + new BmMatchParam(BmMatchParamType.EXACT)
163 + .setExact(new BmMatchParamExact(
164 + ((Bmv2ExactMatchParam) x).value().asReadOnlyBuffer())));
165 + break;
166 + case TERNARY:
167 + paramsList.add(
168 + new BmMatchParam(BmMatchParamType.TERNARY)
169 + .setTernary(new BmMatchParamTernary(
170 + ((Bmv2TernaryMatchParam) x).value().asReadOnlyBuffer(),
171 + ((Bmv2TernaryMatchParam) x).mask().asReadOnlyBuffer())));
172 + break;
173 + case LPM:
174 + paramsList.add(
175 + new BmMatchParam(BmMatchParamType.LPM)
176 + .setLpm(new BmMatchParamLPM(
177 + ((Bmv2LpmMatchParam) x).value().asReadOnlyBuffer(),
178 + ((Bmv2LpmMatchParam) x).prefixLength())));
179 + break;
180 + case VALID:
181 + paramsList.add(
182 + new BmMatchParam(BmMatchParamType.VALID)
183 + .setValid(new BmMatchParamValid(
184 + ((Bmv2ValidMatchParam) x).flag())));
185 + break;
186 + default:
187 + // should never be here
188 + throw new RuntimeException("Unknown match param type " + x.type().name());
189 + }
190 + });
191 + return paramsList;
192 + }
193 +
194 + /**
195 + * Build a list of Bmv2/Thrift compatible action parameters.
196 + *
197 + * @param action an action object
198 + * @return list of ByteBuffers
199 + */
200 + private static List<ByteBuffer> buildActionParamsList(Bmv2Action action) {
201 + return action.parameters()
202 + .stream()
203 + .map(ImmutableByteSequence::asReadOnlyBuffer)
204 + .collect(Collectors.toList());
205 + }
206 +
207 + /**
132 * Adds a new table entry. 208 * Adds a new table entry.
133 * 209 *
134 * @param entry a table entry value 210 * @param entry a table entry value
135 * @return table-specific entry ID 211 * @return table-specific entry ID
136 - * @throws Bmv2Exception if any error occurs 212 + * @throws Bmv2RuntimeException if any error occurs
137 */ 213 */
138 - public final long addTableEntry(Bmv2TableEntry entry) throws Bmv2Exception { 214 + public final long addTableEntry(Bmv2TableEntry entry) throws Bmv2RuntimeException {
139 215
140 long entryId = -1; 216 long entryId = -1;
141 217
...@@ -149,9 +225,9 @@ public final class Bmv2ThriftClient { ...@@ -149,9 +225,9 @@ public final class Bmv2ThriftClient {
149 entryId = stdClient.bm_mt_add_entry( 225 entryId = stdClient.bm_mt_add_entry(
150 CONTEXT_ID, 226 CONTEXT_ID,
151 entry.tableName(), 227 entry.tableName(),
152 - entry.matchKey().bmMatchParams(), 228 + buildMatchParamsList(entry.matchKey()),
153 entry.action().name(), 229 entry.action().name(),
154 - entry.action().parameters(), 230 + buildActionParamsList(entry.action()),
155 options); 231 options);
156 232
157 if (entry.hasTimeout()) { 233 if (entry.hasTimeout()) {
...@@ -170,10 +246,10 @@ public final class Bmv2ThriftClient { ...@@ -170,10 +246,10 @@ public final class Bmv2ThriftClient {
170 CONTEXT_ID, entry.tableName(), entryId); 246 CONTEXT_ID, entry.tableName(), entryId);
171 } catch (TException e1) { 247 } catch (TException e1) {
172 // this should never happen as we know the entry is there 248 // this should never happen as we know the entry is there
173 - throw new Bmv2Exception(e1.getMessage(), e1); 249 + throw new Bmv2RuntimeException(e1.getMessage(), e1);
174 } 250 }
175 } 251 }
176 - throw new Bmv2Exception(e.getMessage(), e); 252 + throw new Bmv2RuntimeException(e.getMessage(), e);
177 } 253 }
178 } 254 }
179 255
...@@ -183,11 +259,11 @@ public final class Bmv2ThriftClient { ...@@ -183,11 +259,11 @@ public final class Bmv2ThriftClient {
183 * @param tableName string value of table name 259 * @param tableName string value of table name
184 * @param entryId long value of entry ID 260 * @param entryId long value of entry ID
185 * @param action an action value 261 * @param action an action value
186 - * @throws Bmv2Exception if any error occurs 262 + * @throws Bmv2RuntimeException if any error occurs
187 */ 263 */
188 public final void modifyTableEntry(String tableName, 264 public final void modifyTableEntry(String tableName,
189 long entryId, Bmv2Action action) 265 long entryId, Bmv2Action action)
190 - throws Bmv2Exception { 266 + throws Bmv2RuntimeException {
191 267
192 try { 268 try {
193 stdClient.bm_mt_modify_entry( 269 stdClient.bm_mt_modify_entry(
...@@ -195,10 +271,9 @@ public final class Bmv2ThriftClient { ...@@ -195,10 +271,9 @@ public final class Bmv2ThriftClient {
195 tableName, 271 tableName,
196 entryId, 272 entryId,
197 action.name(), 273 action.name(),
198 - action.parameters() 274 + buildActionParamsList(action));
199 - );
200 } catch (TException e) { 275 } catch (TException e) {
201 - throw new Bmv2Exception(e.getMessage(), e); 276 + throw new Bmv2RuntimeException(e.getMessage(), e);
202 } 277 }
203 } 278 }
204 279
...@@ -207,15 +282,15 @@ public final class Bmv2ThriftClient { ...@@ -207,15 +282,15 @@ public final class Bmv2ThriftClient {
207 * 282 *
208 * @param tableName string value of table name 283 * @param tableName string value of table name
209 * @param entryId long value of entry ID 284 * @param entryId long value of entry ID
210 - * @throws Bmv2Exception if any error occurs 285 + * @throws Bmv2RuntimeException if any error occurs
211 */ 286 */
212 public final void deleteTableEntry(String tableName, 287 public final void deleteTableEntry(String tableName,
213 - long entryId) throws Bmv2Exception { 288 + long entryId) throws Bmv2RuntimeException {
214 289
215 try { 290 try {
216 stdClient.bm_mt_delete_entry(CONTEXT_ID, tableName, entryId); 291 stdClient.bm_mt_delete_entry(CONTEXT_ID, tableName, entryId);
217 } catch (TException e) { 292 } catch (TException e) {
218 - throw new Bmv2Exception(e.getMessage(), e); 293 + throw new Bmv2RuntimeException(e.getMessage(), e);
219 } 294 }
220 } 295 }
221 296
...@@ -224,19 +299,19 @@ public final class Bmv2ThriftClient { ...@@ -224,19 +299,19 @@ public final class Bmv2ThriftClient {
224 * 299 *
225 * @param tableName string value of table name 300 * @param tableName string value of table name
226 * @param action an action value 301 * @param action an action value
227 - * @throws Bmv2Exception if any error occurs 302 + * @throws Bmv2RuntimeException if any error occurs
228 */ 303 */
229 public final void setTableDefaultAction(String tableName, Bmv2Action action) 304 public final void setTableDefaultAction(String tableName, Bmv2Action action)
230 - throws Bmv2Exception { 305 + throws Bmv2RuntimeException {
231 306
232 try { 307 try {
233 stdClient.bm_mt_set_default_action( 308 stdClient.bm_mt_set_default_action(
234 CONTEXT_ID, 309 CONTEXT_ID,
235 tableName, 310 tableName,
236 action.name(), 311 action.name(),
237 - action.parameters()); 312 + buildActionParamsList(action));
238 } catch (TException e) { 313 } catch (TException e) {
239 - throw new Bmv2Exception(e.getMessage(), e); 314 + throw new Bmv2RuntimeException(e.getMessage(), e);
240 } 315 }
241 } 316 }
242 317
...@@ -244,9 +319,9 @@ public final class Bmv2ThriftClient { ...@@ -244,9 +319,9 @@ public final class Bmv2ThriftClient {
244 * Returns information of the ports currently configured in the switch. 319 * Returns information of the ports currently configured in the switch.
245 * 320 *
246 * @return collection of port information 321 * @return collection of port information
247 - * @throws Bmv2Exception if any error occurs 322 + * @throws Bmv2RuntimeException if any error occurs
248 */ 323 */
249 - public Collection<Bmv2PortInfo> getPortsInfo() throws Bmv2Exception { 324 + public Collection<Bmv2PortInfo> getPortsInfo() throws Bmv2RuntimeException {
250 325
251 try { 326 try {
252 List<DevMgrPortInfo> portInfos = stdClient.bm_dev_mgr_show_ports(); 327 List<DevMgrPortInfo> portInfos = stdClient.bm_dev_mgr_show_ports();
...@@ -261,7 +336,7 @@ public final class Bmv2ThriftClient { ...@@ -261,7 +336,7 @@ public final class Bmv2ThriftClient {
261 return bmv2PortInfos; 336 return bmv2PortInfos;
262 337
263 } catch (TException e) { 338 } catch (TException e) {
264 - throw new Bmv2Exception(e.getMessage(), e); 339 + throw new Bmv2RuntimeException(e.getMessage(), e);
265 } 340 }
266 } 341 }
267 342
...@@ -270,28 +345,28 @@ public final class Bmv2ThriftClient { ...@@ -270,28 +345,28 @@ public final class Bmv2ThriftClient {
270 * 345 *
271 * @param tableName string value of table name 346 * @param tableName string value of table name
272 * @return table string dump 347 * @return table string dump
273 - * @throws Bmv2Exception if any error occurs 348 + * @throws Bmv2RuntimeException if any error occurs
274 */ 349 */
275 - public String dumpTable(String tableName) throws Bmv2Exception { 350 + public String dumpTable(String tableName) throws Bmv2RuntimeException {
276 351
277 try { 352 try {
278 return stdClient.bm_dump_table(CONTEXT_ID, tableName); 353 return stdClient.bm_dump_table(CONTEXT_ID, tableName);
279 } catch (TException e) { 354 } catch (TException e) {
280 - throw new Bmv2Exception(e.getMessage(), e); 355 + throw new Bmv2RuntimeException(e.getMessage(), e);
281 } 356 }
282 } 357 }
283 358
284 /** 359 /**
285 * Reset the state of the switch (e.g. delete all entries, etc.). 360 * Reset the state of the switch (e.g. delete all entries, etc.).
286 * 361 *
287 - * @throws Bmv2Exception if any error occurs 362 + * @throws Bmv2RuntimeException if any error occurs
288 */ 363 */
289 - public void resetState() throws Bmv2Exception { 364 + public void resetState() throws Bmv2RuntimeException {
290 365
291 try { 366 try {
292 stdClient.bm_reset_state(); 367 stdClient.bm_reset_state();
293 } catch (TException e) { 368 } catch (TException e) {
294 - throw new Bmv2Exception(e.getMessage(), e); 369 + throw new Bmv2RuntimeException(e.getMessage(), e);
295 } 370 }
296 } 371 }
297 372
...@@ -299,10 +374,10 @@ public final class Bmv2ThriftClient { ...@@ -299,10 +374,10 @@ public final class Bmv2ThriftClient {
299 * Transport/client cache loader. 374 * Transport/client cache loader.
300 */ 375 */
301 private static class ClientLoader 376 private static class ClientLoader
302 - extends CacheLoader<DeviceId, Pair<TTransport, Standard.Iface>> { 377 + extends CacheLoader<DeviceId, Bmv2ThriftClient> {
303 378
304 @Override 379 @Override
305 - public Pair<TTransport, Standard.Iface> load(DeviceId deviceId) 380 + public Bmv2ThriftClient load(DeviceId deviceId)
306 throws TTransportException { 381 throws TTransportException {
307 Pair<String, Integer> info = parseDeviceId(deviceId); 382 Pair<String, Integer> info = parseDeviceId(deviceId);
308 //make the expensive call 383 //make the expensive call
...@@ -314,7 +389,7 @@ public final class Bmv2ThriftClient { ...@@ -314,7 +389,7 @@ public final class Bmv2ThriftClient {
314 389
315 transport.open(); 390 transport.open();
316 391
317 - return ImmutablePair.of(transport, stdClient); 392 + return new Bmv2ThriftClient(transport, stdClient);
318 } 393 }
319 } 394 }
320 395
...@@ -322,14 +397,13 @@ public final class Bmv2ThriftClient { ...@@ -322,14 +397,13 @@ public final class Bmv2ThriftClient {
322 * Client cache removal listener. Close the connection on cache removal. 397 * Client cache removal listener. Close the connection on cache removal.
323 */ 398 */
324 private static class ClientRemovalListener implements 399 private static class ClientRemovalListener implements
325 - RemovalListener<DeviceId, Pair<TTransport, Standard.Iface>> { 400 + RemovalListener<DeviceId, Bmv2ThriftClient> {
326 401
327 @Override 402 @Override
328 public void onRemoval( 403 public void onRemoval(
329 - RemovalNotification<DeviceId, Pair<TTransport, Standard.Iface>> 404 + RemovalNotification<DeviceId, Bmv2ThriftClient> notification) {
330 - notification) {
331 // close the transport connection 405 // close the transport connection
332 - notification.getValue().getKey().close(); 406 + notification.getValue().closeTransport();
333 } 407 }
334 } 408 }
335 } 409 }
......
1 /* 1 /*
2 - * Copyright 2014-2016 Open Networking Laboratory 2 + * Copyright 2016-present Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -14,17 +14,14 @@ ...@@ -14,17 +14,14 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.drivers.bmv2; 17 +package org.onosproject.bmv2.api.model;
18 18
19 import com.eclipsesource.json.Json; 19 import com.eclipsesource.json.Json;
20 import com.eclipsesource.json.JsonObject; 20 import com.eclipsesource.json.JsonObject;
21 import com.google.common.testing.EqualsTester; 21 import com.google.common.testing.EqualsTester;
22 import org.junit.Before; 22 import org.junit.Before;
23 import org.junit.Test; 23 import org.junit.Test;
24 -import org.onosproject.drivers.bmv2.model.Bmv2Model; 24 +import org.onosproject.bmv2.api.runtime.Bmv2MatchParam;
25 -import org.onosproject.drivers.bmv2.model.Bmv2ModelAction;
26 -import org.onosproject.drivers.bmv2.model.Bmv2ModelHeaderType;
27 -import org.onosproject.drivers.bmv2.model.Bmv2ModelTable;
28 25
29 import java.io.BufferedReader; 26 import java.io.BufferedReader;
30 import java.io.InputStreamReader; 27 import java.io.InputStreamReader;
...@@ -156,7 +153,7 @@ public class Bmv2ModelTest { ...@@ -156,7 +153,7 @@ public class Bmv2ModelTest {
156 153
157 // key match type 154 // key match type
158 assertThat("Incorrect value for table key match type", 155 assertThat("Incorrect value for table key match type",
159 - table0.keys().get(0).matchType(), is(equalTo("ternary"))); 156 + table0.keys().get(0).matchType(), is(equalTo(Bmv2MatchParam.Type.TERNARY)));
160 157
161 // header type 158 // header type
162 assertThat("Incorrect value for table key header type", 159 assertThat("Incorrect value for table key header type",
......
...@@ -21,6 +21,7 @@ import com.google.common.base.Objects; ...@@ -21,6 +21,7 @@ import com.google.common.base.Objects;
21 21
22 import java.nio.ByteBuffer; 22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder; 23 import java.nio.ByteOrder;
24 +import java.util.Arrays;
24 25
25 import static com.google.common.base.Preconditions.checkArgument; 26 import static com.google.common.base.Preconditions.checkArgument;
26 import static org.apache.commons.lang3.ArrayUtils.reverse; 27 import static org.apache.commons.lang3.ArrayUtils.reverse;
...@@ -62,7 +63,7 @@ public final class ImmutableByteSequence { ...@@ -62,7 +63,7 @@ public final class ImmutableByteSequence {
62 * the passed byte array. 63 * the passed byte array.
63 * 64 *
64 * @param original a byte array value 65 * @param original a byte array value
65 - * @return a new immutable byte buffer 66 + * @return a new immutable byte sequence
66 */ 67 */
67 public static ImmutableByteSequence copyFrom(byte[] original) { 68 public static ImmutableByteSequence copyFrom(byte[] original) {
68 checkArgument(original != null && original.length > 0, 69 checkArgument(original != null && original.length > 0,
...@@ -101,7 +102,7 @@ public final class ImmutableByteSequence { ...@@ -101,7 +102,7 @@ public final class ImmutableByteSequence {
101 * Creates a new byte sequence of 8 bytes containing the given long value. 102 * Creates a new byte sequence of 8 bytes containing the given long value.
102 * 103 *
103 * @param original a long value 104 * @param original a long value
104 - * @return a new immutable byte buffer 105 + * @return a new immutable byte sequence
105 */ 106 */
106 public static ImmutableByteSequence copyFrom(long original) { 107 public static ImmutableByteSequence copyFrom(long original) {
107 return new ImmutableByteSequence( 108 return new ImmutableByteSequence(
...@@ -112,7 +113,7 @@ public final class ImmutableByteSequence { ...@@ -112,7 +113,7 @@ public final class ImmutableByteSequence {
112 * Creates a new byte sequence of 4 bytes containing the given int value. 113 * Creates a new byte sequence of 4 bytes containing the given int value.
113 * 114 *
114 * @param original an int value 115 * @param original an int value
115 - * @return a new immutable byte buffer 116 + * @return a new immutable byte sequence
116 */ 117 */
117 public static ImmutableByteSequence copyFrom(int original) { 118 public static ImmutableByteSequence copyFrom(int original) {
118 return new ImmutableByteSequence( 119 return new ImmutableByteSequence(
...@@ -123,7 +124,7 @@ public final class ImmutableByteSequence { ...@@ -123,7 +124,7 @@ public final class ImmutableByteSequence {
123 * Creates a new byte sequence of 2 bytes containing the given short value. 124 * Creates a new byte sequence of 2 bytes containing the given short value.
124 * 125 *
125 * @param original a short value 126 * @param original a short value
126 - * @return a new immutable byte buffer 127 + * @return a new immutable byte sequence
127 */ 128 */
128 public static ImmutableByteSequence copyFrom(short original) { 129 public static ImmutableByteSequence copyFrom(short original) {
129 return new ImmutableByteSequence( 130 return new ImmutableByteSequence(
...@@ -134,7 +135,7 @@ public final class ImmutableByteSequence { ...@@ -134,7 +135,7 @@ public final class ImmutableByteSequence {
134 * Creates a new byte sequence of 1 byte containing the given value. 135 * Creates a new byte sequence of 1 byte containing the given value.
135 * 136 *
136 * @param original a byte value 137 * @param original a byte value
137 - * @return a new immutable byte buffer 138 + * @return a new immutable byte sequence
138 */ 139 */
139 public static ImmutableByteSequence copyFrom(byte original) { 140 public static ImmutableByteSequence copyFrom(byte original) {
140 return new ImmutableByteSequence( 141 return new ImmutableByteSequence(
...@@ -142,6 +143,30 @@ public final class ImmutableByteSequence { ...@@ -142,6 +143,30 @@ public final class ImmutableByteSequence {
142 } 143 }
143 144
144 /** 145 /**
146 + * Creates a new byte sequence of the given size where alla bits are 0.
147 + *
148 + * @param size number of bytes
149 + * @return a new immutable byte sequence
150 + */
151 + public static ImmutableByteSequence ofZeros(int size) {
152 + byte[] bytes = new byte[size];
153 + Arrays.fill(bytes, (byte) 0);
154 + return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
155 + }
156 +
157 + /**
158 + * Creates a new byte sequence of the given size where alla bits are 1.
159 + *
160 + * @param size number of bytes
161 + * @return a new immutable byte sequence
162 + */
163 + public static ImmutableByteSequence ofOnes(int size) {
164 + byte[] bytes = new byte[size];
165 + Arrays.fill(bytes, (byte) 0xFF);
166 + return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
167 + }
168 +
169 + /**
145 * Returns a view of this sequence as a read-only {@link ByteBuffer}. 170 * Returns a view of this sequence as a read-only {@link ByteBuffer}.
146 * <p> 171 * <p>
147 * The returned buffer will have position 0, while limit and capacity will 172 * The returned buffer will have position 0, while limit and capacity will
......