blob: cbdde5f6a16b25ae037398c5760271fd11ab9da3 [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 */
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070016package org.opencord.cordvtn.api.net;
Hyunsun Moon395542a2016-09-01 13:53:08 -070017
18import com.google.common.base.MoreObjects;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070019import com.google.common.collect.ImmutableSet;
Hyunsun Moon395542a2016-09-01 13:53:08 -070020
Hyunsun Moon395542a2016-09-01 13:53:08 -070021import java.util.Objects;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070022import java.util.Set;
Hyunsun Moon395542a2016-09-01 13:53:08 -070023
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
Hyunsun Moon5c143952016-10-19 18:34:46 -070027 * Representation of a service network which holds service specific information,
28 * like service type or dependency, in addition to the common network.
Hyunsun Moon395542a2016-09-01 13:53:08 -070029 */
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070030public class ServiceNetwork {
31
32 private static final String ERR_ID = "Service network ID cannot be null";
33 private static final String ERR_TYPE = "Service network type cannot be null";
Hyunsun Moon395542a2016-09-01 13:53:08 -070034
35 public enum ServiceNetworkType {
36 PRIVATE,
37 PUBLIC,
38 MANAGEMENT_HOST,
39 MANAGEMENT_LOCAL,
40 VSG,
41 ACCESS_AGENT
42 }
43
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070044 protected final NetworkId id;
45 protected final ServiceNetworkType type;
46 protected final Set<ProviderNetwork> providers;
Hyunsun Moon395542a2016-09-01 13:53:08 -070047
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070048 public ServiceNetwork(NetworkId id,
49 ServiceNetworkType type,
50 Set<ProviderNetwork> providers) {
51 this.id = checkNotNull(id, ERR_ID);
52 this.type = checkNotNull(type, ERR_TYPE);
53 this.providers = providers == null ? ImmutableSet.of() : providers;
Hyunsun Moon395542a2016-09-01 13:53:08 -070054 }
55
56 /**
57 * Returns the network id of the service network.
58 *
59 * @return network id
60 */
61 public NetworkId id() {
62 return id;
63 }
64
65 /**
66 * Returns the type of the service network.
67 *
68 * @return service network type
69 */
70 public ServiceNetworkType type() {
71 return type;
72 }
73
74 /**
75 * Returns the provider networks of this service network if exists.
76 *
77 * @return provider networks
78 */
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070079 public Set<ProviderNetwork> providers() {
Hyunsun Moon395542a2016-09-01 13:53:08 -070080 return providers;
81 }
82
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070083 /**
84 * Returns if the given network is the provider of this network or not.
85 *
86 * @param netId network id
87 * @return true if the given network is the provider of this network
88 */
89 public boolean isProvider(NetworkId netId) {
90 return providers.stream().filter(p -> Objects.equals(p.id(), netId))
91 .findAny().isPresent();
92 }
93
Hyunsun Moon395542a2016-09-01 13:53:08 -070094 @Override
95 public boolean equals(Object obj) {
96 if (this == obj) {
97 return true;
98 }
99
100 if (obj instanceof ServiceNetwork) {
101 ServiceNetwork that = (ServiceNetwork) obj;
102 if (Objects.equals(id, that.id) &&
103 Objects.equals(type, that.type) &&
104 Objects.equals(providers, that.providers)) {
105 return true;
106 }
107 }
108 return false;
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hash(id, type, providers);
114 }
115
116 @Override
117 public String toString() {
118 return MoreObjects.toStringHelper(getClass())
119 .add("networkId", id)
120 .add("type", type)
121 .add("providers", providers)
122 .toString();
123 }
Hyunsun Moon395542a2016-09-01 13:53:08 -0700124}