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