ed480fb7c68b827482a00bc3874602ea4b6a63b7
[onosfw.git] /
1 /*
2  * Copyright 2015 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  */
16 package org.onosproject.vtnrsc.sfc;
17
18 import static com.google.common.base.MoreObjects.toStringHelper;
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import java.util.Objects;
22
23 import org.onosproject.vtnrsc.TenantId;
24
25 /**
26  * Implementation of port pair.
27  */
28 public final class DefaultPortPair implements PortPair {
29
30     private final PortPairId portPairId;
31     private final TenantId tenantId;
32     private final String name;
33     private final String description;
34     private final String ingress;
35     private final String egress;
36
37     /**
38      * Default constructor to create Port Pair.
39      *
40      * @param portPairId port pair id
41      * @param tenantId tenant id
42      * @param name name of port pair
43      * @param description description of port pair
44      * @param ingress ingress port
45      * @param egress egress port
46      */
47     private DefaultPortPair(PortPairId portPairId, TenantId tenantId,
48                             String name, String description,
49                             String ingress, String egress) {
50
51         this.portPairId = portPairId;
52         this.tenantId = tenantId;
53         this.name = name;
54         this.description = description;
55         this.ingress = ingress;
56         this.egress = egress;
57     }
58
59     @Override
60     public PortPairId portPairId() {
61         return portPairId;
62     }
63
64     @Override
65     public TenantId tenantId() {
66         return tenantId;
67     }
68
69     @Override
70     public String name() {
71         return name;
72     }
73
74     @Override
75     public String description() {
76         return description;
77     }
78
79     @Override
80     public String ingress() {
81         return ingress;
82     }
83
84     @Override
85     public String egress() {
86         return egress;
87     }
88
89     @Override
90     public int hashCode() {
91         return Objects.hash(portPairId, tenantId, name, description,
92                             ingress, egress);
93     }
94
95     @Override
96     public boolean equals(Object obj) {
97         if (this == obj) {
98             return true;
99         }
100         if (obj instanceof DefaultPortPair) {
101             DefaultPortPair that = (DefaultPortPair) obj;
102             return Objects.equals(portPairId, that.portPairId) &&
103                     Objects.equals(tenantId, that.tenantId) &&
104                     Objects.equals(name, that.name) &&
105                     Objects.equals(description, that.description) &&
106                     Objects.equals(ingress, that.ingress) &&
107                     Objects.equals(egress, that.egress);
108         }
109         return false;
110     }
111
112     @Override
113     public boolean exactMatch(PortPair portPair) {
114         return this.equals(portPair) &&
115                 Objects.equals(this.portPairId, portPair.portPairId()) &&
116                 Objects.equals(this.tenantId, portPair.tenantId());
117     }
118
119     @Override
120     public String toString() {
121         return toStringHelper(this)
122                 .add("id", portPairId.toString())
123                 .add("tenantId", tenantId.tenantId())
124                 .add("name", name)
125                 .add("description", description)
126                 .add("ingress", ingress)
127                 .add("egress", egress)
128                 .toString();
129     }
130
131     /**
132      * To create an instance of the builder.
133      *
134      * @return instance of builder
135      */
136     public static Builder builder() {
137         return new Builder();
138     }
139
140     /**
141      * Builder class for Port pair.
142      */
143     public static final class Builder implements PortPair.Builder {
144
145         private PortPairId portPairId;
146         private TenantId tenantId;
147         private String name;
148         private String description;
149         private String ingress;
150         private String egress;
151
152         @Override
153         public Builder setId(PortPairId portPairId) {
154             this.portPairId = portPairId;
155             return this;
156         }
157
158         @Override
159         public Builder setTenantId(TenantId tenantId) {
160             this.tenantId = tenantId;
161             return this;
162         }
163
164         @Override
165         public Builder setName(String name) {
166             this.name = name;
167             return this;
168         }
169
170         @Override
171         public Builder setDescription(String description) {
172             this.description = description;
173             return this;
174         }
175
176         @Override
177         public Builder setIngress(String ingress) {
178             this.ingress = ingress;
179             return this;
180         }
181
182         @Override
183         public Builder setEgress(String egress) {
184             this.egress = egress;
185             return this;
186         }
187
188         @Override
189         public PortPair build() {
190
191             checkNotNull(portPairId, "Port pair id cannot be null");
192             checkNotNull(tenantId, "Tenant id cannot be null");
193             checkNotNull(ingress, "Ingress of a port pair cannot be null");
194             checkNotNull(egress, "Egress of a port pair cannot be null");
195
196             return new DefaultPortPair(portPairId, tenantId, name, description,
197                                        ingress, egress);
198         }
199     }
200 }