blob: f4a8cec54a937e5dedf28f85acbdd34de96a0247 [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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.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 Moone7e4bb32016-05-16 04:32:45 -070023import org.onlab.packet.IpPrefix;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080024import org.onlab.packet.MacAddress;
Hyunsun Moone9d75992015-09-15 22:39:16 -070025import org.onlab.packet.TpPort;
26import org.onosproject.core.ApplicationId;
Hyunsun Moon7dca9b32015-10-08 22:25:30 -070027import org.onosproject.net.DeviceId;
Hyunsun Moone9d75992015-09-15 22:39:16 -070028import org.onosproject.net.config.Config;
Hyunsun Moon486ed1b2016-05-13 18:58:35 -070029import org.onosproject.xosclient.api.OpenStackAccess;
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070030import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080031import org.slf4j.Logger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070032
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080033import java.util.Map;
Hyunsun Moone9d75992015-09-15 22:39:16 -070034import java.util.Set;
35
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080036import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moone9d75992015-09-15 22:39:16 -070037
38/**
Hyunsun Moon9661d642015-09-23 13:24:35 -070039 * Configuration object for CordVtn service.
Hyunsun Moone9d75992015-09-15 22:39:16 -070040 */
41public class CordVtnConfig extends Config<ApplicationId> {
42
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080043 protected final Logger log = getLogger(getClass());
44
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080045 public static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac";
46 public static final String PUBLIC_GATEWAYS = "publicGateways";
47 public static final String GATEWAY_IP = "gatewayIp";
Hyunsun Moon3fc17f72016-01-24 21:47:06 -080048 public static final String GATEWAY_MAC = "gatewayMac";
Hyunsun Moon126171d2016-02-09 01:55:48 -080049 public static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070050 public static final String MANAGEMENT_IP = "managementIpRange";
Hyunsun Moon126171d2016-02-09 01:55:48 -080051 public static final String OVSDB_PORT = "ovsdbPort";
Hyunsun Moond05b32e2016-03-02 19:27:26 -080052
Hyunsun Moond05b32e2016-03-02 19:27:26 -080053 public static final String CORDVTN_NODES = "nodes";
Hyunsun Moon126171d2016-02-09 01:55:48 -080054 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 Moone9d75992015-09-15 22:39:16 -070059
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070060 public static final String SSH = "ssh";
61 public static final String SSH_PORT = "sshPort";
62 public static final String SSH_USER = "sshUser";
63 public static final String SSH_KEY_FILE = "sshKeyFile";
64
65 public static final String OPENSTACK = "openstack";
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070066 public static final String XOS = "xos";
67
68 public static final String ENDPOINT = "endpoint";
69 public static final String TENANT = "tenant";
70 public static final String USER = "user";
71 public static final String PASSWORD = "password";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070072
Hyunsun Moone9d75992015-09-15 22:39:16 -070073 /**
Hyunsun Moon4edb0172015-11-07 22:08:43 -080074 * Returns the set of nodes read from network config.
Hyunsun Moone9d75992015-09-15 22:39:16 -070075 *
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080076 * @return set of CordVtnNodeConfig or empty set
Hyunsun Moone9d75992015-09-15 22:39:16 -070077 */
Hyunsun Moond05b32e2016-03-02 19:27:26 -080078 public Set<CordVtnNode> cordVtnNodes() {
Hyunsun Moone9d75992015-09-15 22:39:16 -070079
Hyunsun Moond05b32e2016-03-02 19:27:26 -080080 Set<CordVtnNode> nodes = Sets.newHashSet();
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070081
82 JsonNode cordvtnNodes = object.get(CORDVTN_NODES);
83 if (cordvtnNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -080084 log.debug("No CORD VTN nodes found");
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080085 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -070086 }
Hyunsun Moon126171d2016-02-09 01:55:48 -080087
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070088 JsonNode sshNode = object.get(SSH);
89 if (sshNode == null) {
90 log.warn("SSH information not found");
91 return nodes;
92 }
93
94 for (JsonNode cordvtnNode : cordvtnNodes) {
Hyunsun Moon126171d2016-02-09 01:55:48 -080095 try {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070096 NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(cordvtnNode, HOST_MANAGEMENT_IP));
Hyunsun Moond05b32e2016-03-02 19:27:26 -080097 NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP));
98 if (hostMgmt.prefix().contains(localMgmt.prefix()) ||
99 localMgmt.prefix().contains(hostMgmt.prefix())) {
100 log.error("hostMamt and localMgmt cannot be overlapped, skip this node");
101 continue;
102 }
103
104 Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address();
105 SshAccessInfo sshInfo = new SshAccessInfo(
106 hostMgmtIp,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700107 TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))),
108 getConfig(sshNode, SSH_USER), getConfig(sshNode, SSH_KEY_FILE));
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800109
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700110 String hostname = getConfig(cordvtnNode, HOSTNAME);
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800111 CordVtnNode newNode = new CordVtnNode(
112 hostname, hostMgmt, localMgmt,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700113 NetworkAddress.valueOf(getConfig(cordvtnNode, DATA_PLANE_IP)),
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800114 TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))),
115 sshInfo,
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700116 DeviceId.deviceId(getConfig(cordvtnNode, BRIDGE_ID)),
117 getConfig(cordvtnNode, DATA_PLANE_INTF),
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -0800118 CordVtnNodeState.noState());
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800119
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800120 nodes.add(newNode);
Hyunsun Moon126171d2016-02-09 01:55:48 -0800121 } catch (IllegalArgumentException | NullPointerException e) {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700122 log.error("{}", e);
Hyunsun Moon126171d2016-02-09 01:55:48 -0800123 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800124 }
Hyunsun Moone9d75992015-09-15 22:39:16 -0700125
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800126 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -0700127 }
128
129 /**
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800130 * Returns value of a given path. If the path is missing, show log and return
131 * null.
132 *
133 * @param path path
134 * @return value or null
135 */
136 private String getConfig(JsonNode jsonNode, String path) {
137 jsonNode = jsonNode.path(path);
138
139 if (jsonNode.isMissingNode()) {
140 log.error("{} is not configured", path);
141 return null;
142 } else {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800143 return jsonNode.asText();
144 }
145 }
146
147 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800148 * Returns private network gateway MAC address.
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800149 *
150 * @return mac address, or null
151 */
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800152 public MacAddress privateGatewayMac() {
153 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800154 if (jsonNode == null) {
155 return null;
156 }
157
158 try {
159 return MacAddress.valueOf(jsonNode.asText());
160 } catch (IllegalArgumentException e) {
161 log.error("Wrong MAC address format {}", jsonNode.asText());
162 return null;
163 }
164 }
165
166 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800167 * Returns public network gateway IP and MAC address pairs.
168 *
169 * @return map of ip and mac address
170 */
171 public Map<IpAddress, MacAddress> publicGateways() {
172 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
173 if (jsonNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800174 return Maps.newHashMap();
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800175 }
176
177 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
178 jsonNodes.forEach(jsonNode -> {
179 try {
180 publicGateways.put(
181 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
182 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
183 } catch (IllegalArgumentException | NullPointerException e) {
184 log.error("Wrong address format {}", e.toString());
185 }
186 });
187
188 return publicGateways;
189 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800190
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700191 /**
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700192 * Returns management IP address range.
193 *
194 * @return management network ip prefix, or null
195 */
196 public IpPrefix managementIpRange() {
197 JsonNode jsonNode = object.get(MANAGEMENT_IP);
198 if (jsonNode == null) {
199 return null;
200 }
201
202 try {
203 return IpPrefix.valueOf(jsonNode.asText());
204 } catch (IllegalArgumentException e) {
205 log.error("{}:{} wrong address format", MANAGEMENT_IP, jsonNode);
206 return null;
207 }
208 }
209
210 /**
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700211 * Returns XOS access information.
212 *
213 * @return XOS access, or null
214 */
215 public XosAccess xosAccess() {
216 JsonNode jsonNode = object.get(XOS);
217 if (jsonNode == null) {
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700218 return null;
219 }
220
221 try {
222 return new XosAccess(getConfig(jsonNode, ENDPOINT),
223 getConfig(jsonNode, USER),
224 getConfig(jsonNode, PASSWORD));
225 } catch (NullPointerException e) {
226 log.error("Failed to get XOS access");
227 return null;
228 }
229 }
230
231 /**
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700232 * Returns OpenStack API access information.
233 *
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700234 * @return openstack access
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700235 */
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700236 public OpenStackAccess openstackAccess() {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700237 JsonNode jsonNode = object.get(OPENSTACK);
238 if (jsonNode == null) {
239 log.error("Failed to get OpenStack configurations");
240 return null;
241 }
242
243 try {
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700244 return new OpenStackAccess(
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700245 jsonNode.path(ENDPOINT).asText(),
246 jsonNode.path(TENANT).asText(),
247 jsonNode.path(USER).asText(),
248 jsonNode.path(PASSWORD).asText());
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700249 } catch (IllegalArgumentException | NullPointerException e) {
250 log.error("Failed to get OpenStack configurations");
251 return null;
252 }
253 }
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700254}