Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 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 | package org.onosproject.vtnrsc; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.List; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 22 | import java.util.Map; |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 23 | import java.util.Objects; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 24 | import java.util.concurrent.ConcurrentHashMap; |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 25 | |
| 26 | import com.google.common.collect.ImmutableList; |
Phaneendra Manda | 0c42342 | 2016-04-19 00:31:53 +0530 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableMap; |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Implementation of port pair group. |
| 31 | */ |
| 32 | public 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 Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 39 | private final Map<PortPairId, Integer> portPairLoadMap; |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 40 | |
| 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 Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 59 | portPairLoadMap = new ConcurrentHashMap<>(); |
| 60 | for (PortPairId portPairId : portPairList) { |
| 61 | portPairLoadMap.put(portPairId, new Integer(0)); |
| 62 | } |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 63 | } |
| 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 Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 91 | 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 |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame^] | 98 | public void resetLoad() { |
| 99 | for (PortPairId portPairId : portPairList) { |
| 100 | portPairLoadMap.put(portPairId, new Integer(0)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 105 | public int getLoad(PortPairId portPairId) { |
| 106 | return portPairLoadMap.get(portPairId); |
| 107 | } |
| 108 | |
| 109 | @Override |
Phaneendra Manda | 0c42342 | 2016-04-19 00:31:53 +0530 | [diff] [blame] | 110 | public Map<PortPairId, Integer> portPairLoadMap() { |
| 111 | return ImmutableMap.copyOf(portPairLoadMap); |
| 112 | } |
| 113 | |
| 114 | @Override |
Phaneendra Manda | 63fecef | 2015-10-26 22:21:03 +0530 | [diff] [blame] | 115 | public int hashCode() { |
| 116 | return Objects.hash(portPairGroupId, tenantId, name, description, |
| 117 | portPairList); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean equals(Object obj) { |
| 122 | if (this == obj) { |
| 123 | return true; |
| 124 | } |
| 125 | if (obj instanceof DefaultPortPairGroup) { |
| 126 | DefaultPortPairGroup that = (DefaultPortPairGroup) obj; |
| 127 | return Objects.equals(portPairGroupId, that.portPairGroupId) && |
| 128 | Objects.equals(tenantId, that.tenantId) && |
| 129 | Objects.equals(name, that.name) && |
| 130 | Objects.equals(description, that.description) && |
| 131 | Objects.equals(portPairList, that.portPairList); |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public boolean exactMatch(PortPairGroup portPairGroup) { |
| 138 | return this.equals(portPairGroup) && |
| 139 | Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) && |
| 140 | Objects.equals(this.tenantId, portPairGroup.tenantId()); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
| 145 | return toStringHelper(this) |
| 146 | .add("id", portPairGroupId.toString()) |
| 147 | .add("tenantId", tenantId.toString()) |
| 148 | .add("name", name) |
| 149 | .add("description", description) |
| 150 | .add("portPairGroupList", portPairList) |
| 151 | .toString(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * To create an instance of the builder. |
| 156 | * |
| 157 | * @return instance of builder |
| 158 | */ |
| 159 | public static Builder builder() { |
| 160 | return new Builder(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Builder class for Port pair group. |
| 165 | */ |
| 166 | public static final class Builder implements PortPairGroup.Builder { |
| 167 | |
| 168 | private PortPairGroupId portPairGroupId; |
| 169 | private TenantId tenantId; |
| 170 | private String name; |
| 171 | private String description; |
| 172 | private List<PortPairId> portPairList; |
| 173 | |
| 174 | @Override |
| 175 | public Builder setId(PortPairGroupId portPairGroupId) { |
| 176 | this.portPairGroupId = portPairGroupId; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Builder setTenantId(TenantId tenantId) { |
| 182 | this.tenantId = tenantId; |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Builder setName(String name) { |
| 188 | this.name = name; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Builder setDescription(String description) { |
| 194 | this.description = description; |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Builder setPortPairs(List<PortPairId> portPairs) { |
| 200 | this.portPairList = portPairs; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public PortPairGroup build() { |
| 206 | |
| 207 | checkNotNull(portPairGroupId, "Port pair group id cannot be null"); |
| 208 | checkNotNull(tenantId, "Tenant id cannot be null"); |
| 209 | checkNotNull(portPairList, "Port pairs cannot be null"); |
| 210 | |
| 211 | return new DefaultPortPairGroup(portPairGroupId, tenantId, name, description, |
| 212 | portPairList); |
| 213 | } |
| 214 | } |
| 215 | } |