Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.api; |
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; |
| 24 | import org.onosproject.xosclient.api.VtnPortId; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 25 | import org.onosproject.xosclient.api.VtnServiceApi.ServiceType; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 26 | import org.onosproject.xosclient.api.VtnServiceId; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Provides methods to help to handle network service instance. |
| 33 | */ |
| 34 | public final class Instance { |
| 35 | |
| 36 | public static final String SERVICE_ID = "serviceId"; |
| 37 | public static final String SERVICE_TYPE = "serviceType"; |
| 38 | public static final String PORT_ID = "vtnPortId"; |
| 39 | public static final String CREATE_TIME = "createTime"; |
| 40 | public static final String NESTED_INSTANCE = "nestedInstance"; |
| 41 | public static final String TRUE = "true"; |
| 42 | |
| 43 | private final Host host; |
| 44 | |
| 45 | /** |
| 46 | * Default constructor. |
| 47 | * |
| 48 | * @param instance host object of this instance |
| 49 | */ |
| 50 | private Instance(Host instance) { |
| 51 | this.host = instance; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns host object of this instance. |
| 56 | * |
| 57 | * @return host |
| 58 | */ |
| 59 | public Host host() { |
| 60 | return this.host; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns new instance. |
| 65 | * |
| 66 | * @param host host object of this instance |
| 67 | * @return instance |
| 68 | */ |
| 69 | public static Instance of(Host host) { |
| 70 | checkNotNull(host); |
| 71 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(SERVICE_ID))); |
| 72 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(SERVICE_TYPE))); |
| 73 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(PORT_ID))); |
| 74 | checkArgument(!Strings.isNullOrEmpty(host.annotations().value(CREATE_TIME))); |
| 75 | |
| 76 | return new Instance(host); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns service ID of a given host. |
| 81 | * |
| 82 | * @return vtn service id |
| 83 | */ |
| 84 | public VtnServiceId serviceId() { |
| 85 | String serviceId = host.annotations().value(SERVICE_ID); |
| 86 | return VtnServiceId.of(serviceId); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns service type of a given host. |
| 91 | * |
| 92 | * @return vtn service type |
| 93 | */ |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 94 | public ServiceType serviceType() { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 95 | String serviceType = host.annotations().value(SERVICE_TYPE); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 96 | return ServiceType.valueOf(serviceType); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns port ID of a given host. |
| 101 | * |
| 102 | * @return vtn port id |
| 103 | */ |
| 104 | public VtnPortId portId() { |
| 105 | String portId = host.annotations().value(PORT_ID); |
| 106 | return VtnPortId.of(portId); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns if the instance is nested container or not. |
| 111 | * |
| 112 | * @return true if it's nested container; false otherwise |
| 113 | */ |
| 114 | public boolean isNestedInstance() { |
| 115 | return host.annotations().value(NESTED_INSTANCE) != null; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns MAC address of this instance. |
| 120 | * |
| 121 | * @return mac address |
| 122 | */ |
| 123 | public MacAddress mac() { |
| 124 | return host.mac(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns IP address of this instance. |
| 129 | * |
| 130 | * @return ip address |
| 131 | */ |
| 132 | public Ip4Address ipAddress() { |
| 133 | // assume all instance has only one IP address, and only IP4 is supported now |
| 134 | return host.ipAddresses().stream().findFirst().get().getIp4Address(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns device ID of this host. |
| 139 | * |
| 140 | * @return device id |
| 141 | */ |
| 142 | public DeviceId deviceId() { |
| 143 | return host.location().deviceId(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the port number where this host is. |
| 148 | * |
| 149 | * @return port number |
| 150 | */ |
| 151 | public PortNumber portNumber() { |
| 152 | return host.location().port(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns annotation value with a given key. |
| 157 | * |
| 158 | * @param annotationKey annotation key |
| 159 | * @return annotation value |
| 160 | */ |
| 161 | public String getAnnotation(String annotationKey) { |
| 162 | return host.annotations().value(annotationKey); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public String toString() { |
| 167 | return host.toString(); |
| 168 | } |
| 169 | } |