Find service inventory 87/44887/2
authorromaingimbert <romain.gimbert@orange.com>
Thu, 26 Apr 2018 14:27:38 +0000 (16:27 +0200)
committerromaingimbert <romain.gimbert@orange.com>
Fri, 27 Apr 2018 09:13:38 +0000 (11:13 +0200)
- 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>
src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationService.java
src/main/java/org/onap/nbi/apis/servicecatalog/jolt/FindServiceSpecJsonTransformer.java
src/main/java/org/onap/nbi/apis/serviceinventory/AaiClient.java
src/main/java/org/onap/nbi/apis/serviceinventory/ServiceInventoryService.java
src/main/java/org/onap/nbi/apis/serviceinventory/jolt/FindServiceInventoryJsonTransformer.java

index e89960a..f34f965 100644 (file)
  */
 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;
     }
 }
index f9b889e..9ec59ea 100644 (file)
  */
 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;
     }
 
 }
index 698981b..273e287 100644 (file)
@@ -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) {
index 3208de4..df7ab74 100644 (file)
@@ -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);
         }
 
 
index 05f789f..70fe0c8 100644 (file)
  */
 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;
     }
 
 }