blob: 060ba3d3e2113bbb7828a34a69ad06f24896fe25 [file] [log] [blame]
Hyunsun Moone7e4bb32016-05-16 04:32:45 -07001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moone7e4bb32016-05-16 04:32:45 -07003 *
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 Moon187bf532017-01-19 10:57:40 +090016package org.opencord.cordvtn.api.core;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070017
18import com.google.common.base.Strings;
19import org.onlab.packet.Ip4Address;
20import org.onlab.packet.MacAddress;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.Host;
23import org.onosproject.net.PortNumber;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070024import org.opencord.cordvtn.api.net.NetworkId;
25import org.opencord.cordvtn.api.net.PortId;
Hyunsun Moon187bf532017-01-19 10:57:40 +090026import org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070027
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070032 * Provides methods to help to handle network network instance.
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070033 */
34public final class Instance {
35
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070036 public static final String NETWORK_ID = "networkId";
37 public static final String NETWORK_TYPE = "networkType";
38 public static final String PORT_ID = "portId";
Hyunsun Moond02a5a72017-02-05 22:00:05 +090039 public static final String ORIGINAL_HOST_ID = "originalHostId";
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070040 public static final String CREATE_TIME = "createTime";
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070041
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 Mooneaf75e62016-09-27 16:40:23 -070070 checkArgument(!Strings.isNullOrEmpty(host.annotations().value(NETWORK_ID)));
71 checkArgument(!Strings.isNullOrEmpty(host.annotations().value(NETWORK_TYPE)));
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070072 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 Mooneaf75e62016-09-27 16:40:23 -070079 * Returns network ID of a given host.
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070080 *
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070081 * @return network id
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070082 */
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070083 public NetworkId netId() {
84 String netId = host.annotations().value(NETWORK_ID);
85 return NetworkId.of(netId);
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070086 }
87
88 /**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070089 * Returns network type of a given host.
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070090 *
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070091 * @return network type
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070092 */
Hyunsun Moon187bf532017-01-19 10:57:40 +090093 public NetworkType netType() {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070094 String netType = host.annotations().value(NETWORK_TYPE);
Hyunsun Moon187bf532017-01-19 10:57:40 +090095 return NetworkType.valueOf(netType);
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070096 }
97
98 /**
99 * Returns port ID of a given host.
100 *
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700101 * @return port id
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700102 */
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700103 public PortId portId() {
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700104 String portId = host.annotations().value(PORT_ID);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700105 return PortId.of(portId);
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700106 }
107
108 /**
Hyunsun Moond02a5a72017-02-05 22:00:05 +0900109 * Returns if the instance is original instance or additional one.
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700110 *
Hyunsun Moond02a5a72017-02-05 22:00:05 +0900111 * @return true if it's additional instance; false otherwise
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700112 */
Hyunsun Moond02a5a72017-02-05 22:00:05 +0900113 public boolean isAdditionalInstance() {
114 return host.annotations().value(ORIGINAL_HOST_ID) != null;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700115 }
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}