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=========================================================
20 package org.onap.aai.util;
22 import java.security.KeyManagementException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.UUID;
27 import org.onap.aai.exceptions.AAIException;
28 import org.onap.aai.logging.LoggingContext;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.type.TypeFactory;
34 import com.sun.jersey.api.client.Client;
35 import com.sun.jersey.api.client.ClientHandlerException;
36 import com.sun.jersey.api.client.ClientResponse;
38 public class RestController implements RestControllerInterface {
40 private static final String TARGET_NAME = "AAI";
41 private static EELFLogger LOGGER = EELFManager.getInstance().getLogger(RestController.class);
43 private static Client client = null;
45 private String restSrvrBaseURL;
47 //To do - Come up with helper function that will automatically
48 //generate the REST API path based on path parameter(s) and query parameter(s)!
49 public static final String REST_APIPATH_COMPLEXES = "cloud-infrastructure/complexes";
50 public static final String REST_APIPATH_COMPLEX = "cloud-infrastructure/complexes/complex/";
51 public static final String REST_APIPATH_PSERVERS = "cloud-infrastructure/pservers";
52 public static final String REST_APIPATH_PSERVER = "cloud-infrastructure/pservers/pserver/";
53 public static final String REST_APIPATH_PHYSICALLINKS = "network/physical-links/";
54 public static final String REST_APIPATH_PHYSICALLINK = "network/physical-links/physical-link/";
55 public static final String REST_APIPATH_PINTERFACES = "network/p-interfaces/";
56 public static final String REST_APIPATH_PINTERFACE = "network/p-interfaces/p-interface/";
57 public static final String REST_APIPATH_VPLSPES = "network/vpls-pes/";
58 public static final String REST_APIPATH_VPLSPE = "network/vpls-pes/vpls-pe/";
59 public static final String REST_APIPATH_UPDATE = "actions/update/";
60 public static final String REST_APIPATH_SEARCH = "search/nodes-query?search-node-type=";
62 public static final String REST_APIPATH_CLOUDREGION = "cloud-infrastructure/cloud-regions/cloud-region/";
63 public static final String REST_APIPATH_TENANT = "cloud-infrastructure/tenants/tenant/";
64 public static final String REST_APIPATH_VIRTUAL_DATA_CENTER = "cloud-infrastructure/virtual-data-centers/virtual-data-center/";
65 public static final String REST_APIPATH_VIRTUAL_DATA_CENTERS = "cloud-infrastructure/virtual-data-centers/";
66 public static final String REST_APIPATH_GENERIC_VNF = "network/generic-vnfs/generic-vnf/";
67 public static final String REST_APIPATH_GENERIC_VNFS = "network/generic-vnfs";
68 public static final String REST_APIPATH_L3_NETWORK = "network/l3-networks/l3-network/";
69 public static final String REST_APIPATH_L3_NETWORKS = "network/l3-networks";
70 public static final String REST_APIPATH_INSTANCE_GROUP = "network/instance-groups/instance-group";
71 public static final String REST_APIPATH_INSTANCE_GROUPS = "network/instance-groups";
72 public static final String REST_APIPATH_VFMODULE = "nodes/vf-modules/vf-module/";
74 public static final String REST_APIPATH_VCE = "network/vces/vce/";
76 public static final String REST_APIPATH_SERVICE = "service-design-and-creation/services/service/";
77 public static final String REST_APIPATH_LOGICALLINKS = "network/logical-links/";
78 public static final String REST_APIPATH_LOGICALLINK = "network/logical-links/logical-link/";
80 public RestController() throws AAIException {
81 this.initRestClient();
84 * Inits the rest client.
86 * @throws AAIException the AAI exception
88 public void initRestClient() throws AAIException
92 client = getHttpsAuthClient();
94 catch (KeyManagementException e){
95 throw new AAIException("AAI_7117", "KeyManagementException in REST call to DB: " + e.toString());
96 } catch (Exception e) {
97 throw new AAIException("AAI_7117", " Exception in REST call to DB: " + e.toString());
102 public Client getHttpsAuthClient() throws KeyManagementException {
103 return HttpsAuthClient.getClient();
108 * Sets the rest srvr base URL.
110 * @param baseURL the base URL
111 * @throws AAIException the AAI exception
113 public void SetRestSrvrBaseURL(String baseURL) throws AAIException
116 throw new AAIException("AAI_7117", "REST Server base URL cannot be null.");
117 restSrvrBaseURL = baseURL;
121 * Gets the rest srvr base URL.
123 * @return the rest srvr base URL
125 public String getRestSrvrBaseURL()
127 return restSrvrBaseURL;
131 public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver) throws AAIException {
132 Get(t, sourceID, transId, path, restObject, oldserver, AAIConstants.AAI_RESOURCES_PORT);
135 * To do - optimization and automation. Also make it as generic as possible.
137 * @param <T> the generic type
139 * @param sourceID the source ID
140 * @param transId the trans id
141 * @param path the path
142 * @param restObject the rest object
143 * @param oldserver the oldserver
144 * @throws AAIException the AAI exception
146 @SuppressWarnings("unchecked")
147 public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver, int port) throws AAIException {
148 String methodName = "Get";
150 transId += ":" + UUID.randomUUID().toString();
152 LoggingContext.save();
153 LoggingContext.partnerName(sourceID);
154 LoggingContext.targetEntity(TARGET_NAME);
155 LoggingContext.requestId(transId);
156 LoggingContext.serviceName(methodName);
157 LoggingContext.targetServiceName(methodName);
159 LOGGER.debug(methodName + " start");
164 url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path;
166 url = String.format(AAIConstants.AAI_LOCAL_REST, port, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
168 LOGGER.debug(url + " for the get REST API");
169 ClientResponse cres = client.resource(url)
170 .accept("application/json")
171 .header("X-TransactionId", transId)
172 .header("X-FromAppId", sourceID)
173 .header("Real-Time", "true")
174 .type("application/json")
175 .get(ClientResponse.class);
177 // System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString());
178 // System.out.println("cres.tostring()="+cres.toString());
180 if (cres.getStatus() == 200) {
181 // System.out.println(methodName + ": url=" + url);
182 t = (T) cres.getEntity(t.getClass());
184 LOGGER.debug(methodName + "REST api GET was successfull!");
186 LoggingContext.restore();
187 // System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus());
188 throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url);
191 LoggingContext.restore();
195 * To do - optimization and automation. Also make it as generic as possible.
197 * @param <T> the generic type
199 * @param sourceID the source ID
200 * @param transId the trans id
201 * @param path the path
202 * @param restObject the rest object
203 * @param oldserver the oldserver
204 * @throws AAIException the AAI exception
206 @SuppressWarnings("unchecked")
207 public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, String apiVersion) throws AAIException {
208 String methodName = "Get";
210 transId += ":" + UUID.randomUUID().toString();
212 LoggingContext.save();
213 LoggingContext.partnerName(sourceID);
214 LoggingContext.targetEntity(TARGET_NAME);
215 LoggingContext.requestId(transId);
216 LoggingContext.serviceName(methodName);
217 LoggingContext.targetServiceName(methodName);
219 LOGGER.debug(methodName + " start");
223 url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/"+ path;
226 LOGGER.debug(url + " for the get REST API");
227 ClientResponse cres = client.resource(url)
228 .accept("application/json")
229 .header("X-TransactionId", transId)
230 .header("X-FromAppId", sourceID)
231 .header("Real-Time", "true")
232 .type("application/json")
233 .get(ClientResponse.class);
235 // System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString());
236 // System.out.println("cres.tostring()="+cres.toString());
238 if (cres.getStatus() == 200) {
239 // System.out.println(methodName + ": url=" + url);
240 t = (T) cres.getEntity(t.getClass());
242 LOGGER.debug(methodName + "REST api GET was successfull!");
244 LoggingContext.restore();
245 // System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus());
246 throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url);
249 LoggingContext.restore();
253 * Map json to object list.
255 * @param <T> the generic type
256 * @param typeDef the type def
257 * @param json the json
258 * @param clazz the clazz
260 * @throws Exception the exception
262 private <T> List<T> mapJsonToObjectList(T typeDef,String json, Class clazz) throws Exception
265 ObjectMapper mapper = new ObjectMapper();
266 System.out.println(json);
267 TypeFactory t = TypeFactory.defaultInstance();
268 list = mapper.readValue(json, t.constructCollectionType(ArrayList.class,clazz));
276 * @param <T> the generic type
278 * @param sourceID the source ID
279 * @param transId the trans id
280 * @param path the path
281 * @throws AAIException the AAI exception
283 public <T> void Put(T t, String sourceID, String transId, String path) throws AAIException {
284 Put( t, sourceID, transId, path, false, AAIConstants.AAI_RESOURCES_PORT);
290 * @param <T> the generic type
292 * @param sourceID the source ID
293 * @param transId the trans id
294 * @param path the path
295 * @throws AAIException the AAI exception
297 public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException {
298 Put( t, sourceID, transId, path, oldserver, AAIConstants.AAI_RESOURCES_PORT);
304 * @param <T> the generic type
306 * @param sourceID the source ID
307 * @param transId the trans id
308 * @param path the path
309 * @param oldserver the oldserver
310 * @throws AAIException the AAI exception
312 public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver, int port) throws AAIException {
313 String methodName = "Put";
315 transId += ":" + UUID.randomUUID().toString();
317 LoggingContext.save();
318 LoggingContext.partnerName(sourceID);
319 LoggingContext.targetEntity(TARGET_NAME);
320 LoggingContext.requestId(transId);
321 LoggingContext.serviceName(methodName);
322 LoggingContext.targetServiceName(methodName);
324 LOGGER.debug(methodName + " start");
329 url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path;
331 url = String.format(AAIConstants.AAI_LOCAL_REST, port, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
333 ClientResponse cres = client.resource(url)
334 .accept("application/json")
335 .header("X-TransactionId", transId)
336 .header("X-FromAppId", sourceID)
337 .header("Real-Time", "true")
338 .type("application/json")
340 .put(ClientResponse.class);
342 // System.out.println("cres.tostring()="+cres.toString());
344 int statuscode = cres.getStatus();
345 if ( statuscode >= 200 && statuscode <= 299 ) {
346 LOGGER.debug(methodName+": url=" + url + ", request=" + path);
347 LoggingContext.restore();
349 LoggingContext.restore();
350 throw new AAIException("AAI_7116", methodName +" with status="+statuscode+", url="+url + ", msg=" + cres.getEntity(String.class));
354 public void Delete(String sourceID, String transId, String path) throws AAIException {
355 Delete(sourceID, transId, path, AAIConstants.AAI_RESOURCES_PORT);
360 * @param sourceID the source ID
361 * @param transId the trans id
362 * @param path the path
363 * @throws AAIException the AAI exception
365 public void Delete(String sourceID, String transId, String path, int port) throws AAIException {
366 String methodName = "Delete";
368 transId += ":" + UUID.randomUUID().toString();
370 LoggingContext.save();
371 LoggingContext.partnerName(sourceID);
372 LoggingContext.targetEntity(TARGET_NAME);
373 LoggingContext.requestId(transId);
374 LoggingContext.serviceName(methodName);
375 LoggingContext.targetServiceName(methodName);
377 LOGGER.debug(methodName + " start");
380 String request = "{}";
381 url = String.format(AAIConstants.AAI_LOCAL_REST, port, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
382 ClientResponse cres = client.resource(url)
383 .accept("application/json")
384 .header("X-TransactionId", transId)
385 .header("X-FromAppId", sourceID)
386 .header("Real-Time", "true")
387 .type("application/json")
389 .delete(ClientResponse.class);
391 if (cres.getStatus() == 404) { // resource not found
392 LOGGER.info("Resource does not exist...: " + cres.getStatus()
393 + ":" + cres.getEntity(String.class));
394 LoggingContext.restore();
395 } else if (cres.getStatus() == 200 || cres.getStatus() == 204){
396 LOGGER.info("Resource " + url + " deleted");
397 LoggingContext.restore();
399 LOGGER.error("Deleting Resource failed: " + cres.getStatus()
400 + ":" + cres.getEntity(String.class));
401 LoggingContext.restore();
402 throw new AAIException("AAI_7116", "Error during DELETE");
406 public <T> String Post(T t, String sourceID, String transId, String path) throws Exception {
407 return Post(t, sourceID, transId, path, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP));
412 * @param <T> the generic type
414 * @param sourceID the source ID
415 * @param transId the trans id
416 * @param path the path
417 * @param apiVersion the apiVersion
419 * @throws Exception the exception
421 public <T> String Post(T t, String sourceID, String transId, String path, String apiVersion) throws Exception {
422 String methodName = "Post";
424 transId += ":" + UUID.randomUUID().toString();
426 LoggingContext.save();
427 LoggingContext.partnerName(sourceID);
428 LoggingContext.targetEntity(TARGET_NAME);
429 LoggingContext.requestId(transId);
430 LoggingContext.serviceName(methodName);
431 LoggingContext.targetServiceName(methodName);
433 LOGGER.debug(methodName + " start");
438 url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/" + path;
440 ClientResponse cres = client.resource(url)
441 .accept("application/json")
442 .header("X-TransactionId", transId)
443 .header("X-FromAppId", sourceID)
444 .header("Real-Time", "true")
445 .type("application/json")
447 .post(ClientResponse.class);
449 int statuscode = cres.getStatus();
450 if ( statuscode >= 200 && statuscode <= 299 ) {
451 LOGGER.debug(methodName + "REST api POST was successful!");
452 return cres.getEntity(String.class);
454 throw new AAIException("AAI_7116", methodName +" with status="+statuscode+", url="+url + ", msg=" + cres.getEntity(String.class));
457 } catch (AAIException e) {
458 throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString());
459 } catch (Exception e)
461 throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString());
465 LoggingContext.restore();
471 * Gets the single instance of RestController.
473 * @param <T> the generic type
474 * @param clazz the clazz
475 * @return single instance of RestController
476 * @throws IllegalAccessException the illegal access exception
477 * @throws InstantiationException the instantiation exception
479 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
481 return clazz.newInstance();
485 * Does resource exist.
487 * @param <T> the generic type
488 * @param resourcePath the resource path
489 * @param resourceClassName the resource class name
490 * @param fromAppId the from app id
491 * @param transId the trans id
497 * To check whether a resource exist or get a copy of the existing version of the resource
499 * Resourcepath: should contain the qualified resource path (including encoded unique key identifier value),
500 * resourceClassName: is the canonical name of the resource class name,
504 * Will return null (if the resource doesn’t exist) (or)
505 * Will return the specified resource from the Graph.
508 * LogicalLink llink = new LogicalLink();
509 * String resourceClassName = llink.getClass().getCanonicalName();
510 * llink = RestController.DoesResourceExist("network/logical-links/logical-link/" + <encoded-link-name>, resourceClassName, fromAppId, transId);
512 public <T> T DoesResourceExist(String resourcePath, String resourceClassName, String fromAppId, String transId) {
516 RestObject<T> restObj = new RestObject<T>();
517 @SuppressWarnings("unchecked")
518 T resourceObj = (T)getInstance(Class.forName(resourceClassName));
519 restObj.set(resourceObj);
520 Get(resourceObj, fromAppId, transId, resourcePath, restObj, false, AAIConstants.AAI_RESOURCES_PORT);
522 resourceObj = restObj.get();
523 if (resourceObj != null)
526 } catch (AAIException e) {
528 } catch (ClientHandlerException che) {
530 }catch (Exception e) {
540 * @param <T> the generic type
541 * @param sourceID the source ID
542 * @param transId the trans id
543 * @param path the path
544 * @throws AAIException the AAI exception
546 public <T> void Patch(T t, String sourceID, String transId, String path) throws AAIException {
547 String methodName = "Patch";
549 transId += ":" + UUID.randomUUID().toString();
551 LoggingContext.save();
552 LoggingContext.partnerName(sourceID);
553 LoggingContext.targetEntity(TARGET_NAME);
554 LoggingContext.requestId(transId);
555 LoggingContext.serviceName(methodName);
556 LoggingContext.targetServiceName(methodName);
560 ClientResponse cres = null;
564 url = String.format(AAIConstants.AAI_LOCAL_REST, AAIConstants.AAI_RESOURCES_PORT, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path;
569 cres = client.resource(url)
570 .accept("application/json")
571 .header("X-TransactionId", transId)
572 .header("X-FromAppId", sourceID)
573 .header("X-HTTP-Method-Override", "PATCH")
574 .type("application/merge-patch+json")
576 .post(ClientResponse.class);
578 statusCode = cres.getStatus();
580 if ( statusCode >= 200 && statusCode <= 299 ) {
581 LOGGER.debug(methodName + "REST api PATCH was successful!");
584 LOGGER.debug(methodName + "Unable to make the patch request to url " + url + " so retrying");
589 } while(numRetries >= 0);
591 LOGGER.debug(methodName + "Unable to make the patch request to url " + url + " even after trying = " + numRetries + " times.");
592 throw new AAIException("AAI_7116", methodName +" with status="+statusCode+", url="+url + ", msg=" + cres.getEntity(String.class));
594 } catch (AAIException e) {
595 throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString());
596 } catch (Exception e)
598 throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString());
602 LoggingContext.restore();