2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.util;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.type.TypeFactory;
27 import com.sun.jersey.api.client.Client;
28 import com.sun.jersey.api.client.ClientHandlerException;
29 import com.sun.jersey.api.client.ClientResponse;
31 import java.io.IOException;
32 import java.security.KeyManagementException;
33 import java.security.KeyStoreException;
34 import java.security.UnrecoverableKeyException;
35 import java.security.cert.CertificateException;
36 import java.security.NoSuchAlgorithmException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.UUID;
41 import org.onap.aai.exceptions.AAIException;
43 public class RestController implements RestControllerInterface {
45 private static final String TARGET_NAME = "AAI";
46 private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class);
48 private static Client client = null;
50 private String restSrvrBaseURL;
52 private String overrideLocalHost = null;
54 // To do - Come up with helper function that will automatically
55 // generate the REST API path based on path parameter(s) and query parameter(s)!
56 public static final String REST_APIPATH_COMPLEXES = "cloud-infrastructure/complexes";
57 public static final String REST_APIPATH_COMPLEX = "cloud-infrastructure/complexes/complex/";
58 public static final String REST_APIPATH_PSERVERS = "cloud-infrastructure/pservers";
59 public static final String REST_APIPATH_PSERVER = "cloud-infrastructure/pservers/pserver/";
60 public static final String REST_APIPATH_PHYSICALLINKS = "network/physical-links/";
61 public static final String REST_APIPATH_PHYSICALLINK = "network/physical-links/physical-link/";
62 public static final String REST_APIPATH_PINTERFACES = "network/p-interfaces/";
63 public static final String REST_APIPATH_PINTERFACE = "network/p-interfaces/p-interface/";
64 public static final String REST_APIPATH_VPLSPES = "network/vpls-pes/";
65 public static final String REST_APIPATH_VPLSPE = "network/vpls-pes/vpls-pe/";
66 public static final String REST_APIPATH_UPDATE = "actions/update/";
67 public static final String REST_APIPATH_SEARCH = "search/nodes-query?search-node-type=";
69 public static final String REST_APIPATH_CLOUDREGION = "cloud-infrastructure/cloud-regions/cloud-region/";
70 public static final String REST_APIPATH_TENANT = "cloud-infrastructure/tenants/tenant/";
71 public static final String REST_APIPATH_VIRTUAL_DATA_CENTER =
72 "cloud-infrastructure/virtual-data-centers/virtual-data-center/";
73 public static final String REST_APIPATH_VIRTUAL_DATA_CENTERS = "cloud-infrastructure/virtual-data-centers/";
74 public static final String REST_APIPATH_GENERIC_VNF = "network/generic-vnfs/generic-vnf/";
75 public static final String REST_APIPATH_GENERIC_VNFS = "network/generic-vnfs";
76 public static final String REST_APIPATH_L3_NETWORK = "network/l3-networks/l3-network/";
77 public static final String REST_APIPATH_L3_NETWORKS = "network/l3-networks";
78 public static final String REST_APIPATH_INSTANCE_GROUP = "network/instance-groups/instance-group";
79 public static final String REST_APIPATH_INSTANCE_GROUPS = "network/instance-groups";
80 public static final String REST_APIPATH_VFMODULE = "nodes/vf-modules/vf-module/";
82 public static final String REST_APIPATH_VCE = "network/vces/vce/";
84 public static final String REST_APIPATH_SERVICE = "service-design-and-creation/services/service/";
85 public static final String REST_APIPATH_LOGICALLINKS = "network/logical-links/";
86 public static final String REST_APIPATH_LOGICALLINK = "network/logical-links/logical-link/";
88 public RestController(String truststorePath, String truststorePassword, String keystorePath, String keystorePassword) throws AAIException {
89 this.initRestClient(truststorePath, truststorePassword, keystorePath, keystorePassword);
92 * Inits the rest client.
94 * @throws AAIException the AAI exception
96 public void initRestClient(String truststorePath, String truststorePassword, String keystorePath, String keystorePassword) throws AAIException {
99 client = getHttpsAuthClient(truststorePath, truststorePassword, keystorePath, keystorePassword);
100 } catch (KeyManagementException e) {
101 throw new AAIException("AAI_7117", "KeyManagementException in REST call to DB: " + e.toString());
102 } catch (Exception e) {
103 throw new AAIException("AAI_7117", " Exception in REST call to DB: " + e.toString());
107 public Client getHttpsAuthClient(String truststorePath, String truststorePassword, String keystorePath, String keystorePassword) throws KeyManagementException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
108 return HttpsAuthClient.getClient(truststorePath, truststorePassword, keystorePath, keystorePassword);
111 public Client getHttpsAuthClient() throws KeyManagementException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, AAIException {
112 return HttpsAuthClient.getClient();
115 * Sets the rest srvr base URL.
117 * @param baseURL the base URL
118 * @throws AAIException the AAI exception
120 public void SetRestSrvrBaseURL(String baseURL) throws AAIException {
122 throw new AAIException("AAI_7117", "REST Server base URL cannot be null.");
123 restSrvrBaseURL = baseURL;
127 * Gets the rest srvr base URL.
129 * @return the rest srvr base URL
131 public String getRestSrvrBaseURL() {
132 return restSrvrBaseURL;
135 public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver)
136 throws AAIException {
137 Get(t, sourceID, transId, path, restObject, oldserver, AAIConstants.AAI_RESOURCES_PORT);
141 * To do - optimization and automation. Also make it as generic as possible.
143 * @param <T> the generic type
145 * @param sourceID the source ID
146 * @param transId the trans id
147 * @param path the path
148 * @param restObject the rest object
149 * @param oldserver the oldserver
150 * @throws AAIException the AAI exception
152 @SuppressWarnings("unchecked")
153 public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver,
154 int port) throws AAIException {
155 String methodName = "Get";
157 transId += ":" + UUID.randomUUID().toString();
159 LOGGER.debug(methodName + " start");
164 url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path;
166 if (overrideLocalHost == null) {
168 AAIConfig.get(AAIConstants.AAI_LOCAL_OVERRIDE, AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT);
170 if (AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT.equals(overrideLocalHost)) {
171 url = String.format(AAIConstants.AAI_LOCAL_REST, port,
172 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
174 url = String.format(AAIConstants.AAI_LOCAL_REST_OVERRIDE, overrideLocalHost,
175 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
178 LOGGER.debug(url + " for the get REST API");
179 ClientResponse cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
180 .header("X-FromAppId", sourceID).header("Real-Time", "true").type("application/json")
181 .get(ClientResponse.class);
183 // System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString());
184 // System.out.println("cres.tostring()="+cres.toString());
186 if (cres.getStatus() == 200) {
187 // System.out.println(methodName + ": url=" + url);
188 t = (T) cres.getEntity(t.getClass());
190 LOGGER.debug(methodName + "REST api GET was successfull!");
192 // System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus());
193 throw new AAIException("AAI_7116", methodName + " with status=" + cres.getStatus() + ", url=" + url);
198 * To do - optimization and automation. Also make it as generic as possible.
200 * @param <T> the generic type
202 * @param sourceID the source ID
203 * @param transId the trans id
204 * @param path the path
205 * @param restObject the rest object
206 * @param oldserver the oldserver
207 * @throws AAIException the AAI exception
209 @SuppressWarnings("unchecked")
210 public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, String apiVersion)
211 throws AAIException {
212 String methodName = "Get";
214 transId += ":" + UUID.randomUUID().toString();
216 LOGGER.debug(methodName + " start");
220 url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/" + path;
222 LOGGER.debug(url + " for the get REST API");
223 ClientResponse cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
224 .header("X-FromAppId", sourceID).header("Real-Time", "true").type("application/json")
225 .get(ClientResponse.class);
227 // System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString());
228 // System.out.println("cres.tostring()="+cres.toString());
230 if (cres.getStatus() == 200) {
231 // System.out.println(methodName + ": url=" + url);
232 t = (T) cres.getEntity(t.getClass());
234 LOGGER.debug(methodName + "REST api GET was successfull!");
236 // System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus());
237 throw new AAIException("AAI_7116", methodName + " with status=" + cres.getStatus() + ", url=" + url);
242 * Map json to object list.
244 * @param <T> the generic type
245 * @param typeDef the type def
246 * @param json the json
247 * @param clazz the clazz
249 * @throws Exception the exception
251 private <T> List<T> mapJsonToObjectList(T typeDef, String json, Class clazz) throws Exception {
253 ObjectMapper mapper = new ObjectMapper();
254 System.out.println(json);
255 TypeFactory t = TypeFactory.defaultInstance();
256 list = mapper.readValue(json, t.constructCollectionType(ArrayList.class, clazz));
264 * @param <T> the generic type
266 * @param sourceID the source ID
267 * @param transId the trans id
268 * @param path the path
269 * @throws AAIException the AAI exception
271 public <T> void Put(T t, String sourceID, String transId, String path) throws AAIException {
272 Put(t, sourceID, transId, path, false, AAIConstants.AAI_RESOURCES_PORT);
278 * @param <T> the generic type
280 * @param sourceID the source ID
281 * @param transId the trans id
282 * @param path the path
283 * @throws AAIException the AAI exception
285 public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException {
286 Put(t, sourceID, transId, path, oldserver, AAIConstants.AAI_RESOURCES_PORT);
292 * @param <T> the generic type
294 * @param sourceID the source ID
295 * @param transId the trans id
296 * @param path the path
297 * @param oldserver the oldserver
298 * @throws AAIException the AAI exception
300 public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver, int port)
301 throws AAIException {
302 String methodName = "Put";
304 transId += ":" + UUID.randomUUID().toString();
306 LOGGER.debug(methodName + " start");
309 url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path;
311 if (overrideLocalHost == null) {
313 AAIConfig.get(AAIConstants.AAI_LOCAL_OVERRIDE, AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT);
315 if (AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT.equals(overrideLocalHost)) {
316 url = String.format(AAIConstants.AAI_LOCAL_REST, port,
317 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
319 url = String.format(AAIConstants.AAI_LOCAL_REST_OVERRIDE, overrideLocalHost,
320 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
324 ClientResponse cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
325 .header("X-FromAppId", sourceID).header("Real-Time", "true").type("application/json").entity(t)
326 .put(ClientResponse.class);
328 // System.out.println("cres.tostring()="+cres.toString());
330 int statuscode = cres.getStatus();
331 if (statuscode >= 200 && statuscode <= 299) {
332 LOGGER.debug(methodName + ": url=" + url + ", request=" + path);
334 throw new AAIException("AAI_7116", methodName + " with status=" + statuscode + ", url=" + url + ", msg="
335 + cres.getEntity(String.class));
342 * @param <T> the generic type
344 * @param sourceID the source ID
345 * @param transId the trans id
346 * @param path the path
347 * @param apiVersion version number
348 * @throws AAIException the AAI exception
350 public <T> void Put(T t, String sourceID, String transId, String path, String apiVersion)
351 throws AAIException {
352 String methodName = "Put";
354 transId += ":" + UUID.randomUUID().toString();
356 LOGGER.debug(methodName + " start");
358 url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/" + path;
360 ClientResponse cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
361 .header("X-FromAppId", sourceID).header("Real-Time", "true").type("application/json").entity(t)
362 .put(ClientResponse.class);
364 // System.out.println("cres.tostring()="+cres.toString());
366 int statuscode = cres.getStatus();
367 if (statuscode >= 200 && statuscode <= 299) {
368 LOGGER.debug(methodName + ": url=" + url + ", request=" + path);
370 throw new AAIException("AAI_7116", methodName + " with status=" + statuscode + ", url=" + url + ", msg="
371 + cres.getEntity(String.class));
375 public void Delete(String sourceID, String transId, String path) throws AAIException {
376 Delete(sourceID, transId, path, AAIConstants.AAI_RESOURCES_PORT);
382 * @param sourceID the source ID
383 * @param transId the trans id
384 * @param path the path
385 * @throws AAIException the AAI exception
387 public void Delete(String sourceID, String transId, String path, int port) throws AAIException {
388 String methodName = "Delete";
390 transId += ":" + UUID.randomUUID().toString();
392 LOGGER.debug(methodName + " start");
394 String request = "{}";
395 if (overrideLocalHost == null) {
396 overrideLocalHost = AAIConfig.get(AAIConstants.AAI_LOCAL_OVERRIDE, AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT);
398 if (AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT.equals(overrideLocalHost)) {
399 url = String.format(AAIConstants.AAI_LOCAL_REST, port,
400 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
402 url = String.format(AAIConstants.AAI_LOCAL_REST_OVERRIDE, overrideLocalHost,
403 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
405 ClientResponse cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
406 .header("X-FromAppId", sourceID).header("Real-Time", "true").type("application/json").entity(request)
407 .delete(ClientResponse.class);
409 if (cres.getStatus() == 404) { // resource not found
410 LOGGER.info("Resource does not exist...: " + cres.getStatus() + ":" + cres.getEntity(String.class));
411 } else if (cres.getStatus() == 200 || cres.getStatus() == 204) {
412 LOGGER.info("Resource " + url + " deleted");
414 LOGGER.error("Deleting Resource failed: " + cres.getStatus() + ":" + cres.getEntity(String.class));
415 throw new AAIException("AAI_7116", "Error during DELETE");
419 public <T> String Post(T t, String sourceID, String transId, String path) throws Exception {
420 return Post(t, sourceID, transId, path, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP));
426 * @param <T> the generic type
428 * @param sourceID the source ID
429 * @param transId the trans id
430 * @param path the path
431 * @param apiVersion the apiVersion
433 * @throws Exception the exception
435 public <T> String Post(T t, String sourceID, String transId, String path, String apiVersion) throws Exception {
436 String methodName = "Post";
438 transId += ":" + UUID.randomUUID().toString();
440 LOGGER.debug(methodName + " start");
444 url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/" + path;
446 ClientResponse cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
447 .header("X-FromAppId", sourceID).header("Real-Time", "true").type("application/json").entity(t)
448 .post(ClientResponse.class);
450 int statuscode = cres.getStatus();
451 if (statuscode >= 200 && statuscode <= 299) {
452 LOGGER.debug(methodName + "REST api POST was successful!");
453 return cres.getEntity(String.class);
455 throw new AAIException("AAI_7116", methodName + " with status=" + statuscode + ", url=" + url + ", msg="
456 + cres.getEntity(String.class));
459 } catch (AAIException e) {
460 throw new AAIException("AAI_7116", methodName + " with url=" + url + ", Exception: " + e.toString());
461 } catch (Exception e) {
462 throw new AAIException("AAI_7116", methodName + " with url=" + url + ", Exception: " + e.toString());
469 * Gets the single instance of RestController.
471 * @param <T> the generic type
472 * @param clazz the clazz
473 * @return single instance of RestController
474 * @throws IllegalAccessException the illegal access exception
475 * @throws InstantiationException the instantiation exception
477 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
478 return clazz.newInstance();
482 * Does resource exist.
484 * @param <T> the generic type
485 * @param resourcePath the resource path
486 * @param resourceClassName the resource class name
487 * @param fromAppId the from app id
488 * @param transId the trans id
494 * To check whether a resource exist or get a copy of the existing version of the resource
496 * Resourcepath: should contain the qualified resource path (including encoded unique key identifier value),
497 * resourceClassName: is the canonical name of the resource class name,
501 * Will return null (if the resource doesn’t exist) (or)
502 * Will return the specified resource from the Graph.
505 * LogicalLink llink = new LogicalLink();
506 * String resourceClassName = llink.getClass().getCanonicalName();
507 * llink = RestController.DoesResourceExist("network/logical-links/logical-link/" + <encoded-link-name>,
508 * resourceClassName, fromAppId, transId);
510 public <T> T DoesResourceExist(String resourcePath, String resourceClassName, String fromAppId, String transId) {
514 RestObject<T> restObj = new RestObject<T>();
515 @SuppressWarnings("unchecked")
516 T resourceObj = (T) getInstance(Class.forName(resourceClassName));
517 restObj.set(resourceObj);
518 Get(resourceObj, fromAppId, transId, resourcePath, restObj, false, AAIConstants.AAI_RESOURCES_PORT);
520 resourceObj = restObj.get();
521 if (resourceObj != null)
524 } catch (AAIException e) {
526 } catch (ClientHandlerException che) {
528 } catch (Exception e) {
538 * @param <T> the generic type
539 * @param sourceID the source ID
540 * @param transId the trans id
541 * @param path the path
542 * @throws AAIException the AAI exception
544 public <T> void Patch(T t, String sourceID, String transId, String path) throws AAIException {
545 String methodName = "Patch";
547 transId += ":" + UUID.randomUUID().toString();
550 ClientResponse cres = null;
554 if (overrideLocalHost == null) {
556 AAIConfig.get(AAIConstants.AAI_LOCAL_OVERRIDE, AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT);
558 if (AAIConstants.AAI_LOCAL_OVERRIDE_DEFAULT.equals(overrideLocalHost)) {
559 url = String.format(AAIConstants.AAI_LOCAL_REST, AAIConstants.AAI_RESOURCES_PORT,
560 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
562 url = String.format(AAIConstants.AAI_LOCAL_REST_OVERRIDE, overrideLocalHost,
563 AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
568 cres = client.resource(url).accept("application/json").header("X-TransactionId", transId)
569 .header("X-FromAppId", sourceID).header("X-HTTP-Method-Override", "PATCH")
570 .type("application/merge-patch+json").entity(t).post(ClientResponse.class);
572 statusCode = cres.getStatus();
574 if (statusCode >= 200 && statusCode <= 299) {
575 LOGGER.debug(methodName + "REST api PATCH was successful!");
578 LOGGER.debug(methodName + "Unable to make the patch request to url " + url + " so retrying");
583 } while (numRetries >= 0);
585 LOGGER.debug(methodName + "Unable to make the patch request to url " + url + " even after trying = "
586 + numRetries + " times.");
587 throw new AAIException("AAI_7116", methodName + " with status=" + statusCode + ", url=" + url + ", msg="
588 + cres.getEntity(String.class));
590 } catch (AAIException e) {
591 throw new AAIException("AAI_7116", methodName + " with url=" + url + ", Exception: " + e.toString());
592 } catch (Exception e) {
593 throw new AAIException("AAI_7116", methodName + " with url=" + url + ", Exception: " + e.toString());