blob: 1d9a94b64a8b9f32114e8a2f47e43521d5adb511 [file] [log] [blame]
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
22import com.google.common.collect.ImmutableSet;
23import com.google.common.collect.Maps;
Carmelo Cascone87892e22017-11-13 16:01:29 -080024import org.onosproject.net.pi.model.PiActionGroupType;
25import org.onosproject.net.pi.model.PiActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040026
27import java.util.Collection;
28import java.util.Map;
29
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080034 * Instance of an action group of a protocol-independent pipeline.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040035 */
36@Beta
37public final class PiActionGroup {
38
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040039 private final PiActionGroupId id;
Carmelo Cascone87892e22017-11-13 16:01:29 -080040 private final PiActionGroupType type;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040041 private final ImmutableSet<PiActionGroupMember> members;
Yi Tseng82512da2017-08-16 19:46:36 -070042 private final PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040043
Carmelo Cascone87892e22017-11-13 16:01:29 -080044 private PiActionGroup(PiActionGroupId id, PiActionGroupType type,
Yi Tseng82512da2017-08-16 19:46:36 -070045 ImmutableSet<PiActionGroupMember> members,
46 PiActionProfileId piActionProfileId) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040047 this.id = id;
48 this.type = type;
49 this.members = members;
Yi Tseng82512da2017-08-16 19:46:36 -070050 this.piActionProfileId = piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040051 }
52
53 /**
54 * Returns the identifier of this action group.
55 *
56 * @return action group identifier
57 */
58 public PiActionGroupId id() {
59 return id;
60 }
61
62 /**
63 * Returns the type of this action group.
64 *
65 * @return action group type
66 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080067 public PiActionGroupType type() {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040068 return type;
69 }
70
71 /**
72 * Returns the members of this action group.
73 *
74 * @return collection of action members.
75 */
76 public Collection<PiActionGroupMember> members() {
77 return members;
78 }
79
Yi Tseng82512da2017-08-16 19:46:36 -070080 /**
81 * Gets identifier of the action profile.
82 *
83 * @return action profile id
84 */
85 public PiActionProfileId actionProfileId() {
86 return piActionProfileId;
87 }
88
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040089 @Override
90 public boolean equals(Object o) {
91 if (this == o) {
92 return true;
93 }
Yi Tseng82512da2017-08-16 19:46:36 -070094 if (o == null || !(o instanceof PiActionGroup)) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040095 return false;
96 }
97 PiActionGroup that = (PiActionGroup) o;
Yi Tseng82512da2017-08-16 19:46:36 -070098 return Objects.equal(id, that.id) &&
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040099 Objects.equal(type, that.type) &&
Yi Tseng82512da2017-08-16 19:46:36 -0700100 Objects.equal(members, that.members) &&
101 Objects.equal(piActionProfileId, that.piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400102 }
103
104 @Override
105 public int hashCode() {
106 return Objects.hashCode(id, type, members);
107 }
108
109 @Override
110 public String toString() {
111 return MoreObjects.toStringHelper(this)
112 .add("groupId", id)
113 .add("type", type)
114 .add("members", members)
Yi Tseng82512da2017-08-16 19:46:36 -0700115 .add("piActionProfileId", piActionProfileId)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400116 .toString();
117 }
118
119 /**
120 * Returns a new builder of action groups.
121 *
122 * @return action group builder
123 */
124 public static Builder builder() {
125 return new Builder();
126 }
127
128 /**
129 * Builder of action groups.
130 */
131 public static final class Builder {
132
133 private PiActionGroupId id;
Carmelo Cascone87892e22017-11-13 16:01:29 -0800134 private PiActionGroupType type;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400135 private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap();
Yi Tseng82512da2017-08-16 19:46:36 -0700136 private PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400137
138 private Builder() {
139 // hides constructor.
140 }
141
142 /**
143 * Sets the identifier of this action group.
144 *
145 * @param id action group identifier
146 * @return this
147 */
148 public Builder withId(PiActionGroupId id) {
149 this.id = id;
150 return this;
151 }
152
153 /**
154 * Sets the type of this action group.
155 *
156 * @param type action group type
157 * @return this
158 */
Carmelo Cascone87892e22017-11-13 16:01:29 -0800159 public Builder withType(PiActionGroupType type) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400160 this.type = type;
161 return this;
162 }
163
164 /**
165 * Adds one member to this action group.
166 *
167 * @param member action group member
168 * @return this
169 */
170 public Builder addMember(PiActionGroupMember member) {
171 members.put(member.id(), member);
172 return this;
173 }
174
175 /**
176 * Adds many members to this action group.
177 *
178 * @param members action group members
179 * @return this
180 */
181 public Builder addMembers(Collection<PiActionGroupMember> members) {
182 members.forEach(this::addMember);
183 return this;
184 }
185
186 /**
Yi Tseng82512da2017-08-16 19:46:36 -0700187 * Sets the identifier of the action profile.
188 *
189 * @param piActionProfileId the identifier of the action profile
190 * @return this
191 */
192 public Builder withActionProfileId(PiActionProfileId piActionProfileId) {
193 this.piActionProfileId = piActionProfileId;
194 return this;
195 }
196
197 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400198 * Creates a new action group.
199 *
200 * @return action group
201 */
202 public PiActionGroup build() {
203 checkNotNull(id);
204 checkNotNull(type);
Yi Tseng82512da2017-08-16 19:46:36 -0700205 checkArgument(!members.isEmpty(), "Members cannot be empty");
206 checkNotNull(piActionProfileId);
207 return new PiActionGroup(id, type,
208 ImmutableSet.copyOf(members.values()),
209 piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400210 }
211 }
212}