get resource data for operational passthrough
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImpl.java
index d6d1ec7..84dcc77 100755 (executable)
@@ -26,21 +26,29 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.api.CpsQueryService;
 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
+import org.onap.cps.ncmp.api.impl.exception.NcmpException;
+import org.onap.cps.ncmp.api.impl.operation.DmiOperations;
 import org.onap.cps.ncmp.api.models.CmHandle;
 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
+import org.onap.cps.ncmp.api.models.GenericRequestBody;
 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
 import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.exceptions.DataValidationException;
 import org.onap.cps.spi.model.DataNode;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
+
 @Slf4j
 @Service
 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
@@ -49,7 +57,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
 
     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
 
-    private static final String NCMP_ANCHOR_NAME = "ncmp-dmi-registry";
+    private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
 
     private CpsDataService cpsDataService;
 
@@ -57,14 +65,18 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
 
     private CpsQueryService cpsQueryService;
 
+    private DmiOperations dmiOperations;
+
     /**
      * Constructor Injection for Dependencies.
+     * @param dmiOperations dmi operation
      * @param cpsDataService Data Service Interface
      * @param cpsQueryService Query Service Interface
      * @param objectMapper Object Mapper
      */
-    public NetworkCmProxyDataServiceImpl(final CpsDataService cpsDataService,
+    public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations, final CpsDataService cpsDataService,
         final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
+        this.dmiOperations = dmiOperations;
         this.cpsDataService = cpsDataService;
         this.cpsQueryService = cpsQueryService;
         this.objectMapper = objectMapper;
@@ -82,7 +94,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
 
     @Override
     public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
-        final FetchDescendantsOption fetchDescendantsOption) {
+                                               final FetchDescendantsOption fetchDescendantsOption) {
         return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
     }
 
@@ -124,8 +136,10 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
             }
             final var persistenceCmHandlesList = new PersistenceCmHandlesList();
             persistenceCmHandlesList.setCmHandles(persistenceCmHandles);
-            final var cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
-            cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_ANCHOR_NAME, "/dmi-registry",
+            final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
+            cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME,
+                    NCMP_DMI_REGISTRY_ANCHOR,
+                    "/dmi-registry",
                 cmHandleJsonData);
         } catch (final JsonProcessingException e) {
             throw new DataValidationException(
@@ -133,4 +147,74 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
                 .getMessage(), e);
         }
     }
+
+    @Override
+    public Object getResourceDataOperationalFoCmHandle(final String cmHandle,
+                                                       final String resourceIdentifier,
+                                                       final String acceptParam,
+                                                       final String fieldsQueryParam,
+                                                       final Integer depthQueryParam) {
+
+        final DataNode dataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
+        final String dmiServiceName = String.valueOf(dataNode.getLeaves().get("dmi-service-name"));
+        final Collection<DataNode> additionalPropsList = dataNode.getChildDataNodes();
+        final String jsonBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList);
+        final ResponseEntity<Object> response = dmiOperations.getResouceDataFromDmi(dmiServiceName,
+                cmHandle,
+                resourceIdentifier,
+                fieldsQueryParam,
+                depthQueryParam,
+                acceptParam,
+                jsonBody);
+        return handleResponse(response);
+    }
+
+    private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) {
+        final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
+        final var dataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
+                NCMP_DMI_REGISTRY_ANCHOR,
+                xpathForDmiRegistryToFetchCmHandle,
+                FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
+        return dataNode;
+    }
+
+    private String prepareOperationBody(final GenericRequestBody.OperationEnum operation,
+                                        final Collection<DataNode> additionalPropertyList) {
+        final GenericRequestBody requestBody = new GenericRequestBody();
+        final Map<String, String> additionalPropertyMap = getAdditionalPropertiesMap(additionalPropertyList);
+        requestBody.setOperation(GenericRequestBody.OperationEnum.READ);
+        requestBody.setCmHandleProperties(additionalPropertyMap);
+        try {
+            final String requestJson = objectMapper.writeValueAsString(requestBody);
+            return requestJson;
+        } catch (final JsonProcessingException je) {
+            log.error("Parsing error occurred while converting Object to JSON.");
+            throw new NcmpException("Parsing error occurred while converting given object to JSON.",
+                    je.getMessage());
+        }
+    }
+
+    private Map<String, String> getAdditionalPropertiesMap(final Collection<DataNode> additionalPropertyList) {
+        if (additionalPropertyList == null || additionalPropertyList.size() == 0) {
+            return null;
+        }
+        final Map<String, String> additionalPropertyMap = new LinkedHashMap<>();
+        for (final DataNode node: additionalPropertyList) {
+            additionalPropertyMap.put(String.valueOf(node.getLeaves().get("name")),
+                    String.valueOf(node.getLeaves().get("value")));
+        }
+        return additionalPropertyMap;
+    }
+
+    private Object handleResponse(final ResponseEntity<Object> responseEntity) {
+        if (responseEntity.getStatusCode() == HttpStatus.OK) {
+            return responseEntity.getBody();
+        } else {
+            throw new NcmpException("Not able to get resource data.",
+                    "DMI status code: " + responseEntity.getStatusCodeValue()
+                            + ", DMI response body: " + responseEntity.getBody());
+        }
+    }
+
+
 }