blob: 1c5ecb30036d065dc07b6b7f987bd714a53a77fd [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;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080026 * Instance of a member of an action group in a protocol-independent pipeline.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040027 */
28@Beta
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080029public final class PiActionGroupMember implements PiEntity {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040030
31 private final PiActionGroupMemberId id;
32 private final PiAction action;
33 private final int weight;
34
35 private PiActionGroupMember(PiActionGroupMemberId id, PiAction action, int weight) {
36 this.id = id;
37 this.action = action;
38 this.weight = weight;
39 }
40
41 /**
42 * Returns the identifier of this member.
43 *
44 * @return member identifier
45 */
46 public PiActionGroupMemberId id() {
47 return id;
48 }
49
50 /**
51 * Returns the action associated to this member.
52 *
53 * @return action
54 */
55 public PiAction action() {
56 return action;
57 }
58
59 /**
Andrea Campanella8bcd5862017-12-11 11:34:45 +010060 * Returns the weight associated to this member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040061 *
62 * @return weight
63 */
64 public int weight() {
65 return weight;
66 }
67
68 @Override
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080069 public PiEntityType piEntityType() {
70 return PiEntityType.GROUP_MEMBER;
71 }
72
73 @Override
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040074 public boolean equals(Object o) {
75 if (this == o) {
76 return true;
77 }
78 if (!(o instanceof PiActionGroupMember)) {
79 return false;
80 }
81 PiActionGroupMember that = (PiActionGroupMember) o;
82 return weight == that.weight &&
83 Objects.equal(id, that.id) &&
84 Objects.equal(action, that.action);
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hashCode(id, action, weight);
90 }
91
92 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(this)
95 .add("id", id)
96 .add("action", action)
97 .add("weight", weight)
98 .toString();
99 }
100
101 /**
102 * Returns a new builder of action group members.
103 *
104 * @return member builder
105 */
106 public static Builder builder() {
107 return new Builder();
108 }
109
110 /**
111 * Builder of action group members.
112 */
113 public static final class Builder {
114
115 private PiActionGroupMemberId id;
116 private PiAction action;
117 private int weight;
118
119 private Builder() {
120 // Hides constructor.
121 }
122
123 /**
124 * Sets the identifier of this member.
125 *
126 * @param id member identifier
127 * @return this
128 */
129 public Builder withId(PiActionGroupMemberId id) {
130 this.id = id;
131 return this;
132 }
133
134 /**
135 * Sets the action of this member.
136 *
137 * @param action action
138 * @return this
139 */
140 public Builder withAction(PiAction action) {
141 this.action = action;
142 return this;
143 }
144
145 /**
Andrea Campanella8bcd5862017-12-11 11:34:45 +0100146 * Sets the weight of this member.
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400147 * <p>
148 * Default value is 0.
149 *
150 * @param weight weight
151 * @return this
152 */
153 public Builder withWeight(int weight) {
154 this.weight = weight;
155 return this;
156 }
157
158 /**
159 * Creates a new action group member.
160 *
161 * @return action group member
162 */
163 public PiActionGroupMember build() {
164 checkNotNull(id);
165 checkNotNull(action);
166 return new PiActionGroupMember(id, action, weight);
167 }
168 }
169}