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 | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 63 | * @return set of CordVtnNodeConfig or empty set |
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 | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 71 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 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)), |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 97 | getConfig(jsonNode, DATA_PLANE_INTF), |
| 98 | CordVtnNodeState.noState()); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 99 | |
| 100 | log.info("Successfully read {} from the config", hostname); |
| 101 | nodes.add(newNode); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 102 | } catch (IllegalArgumentException | NullPointerException e) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 103 | log.error("{}", e.toString()); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 104 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 105 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 106 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 107 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 111 | * Returns value of a given path. If the path is missing, show log and return |
| 112 | * null. |
| 113 | * |
| 114 | * @param path path |
| 115 | * @return value or null |
| 116 | */ |
| 117 | private String getConfig(JsonNode jsonNode, String path) { |
| 118 | jsonNode = jsonNode.path(path); |
| 119 | |
| 120 | if (jsonNode.isMissingNode()) { |
| 121 | log.error("{} is not configured", path); |
| 122 | return null; |
| 123 | } else { |
| 124 | log.debug("{} : {}", path, jsonNode.asText()); |
| 125 | return jsonNode.asText(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 130 | * Returns private network gateway MAC address. |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 131 | * |
| 132 | * @return mac address, or null |
| 133 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 134 | public MacAddress privateGatewayMac() { |
| 135 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 136 | if (jsonNode == null) { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | try { |
| 141 | return MacAddress.valueOf(jsonNode.asText()); |
| 142 | } catch (IllegalArgumentException e) { |
| 143 | log.error("Wrong MAC address format {}", jsonNode.asText()); |
| 144 | return null; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 149 | * Returns public network gateway IP and MAC address pairs. |
| 150 | * |
| 151 | * @return map of ip and mac address |
| 152 | */ |
| 153 | public Map<IpAddress, MacAddress> publicGateways() { |
| 154 | JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS); |
| 155 | if (jsonNodes == null) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 156 | return Maps.newHashMap(); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 160 | jsonNodes.forEach(jsonNode -> { |
| 161 | try { |
| 162 | publicGateways.put( |
| 163 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 164 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText())); |
| 165 | } catch (IllegalArgumentException | NullPointerException e) { |
| 166 | log.error("Wrong address format {}", e.toString()); |
| 167 | } |
| 168 | }); |
| 169 | |
| 170 | return publicGateways; |
| 171 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 172 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 173 | |