blob: b50d6fc6081a0b6686e036511b53b8aa34ef0474 [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 Moon486ed1b2016-05-13 18:58:35 -070028import org.onosproject.xosclient.api.OpenStackAccess;
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070029import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080030import org.slf4j.Logger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070031
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080032import java.util.Map;
Hyunsun Moone9d75992015-09-15 22:39:16 -070033import java.util.Set;
34
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080035import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070036
37/**
Hyunsun Moon9661d642015-09-23 13:24:35 -070038 * Configuration object for CordVtn service.
Hyunsun Moone9d75992015-09-15 22:39:16 -070039 */
40public class CordVtnConfig extends Config<ApplicationId> {
41
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080042 protected final Logger log = getLogger(getClass());
43
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080044 public static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac";
45 public static final String PUBLIC_GATEWAYS = "publicGateways";
46 public static final String GATEWAY_IP = "gatewayIp";
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080047 public static final String GATEWAY_MAC = "gatewayMac";
Hyunsun Moon126171d2016-02-09 01:55:48 -080048 public static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
49 public static final String OVSDB_PORT = "ovsdbPort";
Hyunsun Moond05b32e2016-03-02 19:27:26 -080050
Hyunsun Moond05b32e2016-03-02 19:27:26 -080051 public static final String CORDVTN_NODES = "nodes";
Hyunsun Moon126171d2016-02-09 01:55:48 -080052 public static final String HOSTNAME = "hostname";
53 public static final String HOST_MANAGEMENT_IP = "hostManagementIp";
54 public static final String DATA_PLANE_IP = "dataPlaneIp";
55 public static final String DATA_PLANE_INTF = "dataPlaneIntf";
56 public static final String BRIDGE_ID = "bridgeId";
Hyunsun Moone9d75992015-09-15 22:39:16 -070057
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070058 public static final String SSH = "ssh";
59 public static final String SSH_PORT = "sshPort";
60 public static final String SSH_USER = "sshUser";
61 public static final String SSH_KEY_FILE = "sshKeyFile";
62
63 public static final String OPENSTACK = "openstack";
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070064 public static final String XOS = "xos";
65
66 public static final String ENDPOINT = "endpoint";
67 public static final String TENANT = "tenant";
68 public static final String USER = "user";
69 public static final String PASSWORD = "password";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070070
Hyunsun Moone9d75992015-09-15 22:39:16 -070071 /**
Hyunsun Moon4edb0172015-11-07 22:08:43 -080072 * Returns the set of nodes read from network config.
Hyunsun Moone9d75992015-09-15 22:39:16 -070073 *
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080074 * @return set of CordVtnNodeConfig or empty set
Hyunsun Moone9d75992015-09-15 22:39:16 -070075 */
Hyunsun Moond05b32e2016-03-02 19:27:26 -080076 public Set<CordVtnNode> cordVtnNodes() {
Hyunsun Moone9d75992015-09-15 22:39:16 -070077
Hyunsun Moond05b32e2016-03-02 19:27:26 -080078 Set<CordVtnNode> nodes = Sets.newHashSet();
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070079
80 JsonNode cordvtnNodes = object.get(CORDVTN_NODES);
81 if (cordvtnNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -080082 log.debug("No CORD VTN nodes found");
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080083 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -070084 }
Hyunsun Moon126171d2016-02-09 01:55:48 -080085
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070086 JsonNode sshNode = object.get(SSH);
87 if (sshNode == null) {
88 log.warn("SSH information not found");
89 return nodes;
90 }
91
92 for (JsonNode cordvtnNode : cordvtnNodes) {
Hyunsun Moon126171d2016-02-09 01:55:48 -080093 try {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070094 NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(cordvtnNode, HOST_MANAGEMENT_IP));
Hyunsun Moond05b32e2016-03-02 19:27:26 -080095 NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP));
96 if (hostMgmt.prefix().contains(localMgmt.prefix()) ||
97 localMgmt.prefix().contains(hostMgmt.prefix())) {
98 log.error("hostMamt and localMgmt cannot be overlapped, skip this node");
99 continue;
100 }
101
102 Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address();
103 SshAccessInfo sshInfo = new SshAccessInfo(
104 hostMgmtIp,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700105 TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))),
106 getConfig(sshNode, SSH_USER), getConfig(sshNode, SSH_KEY_FILE));
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800107
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700108 String hostname = getConfig(cordvtnNode, HOSTNAME);
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800109 CordVtnNode newNode = new CordVtnNode(
110 hostname, hostMgmt, localMgmt,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700111 NetworkAddress.valueOf(getConfig(cordvtnNode, DATA_PLANE_IP)),
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800112 TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))),
113 sshInfo,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700114 DeviceId.deviceId(getConfig(cordvtnNode, BRIDGE_ID)),
115 getConfig(cordvtnNode, DATA_PLANE_INTF),
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800116 CordVtnNodeState.noState());
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800117
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800118 nodes.add(newNode);
Hyunsun Moon126171d2016-02-09 01:55:48 -0800119 } catch (IllegalArgumentException | NullPointerException e) {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700120 log.error("{}", e);
Hyunsun Moon126171d2016-02-09 01:55:48 -0800121 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800122 }
Hyunsun Moone9d75992015-09-15 22:39:16 -0700123
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800124 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -0700125 }
126
127 /**
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800128 * Returns value of a given path. If the path is missing, show log and return
129 * null.
130 *
131 * @param path path
132 * @return value or null
133 */
134 private String getConfig(JsonNode jsonNode, String path) {
135 jsonNode = jsonNode.path(path);
136
137 if (jsonNode.isMissingNode()) {
138 log.error("{} is not configured", path);
139 return null;
140 } else {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800141 return jsonNode.asText();
142 }
143 }
144
145 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800146 * Returns private network gateway MAC address.
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800147 *
148 * @return mac address, or null
149 */
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800150 public MacAddress privateGatewayMac() {
151 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800152 if (jsonNode == null) {
153 return null;
154 }
155
156 try {
157 return MacAddress.valueOf(jsonNode.asText());
158 } catch (IllegalArgumentException e) {
159 log.error("Wrong MAC address format {}", jsonNode.asText());
160 return null;
161 }
162 }
163
164 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800165 * Returns public network gateway IP and MAC address pairs.
166 *
167 * @return map of ip and mac address
168 */
169 public Map<IpAddress, MacAddress> publicGateways() {
170 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
171 if (jsonNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800172 return Maps.newHashMap();
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800173 }
174
175 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
176 jsonNodes.forEach(jsonNode -> {
177 try {
178 publicGateways.put(
179 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
180 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
181 } catch (IllegalArgumentException | NullPointerException e) {
182 log.error("Wrong address format {}", e.toString());
183 }
184 });
185
186 return publicGateways;
187 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800188
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700189 /**
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700190 * Returns XOS access information.
191 *
192 * @return XOS access, or null
193 */
194 public XosAccess xosAccess() {
195 JsonNode jsonNode = object.get(XOS);
196 if (jsonNode == null) {
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700197 return null;
198 }
199
200 try {
201 return new XosAccess(getConfig(jsonNode, ENDPOINT),
202 getConfig(jsonNode, USER),
203 getConfig(jsonNode, PASSWORD));
204 } catch (NullPointerException e) {
205 log.error("Failed to get XOS access");
206 return null;
207 }
208 }
209
210 /**
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700211 * Returns OpenStack API access information.
212 *
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700213 * @return openstack access
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700214 */
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700215 public OpenStackAccess openstackAccess() {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700216 JsonNode jsonNode = object.get(OPENSTACK);
217 if (jsonNode == null) {
218 log.error("Failed to get OpenStack configurations");
219 return null;
220 }
221
222 try {
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700223 return new OpenStackAccess(
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700224 jsonNode.path(ENDPOINT).asText(),
225 jsonNode.path(TENANT).asText(),
226 jsonNode.path(USER).asText(),
227 jsonNode.path(PASSWORD).asText());
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700228 } catch (IllegalArgumentException | NullPointerException e) {
229 log.error("Failed to get OpenStack configurations");
230 return null;
231 }
232 }
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700233}