blob: bc639daa1ca2ba5e3768e062e705f53bb0d86880 [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.ImmutableMap;
20import com.google.common.collect.Maps;
21
22import java.util.Map;
23import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Representation of a service network.
29 */
30public final class ServiceNetwork {
31
32 public enum ServiceNetworkType {
33 PRIVATE,
34 PUBLIC,
35 MANAGEMENT_HOST,
36 MANAGEMENT_LOCAL,
37 VSG,
38 ACCESS_AGENT
39 }
40
41 public enum DirectAccessType {
42 BIDIRECTIONAL,
43 UNIDIRECTIONAL
44 }
45
46 private final NetworkId id;
47 private final ServiceNetworkType type;
48 private final Map<NetworkId, DirectAccessType> providers;
49
50 private ServiceNetwork(NetworkId id,
51 ServiceNetworkType type,
52 Map<NetworkId, DirectAccessType> providers) {
53 this.id = id;
54 this.type = type;
55 this.providers = providers;
56 }
57
58 /**
59 * Returns the network id of the service network.
60 *
61 * @return network id
62 */
63 public NetworkId id() {
64 return id;
65 }
66
67 /**
68 * Returns the type of the service network.
69 *
70 * @return service network type
71 */
72 public ServiceNetworkType type() {
73 return type;
74 }
75
76 /**
77 * Returns the provider networks of this service network if exists.
78 *
79 * @return provider networks
80 */
81 public Map<NetworkId, DirectAccessType> providers() {
82 return providers;
83 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90
91 if (obj instanceof ServiceNetwork) {
92 ServiceNetwork that = (ServiceNetwork) obj;
93 if (Objects.equals(id, that.id) &&
94 Objects.equals(type, that.type) &&
95 Objects.equals(providers, that.providers)) {
96 return true;
97 }
98 }
99 return false;
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(id, type, providers);
105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(getClass())
110 .add("networkId", id)
111 .add("type", type)
112 .add("providers", providers)
113 .toString();
114 }
115
116 /**
117 * Returns new service network builder instance.
118 *
119 * @return service network builder
120 */
121 public static Builder builder() {
122 return new Builder();
123 }
124
125 /**
126 * Builder of the service network entities.
127 */
128 public static final class Builder {
129 private NetworkId id;
130 private ServiceNetworkType type;
131 private Map<NetworkId, DirectAccessType> providers = Maps.newHashMap();
132
133 private Builder() {
134 }
135
136 /**
137 * Builds an immutable service network.
138 *
139 * @return service network instance
140 */
141 public ServiceNetwork build() {
142 checkNotNull(id, "Service network id cannot be null");
143 checkNotNull(type, "Service network type cannot be null");
144 providers = providers == null ? ImmutableMap.of() : providers;
145
146 return new ServiceNetwork(id, type, providers);
147 }
148
149 /**
150 * Returns service network builder with the supplied network ID.
151 *
152 * @param id network id
153 * @return service network builder
154 */
155 public Builder id(NetworkId id) {
156 this.id = id;
157 return this;
158 }
159
160 /**
161 * Returns service network builder with the supplied service network type.
162 *
163 * @param type service network type
164 * @return service network builder
165 */
166 public Builder type(ServiceNetworkType type) {
167 this.type = type;
168 return this;
169 }
170
171 /**
172 * Returns service network builder with the supplied provider service networks.
173 *
174 * @param providers provider service networks
175 * @return service network builder
176 */
177 public Builder providers(Map<NetworkId, DirectAccessType> providers) {
178 this.providers = providers;
179 return this;
180 }
181
182 /**
183 * Returns service network builder with the given additional provider network.
184 *
185 * @param id provider network id
186 * @param type direct access type to the provider network
187 * @return service network builder
188 */
189 public Builder addProvider(NetworkId id, DirectAccessType type) {
190 checkNotNull(id, "Provider network ID cannot be null");
191 checkNotNull(type, "Provider network type cannot be null");
192
193 this.providers.put(id, type);
194 return this;
195 }
196 }
197}