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;
@Autowired
private LoopTemplatesService loopTemplateService;
+ @Autowired
+ private ToscaConverterWithDictionarySupport toscaConverter;
+
Loop saveOrUpdateLoop(Loop loop) {
return loopsRepository.save(loop);
}
/**
* 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));
}
/**
/**
* 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 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) {
+ 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, null, 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<OperationalPolicy> newOperationalPolicies) {
return loopsRepository.findById(loopName)
.orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
}
-
- /**
- * Api to refresh the Operational Policy UI window.
- *
- * @param loopName The loop Name
- * @return The refreshed loop object
- */
- public Loop refreshOpPolicyJsonRepresentation(String loopName) {
- Loop loop = findClosedLoopByName(loopName);
- Set<OperationalPolicy> policyList = loop.getOperationalPolicies();
- for (OperationalPolicy policy : policyList) {
- policy.updateJsonRepresentation();
- }
- loop.setOperationalPolicies(policyList);
- return loopsRepository.save(loop);
- }
}
+