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 | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 16 | package org.opencord.cordvtn.api; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.Lists; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 25 | import org.onlab.osgi.DefaultServiceDirectory; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 28 | import org.onlab.packet.TpPort; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.ControllerInfo; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 32 | import org.onosproject.net.config.Config; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 33 | import org.onosproject.net.config.InvalidFieldException; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 34 | import org.opencord.cordvtn.api.net.CidrAddr; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 35 | import org.opencord.cordvtn.api.node.CordVtnNode; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 36 | import org.opencord.cordvtn.api.node.SshAccessInfo; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 38 | |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 39 | import java.util.List; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 40 | import java.util.Map; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 41 | import java.util.Set; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 42 | import java.util.stream.Collectors; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 43 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 44 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 45 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 46 | import static org.opencord.cordvtn.api.Constants.DEFAULT_OF_PORT; |
| 47 | import static org.opencord.cordvtn.api.Constants.DEFAULT_OF_PROTOCOL; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 51 | * Configuration object for CordVtn service. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 52 | */ |
| 53 | public class CordVtnConfig extends Config<ApplicationId> { |
| 54 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 55 | protected final Logger log = getLogger(getClass()); |
| 56 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 57 | private static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac"; |
| 58 | private static final String PUBLIC_GATEWAYS = "publicGateways"; |
| 59 | private static final String GATEWAY_IP = "gatewayIp"; |
| 60 | private static final String GATEWAY_MAC = "gatewayMac"; |
| 61 | private static final String LOCAL_MANAGEMENT_IP = "localManagementIp"; |
| 62 | private static final String OVSDB_PORT = "ovsdbPort"; |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 63 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 64 | private static final String CORDVTN_NODES = "nodes"; |
| 65 | private static final String HOSTNAME = "hostname"; |
| 66 | private static final String HOST_MANAGEMENT_IP = "hostManagementIp"; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 67 | private static final String HOST_MANAGEMENT_IFACE = "hostManagementIface"; |
| 68 | private static final String DATA_IP = "dataPlaneIp"; |
| 69 | private static final String DATA_IFACE = "dataPlaneIntf"; |
| 70 | private static final String INTEGRATION_BRIDGE_ID = "bridgeId"; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 71 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 72 | private static final String SSH = "ssh"; |
| 73 | private static final String SSH_PORT = "sshPort"; |
| 74 | private static final String SSH_USER = "sshUser"; |
| 75 | private static final String SSH_KEY_FILE = "sshKeyFile"; |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 76 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 77 | @Deprecated |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 78 | private static final String OPENSTACK = "openstack"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 79 | @Deprecated |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 80 | private static final String XOS = "xos"; |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 81 | |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 82 | private static final String CONTROLLERS = "controllers"; |
| 83 | private static final int INDEX_IP = 0; |
| 84 | private static final int INDEX_PORT = 1; |
| 85 | |
| 86 | private final ClusterService clusterService = |
| 87 | DefaultServiceDirectory.getService(ClusterService.class); |
| 88 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public boolean isValid() { |
| 91 | // check only allowed fields are present |
| 92 | boolean result = hasOnlyFields( |
| 93 | PRIVATE_GATEWAY_MAC, |
| 94 | PUBLIC_GATEWAYS, |
| 95 | LOCAL_MANAGEMENT_IP, |
| 96 | OVSDB_PORT, |
| 97 | SSH, |
| 98 | OPENSTACK, |
| 99 | XOS, |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 100 | CORDVTN_NODES, |
| 101 | CONTROLLERS); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 102 | |
| 103 | if (object.get(CORDVTN_NODES) == null || object.get(CORDVTN_NODES).size() < 1) { |
| 104 | final String msg = "No node is present"; |
| 105 | throw new IllegalArgumentException(msg); |
| 106 | } |
| 107 | |
| 108 | // check all mandatory fields are present and valid |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 109 | result = result && isMacAddress(PRIVATE_GATEWAY_MAC, MANDATORY); |
| 110 | result = result && isIpPrefix(LOCAL_MANAGEMENT_IP, MANDATORY); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 111 | |
| 112 | for (JsonNode node : object.get(CORDVTN_NODES)) { |
| 113 | ObjectNode vtnNode = (ObjectNode) node; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 114 | result = result && hasFields( |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 115 | vtnNode, |
| 116 | HOSTNAME, |
| 117 | HOST_MANAGEMENT_IP, |
| 118 | DATA_IP, |
| 119 | DATA_IFACE, |
| 120 | INTEGRATION_BRIDGE_ID); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 121 | result = result && isIpPrefix(vtnNode, HOST_MANAGEMENT_IP, MANDATORY); |
| 122 | result = result && isIpPrefix(vtnNode, DATA_IP, MANDATORY); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 123 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 124 | CidrAddr localMgmt = CidrAddr.valueOf(get(LOCAL_MANAGEMENT_IP, "")); |
| 125 | CidrAddr hostsMgmt = CidrAddr.valueOf(getConfig(vtnNode, HOST_MANAGEMENT_IP)); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 126 | if (hostsMgmt.prefix().contains(localMgmt.prefix()) || |
| 127 | localMgmt.prefix().contains(hostsMgmt.prefix())) { |
| 128 | final String msg = "Host and local management network IP conflict"; |
| 129 | throw new IllegalArgumentException(msg); |
| 130 | } |
| 131 | } |
| 132 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 133 | result = result && hasFields( |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 134 | (ObjectNode) object.get(SSH), |
| 135 | SSH_PORT, |
| 136 | SSH_USER, |
| 137 | SSH_KEY_FILE); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 138 | result = result && isTpPort( |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 139 | (ObjectNode) object.get(SSH), |
| 140 | SSH_PORT, |
| 141 | MANDATORY); |
| 142 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 143 | // check all optional fields are valid |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 144 | result = result && isTpPort(OVSDB_PORT, OPTIONAL); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 145 | |
| 146 | if (object.get(PUBLIC_GATEWAYS) != null && object.get(PUBLIC_GATEWAYS).isArray()) { |
| 147 | for (JsonNode node : object.get(PUBLIC_GATEWAYS)) { |
| 148 | ObjectNode gateway = (ObjectNode) node; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 149 | result = result && isIpAddress(gateway, GATEWAY_IP, MANDATORY); |
| 150 | result = result && isMacAddress(gateway, GATEWAY_MAC, MANDATORY); |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | if (object.get(CONTROLLERS) != null) { |
| 155 | for (JsonNode jsonNode : object.get(CONTROLLERS)) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 156 | result = result && isController(jsonNode); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | return result; |
| 160 | } |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 161 | |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 162 | private boolean isController(JsonNode jsonNode) { |
| 163 | String[] ctrl = jsonNode.asText().split(":"); |
| 164 | final String error = "Malformed controller string " + jsonNode.asText() + |
| 165 | ". Controller only takes a list of 'IP:port', 'IP', " + |
| 166 | "or just one ':port'."; |
| 167 | try { |
| 168 | if (ctrl.length == 1) { |
| 169 | IpAddress.valueOf(ctrl[INDEX_IP]); |
| 170 | return true; |
| 171 | } |
| 172 | if (ctrl.length == 2 && ctrl[INDEX_IP].isEmpty() && |
| 173 | object.get(CONTROLLERS).size() == 1) { |
| 174 | TpPort.tpPort(Integer.valueOf(ctrl[INDEX_PORT])); |
| 175 | return true; |
| 176 | } |
| 177 | if (ctrl.length == 2 && !ctrl[INDEX_IP].isEmpty()) { |
| 178 | IpAddress.valueOf(ctrl[INDEX_IP]); |
| 179 | TpPort.tpPort(Integer.valueOf(ctrl[INDEX_PORT])); |
| 180 | return true; |
| 181 | } |
| 182 | throw new InvalidFieldException(CONTROLLERS, error); |
| 183 | } catch (IllegalArgumentException e) { |
| 184 | throw new InvalidFieldException(CONTROLLERS, error); |
| 185 | } |
| 186 | } |
| 187 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 188 | /** |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 189 | * Returns the set of nodes read from network config. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 190 | * |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 191 | * @return set of CordVtnNodeConfig or empty set |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 192 | */ |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 193 | public Set<CordVtnNode> cordVtnNodes() { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 194 | Set<CordVtnNode> nodes = Sets.newHashSet(); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 195 | JsonNode sshNode = object.get(SSH); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 196 | String ovsdbPort = getConfig(object, OVSDB_PORT); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 197 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 198 | object.get(CORDVTN_NODES).forEach(vtnNode -> { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 199 | CidrAddr localMgmt = CidrAddr.valueOf(get(LOCAL_MANAGEMENT_IP, "")); |
| 200 | CidrAddr hostsMgmt = CidrAddr.valueOf(getConfig(vtnNode, HOST_MANAGEMENT_IP)); |
Hyunsun Moon | 0592c3d | 2016-06-23 14:47:52 -0700 | [diff] [blame] | 201 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 202 | SshAccessInfo sshInfo = new SshAccessInfo( |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 203 | hostsMgmt.ip().getIp4Address(), |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 204 | TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))), |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 205 | getConfig(sshNode, SSH_USER), |
| 206 | getConfig(sshNode, SSH_KEY_FILE)); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 207 | |
| 208 | CordVtnNode.Builder nodeBuilder = CordVtnNode.builder() |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 209 | .hostname(getConfig(vtnNode, HOSTNAME)) |
| 210 | .hostMgmtIp(hostsMgmt) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 211 | .localMgmtIp(localMgmt) |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 212 | .dataIp(getConfig(vtnNode, DATA_IP)) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 213 | .sshInfo(sshInfo) |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 214 | .integrationBridgeId(getConfig(vtnNode, INTEGRATION_BRIDGE_ID)) |
| 215 | .dataIface(getConfig(vtnNode, DATA_IFACE)); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 216 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 217 | if (!Strings.isNullOrEmpty(ovsdbPort)) { |
| 218 | nodeBuilder.ovsdbPort(Integer.parseInt(ovsdbPort)); |
| 219 | } |
| 220 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 221 | String hostMgmtIface = getConfig(vtnNode, HOST_MANAGEMENT_IFACE); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 222 | if (!Strings.isNullOrEmpty(hostMgmtIface)) { |
| 223 | nodeBuilder.hostMgmtIface(hostMgmtIface); |
| 224 | } |
| 225 | |
| 226 | nodes.add(nodeBuilder.build()); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 227 | }); |
| 228 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 229 | return nodes; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 233 | * Gets the specified property as a string. |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 234 | * |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 235 | * @param jsonNode node whose fields to get |
| 236 | * @param path property to get |
| 237 | * @return value as a string |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 238 | */ |
| 239 | private String getConfig(JsonNode jsonNode, String path) { |
| 240 | jsonNode = jsonNode.path(path); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 241 | return jsonNode.asText(); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 245 | * Returns private network gateway MAC address. |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 246 | * |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 247 | * @return mac address |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 248 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 249 | public MacAddress privateGatewayMac() { |
| 250 | JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 251 | return MacAddress.valueOf(jsonNode.asText()); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 255 | * Returns public network gateway IP and MAC address pairs. |
| 256 | * |
| 257 | * @return map of ip and mac address |
| 258 | */ |
| 259 | public Map<IpAddress, MacAddress> publicGateways() { |
| 260 | JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS); |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 261 | Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap(); |
| 262 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 263 | if (jsonNodes == null) { |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 264 | return publicGateways; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Hyunsun Moon | 153db18 | 2016-08-04 14:06:40 -0700 | [diff] [blame] | 267 | jsonNodes.forEach(jsonNode -> publicGateways.put( |
| 268 | IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()), |
| 269 | MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()))); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 270 | return publicGateways; |
| 271 | } |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 272 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 273 | /** |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 274 | * Returns controllers for the integration bridge. |
| 275 | * It returns the information taken from cluster service with the default OF |
| 276 | * port if no controller is specified in the network config. |
| 277 | * |
| 278 | * @return list of controller information |
| 279 | */ |
| 280 | public List<ControllerInfo> controllers() { |
| 281 | List<ControllerInfo> ctrls = Lists.newArrayList(); |
| 282 | JsonNode ctrlNodes = object.get(CONTROLLERS); |
| 283 | |
| 284 | if (ctrlNodes == null || isCtrlPortOnly()) { |
| 285 | ctrls = clusterService.getNodes().stream() |
| 286 | .map(ctrl -> new ControllerInfo( |
| 287 | ctrl.ip(), |
| 288 | ctrlNodes == null ? DEFAULT_OF_PORT : getCtrlPort(), |
| 289 | DEFAULT_OF_PROTOCOL)) |
| 290 | .collect(Collectors.toList()); |
| 291 | } else { |
| 292 | for (JsonNode ctrlNode : ctrlNodes) { |
| 293 | String[] ctrl = ctrlNode.asText().split(":"); |
| 294 | ctrls.add(new ControllerInfo( |
| 295 | IpAddress.valueOf(ctrl[INDEX_IP]), |
| 296 | ctrl.length == 1 ? DEFAULT_OF_PORT : |
| 297 | Integer.parseInt(ctrl[INDEX_PORT]), |
| 298 | DEFAULT_OF_PROTOCOL)); |
| 299 | } |
| 300 | } |
| 301 | return ImmutableList.copyOf(ctrls); |
| 302 | } |
| 303 | |
| 304 | private boolean isCtrlPortOnly() { |
| 305 | if (object.get(CONTROLLERS).size() != 1) { |
| 306 | return false; |
| 307 | } |
| 308 | JsonNode jsonNode = object.get(CONTROLLERS).get(0); |
| 309 | String[] ctrl = jsonNode.asText().split(":"); |
| 310 | return ctrl.length == 2 && ctrl[INDEX_IP].isEmpty(); |
| 311 | } |
| 312 | |
| 313 | private int getCtrlPort() { |
| 314 | JsonNode jsonNode = object.get(CONTROLLERS).get(0); |
| 315 | String[] ctrl = jsonNode.asText().split(":"); |
| 316 | return Integer.parseInt(ctrl[INDEX_PORT]); |
| 317 | } |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 318 | } |