blob: 29ffaea7c4f93190e49ff1b3ffcc45bd555ec3a1 [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 Moon3ef52492016-06-15 14:56:31 -070019import com.google.common.base.Strings;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080020import com.google.common.collect.Maps;
Hyunsun Moone9d75992015-09-15 22:39:16 -070021import com.google.common.collect.Sets;
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;
26import org.onosproject.net.config.Config;
Hyunsun Moon486ed1b2016-05-13 18:58:35 -070027import org.onosproject.xosclient.api.OpenStackAccess;
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 Moon60a10672016-06-12 17:39:12 -070043 private static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac";
44 private static final String PUBLIC_GATEWAYS = "publicGateways";
45 private static final String GATEWAY_IP = "gatewayIp";
46 private static final String GATEWAY_MAC = "gatewayMac";
47 private static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
48 private static final String OVSDB_PORT = "ovsdbPort";
Hyunsun Moond05b32e2016-03-02 19:27:26 -080049
Hyunsun Moon60a10672016-06-12 17:39:12 -070050 private static final String CORDVTN_NODES = "nodes";
51 private static final String HOSTNAME = "hostname";
52 private static final String HOST_MANAGEMENT_IP = "hostManagementIp";
Hyunsun Moon3ef52492016-06-15 14:56:31 -070053 private static final String HOST_MANAGEMENT_IFACE = "hostManagementIface";
54 private static final String DATA_IP = "dataPlaneIp";
55 private static final String DATA_IFACE = "dataPlaneIntf";
56 private static final String INTEGRATION_BRIDGE_ID = "bridgeId";
Hyunsun Moone9d75992015-09-15 22:39:16 -070057
Hyunsun Moon60a10672016-06-12 17:39:12 -070058 private static final String SSH = "ssh";
59 private static final String SSH_PORT = "sshPort";
60 private static final String SSH_USER = "sshUser";
61 private static final String SSH_KEY_FILE = "sshKeyFile";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070062
Hyunsun Moon60a10672016-06-12 17:39:12 -070063 private static final String OPENSTACK = "openstack";
64 private static final String XOS = "xos";
Hyunsun Moonb9c366e2016-04-24 17:43:32 -070065
Hyunsun Moon60a10672016-06-12 17:39:12 -070066 private static final String ENDPOINT = "endpoint";
67 private static final String TENANT = "tenant";
68 private static final String USER = "user";
69 private static final String PASSWORD = "password";
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070070
Hyunsun Moon3ef52492016-06-15 14:56:31 -070071 // TODO implement isValid
72
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
Hyunsun Moon3ef52492016-06-15 14:56:31 -070082 // TODO implement isValid and move these blocks to it
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070083 JsonNode cordvtnNodes = object.get(CORDVTN_NODES);
84 if (cordvtnNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -080085 log.debug("No CORD VTN nodes found");
Hyunsun Moon58ddbdc2016-03-07 16:37:17 -080086 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -070087 }
Hyunsun Moon126171d2016-02-09 01:55:48 -080088
Hyunsun Moon61e79ee2016-04-14 19:04:23 -070089 JsonNode sshNode = object.get(SSH);
90 if (sshNode == null) {
91 log.warn("SSH information not found");
92 return nodes;
93 }
94
95 for (JsonNode cordvtnNode : cordvtnNodes) {
Hyunsun Moon3ef52492016-06-15 14:56:31 -070096 // TODO implement isValid and move this block to it
97 NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(cordvtnNode, HOST_MANAGEMENT_IP));
98 NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP));
99 if (hostMgmt.prefix().contains(localMgmt.prefix()) ||
100 localMgmt.prefix().contains(hostMgmt.prefix())) {
101 log.error("hostMamt and localMgmt cannot be overlapped, skip this node");
102 continue;
Hyunsun Moon126171d2016-02-09 01:55:48 -0800103 }
Hyunsun Moone9d75992015-09-15 22:39:16 -0700104
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700105 String hostname = getConfig(cordvtnNode, HOSTNAME);
106 SshAccessInfo sshInfo = new SshAccessInfo(
107 hostMgmt.ip().getIp4Address(),
108 TpPort.tpPort(Integer.parseInt(getConfig(sshNode, SSH_PORT))),
109 getConfig(sshNode, SSH_USER), getConfig(sshNode, SSH_KEY_FILE));
110
111 CordVtnNode.Builder nodeBuilder = CordVtnNode.builder()
112 .hostname(hostname)
113 .hostMgmtIp(hostMgmt)
114 .localMgmtIp(localMgmt)
115 .dataIp(getConfig(cordvtnNode, DATA_IP))
116 .sshInfo(sshInfo)
117 .integrationBridgeId(getConfig(cordvtnNode, INTEGRATION_BRIDGE_ID))
118 .dataIface(getConfig(cordvtnNode, DATA_IFACE));
119
120 String ovsdbPort = getConfig(object, OVSDB_PORT);
121 if (!Strings.isNullOrEmpty(ovsdbPort)) {
122 nodeBuilder.ovsdbPort(Integer.parseInt(ovsdbPort));
123 }
124
125 String hostMgmtIface = getConfig(cordvtnNode, HOST_MANAGEMENT_IFACE);
126 if (!Strings.isNullOrEmpty(hostMgmtIface)) {
127 nodeBuilder.hostMgmtIface(hostMgmtIface);
128 }
129
130 nodes.add(nodeBuilder.build());
131 }
Hyunsun Moon4edb0172015-11-07 22:08:43 -0800132 return nodes;
Hyunsun Moone9d75992015-09-15 22:39:16 -0700133 }
134
135 /**
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800136 * Returns value of a given path. If the path is missing, show log and return
137 * null.
138 *
139 * @param path path
140 * @return value or null
141 */
142 private String getConfig(JsonNode jsonNode, String path) {
143 jsonNode = jsonNode.path(path);
144
145 if (jsonNode.isMissingNode()) {
Hyunsun Moon3ef52492016-06-15 14:56:31 -0700146 log.debug("{} is not configured", path);
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800147 return null;
148 } else {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800149 return jsonNode.asText();
150 }
151 }
152
153 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800154 * Returns private network gateway MAC address.
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800155 *
156 * @return mac address, or null
157 */
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800158 public MacAddress privateGatewayMac() {
159 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon3fc17f72016-01-24 21:47:06 -0800160 if (jsonNode == null) {
161 return null;
162 }
163
164 try {
165 return MacAddress.valueOf(jsonNode.asText());
166 } catch (IllegalArgumentException e) {
167 log.error("Wrong MAC address format {}", jsonNode.asText());
168 return null;
169 }
170 }
171
172 /**
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800173 * Returns public network gateway IP and MAC address pairs.
174 *
175 * @return map of ip and mac address
176 */
177 public Map<IpAddress, MacAddress> publicGateways() {
178 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
179 if (jsonNodes == null) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800180 return Maps.newHashMap();
Hyunsun Moonb5f92e52016-02-17 15:02:06 -0800181 }
182
183 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
184 jsonNodes.forEach(jsonNode -> {
185 try {
186 publicGateways.put(
187 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
188 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
189 } catch (IllegalArgumentException | NullPointerException e) {
190 log.error("Wrong address format {}", e.toString());
191 }
192 });
193
194 return publicGateways;
195 }
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800196
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700197 /**
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700198 * Returns XOS access information.
199 *
200 * @return XOS access, or null
201 */
202 public XosAccess xosAccess() {
203 JsonNode jsonNode = object.get(XOS);
204 if (jsonNode == null) {
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700205 return null;
206 }
207
208 try {
209 return new XosAccess(getConfig(jsonNode, ENDPOINT),
210 getConfig(jsonNode, USER),
211 getConfig(jsonNode, PASSWORD));
212 } catch (NullPointerException e) {
213 log.error("Failed to get XOS access");
214 return null;
215 }
216 }
217
218 /**
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700219 * Returns OpenStack API access information.
220 *
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700221 * @return openstack access
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700222 */
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700223 public OpenStackAccess openstackAccess() {
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700224 JsonNode jsonNode = object.get(OPENSTACK);
225 if (jsonNode == null) {
226 log.error("Failed to get OpenStack configurations");
227 return null;
228 }
229
230 try {
Hyunsun Moon486ed1b2016-05-13 18:58:35 -0700231 return new OpenStackAccess(
Hyunsun Moonb9c366e2016-04-24 17:43:32 -0700232 jsonNode.path(ENDPOINT).asText(),
233 jsonNode.path(TENANT).asText(),
234 jsonNode.path(USER).asText(),
235 jsonNode.path(PASSWORD).asText());
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700236 } catch (IllegalArgumentException | NullPointerException e) {
237 log.error("Failed to get OpenStack configurations");
238 return null;
239 }
240 }
Hyunsun Moon61e79ee2016-04-14 19:04:23 -0700241}