blob: 6b26daf3b5d53a7b5b8c6d34a1e3f042829511c3 [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;
Hyunsun Moon0984cbd2016-12-01 17:34:11 -080025import static org.opencord.cordvtn.api.dependency.Dependency.Type.BIDIRECTIONAL;
Hyunsun Moon395542a2016-09-01 13:53:08 -070026
27/**
Hyunsun Moon5c143952016-10-19 18:34:46 -070028 * Representation of a service network which holds service specific information,
29 * like service type or dependency, in addition to the common network.
Hyunsun Moon395542a2016-09-01 13:53:08 -070030 */
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070031public class ServiceNetwork {
32
33 private static final String ERR_ID = "Service network ID cannot be null";
34 private static final String ERR_TYPE = "Service network type cannot be null";
Hyunsun Moon395542a2016-09-01 13:53:08 -070035
36 public enum ServiceNetworkType {
37 PRIVATE,
38 PUBLIC,
39 MANAGEMENT_HOST,
40 MANAGEMENT_LOCAL,
41 VSG,
42 ACCESS_AGENT
43 }
44
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070045 protected final NetworkId id;
46 protected final ServiceNetworkType type;
47 protected final Set<ProviderNetwork> providers;
Hyunsun Moon395542a2016-09-01 13:53:08 -070048
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070049 public ServiceNetwork(NetworkId id,
50 ServiceNetworkType type,
51 Set<ProviderNetwork> providers) {
52 this.id = checkNotNull(id, ERR_ID);
53 this.type = checkNotNull(type, ERR_TYPE);
54 this.providers = providers == null ? ImmutableSet.of() : providers;
Hyunsun Moon395542a2016-09-01 13:53:08 -070055 }
56
57 /**
58 * Returns the network id of the service network.
59 *
60 * @return network id
61 */
62 public NetworkId id() {
63 return id;
64 }
65
66 /**
67 * Returns the type of the service network.
68 *
69 * @return service network type
70 */
71 public ServiceNetworkType type() {
72 return type;
73 }
74
75 /**
76 * Returns the provider networks of this service network if exists.
77 *
78 * @return provider networks
79 */
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070080 public Set<ProviderNetwork> providers() {
Hyunsun Moon395542a2016-09-01 13:53:08 -070081 return providers;
82 }
83
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070084 /**
85 * Returns if the given network is the provider of this network or not.
86 *
87 * @param netId network id
88 * @return true if the given network is the provider of this network
89 */
90 public boolean isProvider(NetworkId netId) {
91 return providers.stream().filter(p -> Objects.equals(p.id(), netId))
92 .findAny().isPresent();
93 }
94
Hyunsun Moon0984cbd2016-12-01 17:34:11 -080095 /**
96 * Returns if the given network is the provider of this network with
97 * bidirectional access type.
98 *
99 * @param netId network id
100 * @return true if the given network is a bidrectional provider
101 */
102 public boolean isBidirectionalProvider(NetworkId netId) {
103 return providers.stream().filter(p -> Objects.equals(p.id(), netId))
104 .filter(p -> p.type() == BIDIRECTIONAL)
105 .findAny().isPresent();
106 }
107
Hyunsun Moon395542a2016-09-01 13:53:08 -0700108 @Override
109 public boolean equals(Object obj) {
110 if (this == obj) {
111 return true;
112 }
113
114 if (obj instanceof ServiceNetwork) {
115 ServiceNetwork that = (ServiceNetwork) obj;
116 if (Objects.equals(id, that.id) &&
117 Objects.equals(type, that.type) &&
118 Objects.equals(providers, that.providers)) {
119 return true;
120 }
121 }
122 return false;
123 }
124
125 @Override
126 public int hashCode() {
127 return Objects.hash(id, type, providers);
128 }
129
130 @Override
131 public String toString() {
132 return MoreObjects.toStringHelper(getClass())
133 .add("networkId", id)
134 .add("type", type)
135 .add("providers", providers)
136 .toString();
137 }
Hyunsun Moon395542a2016-09-01 13:53:08 -0700138}