diff options
author | a.sreekumar <ajith.sreekumar@bell.ca> | 2020-09-02 17:19:26 +0100 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@bell.ca> | 2020-09-09 10:18:02 +0100 |
commit | 4c5c31eb9a2513af080d60d0f537b8339856150d (patch) | |
tree | 79aaf0d50cc261f57ce77f2eeae927c3c264478f /services/services-onappf/src/main | |
parent | d350fd659d716ca0b1678029230cc799cead2056 (diff) |
APEX standalone support for ToscaPolicy format
Legacy format support is removed, and Tosca format support is added.
Change-Id: I3cfc181ccb5471a5d224c0162af18c1fa0fdbc70
Issue-ID: POLICY-2812
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
Diffstat (limited to 'services/services-onappf/src/main')
-rw-r--r-- | services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java | 56 |
1 files changed, 14 insertions, 42 deletions
diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 699ec4584..f90938013 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -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. @@ -20,16 +21,12 @@ 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,52 +89,26 @@ 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. */ public List<AxEngineModel> getEngineStats() { |