blob: e05803c29049ea46b6befde2a64ce2fbfb05fbe7 [file] [log] [blame]
Carmelo Cascone2ea177b2016-02-25 18:38:42 -08001/*
Carmelo Casconeaa8b6292016-04-13 14:27:06 -07002 * Copyright 2016-present Open Networking Laboratory
Carmelo Cascone2ea177b2016-02-25 18:38:42 -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
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070017package org.onosproject.bmv2.api.runtime;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080018
Carmelo Cascone9e39e312016-06-16 14:47:09 -070019import com.google.common.annotations.Beta;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080020import com.google.common.base.MoreObjects;
21import com.google.common.collect.Lists;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070022import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080023
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080024import java.util.Collections;
25import java.util.List;
26import java.util.Objects;
27
28import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070029import static com.google.common.base.Preconditions.checkState;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080030
31/**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070032 * An action of a BMv2 match-action table entry.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080033 */
Carmelo Cascone9e39e312016-06-16 14:47:09 -070034@Beta
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080035public final class Bmv2Action {
36
37 private final String name;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070038 private final List<ImmutableByteSequence> parameters;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080039
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070040 private Bmv2Action(String name, List<ImmutableByteSequence> parameters) {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080041 // hide constructor
42 this.name = name;
43 this.parameters = parameters;
44 }
45
46 /**
47 * Returns a new action builder.
48 */
49 public static Builder builder() {
50 return new Builder();
51 }
52
53 /**
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070054 * Return the name of this action.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080055 *
56 * @return action name
57 */
58 public final String name() {
59 return name;
60 }
61
62 /**
Carmelo Cascone9e39e312016-06-16 14:47:09 -070063 * Returns an immutable view of the list of parameters of this action.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080064 *
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070065 * @return list of byte sequence
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080066 */
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070067 public final List<ImmutableByteSequence> parameters() {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080068 return Collections.unmodifiableList(parameters);
69 }
70
71 @Override
72 public final int hashCode() {
73 return Objects.hash(name, parameters);
74 }
75
76 @Override
77 public final boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81 if (obj == null || getClass() != obj.getClass()) {
82 return false;
83 }
84 final Bmv2Action other = (Bmv2Action) obj;
85 return Objects.equals(this.name, other.name)
86 && Objects.equals(this.parameters, other.parameters);
87 }
88
89 @Override
90 public final String toString() {
91 return MoreObjects.toStringHelper(this)
92 .add("name", name)
93 .add("parameters", parameters)
94 .toString();
95 }
96
97 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070098 * A BMv2 action builder.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080099 */
100 public static final class Builder {
101
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700102 private String name = null;
103 private List<ImmutableByteSequence> parameters;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800104
105 private Builder() {
106 this.parameters = Lists.newArrayList();
107 }
108
109 /**
Carmelo Cascone9e39e312016-06-16 14:47:09 -0700110 * Sets the action name.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800111 *
112 * @param actionName a string value
113 * @return this
114 */
115 public Builder withName(String actionName) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700116 this.name = checkNotNull(actionName);
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800117 return this;
118 }
119
120 /**
Carmelo Cascone9e39e312016-06-16 14:47:09 -0700121 * Adds a parameter at the end of the parameters list.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800122 *
123 * @param parameter a ByteBuffer value
124 * @return this
125 */
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700126 public Builder addParameter(ImmutableByteSequence parameter) {
127 parameters.add(checkNotNull(parameter));
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800128 return this;
129 }
130
131 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -0700132 * Builds a BMv2 action object.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800133 *
Carmelo Cascone17fc9e42016-05-31 11:29:21 -0700134 * @return a BMv2 action
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800135 */
136 public Bmv2Action build() {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700137 checkState(name != null, "action name not set");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800138 return new Bmv2Action(name, parameters);
139 }
140 }
141}