blob: 96168b9375f84a6881a79e1988a1a74109bffadf [file] [log] [blame]
Carmelo Casconef2b7bfe2018-09-11 15:17:23 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
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;
24import org.onosproject.net.pi.model.PiActionProfileId;
25
26import java.util.Collection;
27import java.util.Map;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Instance of an action profile group of a protocol-independent pipeline.
33 */
34@Beta
35public final class PiActionProfileGroup implements PiEntity {
36
37 private final PiActionProfileGroupId id;
38 private final ImmutableSet<PiActionProfileMember> members;
39 private final PiActionProfileId actionProfileId;
40
41 private PiActionProfileGroup(PiActionProfileGroupId id,
42 ImmutableSet<PiActionProfileMember> members,
43 PiActionProfileId actionProfileId) {
44 this.id = id;
45 this.members = members;
46 this.actionProfileId = actionProfileId;
47 }
48
49 /**
50 * Returns the identifier of this action profile group.
51 *
52 * @return action profile group identifier
53 */
54 public PiActionProfileGroupId id() {
55 return id;
56 }
57
58 /**
59 * Returns the members of this action profile group.
60 *
61 * @return collection of action profile members.
62 */
63 public Collection<PiActionProfileMember> members() {
64 return members;
65 }
66
67 /**
68 * Gets identifier of the action profile.
69 *
70 * @return action profile id
71 */
72 public PiActionProfileId actionProfileId() {
73 return actionProfileId;
74 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (this == o) {
79 return true;
80 }
81 if (o == null || !(o instanceof PiActionProfileGroup)) {
82 return false;
83 }
84 PiActionProfileGroup that = (PiActionProfileGroup) o;
85 return Objects.equal(id, that.id) &&
86 Objects.equal(members, that.members) &&
87 Objects.equal(actionProfileId, that.actionProfileId);
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hashCode(id, members);
93 }
94
95 @Override
96 public String toString() {
97 return MoreObjects.toStringHelper(this)
98 .add("groupId", id)
99 .add("members", members)
100 .add("piActionProfileId", actionProfileId)
101 .toString();
102 }
103
104 /**
105 * Returns a new builder of action profile groups.
106 *
107 * @return action profile group builder
108 */
109 public static Builder builder() {
110 return new Builder();
111 }
112
113 @Override
114 public PiEntityType piEntityType() {
115 return PiEntityType.ACTION_PROFILE_GROUP;
116 }
117
118 /**
119 * Builder of action profile groups.
120 */
121 public static final class Builder {
122
123 private PiActionProfileGroupId id;
124 private Map<PiActionProfileMemberId, PiActionProfileMember> members = Maps.newHashMap();
125 private PiActionProfileId piActionProfileId;
126
127 private Builder() {
128 // hides constructor.
129 }
130
131 /**
132 * Sets the identifier of this action profile group.
133 *
134 * @param id action profile group identifier
135 * @return this
136 */
137 public Builder withId(PiActionProfileGroupId id) {
138 this.id = id;
139 return this;
140 }
141
142 /**
143 * Adds one member to this action profile group.
144 *
145 * @param member action profile member
146 * @return this
147 */
148 public Builder addMember(PiActionProfileMember member) {
149 members.put(member.id(), member);
150 return this;
151 }
152
153 /**
154 * Adds many members to this action profile group.
155 *
156 * @param members action profile members
157 * @return this
158 */
159 public Builder addMembers(Collection<PiActionProfileMember> members) {
160 members.forEach(this::addMember);
161 return this;
162 }
163
164 /**
165 * 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 /**
176 * Creates a new action profile group.
177 *
178 * @return action profile group
179 */
180 public PiActionProfileGroup build() {
181 checkNotNull(id);
182 checkNotNull(piActionProfileId);
183 return new PiActionProfileGroup(
184 id, ImmutableSet.copyOf(members.values()), piActionProfileId);
185 }
186 }
187}