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 | */ |
Hyunsun Moon | f948020 | 2016-04-14 16:13:42 -0700 | [diff] [blame] | 16 | package org.onosproject.cordvtn.api; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 24 | import org.onlab.packet.TpPort; |
| 25 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.Config; |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 28 | import org.onosproject.xosclient.api.XosAccess; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 29 | import org.slf4j.Logger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 30 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 31 | import java.util.Map; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 32 | import java.util.Set; |
| 33 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 34 | import static org.slf4j.LoggerFactory.getLogger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 37 | * Configuration object for CordVtn service. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 38 | */ |
| 39 | public class CordVtnConfig extends Config<ApplicationId> { |
| 40 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 41 | protected final Logger log = getLogger(getClass()); |
| 42 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 43 | public static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac"; |
| 44 | public static final String PUBLIC_GATEWAYS = "publicGateways"; |
| 45 | public static final String GATEWAY_IP = "gatewayIp"; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 46 | public static final String GATEWAY_MAC = "gatewayMac"; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 47 | public static final String LOCAL_MANAGEMENT_IP = "localManagementIp"; |
| 48 | public static final String OVSDB_PORT = "ovsdbPort"; |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 49 | |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 50 | public static final String CORDVTN_NODES = "nodes"; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 51 | public static final String HOSTNAME = "hostname"; |
| 52 | public static final String HOST_MANAGEMENT_IP = "hostManagementIp"; |
| 53 | public static final String DATA_PLANE_IP = "dataPlaneIp"; |
| 54 | public static final String DATA_PLANE_INTF = "dataPlaneIntf"; |
| 55 | public static final String BRIDGE_ID = "bridgeId"; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 56 | |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 57 | public static final String SSH = "ssh"; |
| 58 | public static final String SSH_PORT = "sshPort"; |
| 59 | public static final String SSH_USER = "sshUser"; |
| 60 | public static final String SSH_KEY_FILE = "sshKeyFile"; |
| 61 | |
| 62 | public static final String OPENSTACK = "openstack"; |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 63 | public static final String XOS = "xos"; |
| 64 | |
| 65 | public static final String ENDPOINT = "endpoint"; |
| 66 | public static final String TENANT = "tenant"; |
| 67 | public static final String USER = "user"; |
| 68 | public static final String PASSWORD = "password"; |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 69 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 70 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 71 | * Returns the set of nodes read from network config. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 72 | * |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 73 | * @return set of CordVtnNodeConfig or empty set |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 74 | */ |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 75 | public Set<CordVtnNode> cordVtnNodes() { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 76 | |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 77 | Set<CordVtnNode> nodes = Sets.newHashSet(); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 78 | |
| 79 | JsonNode cordvtnNodes = object.get(CORDVTN_NODES); |
| 80 | if (cordvtnNodes == null) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 81 | log.debug("No CORD VTN nodes found"); |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 82 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 83 | } |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 84 | |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 85 | JsonNode sshNode = object.get(SSH); |
| 86 | if (sshNode == null) { |
| 87 | log.warn("SSH information not found"); |
| 88 | return nodes; |
| 89 | } |
| 90 | |
| 91 | for (JsonNode cordvtnNode : cordvtnNodes) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 92 | try { |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 93 | NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(cordvtnNode, HOST_MANAGEMENT_IP)); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 94 | NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP)); |
| 95 | if (hostMgmt.prefix().contains(localMgmt.prefix()) || |
| 96 | localMgmt.prefix().contains(hostMgmt.prefix())) { |
| 97 | log.error("hostMamt and localMgmt cannot be overlapped, skip this node"); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address(); |
| 102 | SshAccessInfo sshInfo = new SshAccessInfo( |
| 103 | hostMgmtIp, |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 104 | TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))), |
| 105 | getConfig(sshNode, SSH_USER), getConfig(sshNode, SSH_KEY_FILE)); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 106 | |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 107 | String hostname = getConfig(cordvtnNode, HOSTNAME); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 108 | CordVtnNode newNode = new CordVtnNode( |
| 109 | hostname, hostMgmt, localMgmt, |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 110 | NetworkAddress.valueOf(getConfig(cordvtnNode, DATA_PLANE_IP)), |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 111 | TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))), |
| 112 | sshInfo, |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 113 | DeviceId.deviceId(getConfig(cordvtnNode, BRIDGE_ID)), |
| 114 | getConfig(cordvtnNode, DATA_PLANE_INTF), |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 115 | CordVtnNodeState.noState()); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 116 | |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 117 | nodes.add(newNode); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 118 | } catch (IllegalArgumentException | NullPointerException e) { |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 119 | log.error("{}", e); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 120 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 121 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 122 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 123 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 127 | * Returns value of a given path. If the path is missing, show log and return |
| 128 | * null. |
| 129 | * |
| 130 | * @param path path |
| 131 | * @return value or null |
| 132 | */ |
| 133 | private String getConfig(JsonNode jsonNode, String path) { |
| 134 | jsonNode = jsonNode.path(path); |
| 135 | |
| 136 | if (jsonNode.isMissingNode()) { |
| 137 | log.error("{} is not configured", path); |
| 138 | return null; |
| 139 | } else { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 140 | return jsonNode.asText(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 145 | * Returns private network gateway MAC address. |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 146 | * |
| 147 | * @return mac address, or null |
| 148 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 149 | public MacAddress privateGatewayMac() { |
| 150 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 151 | if (jsonNode == null) { |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | try { |
| 156 | return MacAddress.valueOf(jsonNode.asText()); |
| 157 | } catch (IllegalArgumentException e) { |
| 158 | log.error("Wrong MAC address format {}", jsonNode.asText()); |
| 159 | return null; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 164 | * Returns public network gateway IP and MAC address pairs. |
| 165 | * |
| 166 | * @return map of ip and mac address |
| 167 | */ |
| 168 | public Map<IpAddress, MacAddress> publicGateways() { |
| 169 | JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS); |
| 170 | if (jsonNodes == null) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 171 | return Maps.newHashMap(); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 175 | jsonNodes.forEach(jsonNode -> { |
| 176 | try { |
| 177 | publicGateways.put( |
| 178 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 179 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText())); |
| 180 | } catch (IllegalArgumentException | NullPointerException e) { |
| 181 | log.error("Wrong address format {}", e.toString()); |
| 182 | } |
| 183 | }); |
| 184 | |
| 185 | return publicGateways; |
| 186 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 187 | |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 188 | /** |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 189 | * Returns XOS access information. |
| 190 | * |
| 191 | * @return XOS access, or null |
| 192 | */ |
| 193 | public XosAccess xosAccess() { |
| 194 | JsonNode jsonNode = object.get(XOS); |
| 195 | if (jsonNode == null) { |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 196 | return null; |
| 197 | } |
| 198 | |
| 199 | try { |
| 200 | return new XosAccess(getConfig(jsonNode, ENDPOINT), |
| 201 | getConfig(jsonNode, USER), |
| 202 | getConfig(jsonNode, PASSWORD)); |
| 203 | } catch (NullPointerException e) { |
| 204 | log.error("Failed to get XOS access"); |
| 205 | return null; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 210 | * Returns OpenStack API access information. |
| 211 | * |
| 212 | * @return openstack config |
| 213 | */ |
| 214 | public OpenStackConfig openstackConfig() { |
| 215 | JsonNode jsonNode = object.get(OPENSTACK); |
| 216 | if (jsonNode == null) { |
| 217 | log.error("Failed to get OpenStack configurations"); |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | try { |
| 222 | return new OpenStackConfig( |
Hyunsun Moon | b9c366e | 2016-04-24 17:43:32 -0700 | [diff] [blame] | 223 | jsonNode.path(ENDPOINT).asText(), |
| 224 | jsonNode.path(TENANT).asText(), |
| 225 | jsonNode.path(USER).asText(), |
| 226 | jsonNode.path(PASSWORD).asText()); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 227 | } catch (IllegalArgumentException | NullPointerException e) { |
| 228 | log.error("Failed to get OpenStack configurations"); |
| 229 | return null; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Configuration for OpenStack API access. |
| 235 | */ |
| 236 | public static class OpenStackConfig { |
| 237 | |
| 238 | private final String endpoint; |
| 239 | private final String tenant; |
| 240 | private final String user; |
| 241 | private final String password; |
| 242 | |
| 243 | /** |
| 244 | * Default constructor. |
| 245 | * |
| 246 | * @param endpoint Keystone endpoint |
| 247 | * @param tenant tenant name |
| 248 | * @param user user name |
| 249 | * @param password passwowrd |
| 250 | */ |
| 251 | public OpenStackConfig(String endpoint, String tenant, String user, String password) { |
| 252 | this.endpoint = endpoint; |
| 253 | this.tenant = tenant; |
| 254 | this.user = user; |
| 255 | this.password = password; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns OpenStack API endpoint. |
| 260 | * |
| 261 | * @return endpoint |
| 262 | */ |
| 263 | public String endpoint() { |
| 264 | return this.endpoint; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns OpenStack tenant name. |
| 269 | * |
| 270 | * @return tenant name |
| 271 | */ |
| 272 | public String tenant() { |
| 273 | return this.tenant; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Returns OpenStack user. |
| 278 | * |
| 279 | * @return user name |
| 280 | */ |
| 281 | public String user() { |
| 282 | return this.user; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns OpenStack password for the user. |
| 287 | * |
| 288 | * @return password |
| 289 | */ |
| 290 | public String password() { |
| 291 | return this.password; |
| 292 | } |
| 293 | } |
| 294 | } |