X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Floop%2FLoopService.java;h=acd125b7804912e830679293a411979262f5aeca;hb=723de7f63f0951d0cfe7a23956cf9d00128809b1;hp=34be2038e58afc40746f8c159bd61b3bd5b15b34;hpb=d2a4df0b62b6a32c42bac45b4bee344016faa8fb;p=clamp.git diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index 34be2038..acd125b7 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -24,14 +24,14 @@ package org.onap.clamp.loop; import com.google.gson.JsonObject; +import java.io.IOException; import java.util.List; import java.util.Set; import javax.persistence.EntityNotFoundException; -import org.apache.commons.lang3.RandomStringUtils; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.loop.template.LoopTemplatesService; import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.loop.template.PolicyModelsService; -import org.onap.clamp.policy.Policy; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; @@ -57,6 +57,9 @@ public class LoopService { @Autowired private LoopTemplatesService loopTemplateService; + @Autowired + private ToscaConverterWithDictionarySupport toscaConverter; + Loop saveOrUpdateLoop(Loop loop) { return loopsRepository.save(loop); } @@ -76,12 +79,13 @@ public class LoopService { /** * Creates a Loop Instance from Loop Template Name. * - * @param loopName Name of the Loop to be created + * @param loopName Name of the Loop to be created * @param templateName Loop Template to used for Loop * @return Loop Instance */ public Loop createLoopFromTemplate(String loopName, String templateName) { - return loopsRepository.save(new Loop(loopName,loopTemplateService.getLoopTemplate(templateName))); + return loopsRepository + .save(new Loop(loopName, loopTemplateService.getLoopTemplate(templateName), toscaConverter)); } /** @@ -102,17 +106,48 @@ public class LoopService { loopsRepository.save(loop); } - Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) { - Loop loop = findClosedLoopByName(loopName); + /** + * This method add an operational policy to a loop instance. + * This creates an operational policy from the policy model info and not the loop element model + * + * @param loopName The loop name + * @param policyType The policy model type + * @param policyVersion The policy model version + * @return The loop modified + */ + Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException { + Loop loop = getLoop(loopName); PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion); if (policyModel == null) { return null; } loop.addOperationalPolicy( - new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL", loop.getModelService().getName(), - loop.getModelService().getVersion(), RandomStringUtils.randomAlphanumeric(3), - RandomStringUtils.randomAlphanumeric(4)), loop, null, policyModel, null)); - return loopsRepository.save(loop); + new OperationalPolicy(loop,loop.getModelService(), policyModel, toscaConverter)); + return loopsRepository.saveAndFlush(loop); + } + + /** + * This method remove an operational policy to a loop instance. + * + * @param loopName The loop name + * @param policyType The policy model type + * @param policyVersion The policy model version + * @return The loop modified + */ + Loop removeOperationalPolicy(String loopName, String policyType, String policyVersion) { + Loop loop = getLoop(loopName); + PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion); + if (policyModel == null) { + return null; + } + for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { + if (opPolicy.getPolicyModel().getPolicyModelType().equals(policyType) && + opPolicy.getPolicyModel().getVersion().equals(policyVersion)) { + loop.removeOperationalPolicy(opPolicy); + break; + } + } + return loopsRepository.saveAndFlush(loop); } Loop updateAndSaveOperationalPolicies(String loopName, List newOperationalPolicies) {