blob: 9084bea64e5eacf7d540dc7c0b930306b2d34c17 [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
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080037public final class PiActionGroup implements PiEntity {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040038
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
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -0800128 @Override
129 public PiEntityType piEntityType() {
130 return PiEntityType.GROUP;
131 }
132
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400133 /**
134 * Builder of action groups.
135 */
136 public static final class Builder {
137
138 private PiActionGroupId id;
Carmelo Cascone87892e22017-11-13 16:01:29 -0800139 private PiActionGroupType type;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400140 private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap();
Yi Tseng82512da2017-08-16 19:46:36 -0700141 private PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400142
143 private Builder() {
144 // hides constructor.
145 }
146
147 /**
148 * Sets the identifier of this action group.
149 *
150 * @param id action group identifier
151 * @return this
152 */
153 public Builder withId(PiActionGroupId id) {
154 this.id = id;
155 return this;
156 }
157
158 /**
159 * Sets the type of this action group.
160 *
161 * @param type action group type
162 * @return this
163 */
Carmelo Cascone87892e22017-11-13 16:01:29 -0800164 public Builder withType(PiActionGroupType type) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400165 this.type = type;
166 return this;
167 }
168
169 /**
170 * Adds one member to this action group.
171 *
172 * @param member action group member
173 * @return this
174 */
175 public Builder addMember(PiActionGroupMember member) {
176 members.put(member.id(), member);
177 return this;
178 }
179
180 /**
181 * Adds many members to this action group.
182 *
183 * @param members action group members
184 * @return this
185 */
186 public Builder addMembers(Collection<PiActionGroupMember> members) {
187 members.forEach(this::addMember);
188 return this;
189 }
190
191 /**
Yi Tseng82512da2017-08-16 19:46:36 -0700192 * Sets the identifier of the action profile.
193 *
194 * @param piActionProfileId the identifier of the action profile
195 * @return this
196 */
197 public Builder withActionProfileId(PiActionProfileId piActionProfileId) {
198 this.piActionProfileId = piActionProfileId;
199 return this;
200 }
201
202 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400203 * Creates a new action group.
204 *
205 * @return action group
206 */
207 public PiActionGroup build() {
208 checkNotNull(id);
209 checkNotNull(type);
Yi Tseng82512da2017-08-16 19:46:36 -0700210 checkArgument(!members.isEmpty(), "Members cannot be empty");
211 checkNotNull(piActionProfileId);
212 return new PiActionGroup(id, type,
213 ImmutableSet.copyOf(members.values()),
214 piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400215 }
216 }
217}