blob: c76f90cbb3ba4a9866e80db4cac4559ac4bb8a27 [file] [log] [blame]
Hyunsun Moone9d75992015-09-15 22:39:16 -07001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moone9d75992015-09-15 22:39:16 -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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.api;
Hyunsun Moone9d75992015-09-15 22:39:16 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moon23db4182016-06-15 18:08:04 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Hyunsun Moon3ef52492016-06-15 14:56:31 -070020import com.google.common.base.Strings;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080021import com.google.common.collect.Maps;
Hyunsun Moone9d75992015-09-15 22:39:16 -070022import com.google.common.collect.Sets;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080023import org.onlab.packet.IpAddress;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080024import org.onlab.packet.MacAddress;
Hyunsun Moone9d75992015-09-15 22:39:16 -070025import org.onlab.packet.TpPort;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.net.config.Config;
Hyunsun Moon486ed1b2016-05-13 18:58:35 -070028import org.onosproject.xosclient.api.OpenStackAccess;
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070029import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080030import org.slf4j.Logger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070031
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080032import java.util.Map;
Hyunsun Moone9d75992015-09-15 22:39:16 -070033import java.util.Set;
34
Hyunsun Moon23db4182016-06-15 18:08:04 -070035import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
36import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080037import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070038
39/**
Hyunsun Moon9661d642015-09-23 13:24:35 -070040 * Configuration object for CordVtn service.
Hyunsun Moone9d75992015-09-15 22:39:16 -070041 */
42public class CordVtnConfig extends Config<ApplicationId> {
43
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080044 protected final Logger log = getLogger(getClass());
45
Hyunsun Moon60a10672016-06-12 17:39:12 -070046 private static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac";
47 private static final String PUBLIC_GATEWAYS = "publicGateways";
48 private static final String GATEWAY_IP = "gatewayIp";
49 private static final String GATEWAY_MAC = "gatewayMac";
50 private static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
51 private static final String OVSDB_PORT = "ovsdbPort";
Hyunsun Moond05b32e2016-03-02 19:27:26 -080052
Hyunsun Moon60a10672016-06-12 17:39:12 -070053 private static final String CORDVTN_NODES = "nodes";
54 private static final String HOSTNAME = "hostname";
55 private static final String HOST_MANAGEMENT_IP = "hostManagementIp";
Hyunsun Moon3ef52492016-06-15 14:56:31 -070056 private static final String HOST_MANAGEMENT_IFACE = "hostManagementIface";
57 private static final String DATA_IP = "dataPlaneIp";
58 private static final String DATA_IFACE = "dataPlaneIntf";
59 private static final String INTEGRATION_BRIDGE_ID = "bridgeId";
Hyunsun Moone9d75992015-09-15 22:39:16 -070060
Hyunsun Moon60a10672016-06-12 17:39:12 -070061 private static final String SSH = "ssh";
62 private static final String SSH_PORT = "sshPort";
63 private static final String SSH_USER = "sshUser";
64 private static final String SSH_KEY_FILE = "sshKeyFile";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070065
Hyunsun Moon60a10672016-06-12 17:39:12 -070066 private static final String OPENSTACK = "openstack";
67 private static final String XOS = "xos";
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070068
Hyunsun Moon60a10672016-06-12 17:39:12 -070069 private static final String ENDPOINT = "endpoint";
70 private static final String TENANT = "tenant";
71 private static final String USER = "user";
72 private static final String PASSWORD = "password";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070073
Hyunsun Moon3ef52492016-06-15 14:56:31 -070074 // TODO implement isValid
Hyunsun Moon23db4182016-06-15 18:08:04 -070075 @Override
76 public boolean isValid() {
77 // check only allowed fields are present
78 boolean result = hasOnlyFields(
79 PRIVATE_GATEWAY_MAC,
80 PUBLIC_GATEWAYS,
81 LOCAL_MANAGEMENT_IP,
82 OVSDB_PORT,
83 SSH,
84 OPENSTACK,
85 XOS,
86 CORDVTN_NODES);
87
88 if (object.get(CORDVTN_NODES) == null || object.get(CORDVTN_NODES).size() < 1) {
89 final String msg = "No node is present";
90 throw new IllegalArgumentException(msg);
91 }
92
93 // check all mandatory fields are present and valid
94 result = result && isMacAddress(PRIVATE_GATEWAY_MAC, MANDATORY);
95 result = result && isIpPrefix(LOCAL_MANAGEMENT_IP, MANDATORY);
96
97 for (JsonNode node : object.get(CORDVTN_NODES)) {
98 ObjectNode vtnNode = (ObjectNode) node;
99 result = result && hasFields(
100 vtnNode,
101 HOSTNAME,
102 HOST_MANAGEMENT_IP,
103 DATA_IP,
104 DATA_IFACE,
105 INTEGRATION_BRIDGE_ID);
106 result = result && isIpPrefix(vtnNode, HOST_MANAGEMENT_IP, MANDATORY);
107 result = result && isIpPrefix(vtnNode, DATA_IP, MANDATORY);
108
109 NetworkAddress localMgmt = NetworkAddress.valueOf(get(LOCAL_MANAGEMENT_IP, ""));
110 NetworkAddress hostsMgmt = NetworkAddress.valueOf(getConfig(vtnNode, HOST_MANAGEMENT_IP));
111 if (hostsMgmt.prefix().contains(localMgmt.prefix()) ||
112 localMgmt.prefix().contains(hostsMgmt.prefix())) {
113 final String msg = "Host and local management IP conflict";
114 throw new IllegalArgumentException(msg);
115 }
116 }
117
118 result = result && hasFields(
119 (ObjectNode) object.get(SSH),
120 SSH_PORT,
121 SSH_USER,
122 SSH_KEY_FILE);
123 result = result && isTpPort(
124 (ObjectNode) object.get(SSH),
125 SSH_PORT,
126 MANDATORY);
127
128 result = result && hasFields(
129 (ObjectNode) object.get(OPENSTACK),
130 ENDPOINT,
131 TENANT,
132 USER,
133 PASSWORD);
134
135 result = result && hasFields(
136 (ObjectNode) object.get(XOS),
137 ENDPOINT,
138 USER,
139 PASSWORD);
140
141 // check all optional fields are valid
142 result = result && isTpPort(OVSDB_PORT, OPTIONAL);
143
144 if (object.get(PUBLIC_GATEWAYS) != null && object.get(PUBLIC_GATEWAYS).isArray()) {
145 for (JsonNode node : object.get(PUBLIC_GATEWAYS)) {
146 ObjectNode gateway = (ObjectNode) node;
147 result = result && isIpAddress(gateway, GATEWAY_IP, MANDATORY);
148 result = result && isMacAddress(gateway, GATEWAY_MAC, MANDATORY);
149 }
150 }
151 return result;
152 }
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700153
Hyunsun Moone9d75992015-09-15 22:39:16 -0700154 /**
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800155 * Returns the set of nodes read from network config.
Hyunsun Moone9d75992015-09-15 22:39:16 -0700156 *
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800157 * @return set of CordVtnNodeConfig or empty set
Hyunsun Moone9d75992015-09-15 22:39:16 -0700158 */
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800159 public Set<CordVtnNode> cordVtnNodes() {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800160 Set<CordVtnNode> nodes = Sets.newHashSet();
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700161 JsonNode sshNode = object.get(SSH);
Hyunsun Moon23db4182016-06-15 18:08:04 -0700162 String ovsdbPort = getConfig(object, OVSDB_PORT);
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700163
Hyunsun Moon23db4182016-06-15 18:08:04 -0700164 object.get(CORDVTN_NODES).forEach(vtnNode -> {
165 NetworkAddress localMgmt = NetworkAddress.valueOf(get(LOCAL_MANAGEMENT_IP, ""));
166 NetworkAddress hostsMgmt = NetworkAddress.valueOf(getConfig(vtnNode, HOST_MANAGEMENT_IP));
Hyunsun Moone9d75992015-09-15 22:39:16 -0700167
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700168 SshAccessInfo sshInfo = new SshAccessInfo(
Hyunsun Moon23db4182016-06-15 18:08:04 -0700169 hostsMgmt.ip().getIp4Address(),
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700170 TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))),
Hyunsun Moon23db4182016-06-15 18:08:04 -0700171 getConfig(sshNode, SSH_USER),
172 getConfig(sshNode, SSH_KEY_FILE));
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700173
174 CordVtnNode.Builder nodeBuilder = CordVtnNode.builder()
Hyunsun Moon23db4182016-06-15 18:08:04 -0700175 .hostname(getConfig(vtnNode, HOSTNAME))
176 .hostMgmtIp(hostsMgmt)
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700177 .localMgmtIp(localMgmt)
Hyunsun Moon23db4182016-06-15 18:08:04 -0700178 .dataIp(getConfig(vtnNode, DATA_IP))
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700179 .sshInfo(sshInfo)
Hyunsun Moon23db4182016-06-15 18:08:04 -0700180 .integrationBridgeId(getConfig(vtnNode, INTEGRATION_BRIDGE_ID))
181 .dataIface(getConfig(vtnNode, DATA_IFACE));
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700182
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700183 if (!Strings.isNullOrEmpty(ovsdbPort)) {
184 nodeBuilder.ovsdbPort(Integer.parseInt(ovsdbPort));
185 }
186
Hyunsun Moon23db4182016-06-15 18:08:04 -0700187 String hostMgmtIface = getConfig(vtnNode, HOST_MANAGEMENT_IFACE);
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700188 if (!Strings.isNullOrEmpty(hostMgmtIface)) {
189 nodeBuilder.hostMgmtIface(hostMgmtIface);
190 }
191
192 nodes.add(nodeBuilder.build());
Hyunsun Moon23db4182016-06-15 18:08:04 -0700193 });
194
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800195 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -0700196 }
197
198 /**
Hyunsun Moon23db4182016-06-15 18:08:04 -0700199 * Gets the specified property as a string.
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800200 *
Hyunsun Moon23db4182016-06-15 18:08:04 -0700201 * @param jsonNode node whose fields to get
202 * @param path property to get
203 * @return value as a string
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800204 */
205 private String getConfig(JsonNode jsonNode, String path) {
206 jsonNode = jsonNode.path(path);
Hyunsun Moon23db4182016-06-15 18:08:04 -0700207 return jsonNode.asText();
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800208 }
209
210 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800211 * Returns private network gateway MAC address.
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800212 *
Hyunsun Moon23db4182016-06-15 18:08:04 -0700213 * @return mac address
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800214 */
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800215 public MacAddress privateGatewayMac() {
216 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon23db4182016-06-15 18:08:04 -0700217 return MacAddress.valueOf(jsonNode.asText());
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800218 }
219
220 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800221 * Returns public network gateway IP and MAC address pairs.
222 *
223 * @return map of ip and mac address
224 */
225 public Map<IpAddress, MacAddress> publicGateways() {
226 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
Hyunsun Moon23db4182016-06-15 18:08:04 -0700227 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
228
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800229 if (jsonNodes == null) {
Hyunsun Moon23db4182016-06-15 18:08:04 -0700230 return publicGateways;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800231 }
232
Hyunsun Moon23db4182016-06-15 18:08:04 -0700233 jsonNodes.forEach(jsonNode -> publicGateways.put(
234 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
235 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText())));
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800236 return publicGateways;
237 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800238
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700239 /**
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700240 * Returns XOS access information.
241 *
242 * @return XOS access, or null
243 */
244 public XosAccess xosAccess() {
245 JsonNode jsonNode = object.get(XOS);
Hyunsun Moon23db4182016-06-15 18:08:04 -0700246 return new XosAccess(getConfig(jsonNode, ENDPOINT),
247 getConfig(jsonNode, USER),
248 getConfig(jsonNode, PASSWORD));
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700249 }
250
251 /**
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700252 * Returns OpenStack API access information.
253 *
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700254 * @return openstack access
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700255 */
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700256 public OpenStackAccess openstackAccess() {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700257 JsonNode jsonNode = object.get(OPENSTACK);
Hyunsun Moon23db4182016-06-15 18:08:04 -0700258 return new OpenStackAccess(jsonNode.path(ENDPOINT).asText(),
259 jsonNode.path(TENANT).asText(),
260 jsonNode.path(USER).asText(),
261 jsonNode.path(PASSWORD).asText());
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700262 }
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700263}