blob: af086a250a26b48bb8fd4d83629628082f366054 [file] [log] [blame]
Jimo Jung14e87bf2018-09-03 16:28:13 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.onosproject.openstackvtap.impl;
17
18import org.onlab.packet.IpAddress;
19import org.onosproject.net.AbstractDescription;
20import org.onosproject.net.DefaultAnnotations;
21import org.onosproject.net.SparseAnnotations;
22import org.onosproject.openstackvtap.api.OpenstackVtapNetwork;
23
24import java.util.Objects;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Default implementation of an immutable OpenstackVtapNetwork.
31 */
32public class DefaultOpenstackVtapNetwork extends AbstractDescription implements OpenstackVtapNetwork {
33
34 private final Mode mode;
35 private final Integer networkId;
36 private final IpAddress serverIp;
37
38 /**
39 * Creates an DefaultOpenstackVtapNetwork using the supplied information.
40 *
41 * @param mode mode of vtap network
42 * @param networkId network id of the vtap tunneling network
43 * @param serverIp server IP address used for tunneling
44 * @param annotations optional key/value annotations
45 */
46 protected DefaultOpenstackVtapNetwork(Mode mode,
47 Integer networkId,
48 IpAddress serverIp,
49 SparseAnnotations... annotations) {
50 super(annotations);
51 this.mode = checkNotNull(mode);
52 this.networkId = networkId;
53 this.serverIp = checkNotNull(serverIp);
54 }
55
56 @Override
57 public Mode mode() {
58 return mode;
59 }
60
61 @Override
62 public Integer networkId() {
63 return networkId;
64 }
65
66 @Override
67 public IpAddress serverIp() {
68 return serverIp;
69 }
70
71 @Override
72 public int hashCode() {
73 return Objects.hash(mode, networkId, serverIp);
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81 if (obj instanceof DefaultOpenstackVtapNetwork) {
82 final DefaultOpenstackVtapNetwork other = (DefaultOpenstackVtapNetwork) obj;
83 return Objects.equals(this.mode, other.mode) &&
84 Objects.equals(this.networkId, other.networkId) &&
85 Objects.equals(this.serverIp, other.serverIp) &&
86 Objects.equals(this.annotations(), other.annotations());
87 }
88 return false;
89 }
90
91 @Override
92 public String toString() {
93 return toStringHelper(this)
94 .add("mode", mode())
95 .add("networkId", networkId())
96 .add("serverIp", serverIp())
97 .add("annotations", annotations())
98 .toString();
99 }
100
101 /**
102 * Creates OpenstackVtapNetwork builder with default parameters.
103 *
104 * @return builder
105 */
106 public static Builder builder() {
107 return new Builder();
108 }
109
110 /**
111 * Creates OpenstackVtapNetwork builder inheriting with default parameters,
112 * from specified OpenstackVtapNetwork.
113 *
114 * @param vtapNetwork to inherit default from
115 * @return builder
116 */
117 public static Builder builder(OpenstackVtapNetwork vtapNetwork) {
118 return new Builder(vtapNetwork);
119 }
120
121 /**
122 * Builder for DefaultOpenstackVtapNetwork object.
123 */
124 public static class Builder implements OpenstackVtapNetwork.Builder {
125 private Mode mode;
126 private Integer networkId;
127 private IpAddress serverIp;
128 private SparseAnnotations annotations = DefaultAnnotations.EMPTY;
129
130 // private constructor not intended to use from external
131 private Builder() {
132 }
133
134 Builder(OpenstackVtapNetwork description) {
135 this.mode = description.mode();
136 this.networkId = description.networkId();
137 this.serverIp = description.serverIp();
138 this.annotations = (SparseAnnotations) description.annotations();
139 }
140
141 /**
142 * Sets mandatory field mode.
143 *
144 * @param mode of vtap network
145 * @return self
146 */
147 @Override
148 public Builder mode(Mode mode) {
149 this.mode = mode;
150 return this;
151 }
152
153 /**
154 * Sets mandatory field networkId.
155 *
156 * @param networkId of the vtap tunneling network
157 * @return self
158 */
159 @Override
160 public Builder networkId(Integer networkId) {
161 this.networkId = networkId;
162 return this;
163 }
164
165 /**
166 * Sets mandatory field serverIp.
167 *
168 * @param serverIp address used for tunneling
169 * @return self
170 */
171 @Override
172 public Builder serverIp(IpAddress serverIp) {
173 this.serverIp = serverIp;
174 return this;
175 }
176
177 /**
178 * Sets annotations.
179 *
180 * @param annotations of the vtap network
181 * @return self
182 */
183 @Override
184 public Builder annotations(SparseAnnotations annotations) {
185 this.annotations = annotations;
186 return this;
187 }
188
189 /**
190 * Builds a DefaultOpenstackVtapNetwork instance.
191 *
192 * @return DefaultOpenstackVtapNetwork
193 */
194 @Override
195 public DefaultOpenstackVtapNetwork build() {
196 return new DefaultOpenstackVtapNetwork(checkNotNull(mode),
197 networkId,
198 checkNotNull(serverIp),
199 checkNotNull(annotations));
200 }
201 }
202
203}