From 4c5c31eb9a2513af080d60d0f537b8339856150d Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Wed, 2 Sep 2020 17:19:26 +0100 Subject: 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 --- .../services/onappf/handler/ApexEngineHandler.java | 56 ++++++---------------- .../onappf/comm/TestPdpStateChangeListener.java | 5 +- .../onappf/comm/TestPdpUpdateListener.java | 9 ++-- 3 files changed, 22 insertions(+), 48 deletions(-) (limited to 'services/services-onappf') 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,51 +89,25 @@ public class ApexEngineHandler { throws ApexStarterException { Map 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 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. */ diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java index 17f909fcc..1d491ea03 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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. @@ -156,7 +157,7 @@ public class TestPdpStateChangeListener { assertTrue(outContent.toString().contains("State changed to active. No policies found.")); final ToscaPolicy toscaPolicy = - TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json"); final List toscaPolicies = new ArrayList(); toscaPolicies.add(toscaPolicy); final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); @@ -179,7 +180,7 @@ public class TestPdpStateChangeListener { public void testPdpStateChangeMessageListener_activetopassive() throws InterruptedException, CoderException { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); final ToscaPolicy toscaPolicy = - TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json"); final List toscaPolicies = new ArrayList(); toscaPolicies.add(toscaPolicy); final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java index 05656af96..d9b8fc09c 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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. @@ -142,7 +143,7 @@ public class TestPdpUpdateListener { TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); final ToscaPolicy toscaPolicy = - TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json"); final List toscaPolicies = new ArrayList(); toscaPolicies.add(toscaPolicy); final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); @@ -164,7 +165,7 @@ public class TestPdpUpdateListener { TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); final ToscaPolicy toscaPolicy = - TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json"); final List toscaPolicies = new ArrayList(); toscaPolicies.add(toscaPolicy); final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); @@ -185,7 +186,7 @@ public class TestPdpUpdateListener { System.setOut(new PrintStream(outContent)); final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); final ToscaPolicy toscaPolicy = - TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + TestListenerUtils.createToscaPolicy("apex_policy_name", "1.0", "src/test/resources/dummyProperties.json"); final ToscaPolicy toscaPolicy2 = TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json"); final List toscaPolicies = new ArrayList(); @@ -198,7 +199,7 @@ public class TestPdpUpdateListener { pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); final String outString = outContent.toString(); assertTrue(outString.contains( - "Apex engine started. But, only the following polices are running - apex policy name:1.0 . " + "Apex engine started. But, only the following polices are running - apex_policy_name:1.0 . " + "Other policies failed execution. Please see the logs for more details.")); } } -- cgit 1.2.3-korg