Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.api.net; |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 20 | |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame^] | 25 | import static org.opencord.cordvtn.api.dependency.Dependency.Type.BIDIRECTIONAL; |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 28 | * Representation of a service network which holds service specific information, |
| 29 | * like service type or dependency, in addition to the common network. |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 30 | */ |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 31 | public 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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 35 | |
| 36 | public enum ServiceNetworkType { |
| 37 | PRIVATE, |
| 38 | PUBLIC, |
| 39 | MANAGEMENT_HOST, |
| 40 | MANAGEMENT_LOCAL, |
| 41 | VSG, |
| 42 | ACCESS_AGENT |
| 43 | } |
| 44 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 45 | protected final NetworkId id; |
| 46 | protected final ServiceNetworkType type; |
| 47 | protected final Set<ProviderNetwork> providers; |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 48 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 49 | 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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 55 | } |
| 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 Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 80 | public Set<ProviderNetwork> providers() { |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 81 | return providers; |
| 82 | } |
| 83 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 84 | /** |
| 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 Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame^] | 95 | /** |
| 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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 108 | @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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 138 | } |