Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.api; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 25 | import org.onlab.packet.TpPort; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.net.config.Config; |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 28 | import org.onosproject.xosclient.api.OpenStackAccess; |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 29 | import org.onosproject.xosclient.api.XosAccess; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 30 | import org.slf4j.Logger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 31 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 32 | import java.util.Map; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 33 | import java.util.Set; |
| 34 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 35 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 36 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 37 | import static org.slf4j.LoggerFactory.getLogger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 40 | * Configuration object for CordVtn service. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 41 | */ |
| 42 | public class CordVtnConfig extends Config<ApplicationId> { |
| 43 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 44 | protected final Logger log = getLogger(getClass()); |
| 45 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 46 | 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 Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 52 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 53 | private static final String CORDVTN_NODES = "nodes"; |
| 54 | private static final String HOSTNAME = "hostname"; |
| 55 | private static final String HOST_MANAGEMENT_IP = "hostManagementIp"; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 56 | 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 Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 60 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 61 | 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 Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 65 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 66 | private static final String OPENSTACK = "openstack"; |
| 67 | private static final String XOS = "xos"; |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 68 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 69 | 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 Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 73 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 74 | // TODO implement isValid |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 75 | @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 network 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 Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 153 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 154 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 155 | * Returns the set of nodes read from network config. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 156 | * |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 157 | * @return set of CordVtnNodeConfig or empty set |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 158 | */ |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 159 | public Set<CordVtnNode> cordVtnNodes() { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 160 | Set<CordVtnNode> nodes = Sets.newHashSet(); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 161 | JsonNode sshNode = object.get(SSH); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 162 | String ovsdbPort = getConfig(object, OVSDB_PORT); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 163 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 164 | 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 Moon | 0592c3d | 2016-06-23 14:47:52 -0700 | [diff] [blame] | 167 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 168 | SshAccessInfo sshInfo = new SshAccessInfo( |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 169 | hostsMgmt.ip().getIp4Address(), |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 170 | TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))), |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 171 | getConfig(sshNode, SSH_USER), |
| 172 | getConfig(sshNode, SSH_KEY_FILE)); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 173 | |
| 174 | CordVtnNode.Builder nodeBuilder = CordVtnNode.builder() |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 175 | .hostname(getConfig(vtnNode, HOSTNAME)) |
| 176 | .hostMgmtIp(hostsMgmt) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 177 | .localMgmtIp(localMgmt) |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 178 | .dataIp(getConfig(vtnNode, DATA_IP)) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 179 | .sshInfo(sshInfo) |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 180 | .integrationBridgeId(getConfig(vtnNode, INTEGRATION_BRIDGE_ID)) |
| 181 | .dataIface(getConfig(vtnNode, DATA_IFACE)); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 182 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 183 | if (!Strings.isNullOrEmpty(ovsdbPort)) { |
| 184 | nodeBuilder.ovsdbPort(Integer.parseInt(ovsdbPort)); |
| 185 | } |
| 186 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 187 | String hostMgmtIface = getConfig(vtnNode, HOST_MANAGEMENT_IFACE); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 188 | if (!Strings.isNullOrEmpty(hostMgmtIface)) { |
| 189 | nodeBuilder.hostMgmtIface(hostMgmtIface); |
| 190 | } |
| 191 | |
| 192 | nodes.add(nodeBuilder.build()); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 193 | }); |
| 194 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 195 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 199 | * Gets the specified property as a string. |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 200 | * |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 201 | * @param jsonNode node whose fields to get |
| 202 | * @param path property to get |
| 203 | * @return value as a string |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 204 | */ |
| 205 | private String getConfig(JsonNode jsonNode, String path) { |
| 206 | jsonNode = jsonNode.path(path); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 207 | return jsonNode.asText(); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 211 | * Returns private network gateway MAC address. |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 212 | * |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 213 | * @return mac address |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 214 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 215 | public MacAddress privateGatewayMac() { |
| 216 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 217 | return MacAddress.valueOf(jsonNode.asText()); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 221 | * 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 Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 227 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 228 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 229 | if (jsonNodes == null) { |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 230 | return publicGateways; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 233 | jsonNodes.forEach(jsonNode -> publicGateways.put( |
| 234 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 235 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()))); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 236 | return publicGateways; |
| 237 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 238 | |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 239 | /** |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 240 | * Returns XOS access information. |
| 241 | * |
| 242 | * @return XOS access, or null |
| 243 | */ |
| 244 | public XosAccess xosAccess() { |
| 245 | JsonNode jsonNode = object.get(XOS); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 246 | return new XosAccess(getConfig(jsonNode, ENDPOINT), |
| 247 | getConfig(jsonNode, USER), |
| 248 | getConfig(jsonNode, PASSWORD)); |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 252 | * Returns OpenStack API access information. |
| 253 | * |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 254 | * @return openstack access |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 255 | */ |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 256 | public OpenStackAccess openstackAccess() { |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 257 | JsonNode jsonNode = object.get(OPENSTACK); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 258 | return new OpenStackAccess(jsonNode.path(ENDPOINT).asText(), |
| 259 | jsonNode.path(TENANT).asText(), |
| 260 | jsonNode.path(USER).asText(), |
| 261 | jsonNode.path(PASSWORD).asText()); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 262 | } |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 263 | } |