blob: 2c48ba31432838ea4c93cf278651e0d8876379c1 [file] [log] [blame]
Hyunsun Moond0e932a2015-09-15 22:39:16 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moond0e932a2015-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 Moon7f4ed9d2016-04-14 16:13:42 -070016package org.onosproject.cordvtn.api;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080019import com.google.common.collect.Maps;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070020import com.google.common.collect.Sets;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080021import org.onlab.packet.Ip4Address;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080022import org.onlab.packet.IpAddress;
Hyunsun Moon746956f2016-01-24 21:47:06 -080023import org.onlab.packet.MacAddress;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070024import org.onlab.packet.TpPort;
25import org.onosproject.core.ApplicationId;
Hyunsun Moon1f145552015-10-08 22:25:30 -070026import org.onosproject.net.DeviceId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070027import org.onosproject.net.config.Config;
Hyunsun Moon746956f2016-01-24 21:47:06 -080028import org.slf4j.Logger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070029
Hyunsun Moonae39ae82016-02-17 15:02:06 -080030import java.util.Map;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070031import java.util.Set;
32
Hyunsun Moon746956f2016-01-24 21:47:06 -080033import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070034
35/**
Hyunsun Moon2b530322015-09-23 13:24:35 -070036 * Configuration object for CordVtn service.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070037 */
38public class CordVtnConfig extends Config<ApplicationId> {
39
Hyunsun Moon746956f2016-01-24 21:47:06 -080040 protected final Logger log = getLogger(getClass());
41
Hyunsun Moonae39ae82016-02-17 15:02:06 -080042 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 Moon746956f2016-01-24 21:47:06 -080045 public static final String GATEWAY_MAC = "gatewayMac";
Hyunsun Moon133fd792016-02-09 01:55:48 -080046 public static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
47 public static final String OVSDB_PORT = "ovsdbPort";
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080048
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080049 public static final String CORDVTN_NODES = "nodes";
Hyunsun Moon133fd792016-02-09 01:55:48 -080050 public static final String HOSTNAME = "hostname";
51 public static final String HOST_MANAGEMENT_IP = "hostManagementIp";
52 public static final String DATA_PLANE_IP = "dataPlaneIp";
53 public static final String DATA_PLANE_INTF = "dataPlaneIntf";
54 public static final String BRIDGE_ID = "bridgeId";
Hyunsun Moond0e932a2015-09-15 22:39:16 -070055
Hyunsun Moon576d6872016-04-14 19:04:23 -070056 public static final String SSH = "ssh";
57 public static final String SSH_PORT = "sshPort";
58 public static final String SSH_USER = "sshUser";
59 public static final String SSH_KEY_FILE = "sshKeyFile";
60
61 public static final String OPENSTACK = "openstack";
62 public static final String OPENSTACK_ENDPOINT = "endpoint";
63 public static final String OPENSTACK_TENANT = "tenant";
64 public static final String OPENSTACK_USER = "user";
65 public static final String OPENSTACK_PASSWORD = "password";
66
Hyunsun Moond0e932a2015-09-15 22:39:16 -070067 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -080068 * Returns the set of nodes read from network config.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070069 *
Hyunsun Moonaf520d32016-03-07 16:37:17 -080070 * @return set of CordVtnNodeConfig or empty set
Hyunsun Moond0e932a2015-09-15 22:39:16 -070071 */
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080072 public Set<CordVtnNode> cordVtnNodes() {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070073
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080074 Set<CordVtnNode> nodes = Sets.newHashSet();
Hyunsun Moon576d6872016-04-14 19:04:23 -070075
76 JsonNode cordvtnNodes = object.get(CORDVTN_NODES);
77 if (cordvtnNodes == null) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080078 log.debug("No CORD VTN nodes found");
Hyunsun Moonaf520d32016-03-07 16:37:17 -080079 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070080 }
Hyunsun Moon133fd792016-02-09 01:55:48 -080081
Hyunsun Moon576d6872016-04-14 19:04:23 -070082 JsonNode sshNode = object.get(SSH);
83 if (sshNode == null) {
84 log.warn("SSH information not found");
85 return nodes;
86 }
87
88 for (JsonNode cordvtnNode : cordvtnNodes) {
Hyunsun Moon133fd792016-02-09 01:55:48 -080089 try {
Hyunsun Moon576d6872016-04-14 19:04:23 -070090 NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(cordvtnNode, HOST_MANAGEMENT_IP));
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080091 NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP));
92 if (hostMgmt.prefix().contains(localMgmt.prefix()) ||
93 localMgmt.prefix().contains(hostMgmt.prefix())) {
94 log.error("hostMamt and localMgmt cannot be overlapped, skip this node");
95 continue;
96 }
97
98 Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address();
99 SshAccessInfo sshInfo = new SshAccessInfo(
100 hostMgmtIp,
Hyunsun Moon576d6872016-04-14 19:04:23 -0700101 TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))),
102 getConfig(sshNode, SSH_USER), getConfig(sshNode, SSH_KEY_FILE));
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800103
Hyunsun Moon576d6872016-04-14 19:04:23 -0700104 String hostname = getConfig(cordvtnNode, HOSTNAME);
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800105 CordVtnNode newNode = new CordVtnNode(
106 hostname, hostMgmt, localMgmt,
Hyunsun Moon576d6872016-04-14 19:04:23 -0700107 NetworkAddress.valueOf(getConfig(cordvtnNode, DATA_PLANE_IP)),
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800108 TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))),
109 sshInfo,
Hyunsun Moon576d6872016-04-14 19:04:23 -0700110 DeviceId.deviceId(getConfig(cordvtnNode, BRIDGE_ID)),
111 getConfig(cordvtnNode, DATA_PLANE_INTF),
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800112 CordVtnNodeState.noState());
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800113
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800114 nodes.add(newNode);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800115 } catch (IllegalArgumentException | NullPointerException e) {
Hyunsun Moon576d6872016-04-14 19:04:23 -0700116 log.error("{}", e);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800117 }
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800118 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700119
Hyunsun Moon8539b042015-11-07 22:08:43 -0800120 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700121 }
122
123 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800124 * Returns value of a given path. If the path is missing, show log and return
125 * null.
126 *
127 * @param path path
128 * @return value or null
129 */
130 private String getConfig(JsonNode jsonNode, String path) {
131 jsonNode = jsonNode.path(path);
132
133 if (jsonNode.isMissingNode()) {
134 log.error("{} is not configured", path);
135 return null;
136 } else {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800137 return jsonNode.asText();
138 }
139 }
140
141 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800142 * Returns private network gateway MAC address.
Hyunsun Moon746956f2016-01-24 21:47:06 -0800143 *
144 * @return mac address, or null
145 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800146 public MacAddress privateGatewayMac() {
147 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800148 if (jsonNode == null) {
149 return null;
150 }
151
152 try {
153 return MacAddress.valueOf(jsonNode.asText());
154 } catch (IllegalArgumentException e) {
155 log.error("Wrong MAC address format {}", jsonNode.asText());
156 return null;
157 }
158 }
159
160 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800161 * Returns public network gateway IP and MAC address pairs.
162 *
163 * @return map of ip and mac address
164 */
165 public Map<IpAddress, MacAddress> publicGateways() {
166 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
167 if (jsonNodes == null) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800168 return Maps.newHashMap();
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800169 }
170
171 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
172 jsonNodes.forEach(jsonNode -> {
173 try {
174 publicGateways.put(
175 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
176 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
177 } catch (IllegalArgumentException | NullPointerException e) {
178 log.error("Wrong address format {}", e.toString());
179 }
180 });
181
182 return publicGateways;
183 }
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800184
Hyunsun Moon576d6872016-04-14 19:04:23 -0700185 /**
186 * Returns OpenStack API access information.
187 *
188 * @return openstack config
189 */
190 public OpenStackConfig openstackConfig() {
191 JsonNode jsonNode = object.get(OPENSTACK);
192 if (jsonNode == null) {
193 log.error("Failed to get OpenStack configurations");
194 return null;
195 }
196
197 try {
198 return new OpenStackConfig(
199 jsonNode.path(OPENSTACK_ENDPOINT).asText(),
200 jsonNode.path(OPENSTACK_TENANT).asText(),
201 jsonNode.path(OPENSTACK_USER).asText(),
202 jsonNode.path(OPENSTACK_PASSWORD).asText());
203 } catch (IllegalArgumentException | NullPointerException e) {
204 log.error("Failed to get OpenStack configurations");
205 return null;
206 }
207 }
208
209 /**
210 * Configuration for OpenStack API access.
211 */
212 public static class OpenStackConfig {
213
214 private final String endpoint;
215 private final String tenant;
216 private final String user;
217 private final String password;
218
219 /**
220 * Default constructor.
221 *
222 * @param endpoint Keystone endpoint
223 * @param tenant tenant name
224 * @param user user name
225 * @param password passwowrd
226 */
227 public OpenStackConfig(String endpoint, String tenant, String user, String password) {
228 this.endpoint = endpoint;
229 this.tenant = tenant;
230 this.user = user;
231 this.password = password;
232 }
233
234 /**
235 * Returns OpenStack API endpoint.
236 *
237 * @return endpoint
238 */
239 public String endpoint() {
240 return this.endpoint;
241 }
242
243 /**
244 * Returns OpenStack tenant name.
245 *
246 * @return tenant name
247 */
248 public String tenant() {
249 return this.tenant;
250 }
251
252 /**
253 * Returns OpenStack user.
254 *
255 * @return user name
256 */
257 public String user() {
258 return this.user;
259 }
260
261 /**
262 * Returns OpenStack password for the user.
263 *
264 * @return password
265 */
266 public String password() {
267 return this.password;
268 }
269 }
270}