blob: 9b2a209baf96608df398781ca162dac94bd7f4ae [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
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Lists;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070021import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080022
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080023import java.util.Collections;
24import java.util.List;
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070028import static com.google.common.base.Preconditions.checkState;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080029
30/**
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070031 * Bmv2 action representation.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080032 */
33public final class Bmv2Action {
34
35 private final String name;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070036 private final List<ImmutableByteSequence> parameters;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080037
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070038 private Bmv2Action(String name, List<ImmutableByteSequence> parameters) {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080039 // hide constructor
40 this.name = name;
41 this.parameters = parameters;
42 }
43
44 /**
45 * Returns a new action builder.
46 */
47 public static Builder builder() {
48 return new Builder();
49 }
50
51 /**
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070052 * Return the name of this action.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080053 *
54 * @return action name
55 */
56 public final String name() {
57 return name;
58 }
59
60 /**
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070061 * Returns an immutable view of the ordered list of parameters of this
62 * action.
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080063 *
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070064 * @return list of byte sequence
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080065 */
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070066 public final List<ImmutableByteSequence> parameters() {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080067 return Collections.unmodifiableList(parameters);
68 }
69
70 @Override
71 public final int hashCode() {
72 return Objects.hash(name, parameters);
73 }
74
75 @Override
76 public final 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 Bmv2Action other = (Bmv2Action) obj;
84 return Objects.equals(this.name, other.name)
85 && Objects.equals(this.parameters, other.parameters);
86 }
87
88 @Override
89 public final String toString() {
90 return MoreObjects.toStringHelper(this)
91 .add("name", name)
92 .add("parameters", parameters)
93 .toString();
94 }
95
96 /**
97 * A Bmv2 action builder.
98 */
99 public static final class Builder {
100
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700101 private String name = null;
102 private List<ImmutableByteSequence> parameters;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800103
104 private Builder() {
105 this.parameters = Lists.newArrayList();
106 }
107
108 /**
109 * Set the action name.
110 *
111 * @param actionName a string value
112 * @return this
113 */
114 public Builder withName(String actionName) {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700115 this.name = checkNotNull(actionName);
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800116 return this;
117 }
118
119 /**
120 * Add a parameter at the end of the parameters list.
121 *
122 * @param parameter a ByteBuffer value
123 * @return this
124 */
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700125 public Builder addParameter(ImmutableByteSequence parameter) {
126 parameters.add(checkNotNull(parameter));
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800127 return this;
128 }
129
130 /**
131 * Builds a Bmv2 action object.
132 *
133 * @return a Bmv2 action
134 */
135 public Bmv2Action build() {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700136 checkState(name != null, "action name not set");
Carmelo Cascone2ea177b2016-02-25 18:38:42 -0800137 return new Bmv2Action(name, parameters);
138 }
139 }
140}