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