Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 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 | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 16 | package org.opencord.cordvtn.api.core; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import org.onlab.packet.Ip4Address; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Host; |
| 23 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 24 | import org.opencord.cordvtn.api.net.NetworkId; |
| 25 | import org.opencord.cordvtn.api.net.PortId; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 26 | import org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 32 | * Provides methods to help to handle network network instance. |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 33 | */ |
| 34 | public final class Instance { |
| 35 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 36 | public static final String NETWORK_ID = "networkId"; |
| 37 | public static final String NETWORK_TYPE = "networkType"; |
| 38 | public static final String PORT_ID = "portId"; |
Hyunsun Moon | d02a5a7 | 2017-02-05 22:00:05 +0900 | [diff] [blame] | 39 | public static final String ORIGINAL_HOST_ID = "originalHostId"; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 40 | public static final String CREATE_TIME = "createTime"; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 41 | |
| 42 | private final Host host; |
| 43 | |
| 44 | /** |
| 45 | * Default constructor. |
| 46 | * |
| 47 | * @param instance host object of this instance |
| 48 | */ |
| 49 | private Instance(Host instance) { |
| 50 | this.host = instance; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns host object of this instance. |
| 55 | * |
| 56 | * @return host |
| 57 | */ |
| 58 | public Host host() { |
| 59 | return this.host; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns new instance. |
| 64 | * |
| 65 | * @param host host object of this instance |
| 66 | * @return instance |
| 67 | */ |
| 68 | public static Instance of(Host host) { |
| 69 | checkNotNull(host); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 70 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(NETWORK_ID))); |
| 71 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(NETWORK_TYPE))); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 72 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(PORT_ID))); |
| 73 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(CREATE_TIME))); |
| 74 | |
| 75 | return new Instance(host); |
| 76 | } |
| 77 | |
| 78 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 79 | * Returns network ID of a given host. |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 80 | * |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 81 | * @return network id |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 82 | */ |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 83 | public NetworkId netId() { |
| 84 | String netId = host.annotations().value(NETWORK_ID); |
| 85 | return NetworkId.of(netId); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 89 | * Returns network type of a given host. |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 90 | * |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 91 | * @return network type |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 92 | */ |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 93 | public NetworkType netType() { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 94 | String netType = host.annotations().value(NETWORK_TYPE); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 95 | return NetworkType.valueOf(netType); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns port ID of a given host. |
| 100 | * |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 101 | * @return port id |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 102 | */ |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 103 | public PortId portId() { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 104 | String portId = host.annotations().value(PORT_ID); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 105 | return PortId.of(portId); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
Hyunsun Moon | d02a5a7 | 2017-02-05 22:00:05 +0900 | [diff] [blame] | 109 | * Returns if the instance is original instance or additional one. |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 110 | * |
Hyunsun Moon | d02a5a7 | 2017-02-05 22:00:05 +0900 | [diff] [blame] | 111 | * @return true if it's additional instance; false otherwise |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 112 | */ |
Hyunsun Moon | d02a5a7 | 2017-02-05 22:00:05 +0900 | [diff] [blame] | 113 | public boolean isAdditionalInstance() { |
| 114 | return host.annotations().value(ORIGINAL_HOST_ID) != null; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns MAC address of this instance. |
| 119 | * |
| 120 | * @return mac address |
| 121 | */ |
| 122 | public MacAddress mac() { |
| 123 | return host.mac(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns IP address of this instance. |
| 128 | * |
| 129 | * @return ip address |
| 130 | */ |
| 131 | public Ip4Address ipAddress() { |
| 132 | // assume all instance has only one IP address, and only IP4 is supported now |
| 133 | return host.ipAddresses().stream().findFirst().get().getIp4Address(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns device ID of this host. |
| 138 | * |
| 139 | * @return device id |
| 140 | */ |
| 141 | public DeviceId deviceId() { |
| 142 | return host.location().deviceId(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the port number where this host is. |
| 147 | * |
| 148 | * @return port number |
| 149 | */ |
| 150 | public PortNumber portNumber() { |
| 151 | return host.location().port(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns annotation value with a given key. |
| 156 | * |
| 157 | * @param annotationKey annotation key |
| 158 | * @return annotation value |
| 159 | */ |
| 160 | public String getAnnotation(String annotationKey) { |
| 161 | return host.annotations().value(annotationKey); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String toString() { |
| 166 | return host.toString(); |
| 167 | } |
| 168 | } |