diff options
author | romaingimbert <romain.gimbert@orange.com> | 2018-04-26 16:27:38 +0200 |
---|---|---|
committer | romaingimbert <romain.gimbert@orange.com> | 2018-04-27 11:13:38 +0200 |
commit | 348a73dc21ffb4c883ea95daf6250beadaf2f5ba (patch) | |
tree | 7cd3029e89b8e2ccfcf3bc20736fa2826c808066 | |
parent | 29d157e1fa499b2f9ab134a627bf7543e7dd3409 (diff) |
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 <romain.gimbert@orange.com>
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<LinkedHashMap> find(MultiValueMap<String, String> parametersMap) { List<LinkedHashMap> sdcResponse = sdcClient.callFind(parametersMap); - List<LinkedHashMap> serviceCatalogResponse = - (List<LinkedHashMap>) findServiceSpecJsonTransformer.transform(sdcResponse); + List<LinkedHashMap> 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<LinkedHashMap> transform(Object serviceSpec) { try { - output = chainr.transform(serviceSpec); + return (List<LinkedHashMap>)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<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI()); - return (LinkedHashMap) response.getBody(); + try{ + ResponseEntity<Object> 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<LinkedHashMap> vnfs = new ArrayList<>(); LinkedHashMap relationShip = (LinkedHashMap) serviceResponse.get("relationship-list"); - if(relationShip!=null) { + if (relationShip != null) { List<LinkedHashMap> relationsList = (List<LinkedHashMap>) 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<LinkedHashMap> serviceInstances = new ArrayList<>(); if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) { - LinkedHashMap servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId); - List<LinkedHashMap> servicesInAAI = - (List<LinkedHashMap>) 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<LinkedHashMap> serviceInventoryResponse = - (List<LinkedHashMap>) findServiceInventoryJsonTransformer.transform(serviceInstances); - for (LinkedHashMap serviceInventory : serviceInventoryResponse) { - LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty"); - party.put("id", customerId); + List<LinkedHashMap> 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<LinkedHashMap> serviceInstances) { + LinkedHashMap servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId); + if(servicesInAaiForCustomer!=null){ + List<LinkedHashMap> servicesInAAI = + (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription"); + for (LinkedHashMap service : servicesInAAI) { + String serviceType = (String) service.get("service-type"); + buildServiceInstances(serviceInstances, customerId, serviceType); + } + } } private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) { LinkedHashMap serviceInstancesInAaiForCustomer = - aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType); - List<LinkedHashMap> serviceInstancesForServiceType = + aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType); + if (serviceInstancesInAaiForCustomer != null) { + List<LinkedHashMap> serviceInstancesForServiceType = (List<LinkedHashMap>) 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<LinkedHashMap> transform(List<LinkedHashMap> serviceSpec) { try { - output = chainr.transform(serviceSpec); + return (List<LinkedHashMap>)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; } } |