blob: 61b0e524f12000b15817e8c7ca2b16089cf152a6 [file] [log] [blame]
Hyunsun Moon395542a2016-09-01 13:53:08 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.opencord.cordvtn.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
21import org.onlab.packet.VlanId;
22
23import java.util.Objects;
24import java.util.Optional;
25import java.util.Set;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Representation of a service port.
31 */
32public final class ServicePort {
33
34 private final PortId id;
35 private final VlanId vlanId;
36 private final Set<AddressPair> addressPairs;
37
38 private ServicePort(PortId id,
39 VlanId vlanId,
40 Set<AddressPair> addressPairs) {
41 this.id = id;
42 this.vlanId = vlanId;
43 this.addressPairs = addressPairs;
44 }
45
46 /**
47 * Returns the port id of the service port.
48 *
49 * @return port id
50 */
51 public PortId id() {
52 return id;
53 }
54
55 /**
56 * Returns the vlan id of the the service port if exists.
57 *
58 * @return vlan id
59 */
60 public Optional<VlanId> vlanId() {
61 return Optional.ofNullable(vlanId);
62 }
63
64 /**
65 * Returns the additional address pairs used in this port.
66 *
67 * @return set of ip and mac address pairs
68 */
69 public Set<AddressPair> addressPairs() {
70 return addressPairs;
71 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (this == obj) {
76 return true;
77 }
78
79 if (obj instanceof ServicePort) {
80 ServicePort that = (ServicePort) obj;
81 if (Objects.equals(id, that.id) &&
82 Objects.equals(vlanId, that.vlanId) &&
83 Objects.equals(addressPairs, that.addressPairs)) {
84 return true;
85 }
86 }
87 return false;
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hash(id, vlanId, addressPairs);
93 }
94
95 @Override
96 public String toString() {
97 return MoreObjects.toStringHelper(getClass())
98 .add("portId", id)
99 .add("vlanId", vlanId)
100 .add("addressPairs", addressPairs)
101 .toString();
102 }
103
104 /**
105 * Returns new service port builder instance.
106 *
107 * @return service port builder
108 */
109 public static Builder builder() {
110 return new Builder();
111 }
112
113 /**
114 * Builder of the service port entities.
115 */
116 public static final class Builder {
117 private PortId id;
118 private VlanId vlanId;
119 private Set<AddressPair> addressPairs = Sets.newHashSet();
120
121 private Builder() {
122 }
123
124 /**
125 * Builds an immutable service port.
126 *
127 * @return service port instance
128 */
129 public ServicePort build() {
130 checkNotNull(id, "ServicePort port id cannot be null");
131 addressPairs = addressPairs == null ? ImmutableSet.of() : addressPairs;
132
133 return new ServicePort(id, vlanId, addressPairs);
134 }
135
136 /**
137 * Returns service port builder with the supplied port port id.
138 *
139 * @param id port id
140 * @return service port builder
141 */
142 public Builder id(PortId id) {
143 this.id = id;
144 return this;
145 }
146
147 /**
148 * Returns service port builder with the supplied VLAN ID.
149 *
150 * @param vlanId vlan id
151 * @return service port builder
152 */
153 public Builder vlanId(VlanId vlanId) {
154 this.vlanId = vlanId;
155 return this;
156 }
157
158 /**
159 * Returns service port builder with the supplied address pairs.
160 *
161 * @param addressPairs set of address pairs
162 * @return service port builder
163 */
164 public Builder addressPairs(Set<AddressPair> addressPairs) {
165 this.addressPairs = addressPairs;
166 return this;
167 }
168
169 /**
170 * Returns service port builder with the given additional address pair.
171 *
172 * @param addressPair address pair to add
173 * @return service port builder
174 */
175 public Builder addAddressPair(AddressPair addressPair) {
176 checkNotNull(addressPair, "ServicePort address pair cannot be null");
177
178 this.addressPairs.add(addressPair);
179 return this;
180 }
181 }
182}