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 | 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 | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 49 | public static final String SSH_PORT = "sshPort"; |
| 50 | public static final String SSH_USER = "sshUser"; |
| 51 | public static final String SSH_KEY_FILE = "sshKeyFile"; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -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 | |
| 60 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 61 | * Returns the set of nodes read from network config. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 62 | * |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 63 | * @return set of CordVtnNodeConfig or null |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 64 | */ |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 65 | public Set<CordVtnNode> cordVtnNodes() { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 66 | |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 67 | Set<CordVtnNode> nodes = Sets.newHashSet(); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 68 | JsonNode jsonNodes = object.get(CORDVTN_NODES); |
| 69 | if (jsonNodes == null) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 70 | log.debug("No CORD VTN nodes found"); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 71 | return null; |
| 72 | } |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 73 | |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 74 | for (JsonNode jsonNode : jsonNodes) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 75 | try { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 76 | NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(jsonNode, HOST_MANAGEMENT_IP)); |
| 77 | NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP)); |
| 78 | if (hostMgmt.prefix().contains(localMgmt.prefix()) || |
| 79 | localMgmt.prefix().contains(hostMgmt.prefix())) { |
| 80 | log.error("hostMamt and localMgmt cannot be overlapped, skip this node"); |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address(); |
| 85 | SshAccessInfo sshInfo = new SshAccessInfo( |
| 86 | hostMgmtIp, |
| 87 | TpPort.tpPort(Integer.parseInt(getConfig(object, SSH_PORT))), |
| 88 | getConfig(object, SSH_USER), getConfig(object, SSH_KEY_FILE)); |
| 89 | |
| 90 | String hostname = getConfig(jsonNode, HOSTNAME); |
| 91 | CordVtnNode newNode = new CordVtnNode( |
| 92 | hostname, hostMgmt, localMgmt, |
| 93 | NetworkAddress.valueOf(getConfig(jsonNode, DATA_PLANE_IP)), |
| 94 | TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))), |
| 95 | sshInfo, |
| 96 | DeviceId.deviceId(getConfig(jsonNode, BRIDGE_ID)), |
| 97 | getConfig(jsonNode, DATA_PLANE_INTF)); |
| 98 | |
| 99 | log.info("Successfully read {} from the config", hostname); |
| 100 | nodes.add(newNode); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 101 | } catch (IllegalArgumentException | NullPointerException e) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 102 | log.error("{}", e.toString()); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 103 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 104 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 105 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 106 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 110 | * Returns value of a given path. If the path is missing, show log and return |
| 111 | * null. |
| 112 | * |
| 113 | * @param path path |
| 114 | * @return value or null |
| 115 | */ |
| 116 | private String getConfig(JsonNode jsonNode, String path) { |
| 117 | jsonNode = jsonNode.path(path); |
| 118 | |
| 119 | if (jsonNode.isMissingNode()) { |
| 120 | log.error("{} is not configured", path); |
| 121 | return null; |
| 122 | } else { |
| 123 | log.debug("{} : {}", path, jsonNode.asText()); |
| 124 | return jsonNode.asText(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 129 | * Returns private network gateway MAC address. |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 130 | * |
| 131 | * @return mac address, or null |
| 132 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 133 | public MacAddress privateGatewayMac() { |
| 134 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 135 | if (jsonNode == null) { |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | try { |
| 140 | return MacAddress.valueOf(jsonNode.asText()); |
| 141 | } catch (IllegalArgumentException e) { |
| 142 | log.error("Wrong MAC address format {}", jsonNode.asText()); |
| 143 | return null; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 148 | * Returns public network gateway IP and MAC address pairs. |
| 149 | * |
| 150 | * @return map of ip and mac address |
| 151 | */ |
| 152 | public Map<IpAddress, MacAddress> publicGateways() { |
| 153 | JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS); |
| 154 | if (jsonNodes == null) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 155 | return Maps.newHashMap(); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 159 | jsonNodes.forEach(jsonNode -> { |
| 160 | try { |
| 161 | publicGateways.put( |
| 162 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 163 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText())); |
| 164 | } catch (IllegalArgumentException | NullPointerException e) { |
| 165 | log.error("Wrong address format {}", e.toString()); |
| 166 | } |
| 167 | }); |
| 168 | |
| 169 | return publicGateways; |
| 170 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 171 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 172 | |