Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.cordvtn; |
| 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 | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 23 | import org.onlab.packet.TpPort; |
| 24 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 26 | import org.onosproject.net.config.Config; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 27 | import org.slf4j.Logger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 28 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 29 | import java.util.Map; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 30 | import java.util.Set; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
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"; |
| 48 | public static final String SSH_PORT = "sshPort"; |
| 49 | public static final String SSH_USER = "sshUser"; |
| 50 | public static final String SSH_KEY_FILE = "sshKeyFile"; |
| 51 | public static final String CORDVTN_NODES = "nodes"; |
| 52 | |
| 53 | public static final String HOSTNAME = "hostname"; |
| 54 | public static final String HOST_MANAGEMENT_IP = "hostManagementIp"; |
| 55 | public static final String DATA_PLANE_IP = "dataPlaneIp"; |
| 56 | public static final String DATA_PLANE_INTF = "dataPlaneIntf"; |
| 57 | public static final String BRIDGE_ID = "bridgeId"; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 60 | * Returns the set of nodes read from network config. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 61 | * |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 62 | * @return set of CordVtnNodeConfig or null |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 63 | */ |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 64 | public Set<CordVtnNodeConfig> cordVtnNodes() { |
| 65 | Set<CordVtnNodeConfig> nodes = Sets.newHashSet(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 66 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 67 | JsonNode jsonNodes = object.get(CORDVTN_NODES); |
| 68 | if (jsonNodes == null) { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 69 | return null; |
| 70 | } |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 71 | |
| 72 | jsonNodes.forEach(jsonNode -> { |
| 73 | try { |
| 74 | nodes.add(new CordVtnNodeConfig( |
| 75 | jsonNode.path(HOSTNAME).asText(), |
| 76 | NetworkAddress.valueOf(jsonNode.path(HOST_MANAGEMENT_IP).asText()), |
| 77 | NetworkAddress.valueOf(jsonNode.path(DATA_PLANE_IP).asText()), |
| 78 | jsonNode.path(DATA_PLANE_INTF).asText(), |
| 79 | DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText()))); |
| 80 | } catch (IllegalArgumentException | NullPointerException e) { |
| 81 | log.error("Failed to read {}", e.toString()); |
| 82 | } |
| 83 | }); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 84 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 85 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 89 | * Returns private network gateway MAC address. |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 90 | * |
| 91 | * @return mac address, or null |
| 92 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 93 | public MacAddress privateGatewayMac() { |
| 94 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 95 | if (jsonNode == null) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | try { |
| 100 | return MacAddress.valueOf(jsonNode.asText()); |
| 101 | } catch (IllegalArgumentException e) { |
| 102 | log.error("Wrong MAC address format {}", jsonNode.asText()); |
| 103 | return null; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 108 | * Returns public network gateway IP and MAC address pairs. |
| 109 | * |
| 110 | * @return map of ip and mac address |
| 111 | */ |
| 112 | public Map<IpAddress, MacAddress> publicGateways() { |
| 113 | JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS); |
| 114 | if (jsonNodes == null) { |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 119 | jsonNodes.forEach(jsonNode -> { |
| 120 | try { |
| 121 | publicGateways.put( |
| 122 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 123 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText())); |
| 124 | } catch (IllegalArgumentException | NullPointerException e) { |
| 125 | log.error("Wrong address format {}", e.toString()); |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | return publicGateways; |
| 130 | } |
| 131 | |
| 132 | /** |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 133 | * Returns local management network address. |
| 134 | * |
| 135 | * @return network address |
| 136 | */ |
| 137 | public NetworkAddress localMgmtIp() { |
| 138 | JsonNode jsonNode = object.get(LOCAL_MANAGEMENT_IP); |
| 139 | if (jsonNode == null) { |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | try { |
| 144 | return NetworkAddress.valueOf(jsonNode.asText()); |
| 145 | } catch (IllegalArgumentException e) { |
| 146 | log.error("Wrong address format {}", jsonNode.asText()); |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns the port number used for OVSDB connection. |
| 153 | * |
| 154 | * @return port number, or null |
| 155 | */ |
| 156 | public TpPort ovsdbPort() { |
| 157 | JsonNode jsonNode = object.get(OVSDB_PORT); |
| 158 | if (jsonNode == null) { |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | try { |
| 163 | return TpPort.tpPort(jsonNode.asInt()); |
| 164 | } catch (IllegalArgumentException e) { |
| 165 | log.error("Wrong TCP port format {}", jsonNode.asText()); |
| 166 | return null; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the port number used for SSH connection. |
| 172 | * |
| 173 | * @return port number, or null |
| 174 | */ |
| 175 | public TpPort sshPort() { |
| 176 | JsonNode jsonNode = object.get(SSH_PORT); |
| 177 | if (jsonNode == null) { |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | try { |
| 182 | return TpPort.tpPort(jsonNode.asInt()); |
| 183 | } catch (IllegalArgumentException e) { |
| 184 | log.error("Wrong TCP port format {}", jsonNode.asText()); |
| 185 | return null; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the user name for SSH connection. |
| 191 | * |
| 192 | * @return user name, or null |
| 193 | */ |
| 194 | public String sshUser() { |
| 195 | JsonNode jsonNode = object.get(SSH_USER); |
| 196 | if (jsonNode == null) { |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | return jsonNode.asText(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns the private key file for SSH connection. |
| 205 | * |
| 206 | * @return file path, or null |
| 207 | */ |
| 208 | public String sshKeyFile() { |
| 209 | JsonNode jsonNode = object.get(SSH_KEY_FILE); |
| 210 | if (jsonNode == null) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | return jsonNode.asText(); |
| 215 | } |
| 216 | |
| 217 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 218 | * Configuration for CordVtn node. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 219 | */ |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 220 | public static class CordVtnNodeConfig { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 221 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 222 | private final String hostname; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 223 | private final NetworkAddress hostMgmtIp; |
| 224 | private final NetworkAddress dpIp; |
| 225 | private final String dpIntf; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 226 | private final DeviceId bridgeId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 227 | |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 228 | public CordVtnNodeConfig(String hostname, NetworkAddress hostMgmtIp, NetworkAddress dpIp, |
| 229 | String dpIntf, DeviceId bridgeId) { |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 230 | this.hostname = checkNotNull(hostname); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 231 | this.hostMgmtIp = checkNotNull(hostMgmtIp); |
| 232 | this.dpIp = checkNotNull(dpIp); |
| 233 | this.dpIntf = checkNotNull(dpIntf); |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 234 | this.bridgeId = checkNotNull(bridgeId); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 238 | * Returns hostname of the node. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 239 | * |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 240 | * @return hostname |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 241 | */ |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 242 | public String hostname() { |
| 243 | return this.hostname; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 247 | * Returns the host management network address of the node. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 248 | * |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 249 | * @return management network address |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 250 | */ |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 251 | public NetworkAddress hostMgmtIp() { |
| 252 | return this.hostMgmtIp; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 256 | * Returns the data plane network address. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 257 | * |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 258 | * @return network address |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 259 | */ |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 260 | public NetworkAddress dpIp() { |
| 261 | return this.dpIp; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Returns the data plane interface name. |
| 266 | * |
| 267 | * @return interface name |
| 268 | */ |
| 269 | public String dpIntf() { |
| 270 | return this.dpIntf; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 271 | } |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 272 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 273 | /** |
| 274 | * Returns integration bridge id of the node. |
| 275 | * |
| 276 | * @return device id |
| 277 | */ |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 278 | public DeviceId bridgeId() { |
| 279 | return this.bridgeId; |
| 280 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 281 | } |
| 282 | } |