blob: 78f41a433f046eaeb0d4ad21c6543561735bc004 [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;
27
28/**
29 * Implementation of port pair group.
30 */
31public final class DefaultPortPairGroup implements PortPairGroup {
32
33 private final PortPairGroupId portPairGroupId;
34 private final TenantId tenantId;
35 private final String name;
36 private final String description;
37 private final List<PortPairId> portPairList;
Phaneendra Manda329a1272016-02-10 22:57:00 +053038 private final Map<PortPairId, Integer> portPairLoadMap;
Phaneendra Manda63fecef2015-10-26 22:21:03 +053039
40 /**
41 * Default constructor to create Port Pair Group.
42 *
43 * @param portPairGroupId port pair group id
44 * @param tenantId tenant id
45 * @param name name of port pair group
46 * @param description description of port pair group
47 * @param portPairList list of port pairs
48 */
49 private DefaultPortPairGroup(PortPairGroupId portPairGroupId, TenantId tenantId,
50 String name, String description,
51 List<PortPairId> portPairList) {
52
53 this.portPairGroupId = portPairGroupId;
54 this.tenantId = tenantId;
55 this.name = name;
56 this.description = description;
57 this.portPairList = portPairList;
Phaneendra Manda329a1272016-02-10 22:57:00 +053058 portPairLoadMap = new ConcurrentHashMap<>();
59 for (PortPairId portPairId : portPairList) {
60 portPairLoadMap.put(portPairId, new Integer(0));
61 }
Phaneendra Manda63fecef2015-10-26 22:21:03 +053062 }
63
64 @Override
65 public PortPairGroupId portPairGroupId() {
66 return portPairGroupId;
67 }
68
69 @Override
70 public TenantId tenantId() {
71 return tenantId;
72 }
73
74 @Override
75 public String name() {
76 return name;
77 }
78
79 @Override
80 public String description() {
81 return description;
82 }
83
84 @Override
85 public List<PortPairId> portPairs() {
86 return ImmutableList.copyOf(portPairList);
87 }
88
89 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +053090 public void addLoad(PortPairId portPairId) {
91 int load = portPairLoadMap.get(portPairId);
92 load = load + 1;
93 portPairLoadMap.put(portPairId, new Integer(load));
94 }
95
96 @Override
97 public int getLoad(PortPairId portPairId) {
98 return portPairLoadMap.get(portPairId);
99 }
100
101 @Override
Phaneendra Manda63fecef2015-10-26 22:21:03 +0530102 public int hashCode() {
103 return Objects.hash(portPairGroupId, tenantId, name, description,
104 portPairList);
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112 if (obj instanceof DefaultPortPairGroup) {
113 DefaultPortPairGroup that = (DefaultPortPairGroup) obj;
114 return Objects.equals(portPairGroupId, that.portPairGroupId) &&
115 Objects.equals(tenantId, that.tenantId) &&
116 Objects.equals(name, that.name) &&
117 Objects.equals(description, that.description) &&
118 Objects.equals(portPairList, that.portPairList);
119 }
120 return false;
121 }
122
123 @Override
124 public boolean exactMatch(PortPairGroup portPairGroup) {
125 return this.equals(portPairGroup) &&
126 Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
127 Objects.equals(this.tenantId, portPairGroup.tenantId());
128 }
129
130 @Override
131 public String toString() {
132 return toStringHelper(this)
133 .add("id", portPairGroupId.toString())
134 .add("tenantId", tenantId.toString())
135 .add("name", name)
136 .add("description", description)
137 .add("portPairGroupList", portPairList)
138 .toString();
139 }
140
141 /**
142 * To create an instance of the builder.
143 *
144 * @return instance of builder
145 */
146 public static Builder builder() {
147 return new Builder();
148 }
149
150 /**
151 * Builder class for Port pair group.
152 */
153 public static final class Builder implements PortPairGroup.Builder {
154
155 private PortPairGroupId portPairGroupId;
156 private TenantId tenantId;
157 private String name;
158 private String description;
159 private List<PortPairId> portPairList;
160
161 @Override
162 public Builder setId(PortPairGroupId portPairGroupId) {
163 this.portPairGroupId = portPairGroupId;
164 return this;
165 }
166
167 @Override
168 public Builder setTenantId(TenantId tenantId) {
169 this.tenantId = tenantId;
170 return this;
171 }
172
173 @Override
174 public Builder setName(String name) {
175 this.name = name;
176 return this;
177 }
178
179 @Override
180 public Builder setDescription(String description) {
181 this.description = description;
182 return this;
183 }
184
185 @Override
186 public Builder setPortPairs(List<PortPairId> portPairs) {
187 this.portPairList = portPairs;
188 return this;
189 }
190
191 @Override
192 public PortPairGroup build() {
193
194 checkNotNull(portPairGroupId, "Port pair group id cannot be null");
195 checkNotNull(tenantId, "Tenant id cannot be null");
196 checkNotNull(portPairList, "Port pairs cannot be null");
197
198 return new DefaultPortPairGroup(portPairGroupId, tenantId, name, description,
199 portPairList);
200 }
201 }
202}