From 348a73dc21ffb4c883ea95daf6250beadaf2f5ba Mon Sep 17 00:00:00 2001 From: romaingimbert Date: Thu, 26 Apr 2018 16:27:38 +0200 Subject: [PATCH] Find service inventory - fix NPE exception when no service instance - fix cast exception when use jolt with empty collection Change-Id: I74d65fbb345946cf65c6cd61433ccb3b2a4416d7 Issue-ID: EXTAPI-78 Signed-off-by: romaingimbert --- .../ServiceSpecificationService.java | 9 ++- .../jolt/FindServiceSpecJsonTransformer.java | 13 ++-- .../onap/nbi/apis/serviceinventory/AaiClient.java | 10 ++- .../serviceinventory/ServiceInventoryService.java | 78 ++++++++++++---------- .../jolt/FindServiceInventoryJsonTransformer.java | 13 ++-- 5 files changed, 69 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationService.java b/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationService.java index e89960a..f34f965 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationService.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationService.java @@ -15,8 +15,10 @@ */ package org.onap.nbi.apis.servicecatalog; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; +import org.apache.commons.collections.CollectionUtils; import org.onap.nbi.apis.servicecatalog.jolt.FindServiceSpecJsonTransformer; import org.onap.nbi.apis.servicecatalog.jolt.GetServiceSpecJsonTransformer; import org.slf4j.Logger; @@ -59,8 +61,11 @@ public class ServiceSpecificationService { public List find(MultiValueMap parametersMap) { List sdcResponse = sdcClient.callFind(parametersMap); - List serviceCatalogResponse = - (List) findServiceSpecJsonTransformer.transform(sdcResponse); + List serviceCatalogResponse = new ArrayList<>(); + if(CollectionUtils.isNotEmpty(sdcResponse)){ + serviceCatalogResponse = + findServiceSpecJsonTransformer.transform(sdcResponse); + } return serviceCatalogResponse; } } diff --git a/src/main/java/org/onap/nbi/apis/servicecatalog/jolt/FindServiceSpecJsonTransformer.java b/src/main/java/org/onap/nbi/apis/servicecatalog/jolt/FindServiceSpecJsonTransformer.java index f9b889e..9ec59ea 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/jolt/FindServiceSpecJsonTransformer.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/jolt/FindServiceSpecJsonTransformer.java @@ -13,14 +13,15 @@ */ package org.onap.nbi.apis.servicecatalog.jolt; +import com.bazaarvoice.jolt.Chainr; +import com.bazaarvoice.jolt.JsonUtils; +import com.bazaarvoice.jolt.exception.JoltException; +import java.util.LinkedHashMap; import java.util.List; import org.onap.nbi.exceptions.TechnicalException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import com.bazaarvoice.jolt.Chainr; -import com.bazaarvoice.jolt.JsonUtils; -import com.bazaarvoice.jolt.exception.JoltException; @Service public class FindServiceSpecJsonTransformer { @@ -34,15 +35,13 @@ public class FindServiceSpecJsonTransformer { this.chainr = Chainr.fromSpec(specs); } - public Object transform(Object serviceSpec) { - Object output = null; + public List transform(Object serviceSpec) { try { - output = chainr.transform(serviceSpec); + return (List)chainr.transform(serviceSpec); } catch (JoltException joE) { LOGGER.error("Unable to transform SDC response with JOLT Transformer", joE); throw new TechnicalException("Error while parsing ONAP response"); } - return output; } } diff --git a/src/main/java/org/onap/nbi/apis/serviceinventory/AaiClient.java b/src/main/java/org/onap/nbi/apis/serviceinventory/AaiClient.java index 698981b..273e287 100644 --- a/src/main/java/org/onap/nbi/apis/serviceinventory/AaiClient.java +++ b/src/main/java/org/onap/nbi/apis/serviceinventory/AaiClient.java @@ -84,9 +84,13 @@ public class AaiClient extends BaseClient { StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH); String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId); - - ResponseEntity response = callApiGet(callUrlFormated, buildRequestHeaderForAAI()); - return (LinkedHashMap) response.getBody(); + try{ + ResponseEntity response = callApiGet(callUrlFormated, buildRequestHeaderForAAI()); + return (LinkedHashMap) response.getBody(); + } catch (BackendFunctionalException e) { + LOGGER.error("error on calling {0} , {1}" , callUrlFormated, e); + return null; + } } public LinkedHashMap getServiceInstancesInAaiForCustomer(String customerId, String serviceType) { diff --git a/src/main/java/org/onap/nbi/apis/serviceinventory/ServiceInventoryService.java b/src/main/java/org/onap/nbi/apis/serviceinventory/ServiceInventoryService.java index 3208de4..df7ab74 100644 --- a/src/main/java/org/onap/nbi/apis/serviceinventory/ServiceInventoryService.java +++ b/src/main/java/org/onap/nbi/apis/serviceinventory/ServiceInventoryService.java @@ -1,30 +1,27 @@ /** - * Copyright (c) 2018 Orange + * Copyright (c) 2018 Orange * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. */ package org.onap.nbi.apis.serviceinventory; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; +import org.apache.commons.collections.CollectionUtils; import org.onap.nbi.apis.serviceinventory.jolt.FindServiceInventoryJsonTransformer; import org.onap.nbi.apis.serviceinventory.jolt.GetServiceInventoryJsonTransformer; import org.onap.nbi.exceptions.BackendFunctionalException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; @@ -51,7 +48,7 @@ public class ServiceInventoryService { if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) { throw new BackendFunctionalException(HttpStatus.NOT_FOUND, - "serviceSpecName or serviceSpecId must be provided"); + "serviceSpecName or serviceSpecId must be provided"); } String customerId = getCustomerId(clientId); @@ -61,7 +58,7 @@ public class ServiceInventoryService { if (serviceResponse != null) { addVnfsToResponse(serviceResponse); LinkedHashMap serviceInventoryResponse = - (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse); + (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse); addRelatedPartyId(customerId, serviceInventoryResponse); return serviceInventoryResponse; } else { @@ -103,9 +100,9 @@ public class ServiceInventoryService { List vnfs = new ArrayList<>(); LinkedHashMap relationShip = (LinkedHashMap) serviceResponse.get("relationship-list"); - if(relationShip!=null) { + if (relationShip != null) { List relationsList = (List) relationShip.get("relationship"); - if(relationsList!=null) { + if (relationsList != null) { for (LinkedHashMap relation : relationsList) { String relatedLink = (String) relation.get("related-link"); LinkedHashMap vnf = aaiClient.getVNF(relatedLink); @@ -128,41 +125,52 @@ public class ServiceInventoryService { String serviceName; List serviceInstances = new ArrayList<>(); if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) { - LinkedHashMap servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId); - List servicesInAAI = - (List) servicesInAaiForCustomer.get("service-subscription"); - for (LinkedHashMap service : servicesInAAI) { - String serviceType = (String) service.get("service-type"); - buildServiceInstances(serviceInstances, customerId, serviceType); - } + handleFindWithNoServiceParam(customerId, serviceInstances); } else { serviceName = getServiceName(serviceSpecName, serviceSpecId); buildServiceInstances(serviceInstances, customerId, serviceName); } - - List serviceInventoryResponse = - (List) findServiceInventoryJsonTransformer.transform(serviceInstances); - for (LinkedHashMap serviceInventory : serviceInventoryResponse) { - LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty"); - party.put("id", customerId); + List serviceInventoryResponse = new ArrayList<>(); + if(CollectionUtils.isNotEmpty(serviceInstances)){ + serviceInventoryResponse = + findServiceInventoryJsonTransformer.transform(serviceInstances); + for (LinkedHashMap serviceInventory : serviceInventoryResponse) { + LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty"); + party.put("id", customerId); + } } return serviceInventoryResponse; + + } + + private void handleFindWithNoServiceParam(String customerId, List serviceInstances) { + LinkedHashMap servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId); + if(servicesInAaiForCustomer!=null){ + List servicesInAAI = + (List) servicesInAaiForCustomer.get("service-subscription"); + for (LinkedHashMap service : servicesInAAI) { + String serviceType = (String) service.get("service-type"); + buildServiceInstances(serviceInstances, customerId, serviceType); + } + } } private void buildServiceInstances(List serviceInstances, String customerId, String serviceType) { LinkedHashMap serviceInstancesInAaiForCustomer = - aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType); - List serviceInstancesForServiceType = + aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType); + if (serviceInstancesInAaiForCustomer != null) { + List serviceInstancesForServiceType = (List) serviceInstancesInAaiForCustomer.get("service-instance"); - if(!CollectionUtils.isEmpty(serviceInstancesForServiceType)){ - // add service type for jolt - for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) { - serviceInstanceForServiceType.put("service-type", serviceType); + if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) { + // add service type for jolt + for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) { + serviceInstanceForServiceType.put("service-type", serviceType); + } + serviceInstances.addAll(serviceInstancesForServiceType); } - serviceInstances.addAll(serviceInstancesForServiceType); } diff --git a/src/main/java/org/onap/nbi/apis/serviceinventory/jolt/FindServiceInventoryJsonTransformer.java b/src/main/java/org/onap/nbi/apis/serviceinventory/jolt/FindServiceInventoryJsonTransformer.java index 05f789f..70fe0c8 100644 --- a/src/main/java/org/onap/nbi/apis/serviceinventory/jolt/FindServiceInventoryJsonTransformer.java +++ b/src/main/java/org/onap/nbi/apis/serviceinventory/jolt/FindServiceInventoryJsonTransformer.java @@ -13,14 +13,15 @@ */ package org.onap.nbi.apis.serviceinventory.jolt; +import com.bazaarvoice.jolt.Chainr; +import com.bazaarvoice.jolt.JsonUtils; +import com.bazaarvoice.jolt.exception.JoltException; +import java.util.LinkedHashMap; import java.util.List; import org.onap.nbi.exceptions.TechnicalException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import com.bazaarvoice.jolt.Chainr; -import com.bazaarvoice.jolt.JsonUtils; -import com.bazaarvoice.jolt.exception.JoltException; @Service public class FindServiceInventoryJsonTransformer { @@ -34,15 +35,13 @@ public class FindServiceInventoryJsonTransformer { this.chainr = Chainr.fromSpec(specs); } - public Object transform(Object serviceSpec) { - Object output = null; + public List transform(List serviceSpec) { try { - output = chainr.transform(serviceSpec); + return (List)chainr.transform(serviceSpec); } catch (JoltException joE) { LOGGER.error("Unable to transform SDC response with JOLT Transformer", joE); throw new TechnicalException("Error while parsing ONAP response"); } - return output; } } -- 2.16.6