APEX standalone support for ToscaPolicy format
[policy/apex-pdp.git] / services / services-onappf / src / main / java / org / onap / policy / apex / services / onappf / handler / ApexEngineHandler.java
index 699ec45..f909380 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.services.onappf.handler;
 
-import com.google.gson.JsonObject;
+import java.io.File;
 import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
 import org.onap.policy.apex.service.engine.main.ApexMain;
@@ -38,6 +35,8 @@ import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,7 +46,6 @@ import org.slf4j.LoggerFactory;
  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
  */
 public class ApexEngineHandler {
-    private static final String POLICY_TYPE_IMPL = "policy_type_impl";
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ApexEngineHandler.class);
 
@@ -91,51 +89,25 @@ public class ApexEngineHandler {
         throws ApexStarterException {
         Map<ToscaPolicyIdentifier, String[]> policyArgsMap = new LinkedHashMap<>();
         for (ToscaPolicy policy : policies) {
+            String policyName = policy.getIdentifier().getName();
             final StandardCoder standardCoder = new StandardCoder();
-            String policyModel = "";
-            String apexConfig;
-            JsonObject apexConfigJsonObject = new JsonObject();
+            ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
+            ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
+            toscaTopologyTemplate.setPolicies(List.of(Map.of(policyName, policy)));
+            toscaServiceTemplate.setToscaTopologyTemplate(toscaTopologyTemplate);
+            File file;
             try {
-                for (Entry<String, Object> property : policy.getProperties().entrySet()) {
-                    JsonObject body = standardCoder.decode(standardCoder.encode(property.getValue()), JsonObject.class);
-                    if ("engineServiceParameters".equals(property.getKey())) {
-                        policyModel = standardCoder.encode(body.get(POLICY_TYPE_IMPL));
-                        body.remove(POLICY_TYPE_IMPL);
-                    }
-                    apexConfigJsonObject.add(property.getKey(), body);
-                }
-                apexConfig = standardCoder.encode(apexConfigJsonObject);
-            } catch (CoderException e) {
+                file = File.createTempFile(policyName, ".json");
+                standardCoder.encode(file, toscaServiceTemplate);
+            } catch (CoderException | IOException e) {
                 throw new ApexStarterException(e);
             }
-
-            final String modelFilePath = createFile(policyModel, "modelFile");
-
-            final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile");
-            final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath };
+            final String[] apexArgs = {"-p", file.getAbsolutePath()};
             policyArgsMap.put(policy.getIdentifier(), apexArgs);
         }
         return policyArgsMap;
     }
 
-    /**
-     * Method to create the policy model file.
-     *
-     * @param fileContent the content of the file
-     * @param fileName the name of the file
-     * @throws ApexStarterException if the file creation failed
-     */
-    private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
-        try {
-            final Path path = Files.createTempFile(fileName, ".json");
-            Files.write(path, fileContent.getBytes(StandardCharsets.UTF_8));
-            return path.toAbsolutePath().toString();
-        } catch (final IOException e) {
-            final String errorMessage = "error creating  from the properties received in PdpUpdate.";
-            throw new ApexStarterException(errorMessage, e);
-        }
-    }
-
     /**
      * Method to get the APEX engine statistics.
      */