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; |
| 25 | |
| 26 | /** |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 27 | * Representation of a service network which holds service specific information, |
| 28 | * like service type or dependency, in addition to the common network. |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 29 | */ |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 30 | public 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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 34 | |
| 35 | public enum ServiceNetworkType { |
| 36 | PRIVATE, |
| 37 | PUBLIC, |
| 38 | MANAGEMENT_HOST, |
| 39 | MANAGEMENT_LOCAL, |
| 40 | VSG, |
| 41 | ACCESS_AGENT |
| 42 | } |
| 43 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 44 | protected final NetworkId id; |
| 45 | protected final ServiceNetworkType type; |
| 46 | protected final Set<ProviderNetwork> providers; |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 47 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 48 | 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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 54 | } |
| 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 Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 79 | public Set<ProviderNetwork> providers() { |
Hyunsun Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 80 | return providers; |
| 81 | } |
| 82 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 83 | /** |
| 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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 94 | @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 Moon | 395542a | 2016-09-01 13:53:08 -0700 | [diff] [blame] | 124 | } |