Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Hyunsun Moon | d0e932a | 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 | */ |
| 16 | package org.onosproject.cordvtn; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 24 | import org.onlab.packet.TpPort; |
| 25 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.Config; |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 29 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 30 | import java.util.Map; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 33 | import static org.slf4j.LoggerFactory.getLogger; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 36 | * Configuration object for CordVtn service. |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 37 | */ |
| 38 | public class CordVtnConfig extends Config<ApplicationId> { |
| 39 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 40 | protected final Logger log = getLogger(getClass()); |
| 41 | |
Hyunsun Moon | ae39ae8 | 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 | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 45 | public static final String GATEWAY_MAC = "gatewayMac"; |
Hyunsun Moon | 133fd79 | 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 | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 48 | |
Hyunsun Moon | 133fd79 | 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 | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 52 | |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 53 | public static final String CORDVTN_NODES = "nodes"; |
Hyunsun Moon | 133fd79 | 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 | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 61 | * Returns the set of nodes read from network config. |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 62 | * |
Hyunsun Moon | af520d3 | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 63 | * @return set of CordVtnNodeConfig or empty set |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 64 | */ |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 65 | public Set<CordVtnNode> cordVtnNodes() { |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 66 | |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 67 | Set<CordVtnNode> nodes = Sets.newHashSet(); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 68 | JsonNode jsonNodes = object.get(CORDVTN_NODES); |
| 69 | if (jsonNodes == null) { |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 70 | log.debug("No CORD VTN nodes found"); |
Hyunsun Moon | af520d3 | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 71 | return nodes; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 72 | } |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 73 | |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 74 | for (JsonNode jsonNode : jsonNodes) { |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 75 | try { |
Hyunsun Moon | 32f3b8e | 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 | af520d3 | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 97 | getConfig(jsonNode, DATA_PLANE_INTF), |
| 98 | CordVtnNodeState.noState()); |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 99 | |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 100 | nodes.add(newNode); |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 101 | } catch (IllegalArgumentException | NullPointerException e) { |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 102 | log.error("{}", e.toString()); |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 103 | } |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 104 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 105 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 106 | return nodes; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Hyunsun Moon | 32f3b8e | 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 { |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 123 | return jsonNode.asText(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 128 | * Returns private network gateway MAC address. |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 129 | * |
| 130 | * @return mac address, or null |
| 131 | */ |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 132 | public MacAddress privateGatewayMac() { |
| 133 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 134 | if (jsonNode == null) { |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | try { |
| 139 | return MacAddress.valueOf(jsonNode.asText()); |
| 140 | } catch (IllegalArgumentException e) { |
| 141 | log.error("Wrong MAC address format {}", jsonNode.asText()); |
| 142 | return null; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 147 | * Returns public network gateway IP and MAC address pairs. |
| 148 | * |
| 149 | * @return map of ip and mac address |
| 150 | */ |
| 151 | public Map<IpAddress, MacAddress> publicGateways() { |
| 152 | JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS); |
| 153 | if (jsonNodes == null) { |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 154 | return Maps.newHashMap(); |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 158 | jsonNodes.forEach(jsonNode -> { |
| 159 | try { |
| 160 | publicGateways.put( |
| 161 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 162 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText())); |
| 163 | } catch (IllegalArgumentException | NullPointerException e) { |
| 164 | log.error("Wrong address format {}", e.toString()); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | return publicGateways; |
| 169 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 170 | } |
Hyunsun Moon | 32f3b8e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 171 | |