blob: 2055e34277470cb98503fa9e932d49b9ede5496f [file] [log] [blame]
Hyunsun Moone9d75992015-09-15 22:39:16 -07001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moone9d75992015-09-15 22:39:16 -07003 *
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 Moonf9480202016-04-14 16:13:42 -070016package org.onosproject.cordvtn.api;
Hyunsun Moone9d75992015-09-15 22:39:16 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080019import com.google.common.collect.Maps;
Hyunsun Moone9d75992015-09-15 22:39:16 -070020import com.google.common.collect.Sets;
Hyunsun Moond05b32e2016-03-02 19:27:26 -080021import org.onlab.packet.Ip4Address;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080022import org.onlab.packet.IpAddress;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080023import org.onlab.packet.MacAddress;
Hyunsun Moone9d75992015-09-15 22:39:16 -070024import org.onlab.packet.TpPort;
25import org.onosproject.core.ApplicationId;
Hyunsun Moon7dca9b32015-10-08 22:25:30 -070026import org.onosproject.net.DeviceId;
Hyunsun Moone9d75992015-09-15 22:39:16 -070027import org.onosproject.net.config.Config;
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070028import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080029import org.slf4j.Logger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070030
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080031import java.util.Map;
Hyunsun Moone9d75992015-09-15 22:39:16 -070032import java.util.Set;
33
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080034import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070035
36/**
Hyunsun Moon9661d642015-09-23 13:24:35 -070037 * Configuration object for CordVtn service.
Hyunsun Moone9d75992015-09-15 22:39:16 -070038 */
39public class CordVtnConfig extends Config<ApplicationId> {
40
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080041 protected final Logger log = getLogger(getClass());
42
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080043 public static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac";
44 public static final String PUBLIC_GATEWAYS = "publicGateways";
45 public static final String GATEWAY_IP = "gatewayIp";
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080046 public static final String GATEWAY_MAC = "gatewayMac";
Hyunsun Moon126171d2016-02-09 01:55:48 -080047 public static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
48 public static final String OVSDB_PORT = "ovsdbPort";
Hyunsun Moond05b32e2016-03-02 19:27:26 -080049
Hyunsun Moond05b32e2016-03-02 19:27:26 -080050 public static final String CORDVTN_NODES = "nodes";
Hyunsun Moon126171d2016-02-09 01:55:48 -080051 public static final String HOSTNAME = "hostname";
52 public static final String HOST_MANAGEMENT_IP = "hostManagementIp";
53 public static final String DATA_PLANE_IP = "dataPlaneIp";
54 public static final String DATA_PLANE_INTF = "dataPlaneIntf";
55 public static final String BRIDGE_ID = "bridgeId";
Hyunsun Moone9d75992015-09-15 22:39:16 -070056
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070057 public static final String SSH = "ssh";
58 public static final String SSH_PORT = "sshPort";
59 public static final String SSH_USER = "sshUser";
60 public static final String SSH_KEY_FILE = "sshKeyFile";
61
62 public static final String OPENSTACK = "openstack";
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070063 public static final String XOS = "xos";
64
65 public static final String ENDPOINT = "endpoint";
66 public static final String TENANT = "tenant";
67 public static final String USER = "user";
68 public static final String PASSWORD = "password";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070069
Hyunsun Moone9d75992015-09-15 22:39:16 -070070 /**
Hyunsun Moon4edb0172015-11-07 22:08:43 -080071 * Returns the set of nodes read from network config.
Hyunsun Moone9d75992015-09-15 22:39:16 -070072 *
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080073 * @return set of CordVtnNodeConfig or empty set
Hyunsun Moone9d75992015-09-15 22:39:16 -070074 */
Hyunsun Moond05b32e2016-03-02 19:27:26 -080075 public Set<CordVtnNode> cordVtnNodes() {
Hyunsun Moone9d75992015-09-15 22:39:16 -070076
Hyunsun Moond05b32e2016-03-02 19:27:26 -080077 Set<CordVtnNode> nodes = Sets.newHashSet();
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070078
79 JsonNode cordvtnNodes = object.get(CORDVTN_NODES);
80 if (cordvtnNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -080081 log.debug("No CORD VTN nodes found");
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080082 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -070083 }
Hyunsun Moon126171d2016-02-09 01:55:48 -080084
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070085 JsonNode sshNode = object.get(SSH);
86 if (sshNode == null) {
87 log.warn("SSH information not found");
88 return nodes;
89 }
90
91 for (JsonNode cordvtnNode : cordvtnNodes) {
Hyunsun Moon126171d2016-02-09 01:55:48 -080092 try {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070093 NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(cordvtnNode, HOST_MANAGEMENT_IP));
Hyunsun Moond05b32e2016-03-02 19:27:26 -080094 NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP));
95 if (hostMgmt.prefix().contains(localMgmt.prefix()) ||
96 localMgmt.prefix().contains(hostMgmt.prefix())) {
97 log.error("hostMamt and localMgmt cannot be overlapped, skip this node");
98 continue;
99 }
100
101 Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address();
102 SshAccessInfo sshInfo = new SshAccessInfo(
103 hostMgmtIp,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700104 TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))),
105 getConfig(sshNode, SSH_USER), getConfig(sshNode, SSH_KEY_FILE));
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800106
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700107 String hostname = getConfig(cordvtnNode, HOSTNAME);
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800108 CordVtnNode newNode = new CordVtnNode(
109 hostname, hostMgmt, localMgmt,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700110 NetworkAddress.valueOf(getConfig(cordvtnNode, DATA_PLANE_IP)),
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800111 TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))),
112 sshInfo,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700113 DeviceId.deviceId(getConfig(cordvtnNode, BRIDGE_ID)),
114 getConfig(cordvtnNode, DATA_PLANE_INTF),
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800115 CordVtnNodeState.noState());
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800116
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800117 nodes.add(newNode);
Hyunsun Moon126171d2016-02-09 01:55:48 -0800118 } catch (IllegalArgumentException | NullPointerException e) {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700119 log.error("{}", e);
Hyunsun Moon126171d2016-02-09 01:55:48 -0800120 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800121 }
Hyunsun Moone9d75992015-09-15 22:39:16 -0700122
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800123 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -0700124 }
125
126 /**
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800127 * Returns value of a given path. If the path is missing, show log and return
128 * null.
129 *
130 * @param path path
131 * @return value or null
132 */
133 private String getConfig(JsonNode jsonNode, String path) {
134 jsonNode = jsonNode.path(path);
135
136 if (jsonNode.isMissingNode()) {
137 log.error("{} is not configured", path);
138 return null;
139 } else {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800140 return jsonNode.asText();
141 }
142 }
143
144 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800145 * Returns private network gateway MAC address.
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800146 *
147 * @return mac address, or null
148 */
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800149 public MacAddress privateGatewayMac() {
150 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800151 if (jsonNode == null) {
152 return null;
153 }
154
155 try {
156 return MacAddress.valueOf(jsonNode.asText());
157 } catch (IllegalArgumentException e) {
158 log.error("Wrong MAC address format {}", jsonNode.asText());
159 return null;
160 }
161 }
162
163 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800164 * Returns public network gateway IP and MAC address pairs.
165 *
166 * @return map of ip and mac address
167 */
168 public Map<IpAddress, MacAddress> publicGateways() {
169 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
170 if (jsonNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800171 return Maps.newHashMap();
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800172 }
173
174 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
175 jsonNodes.forEach(jsonNode -> {
176 try {
177 publicGateways.put(
178 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
179 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
180 } catch (IllegalArgumentException | NullPointerException e) {
181 log.error("Wrong address format {}", e.toString());
182 }
183 });
184
185 return publicGateways;
186 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800187
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700188 /**
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700189 * Returns XOS access information.
190 *
191 * @return XOS access, or null
192 */
193 public XosAccess xosAccess() {
194 JsonNode jsonNode = object.get(XOS);
195 if (jsonNode == null) {
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700196 return null;
197 }
198
199 try {
200 return new XosAccess(getConfig(jsonNode, ENDPOINT),
201 getConfig(jsonNode, USER),
202 getConfig(jsonNode, PASSWORD));
203 } catch (NullPointerException e) {
204 log.error("Failed to get XOS access");
205 return null;
206 }
207 }
208
209 /**
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700210 * Returns OpenStack API access information.
211 *
212 * @return openstack config
213 */
214 public OpenStackConfig openstackConfig() {
215 JsonNode jsonNode = object.get(OPENSTACK);
216 if (jsonNode == null) {
217 log.error("Failed to get OpenStack configurations");
218 return null;
219 }
220
221 try {
222 return new OpenStackConfig(
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700223 jsonNode.path(ENDPOINT).asText(),
224 jsonNode.path(TENANT).asText(),
225 jsonNode.path(USER).asText(),
226 jsonNode.path(PASSWORD).asText());
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700227 } catch (IllegalArgumentException | NullPointerException e) {
228 log.error("Failed to get OpenStack configurations");
229 return null;
230 }
231 }
232
233 /**
234 * Configuration for OpenStack API access.
235 */
236 public static class OpenStackConfig {
237
238 private final String endpoint;
239 private final String tenant;
240 private final String user;
241 private final String password;
242
243 /**
244 * Default constructor.
245 *
246 * @param endpoint Keystone endpoint
247 * @param tenant tenant name
248 * @param user user name
249 * @param password passwowrd
250 */
251 public OpenStackConfig(String endpoint, String tenant, String user, String password) {
252 this.endpoint = endpoint;
253 this.tenant = tenant;
254 this.user = user;
255 this.password = password;
256 }
257
258 /**
259 * Returns OpenStack API endpoint.
260 *
261 * @return endpoint
262 */
263 public String endpoint() {
264 return this.endpoint;
265 }
266
267 /**
268 * Returns OpenStack tenant name.
269 *
270 * @return tenant name
271 */
272 public String tenant() {
273 return this.tenant;
274 }
275
276 /**
277 * Returns OpenStack user.
278 *
279 * @return user name
280 */
281 public String user() {
282 return this.user;
283 }
284
285 /**
286 * Returns OpenStack password for the user.
287 *
288 * @return password
289 */
290 public String password() {
291 return this.password;
292 }
293 }
294}