blob: 9817b0a6b5add8788d99c6789886761db4a863f5 [file] [log] [blame]
Phaneendra Manda63fecef2015-10-26 22:21:03 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Phaneendra Manda63fecef2015-10-26 22:21:03 +05303 *
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 */
16package org.onosproject.vtnrsc;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.List;
Phaneendra Manda329a1272016-02-10 22:57:00 +053022import java.util.Map;
Phaneendra Manda63fecef2015-10-26 22:21:03 +053023import java.util.Objects;
Phaneendra Manda329a1272016-02-10 22:57:00 +053024import java.util.concurrent.ConcurrentHashMap;
Phaneendra Manda63fecef2015-10-26 22:21:03 +053025
26import com.google.common.collect.ImmutableList;
Phaneendra Manda0c423422016-04-19 00:31:53 +053027import com.google.common.collect.ImmutableMap;
Phaneendra Manda63fecef2015-10-26 22:21:03 +053028
29/**
30 * Implementation of port pair group.
31 */
32public final class DefaultPortPairGroup implements PortPairGroup {
33
34 private final PortPairGroupId portPairGroupId;
35 private final TenantId tenantId;
36 private final String name;
37 private final String description;
38 private final List<PortPairId> portPairList;
Phaneendra Manda329a1272016-02-10 22:57:00 +053039 private final Map<PortPairId, Integer> portPairLoadMap;
Phaneendra Manda63fecef2015-10-26 22:21:03 +053040
41 /**
42 * Default constructor to create Port Pair Group.
43 *
44 * @param portPairGroupId port pair group id
45 * @param tenantId tenant id
46 * @param name name of port pair group
47 * @param description description of port pair group
48 * @param portPairList list of port pairs
49 */
50 private DefaultPortPairGroup(PortPairGroupId portPairGroupId, TenantId tenantId,
51 String name, String description,
52 List<PortPairId> portPairList) {
53
54 this.portPairGroupId = portPairGroupId;
55 this.tenantId = tenantId;
56 this.name = name;
57 this.description = description;
58 this.portPairList = portPairList;
Phaneendra Manda329a1272016-02-10 22:57:00 +053059 portPairLoadMap = new ConcurrentHashMap<>();
60 for (PortPairId portPairId : portPairList) {
61 portPairLoadMap.put(portPairId, new Integer(0));
62 }
Phaneendra Manda63fecef2015-10-26 22:21:03 +053063 }
64
65 @Override
66 public PortPairGroupId portPairGroupId() {
67 return portPairGroupId;
68 }
69
70 @Override
71 public TenantId tenantId() {
72 return tenantId;
73 }
74
75 @Override
76 public String name() {
77 return name;
78 }
79
80 @Override
81 public String description() {
82 return description;
83 }
84
85 @Override
86 public List<PortPairId> portPairs() {
87 return ImmutableList.copyOf(portPairList);
88 }
89
90 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +053091 public void addLoad(PortPairId portPairId) {
92 int load = portPairLoadMap.get(portPairId);
93 load = load + 1;
94 portPairLoadMap.put(portPairId, new Integer(load));
95 }
96
97 @Override
98 public int getLoad(PortPairId portPairId) {
99 return portPairLoadMap.get(portPairId);
100 }
101
102 @Override
Phaneendra Manda0c423422016-04-19 00:31:53 +0530103 public Map<PortPairId, Integer> portPairLoadMap() {
104 return ImmutableMap.copyOf(portPairLoadMap);
105 }
106
107 @Override
Phaneendra Manda63fecef2015-10-26 22:21:03 +0530108 public int hashCode() {
109 return Objects.hash(portPairGroupId, tenantId, name, description,
110 portPairList);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118 if (obj instanceof DefaultPortPairGroup) {
119 DefaultPortPairGroup that = (DefaultPortPairGroup) obj;
120 return Objects.equals(portPairGroupId, that.portPairGroupId) &&
121 Objects.equals(tenantId, that.tenantId) &&
122 Objects.equals(name, that.name) &&
123 Objects.equals(description, that.description) &&
124 Objects.equals(portPairList, that.portPairList);
125 }
126 return false;
127 }
128
129 @Override
130 public boolean exactMatch(PortPairGroup portPairGroup) {
131 return this.equals(portPairGroup) &&
132 Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
133 Objects.equals(this.tenantId, portPairGroup.tenantId());
134 }
135
136 @Override
137 public String toString() {
138 return toStringHelper(this)
139 .add("id", portPairGroupId.toString())
140 .add("tenantId", tenantId.toString())
141 .add("name", name)
142 .add("description", description)
143 .add("portPairGroupList", portPairList)
144 .toString();
145 }
146
147 /**
148 * To create an instance of the builder.
149 *
150 * @return instance of builder
151 */
152 public static Builder builder() {
153 return new Builder();
154 }
155
156 /**
157 * Builder class for Port pair group.
158 */
159 public static final class Builder implements PortPairGroup.Builder {
160
161 private PortPairGroupId portPairGroupId;
162 private TenantId tenantId;
163 private String name;
164 private String description;
165 private List<PortPairId> portPairList;
166
167 @Override
168 public Builder setId(PortPairGroupId portPairGroupId) {
169 this.portPairGroupId = portPairGroupId;
170 return this;
171 }
172
173 @Override
174 public Builder setTenantId(TenantId tenantId) {
175 this.tenantId = tenantId;
176 return this;
177 }
178
179 @Override
180 public Builder setName(String name) {
181 this.name = name;
182 return this;
183 }
184
185 @Override
186 public Builder setDescription(String description) {
187 this.description = description;
188 return this;
189 }
190
191 @Override
192 public Builder setPortPairs(List<PortPairId> portPairs) {
193 this.portPairList = portPairs;
194 return this;
195 }
196
197 @Override
198 public PortPairGroup build() {
199
200 checkNotNull(portPairGroupId, "Port pair group id cannot be null");
201 checkNotNull(tenantId, "Tenant id cannot be null");
202 checkNotNull(portPairList, "Port pairs cannot be null");
203
204 return new DefaultPortPairGroup(portPairGroupId, tenantId, name, description,
205 portPairList);
206 }
207 }
208}