blob: a22e0394240f3ac9ec35f9d143168e90033fbed9 [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
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040029import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080032 * Instance of an action group of a protocol-independent pipeline.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040033 */
34@Beta
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -080035public final class PiActionGroup implements PiEntity {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040036
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040037 private final PiActionGroupId id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040038 private final ImmutableSet<PiActionGroupMember> members;
Yi Tseng82512da2017-08-16 19:46:36 -070039 private final PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040040
Andrea Campanella8bcd5862017-12-11 11:34:45 +010041 private PiActionGroup(PiActionGroupId id, ImmutableSet<PiActionGroupMember> members,
Yi Tseng82512da2017-08-16 19:46:36 -070042 PiActionProfileId piActionProfileId) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040043 this.id = id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040044 this.members = members;
Yi Tseng82512da2017-08-16 19:46:36 -070045 this.piActionProfileId = piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040046 }
47
48 /**
49 * Returns the identifier of this action group.
50 *
51 * @return action group identifier
52 */
53 public PiActionGroupId id() {
54 return id;
55 }
56
57 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040058 * Returns the members of this action group.
59 *
60 * @return collection of action members.
61 */
62 public Collection<PiActionGroupMember> members() {
63 return members;
64 }
65
Yi Tseng82512da2017-08-16 19:46:36 -070066 /**
67 * Gets identifier of the action profile.
68 *
69 * @return action profile id
70 */
71 public PiActionProfileId actionProfileId() {
72 return piActionProfileId;
73 }
74
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040075 @Override
76 public boolean equals(Object o) {
77 if (this == o) {
78 return true;
79 }
Yi Tseng82512da2017-08-16 19:46:36 -070080 if (o == null || !(o instanceof PiActionGroup)) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040081 return false;
82 }
83 PiActionGroup that = (PiActionGroup) o;
Yi Tseng82512da2017-08-16 19:46:36 -070084 return Objects.equal(id, that.id) &&
Yi Tseng82512da2017-08-16 19:46:36 -070085 Objects.equal(members, that.members) &&
86 Objects.equal(piActionProfileId, that.piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040087 }
88
89 @Override
90 public int hashCode() {
Andrea Campanella8bcd5862017-12-11 11:34:45 +010091 return Objects.hashCode(id, members);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040092 }
93
94 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(this)
97 .add("groupId", id)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040098 .add("members", members)
Yi Tseng82512da2017-08-16 19:46:36 -070099 .add("piActionProfileId", piActionProfileId)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400100 .toString();
101 }
102
103 /**
104 * Returns a new builder of action groups.
105 *
106 * @return action group builder
107 */
108 public static Builder builder() {
109 return new Builder();
110 }
111
Carmelo Cascone1a7e4f92017-11-20 23:04:02 -0800112 @Override
113 public PiEntityType piEntityType() {
114 return PiEntityType.GROUP;
115 }
116
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400117 /**
118 * Builder of action groups.
119 */
120 public static final class Builder {
121
122 private PiActionGroupId id;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400123 private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap();
Yi Tseng82512da2017-08-16 19:46:36 -0700124 private PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400125
126 private Builder() {
127 // hides constructor.
128 }
129
130 /**
131 * Sets the identifier of this action group.
132 *
133 * @param id action group identifier
134 * @return this
135 */
136 public Builder withId(PiActionGroupId id) {
137 this.id = id;
138 return this;
139 }
140
141 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400142 * Adds one member to this action group.
143 *
144 * @param member action group member
145 * @return this
146 */
147 public Builder addMember(PiActionGroupMember member) {
148 members.put(member.id(), member);
149 return this;
150 }
151
152 /**
153 * Adds many members to this action group.
154 *
155 * @param members action group members
156 * @return this
157 */
158 public Builder addMembers(Collection<PiActionGroupMember> members) {
159 members.forEach(this::addMember);
160 return this;
161 }
162
163 /**
Yi Tseng82512da2017-08-16 19:46:36 -0700164 * Sets the identifier of the action profile.
165 *
166 * @param piActionProfileId the identifier of the action profile
167 * @return this
168 */
169 public Builder withActionProfileId(PiActionProfileId piActionProfileId) {
170 this.piActionProfileId = piActionProfileId;
171 return this;
172 }
173
174 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400175 * Creates a new action group.
176 *
177 * @return action group
178 */
179 public PiActionGroup build() {
180 checkNotNull(id);
Yi Tseng82512da2017-08-16 19:46:36 -0700181 checkNotNull(piActionProfileId);
Andrea Campanella8bcd5862017-12-11 11:34:45 +0100182 return new PiActionGroup(id, ImmutableSet.copyOf(members.values()),
Yi Tseng82512da2017-08-16 19:46:36 -0700183 piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400184 }
185 }
186}