diff options
-rw-r--r-- | src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java | 41 | ||||
-rw-r--r-- | src/main/java/org/onap/clamp/loop/template/PolicyModel.java | 16 | ||||
-rw-r--r-- | src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java (renamed from src/main/java/org/onap/clamp/policy/downloader/PolicyDownloader.java) | 52 | ||||
-rw-r--r-- | src/main/resources/application.properties | 5 | ||||
-rw-r--r-- | src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java | 2 | ||||
-rw-r--r-- | src/test/java/org/onap/clamp/policy/downloader/PolicyEngineControllerTestItCase.java | 65 | ||||
-rw-r--r-- | src/test/resources/application.properties | 5 | ||||
-rw-r--r-- | src/test/resources/http-cache/example/policy/api/v1/policytypes?connectionTimeToLive=5000/.file (renamed from src/test/resources/http-cache/example/policy/api/v1/policytypes/.file) | 20 | ||||
-rw-r--r-- | src/test/resources/http-cache/example/policy/api/v1/policytypes?connectionTimeToLive=5000/.header (renamed from src/test/resources/http-cache/example/policy/api/v1/policytypes/.header) | 0 |
9 files changed, 139 insertions, 67 deletions
diff --git a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java index 96294207a..e916afc1f 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -32,8 +32,12 @@ import org.apache.camel.builder.ExchangeBuilder; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; +import org.onap.clamp.loop.template.PolicyModelsRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; /** * The class implements the communication with the Policy Engine to retrieve @@ -45,6 +49,8 @@ import org.springframework.stereotype.Component; public class PolicyEngineServices { private final CamelContext camelContext; + private final PolicyModelsRepository policyModelsRepository; + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.class); private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); @@ -55,9 +61,10 @@ public class PolicyEngineServices { public static final String POLICY_RETRY_LIMIT = "policy.retry.limit"; @Autowired - public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp) { + public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp, + PolicyModelsRepository policyModelsRepository) { this.camelContext = camelContext; - + this.policyModelsRepository = policyModelsRepository; if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) { retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT)); } @@ -66,20 +73,20 @@ public class PolicyEngineServices { } } - public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) - throws InterruptedException { - return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion, - createPolicyAcronym(policyType)); + public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) { + return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion); } - public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService) - throws InterruptedException { + public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService) { return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion()); } - private static String createPolicyAcronym(String policyType) { - String[] policyNameArray = policyType.split("\\."); - return policyNameArray[policyNameArray.length - 1]; + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void createPolicyInDbIfNeeded(PolicyModel policyModel) { + if (!policyModelsRepository + .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) { + policyModelsRepository.save(policyModel); + } } /** @@ -89,7 +96,7 @@ public class PolicyEngineServices { * @return A yaml containing all policy Types and all data types * @throws InterruptedException In case of issue when sleeping during the retry */ - public String downloadAllPolicies() throws InterruptedException { + public String downloadAllPolicies() { return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models"); } @@ -101,12 +108,12 @@ public class PolicyEngineServices { * @return A string with the whole policy tosca model * @throws InterruptedException In case of issue when sleeping during the retry */ - public String downloadOnePolicy(String policyType, String policyVersion) throws InterruptedException { + public String downloadOnePolicy(String policyType, String policyVersion) { return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model"); } - private String callCamelRoute(Exchange exchange, String camelFlow) throws InterruptedException { + private String callCamelRoute(Exchange exchange, String camelFlow) { for (int i = 0; i < retryLimit; i++) { Exchange exchangeResponse = camelContext.createProducerTemplate().send(camelFlow, exchange); if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) { @@ -114,7 +121,11 @@ public class PolicyEngineServices { } else { logger.info("Policy query " + retryInterval + "ms before retrying ..."); // wait for a while and try to connect to DCAE again - Thread.sleep(retryInterval); + try { + Thread.sleep(retryInterval); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } } return ""; diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModel.java b/src/main/java/org/onap/clamp/loop/template/PolicyModel.java index 53539fccb..52f662bb4 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModel.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModel.java @@ -184,6 +184,22 @@ public class PolicyModel extends AuditEntity implements Serializable, Comparable this.policyAcronym = policyAcronym; } + /** + * Constructor with acronym generated by default from policyType. + * + * @param policyType The policyType (referenced in the blueprint + * @param policyModelTosca The policy tosca model in yaml + * @param version the version like 1.0.0 + */ + public PolicyModel(String policyType, String policyModelTosca, String version) { + this(policyType, policyModelTosca, version, createDefaultPolicyAcronym(policyType)); + } + + public static String createDefaultPolicyAcronym(String policyType) { + String[] policyNameArray = policyType.split("\\."); + return policyNameArray[policyNameArray.length - 1]; + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/org/onap/clamp/policy/downloader/PolicyDownloader.java b/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java index 8795a1257..f3eaf0c83 100644 --- a/src/main/java/org/onap/clamp/policy/downloader/PolicyDownloader.java +++ b/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java @@ -26,12 +26,12 @@ package org.onap.clamp.policy.downloader; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map.Entry; import org.onap.clamp.clds.client.PolicyEngineServices; -import org.onap.clamp.loop.template.PolicyModel; -import org.onap.clamp.loop.template.PolicyModelId; import org.onap.clamp.loop.template.PolicyModelsRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @@ -46,51 +46,41 @@ import org.yaml.snakeyaml.Yaml; */ @Configuration @Profile("clamp-policy-controller") -public class PolicyDownloader { +public class PolicyEngineController { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyDownloader.class); + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineController.class); protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval"; public static final String POLICY_RETRY_LIMIT = "policy.retry.limit"; private final PolicyEngineServices policyEngineServices; - private final PolicyModelsRepository policyModelsRepository; @Autowired - public PolicyDownloader(PolicyEngineServices policyEngineService, PolicyModelsRepository policyModelsRepository) { + public PolicyEngineController(PolicyEngineServices policyEngineService, + PolicyModelsRepository policyModelsRepository) { this.policyEngineServices = policyEngineService; - this.policyModelsRepository = policyModelsRepository; } - private void createPolicyInDbIfNeeded(PolicyModel policyModel) { - if (!policyModelsRepository - .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) { - policyModelsRepository.save(policyModel); + @Scheduled(fixedRate = 120000) + public void synchronizeAllPolicies() { + LinkedHashMap<String, Object> loadedYaml; + loadedYaml = new Yaml().load(policyEngineServices.downloadAllPolicies()); + if (loadedYaml == null || loadedYaml.isEmpty()) { + logger.warn("getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty"); + return; } - } - @Scheduled(fixedRate = 120000) - public void synchronizeAllPolicies() throws InterruptedException { - try { - LinkedHashMap<String, Object> loadedYaml = new Yaml().load(policyEngineServices.downloadAllPolicies()); - if (loadedYaml == null || loadedYaml.isEmpty()) { - logger.warn( - "getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty"); - return; - } + List<LinkedHashMap<String, Object>> policyTypesList = (List<LinkedHashMap<String, Object>>) loadedYaml + .get("policy_types"); + policyTypesList.parallelStream().forEach(policyType -> { + Entry<String, Object> policyTypeEntry = (Entry<String, Object>) new ArrayList(policyType.entrySet()).get(0); - LinkedHashMap<String, Object> policyTypesList = (LinkedHashMap<String, Object>) loadedYaml - .get("policy_types"); - for (Entry<String, Object> policyType : policyTypesList.entrySet()) { - createPolicyInDbIfNeeded(policyEngineServices.createPolicyModelFromPolicyEngine(policyType.getKey(), - ((String) ((LinkedHashMap<String, Object>) policyType.getValue()).get("version")))); - } - } catch (InterruptedException e) { - logger.warn("query to policy engine has been interrupted", e); - throw e; - } + policyEngineServices.createPolicyInDbIfNeeded( + policyEngineServices.createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(), + ((String) ((LinkedHashMap<String, Object>) policyTypeEntry.getValue()).get("version")))); + }); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bb25abffc..4422156fd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,11 +23,6 @@ # ### -info.build.artifact=@project.artifactId@ -info.build.name=@project.name@ -info.build.description=@project.description@ -info.build.version=@project.version@ - ### Set the port for HTTP or HTTPS protocol (Controlled by Spring framework, only one at a time). ### (See below for the parameter 'server.http.port' if you want to have both enabled) ### To have only HTTP, keep the lines server.ssl.* commented diff --git a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java index 636684cdb..a1499f720 100644 --- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java @@ -232,7 +232,7 @@ public class CsarInstallerItCase { assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0])) .getLoopElementModel().getName()).isNotEmpty(); - assertThat(policyModelsRepository.findAll().size()).isEqualByComparingTo(1); + assertThat(policyModelsRepository.findAll().size()).isGreaterThanOrEqualTo(1); assertThat(policyModelsRepository .existsById(new PolicyModelId("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"))).isTrue(); assertThat(policyModelsRepository diff --git a/src/test/java/org/onap/clamp/policy/downloader/PolicyEngineControllerTestItCase.java b/src/test/java/org/onap/clamp/policy/downloader/PolicyEngineControllerTestItCase.java new file mode 100644 index 000000000..912e0d604 --- /dev/null +++ b/src/test/java/org/onap/clamp/policy/downloader/PolicyEngineControllerTestItCase.java @@ -0,0 +1,65 @@ +package org.onap.clamp.policy.downloader; +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. 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. + * You may obtain a copy of the License at + * + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.gson.JsonSyntaxException; + +import java.io.IOException; +import java.util.List; + +import javax.transaction.Transactional; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.clamp.clds.Application; +import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelsRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +@ActiveProfiles(profiles = "clamp-default,clamp-policy-controller") +public class PolicyEngineControllerTestItCase { + + @Autowired + PolicyEngineController policyController; + + @Autowired + PolicyModelsRepository policyModelsRepository; + + @Test + @Transactional + public void synchronizeAllPoliciesTest() throws JsonSyntaxException, IOException, InterruptedException { + policyController.synchronizeAllPolicies(); + List<PolicyModel> policyModelsList = policyModelsRepository.findAll(); + assertThat(policyModelsList.size()).isGreaterThanOrEqualTo(8); + assertThat(policyModelsList).contains(new PolicyModel("onap.policies.Monitoring", null, "1.0.0")); + assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0")); + } + +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 17c42f560..5b921e9e3 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -21,11 +21,6 @@ # ### -info.build.artifact=@project.artifactId@ -info.build.name=@project.name@ -info.build.description=@project.description@ -info.build.version=@project.version@ - ### Set the port for HTTP or HTTPS protocol (Controlled by Spring framework, only one at a time). ### (See below for the parameter 'server.http.port' if you want to have both enabled) ### To have only HTTP, keep the lines server.ssl.* commented diff --git a/src/test/resources/http-cache/example/policy/api/v1/policytypes/.file b/src/test/resources/http-cache/example/policy/api/v1/policytypes?connectionTimeToLive=5000/.file index 7394d3f91..ab3b40e2f 100644 --- a/src/test/resources/http-cache/example/policy/api/v1/policytypes/.file +++ b/src/test/resources/http-cache/example/policy/api/v1/policytypes?connectionTimeToLive=5000/.file @@ -6,55 +6,55 @@ policy_types: derived_from: tosca.policies.Root properties: # Omitted for brevity, see Section 1 - - - onap.policies.controlloop.Operational: + + - onap.policies.controlloop.Operational: version: 1.0.0 description: Operational Policy for Control Loops derived_from: tosca.policies.Root properties: # Omitted for brevity, see Section 1 - + - onap.policies.controloop.operational.Drools: version: 1.0.0 description: Operational Policy for Control Loops using the Drools PDP derived_from: onap.policies.controlloop.Operational properties: # Omitted for brevity, see Section 1 - + - onap.policies.controloop.operational.Apex: version: 1.0.0 description: Operational Policy for Control Loops using the APEX PDP derived_from: onap.policies.controlloop.Operational properties: # Omitted for brevity, see Section 1 - - - onap.policies.controlloop.Guard: + + - onap.policies.controlloop.Guard: version: 1.0.0 description: Operational Policy for Control Loops derived_from: tosca.policies.Root properties: # Omitted for brevity, see Section 1 - + - onap.policies.controlloop.guard.FrequencyLimiter: version: 1.0.0 description: Supports limiting the frequency of actions being taken by a Actor. derived_from: onap.policies.controlloop.Guard properties: # Omitted for brevity, see Section 1 - + - onap.policies.controlloop.guard.Blacklist: version: 1.0.0 description: Supports blacklist of VNF's from performing control loop actions on. derived_from: onap.policies.controlloop.Guard properties: # Omitted for brevity, see Section 1 - + - onap.policies.controlloop.guard.MinMax: version: 1.0.0 description: Supports Min/Max number of VF Modules derived_from: onap.policies.controlloop.Guard properties: # Omitted for brevity, see Section 1 - + data_types: # Any bespoke data types referenced by policy type definitions[] diff --git a/src/test/resources/http-cache/example/policy/api/v1/policytypes/.header b/src/test/resources/http-cache/example/policy/api/v1/policytypes?connectionTimeToLive=5000/.header index 6a280d972..6a280d972 100644 --- a/src/test/resources/http-cache/example/policy/api/v1/policytypes/.header +++ b/src/test/resources/http-cache/example/policy/api/v1/policytypes?connectionTimeToLive=5000/.header |