blob: dd90f9bd4bd9429fe9a76852c25bd3e5460c42ce [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.PiActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040025
26import java.util.Collection;
27import java.util.Map;
28
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080033 * Instance of an action group of a protocol-independent pipeline.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040034 */
35@Beta
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080036public final class PiActionGroup implements PiEntity {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040037
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040038 private final PiActionGroupId id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040039 private final ImmutableSet<PiActionGroupMember> members;
Yi Tseng82512da2017-08-16 19:46:36 -070040 private final PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040041
Andrea Campanella8bcd5862017-12-11 11:34:45 +010042 private PiActionGroup(PiActionGroupId id, ImmutableSet<PiActionGroupMember> members,
Yi Tseng82512da2017-08-16 19:46:36 -070043 PiActionProfileId piActionProfileId) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040044 this.id = id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040045 this.members = members;
Yi Tseng82512da2017-08-16 19:46:36 -070046 this.piActionProfileId = piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040047 }
48
49 /**
50 * Returns the identifier of this action group.
51 *
52 * @return action group identifier
53 */
54 public PiActionGroupId id() {
55 return id;
56 }
57
58 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040059 * Returns the members of this action group.
60 *
61 * @return collection of action members.
62 */
63 public Collection<PiActionGroupMember> members() {
64 return members;
65 }
66
Yi Tseng82512da2017-08-16 19:46:36 -070067 /**
68 * Gets identifier of the action profile.
69 *
70 * @return action profile id
71 */
72 public PiActionProfileId actionProfileId() {
73 return piActionProfileId;
74 }
75
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040076 @Override
77 public boolean equals(Object o) {
78 if (this == o) {
79 return true;
80 }
Yi Tseng82512da2017-08-16 19:46:36 -070081 if (o == null || !(o instanceof PiActionGroup)) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040082 return false;
83 }
84 PiActionGroup that = (PiActionGroup) o;
Yi Tseng82512da2017-08-16 19:46:36 -070085 return Objects.equal(id, that.id) &&
Yi Tseng82512da2017-08-16 19:46:36 -070086 Objects.equal(members, that.members) &&
87 Objects.equal(piActionProfileId, that.piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040088 }
89
90 @Override
91 public int hashCode() {
Andrea Campanella8bcd5862017-12-11 11:34:45 +010092 return Objects.hashCode(id, members);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040093 }
94
95 @Override
96 public String toString() {
97 return MoreObjects.toStringHelper(this)
98 .add("groupId", id)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040099 .add("members", members)
Yi Tseng82512da2017-08-16 19:46:36 -0700100 .add("piActionProfileId", piActionProfileId)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400101 .toString();
102 }
103
104 /**
105 * Returns a new builder of action groups.
106 *
107 * @return action group builder
108 */
109 public static Builder builder() {
110 return new Builder();
111 }
112
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -0800113 @Override
114 public PiEntityType piEntityType() {
115 return PiEntityType.GROUP;
116 }
117
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400118 /**
119 * Builder of action groups.
120 */
121 public static final class Builder {
122
123 private PiActionGroupId id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400124 private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap();
Yi Tseng82512da2017-08-16 19:46:36 -0700125 private PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400126
127 private Builder() {
128 // hides constructor.
129 }
130
131 /**
132 * Sets the identifier of this action group.
133 *
134 * @param id action group identifier
135 * @return this
136 */
137 public Builder withId(PiActionGroupId id) {
138 this.id = id;
139 return this;
140 }
141
142 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400143 * Adds one member to this action group.
144 *
145 * @param member action group member
146 * @return this
147 */
148 public Builder addMember(PiActionGroupMember member) {
149 members.put(member.id(), member);
150 return this;
151 }
152
153 /**
154 * Adds many members to this action group.
155 *
156 * @param members action group members
157 * @return this
158 */
159 public Builder addMembers(Collection<PiActionGroupMember> members) {
160 members.forEach(this::addMember);
161 return this;
162 }
163
164 /**
Yi Tseng82512da2017-08-16 19:46:36 -0700165 * Sets the identifier of the action profile.
166 *
167 * @param piActionProfileId the identifier of the action profile
168 * @return this
169 */
170 public Builder withActionProfileId(PiActionProfileId piActionProfileId) {
171 this.piActionProfileId = piActionProfileId;
172 return this;
173 }
174
175 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400176 * Creates a new action group.
177 *
178 * @return action group
179 */
180 public PiActionGroup build() {
181 checkNotNull(id);
Yi Tseng82512da2017-08-16 19:46:36 -0700182 checkArgument(!members.isEmpty(), "Members cannot be empty");
183 checkNotNull(piActionProfileId);
Andrea Campanella8bcd5862017-12-11 11:34:45 +0100184 return new PiActionGroup(id, ImmutableSet.copyOf(members.values()),
Yi Tseng82512da2017-08-16 19:46:36 -0700185 piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400186 }
187 }
188}