diff options
author | sebdet <sebastien.determe@intl.att.com> | 2020-02-17 15:31:28 -0800 |
---|---|---|
committer | sebdet <sebastien.determe@intl.att.com> | 2020-02-25 01:04:12 -0800 |
commit | 4e8e11afced0693e24074fd6bb8d5b2cace98ab6 (patch) | |
tree | c177c0cccbe404a818188e954ce65848b2fb6d8c /src/main | |
parent | 3b7f669088d5867056578b275bf4314af3a439c6 (diff) |
Operational policy modification
policyModel added to Operational policy object so that a user in the UI can add op policies, guard or whatever
Issue-ID: CLAMP-647
Change-Id: I57521bc1c3afaf5ca5a2acf5c59823df06fd4cd9
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/main')
17 files changed, 303 insertions, 263 deletions
diff --git a/src/main/java/org/onap/clamp/clds/service/CldsService.java b/src/main/java/org/onap/clamp/clds/service/CldsService.java index 783ee703..3b84e360 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsService.java @@ -102,11 +102,16 @@ public class CldsService extends SecureServiceBase { @Autowired public CldsService( @Value("${clamp.config.security.permission.type.cl:permission-type-cl}") String cldsPersmissionTypeCl, - @Value("${clamp.config.security.permission.type.cl.manage:permission-type-cl-manage}") String cldsPermissionTypeClManage, - @Value("${clamp.config.security.permission.type.cl.event:permission-type-cl-event}") String cldsPermissionTypeClEvent, - @Value("${clamp.config.security.permission.type.filter.vf:permission-type-filter-vf}") String cldsPermissionTypeFilterVf, - @Value("${clamp.config.security.permission.type.template:permission-type-template}") String cldsPermissionTypeTemplate, - @Value("${clamp.config.security.permission.type.tosca:permission-type-tosca}") String cldsPermissionTypeTosca, + @Value("${clamp.config.security.permission.type.cl.manage:permission-type-cl-manage}") + String cldsPermissionTypeClManage, + @Value("${clamp.config.security.permission.type.cl.event:permission-type-cl-event}") + String cldsPermissionTypeClEvent, + @Value("${clamp.config.security.permission.type.filter.vf:permission-type-filter-vf}") + String cldsPermissionTypeFilterVf, + @Value("${clamp.config.security.permission.type.template:permission-type-template}") + String cldsPermissionTypeTemplate, + @Value("${clamp.config.security.permission.type.tosca:permission-type-tosca}") + String cldsPermissionTypeTosca, @Value("${clamp.config.security.permission.instance:dev}") String cldsPermissionInstance) { this.cldsPermissionTypeFilterVf = cldsPermissionTypeFilterVf; this.cldsPermissionInstance = cldsPermissionInstance; @@ -125,7 +130,7 @@ public class CldsService extends SecureServiceBase { * Gets clds info. CLDS IFO service will return 3 things 1. User Name 2. CLDS * code version that is currently installed from pom.xml file 3. User * permissions - * + * * @return the clds info */ public CldsInfo getCldsInfo() { diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java b/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java index 6ce89873..7edf6c1a 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java @@ -24,16 +24,17 @@ package org.onap.clamp.clds.util.drawing; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; +import java.util.HashSet; +import java.util.Set; +import org.onap.clamp.loop.template.LoopElementModel; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; public class ClampGraphBuilder { - private String policy; + private Set<OperationalPolicy> policies = new HashSet<>(); private String collector; - private List<BlueprintMicroService> microServices = new ArrayList<>(); + private Set<MicroServicePolicy> microServices = new HashSet<>(); + private Set<LoopElementModel> loopElementModels = new HashSet<>(); private final Painter painter; public ClampGraphBuilder(Painter painter) { @@ -45,33 +46,64 @@ public class ClampGraphBuilder { return this; } - public ClampGraphBuilder policy(String policy) { - this.policy = policy; + public ClampGraphBuilder addPolicy(OperationalPolicy policy) { + this.policies.add(policy); return this; } - public ClampGraphBuilder addMicroService(BlueprintMicroService ms) { + public ClampGraphBuilder addAllPolicies(Set<OperationalPolicy> policies) { + this.policies.addAll(policies); + return this; + } + + public ClampGraphBuilder addMicroService(MicroServicePolicy ms) { microServices.add(ms); return this; } - public ClampGraphBuilder addAllMicroServices(List<BlueprintMicroService> msList) { + public ClampGraphBuilder addAllMicroServices(Set<MicroServicePolicy> msList) { microServices.addAll(msList); return this; } /** + * This method adds all loop element specified in input to the current structure. + * + * @param loopElementModels A set of LoopElementModels + * @return Return the current ClampGraphBuilder + */ + public ClampGraphBuilder addAllLoopElementModels(Set<LoopElementModel> loopElementModels) { + for (LoopElementModel elem : loopElementModels) { + this.addLoopElementModel(elem); + } + return this; + } + + /** + * This method adds one loop element specified in input to the current structure. + * + * @param loopElementModel A LoopElementModels + * @return Return the current ClampGraphBuilder + */ + public ClampGraphBuilder addLoopElementModel(LoopElementModel loopElementModel) { + if (LoopElementModel.MICRO_SERVICE_TYPE.equals(loopElementModel.getLoopElementType())) { + microServices.add(new MicroServicePolicy(loopElementModel.getName(), + loopElementModel.getPolicyModels().first(), + false, + null)); + } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(loopElementModel.getLoopElementType())) { + policies.add(new OperationalPolicy(loopElementModel.getName(), null, null, + loopElementModel.getPolicyModels().first())); + } + return this; + } + + /** * Build the SVG. - * + * * @return Clamp graph (SVG) */ public ClampGraph build() { - if (microServices.isEmpty()) { - throw new InvalidStateException("At least one microservice is required"); - } - if (Objects.isNull(policy) || policy.trim().isEmpty()) { - throw new InvalidStateException("Policy element must be present"); - } - return new ClampGraph(painter.doPaint(collector, microServices, policy)); + return new ClampGraph(painter.doPaint(collector, microServices, policies)); } } diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java b/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java index d96c9e53..af62d84a 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java @@ -28,17 +28,17 @@ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Point; import java.awt.RenderingHints; -import java.util.List; - +import java.util.Set; import org.apache.batik.svggen.SVGGraphics2D; -import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; public class Painter { private final int canvasSize; private final SVGGraphics2D g2d; private final DocumentBuilder documentBuilder; - private static final int DEFALUT_CANVAS_SIZE = 900; + private static final int DEFAULT_CANVAS_SIZE = 900; private static final int SLIM_LINE = 2; private static final int THICK_LINE = 4; private static final double RECT_RATIO = 3.0 / 2.0; @@ -54,10 +54,10 @@ public class Painter { public Painter(SVGGraphics2D svgGraphics2D, DocumentBuilder documentBuilder) { this.g2d = svgGraphics2D; this.documentBuilder = documentBuilder; - this.canvasSize = DEFALUT_CANVAS_SIZE; + this.canvasSize = DEFAULT_CANVAS_SIZE; } - DocumentBuilder doPaint(String collector, List<BlueprintMicroService> microServices, String policy) { + DocumentBuilder doPaint(String collector, Set<MicroServicePolicy> microServices, Set<OperationalPolicy> policies) { int numOfRectangles = 2 + microServices.size(); int numOfArrows = numOfRectangles + 1; int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles); @@ -71,20 +71,25 @@ public class Painter { Point origin = new Point(1, rectHeight / 2); ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight); - doTheActualDrawing(collector, microServices, policy, ib); + doTheActualDrawing(collector, microServices, policies, ib); return ib.getDocumentBuilder(); } - private void doTheActualDrawing(String collector, List<BlueprintMicroService> microServices, String policy, - ImageBuilder ib) { + private void doTheActualDrawing(String collector, Set<MicroServicePolicy> microServices, + Set<OperationalPolicy> policies, + ImageBuilder ib) { ib.circle("start-circle", SLIM_LINE).arrow().rectangle(collector, RectTypes.COLECTOR, collector); - for (BlueprintMicroService ms : microServices) { - ib.arrow().rectangle(ms.getModelType(), RectTypes.MICROSERVICE, ms.getName()); + for (MicroServicePolicy ms : microServices) { + ib.arrow().rectangle(ms.getName(), + RectTypes.MICROSERVICE, ms.getPolicyModel().getPolicyAcronym()); } - - ib.arrow().rectangle(policy, RectTypes.POLICY, policy).arrow().circle("stop-circle", THICK_LINE); + for (OperationalPolicy policy : policies) { + ib.arrow().rectangle(policy.getName(), RectTypes.POLICY, policy.getPolicyModel().getPolicyAcronym()) + .arrow(); + } + ib.circle("stop-circle", THICK_LINE); } private void adjustGraphics2DProperties() { diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/SvgFacade.java b/src/main/java/org/onap/clamp/clds/util/drawing/SvgLoopGenerator.java index 251f4886..f289d979 100644 --- a/src/main/java/org/onap/clamp/clds/util/drawing/SvgFacade.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/SvgLoopGenerator.java @@ -24,30 +24,51 @@ package org.onap.clamp.clds.util.drawing; -import java.util.List; - +import java.util.HashSet; +import java.util.Set; import org.apache.batik.svggen.SVGGraphics2D; -import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; import org.onap.clamp.clds.util.XmlTools; -import org.springframework.stereotype.Component; +import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.template.LoopElementModel; +import org.onap.clamp.loop.template.LoopTemplate; +import org.onap.clamp.loop.template.LoopTemplateLoopElementModel; import org.w3c.dom.Document; -@Component -public class SvgFacade { +public class SvgLoopGenerator { + /** + * Generate the SVG images from the loop. + * + * @param loop The loop object, so it won't use the loop template + * @return A String containing the SVG + */ + public static String getSvgImage(Loop loop) { + SVGGraphics2D svgGraphics2D = new SVGGraphics2D(XmlTools.createEmptySvgDocument()); + Document document = XmlTools.createEmptySvgDocument(); + DocumentBuilder dp = new DocumentBuilder(document, svgGraphics2D.getDOMFactory()); + Painter painter = new Painter(svgGraphics2D, dp); + ClampGraphBuilder cgp = new ClampGraphBuilder(painter).collector("VES"); + cgp.addAllMicroServices(loop.getMicroServicePolicies()); + ClampGraph cg = cgp.addAllPolicies(loop.getOperationalPolicies()).build(); + return cg.getAsSvg(); + } + /** - * Generate the SVG images from the microservice Chain. - * - * @param microServicesChain THe chain of microservices + * Generate the SVG images from the loop template. + * + * @param loopTemplate The loop template * @return A String containing the SVG */ - public String getSvgImage(List<BlueprintMicroService> microServicesChain) { + public static String getSvgImage(LoopTemplate loopTemplate) { SVGGraphics2D svgGraphics2D = new SVGGraphics2D(XmlTools.createEmptySvgDocument()); Document document = XmlTools.createEmptySvgDocument(); DocumentBuilder dp = new DocumentBuilder(document, svgGraphics2D.getDOMFactory()); Painter painter = new Painter(svgGraphics2D, dp); ClampGraphBuilder cgp = new ClampGraphBuilder(painter).collector("VES"); - cgp.addAllMicroServices(microServicesChain); - ClampGraph cg = cgp.policy("OperationalPolicy").build(); + Set<LoopElementModel> elementModelsSet = new HashSet<>(); + for (LoopTemplateLoopElementModel elementModelLink:loopTemplate.getLoopElementModelsUsed()) { + elementModelsSet.add(elementModelLink.getLoopElementModel()); + } + ClampGraph cg = cgp.addAllLoopElementModels(elementModelsSet).build(); return cg.getAsSvg(); } diff --git a/src/main/java/org/onap/clamp/loop/CsarInstaller.java b/src/main/java/org/onap/clamp/loop/CsarInstaller.java index c0cfac96..b5ebdb94 100644 --- a/src/main/java/org/onap/clamp/loop/CsarInstaller.java +++ b/src/main/java/org/onap/clamp/loop/CsarInstaller.java @@ -25,12 +25,10 @@ package org.onap.clamp.loop; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; - import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Map.Entry; - import org.json.simple.parser.ParseException; import org.onap.clamp.clds.client.DcaeInventoryServices; import org.onap.clamp.clds.client.PolicyEngineServices; @@ -42,7 +40,6 @@ import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser; import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator; import org.onap.clamp.clds.sdc.controller.installer.CsarHandler; -import org.onap.clamp.clds.util.drawing.SvgFacade; import org.onap.clamp.loop.service.CsarServiceInstaller; import org.onap.clamp.loop.service.Service; import org.onap.clamp.loop.template.LoopElementModel; @@ -79,9 +76,6 @@ public class CsarInstaller { private DcaeInventoryServices dcaeInventoryService; @Autowired - private SvgFacade svgFacade; - - @Autowired private CsarServiceInstaller csarServiceInstaller; @Autowired @@ -89,7 +83,7 @@ public class CsarInstaller { /** * Verify whether Csar is deployed. - * + * * @param csar The Csar Handler * @return The flag indicating whether Csar is deployed * @throws SdcArtifactInstallerException The SdcArtifactInstallerException @@ -100,16 +94,16 @@ public class CsarInstaller { for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) { alreadyInstalled = alreadyInstalled && loopTemplatesRepository.existsById(LoopTemplate.generateLoopTemplateName( - csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(), - blueprint.getValue().getResourceAttached().getResourceInstanceName(), - blueprint.getValue().getBlueprintArtifactName())); + csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(), + blueprint.getValue().getResourceAttached().getResourceInstanceName(), + blueprint.getValue().getBlueprintArtifactName())); } return alreadyInstalled; } /** * Install the service and loop templates from the csar. - * + * * @param csar The Csar Handler * @throws SdcArtifactInstallerException The SdcArtifactInstallerException * @throws InterruptedException The InterruptedException @@ -125,7 +119,7 @@ public class CsarInstaller { /** * Install the loop templates from the csar. - * + * * @param csar The Csar Handler * @param service The service object that is related to the loop * @throws SdcArtifactInstallerException The SdcArtifactInstallerException @@ -150,7 +144,8 @@ public class CsarInstaller { } private LoopTemplate createLoopTemplateFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact, - Service service) throws IOException, ParseException, InterruptedException, BlueprintParserException { + Service service) + throws IOException, ParseException, InterruptedException, BlueprintParserException { LoopTemplate newLoopTemplate = new LoopTemplate(); newLoopTemplate.setBlueprint(blueprintArtifact.getDcaeBlueprint()); newLoopTemplate.setName(LoopTemplate.generateLoopTemplateName(csar.getSdcNotification().getServiceName(), @@ -165,7 +160,6 @@ public class CsarInstaller { newLoopTemplate.setModelService(service); newLoopTemplate.addLoopElementModels(createMicroServiceModels(microServicesChain)); newLoopTemplate.setMaximumInstancesAllowed(0); - newLoopTemplate.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain)); DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact); newLoopTemplate.setDcaeBlueprintId(dcaeResponse.getTypeId()); return newLoopTemplate; @@ -175,8 +169,9 @@ public class CsarInstaller { throws InterruptedException { HashSet<LoopElementModel> newSet = new HashSet<>(); for (BlueprintMicroService microService : microServicesChain) { - LoopElementModel loopElementModel = new LoopElementModel(microService.getModelType(), "CONFIG_POLICY", - null); + LoopElementModel loopElementModel = + new LoopElementModel(microService.getModelType(), LoopElementModel.MICRO_SERVICE_TYPE, + null); newSet.add(loopElementModel); loopElementModel.addPolicyModel(getPolicyModel(microService)); } diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 0ac8030d..676626a1 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -27,7 +27,6 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; - import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; @@ -35,7 +34,6 @@ import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; - import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -50,11 +48,11 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; - import org.hibernate.annotations.SortNatural; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.clds.util.drawing.SvgLoopGenerator; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.components.external.DcaeComponent; @@ -68,7 +66,7 @@ import org.onap.clamp.policy.operational.OperationalPolicy; @Entity @Table(name = "loops") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +@TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public class Loop extends AuditEntity implements Serializable { /** @@ -101,7 +99,7 @@ public class Loop extends AuditEntity implements Serializable { private JsonObject globalPropertiesJson; @Expose - @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }) + @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) @JoinColumn(name = "service_uuid") private Service modelService; @@ -119,7 +117,7 @@ public class Loop extends AuditEntity implements Serializable { private Set<OperationalPolicy> operationalPolicies = new HashSet<>(); @Expose - @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER) + @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER) @JoinTable(name = "loops_to_microservicepolicies", joinColumns = @JoinColumn(name = "loop_name"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_name")) private Set<MicroServicePolicy> microServicePolicies = new HashSet<>(); @@ -130,8 +128,8 @@ public class Loop extends AuditEntity implements Serializable { private SortedSet<LoopLog> loopLogs = new TreeSet<>(); @Expose - @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER) - @JoinColumn(name = "loop_template_name", nullable=false) + @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER) + @JoinColumn(name = "loop_template_name", nullable = false) private LoopTemplate loopTemplate; private void initializeExternalComponents() { @@ -229,14 +227,28 @@ public class Loop extends AuditEntity implements Serializable { this.loopLogs = loopLogs; } - void addOperationalPolicy(OperationalPolicy opPolicy) { + /** + * This method adds an operational policy to the loop. + * It re-computes the Svg as well. + * + * @param opPolicy the operationalPolicy to add + */ + public void addOperationalPolicy(OperationalPolicy opPolicy) { operationalPolicies.add(opPolicy); opPolicy.setLoop(this); + this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this)); } - void addMicroServicePolicy(MicroServicePolicy microServicePolicy) { + /** + * This method adds an micro service policy to the loop. + * It re-computes the Svg as well. + * + * @param microServicePolicy the micro service to add + */ + public void addMicroServicePolicy(MicroServicePolicy microServicePolicy) { microServicePolicies.add(microServicePolicy); microServicePolicy.getUsedByLoops().add(this); + this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this)); } public void addLog(LoopLog log) { @@ -295,7 +307,7 @@ public class Loop extends AuditEntity implements Serializable { * @return The generated loop name */ public static String generateLoopName(String serviceName, String serviceVersion, String resourceName, - String blueprintFileName) { + String blueprintFileName) { StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion) .append("_").append(resourceName).append("_").append(blueprintFileName.replaceAll(".yaml", "")); return buffer.toString().replace('.', '_').replaceAll(" ", ""); diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index fb857fbf..6ea412c0 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -24,12 +24,13 @@ package org.onap.clamp.loop; import com.google.gson.JsonObject; - import java.util.List; import java.util.Set; - import javax.persistence.EntityNotFoundException; - +import org.apache.commons.lang3.RandomStringUtils; +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; @@ -49,6 +50,9 @@ public class LoopService { @Autowired private OperationalPolicyService operationalPolicyService; + @Autowired + private PolicyModelsService policyModelsService; + Loop saveOrUpdateLoop(Loop loop) { return loopsRepository.save(loop); } @@ -67,7 +71,7 @@ public class LoopService { /** * This method is used to refresh the DCAE deployment status fields. - * + * * @param loop The loop instance to be modified * @param deploymentId The deployment ID as returned by DCAE * @param deploymentUrl The Deployment URL as returned by DCAE @@ -83,6 +87,19 @@ public class LoopService { loopsRepository.save(loop); } + Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) { + Loop loop = findClosedLoopByName(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)); + return loopsRepository.save(loop); + } + Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) { Loop loop = findClosedLoopByName(loopName); Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies); @@ -114,11 +131,11 @@ public class LoopService { } /** - * Api to refresh the Operational Policy UI window. - * - * @param loopName The loop Name - * @return The refreshed loop object - */ + * 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(); diff --git a/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java b/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java index ca26b136..89332bbd 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java @@ -54,7 +54,7 @@ public class DcaeComponent extends ExternalComponent { "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"); public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState( "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices " - + "defined in the DCAE blueprint and it's currently processing the request"); + + "defined in the DCAE blueprint and it's currently processing the request"); public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState( "MICROSERVICE_INSTALLATION_FAILED", "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed"); @@ -63,7 +63,7 @@ public class DcaeComponent extends ExternalComponent { "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully"); public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState( "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices " - + "defined in the DCAE blueprint and it's currently processing the request"); + + "defined in the DCAE blueprint and it's currently processing the request"); public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState( "MICROSERVICE_UNINSTALLATION_FAILED", "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed"); @@ -91,7 +91,7 @@ public class DcaeComponent extends ExternalComponent { /** * Convert the json response to a DcaeOperationStatusResponse. - * + * * @param responseBody The DCAE response Json paylaod * @return The dcae object provisioned */ @@ -146,13 +146,14 @@ public class DcaeComponent extends ExternalComponent { /** * Return the deploy payload for DCAE. * - * @param loop The loop object + * @param loop The loop object * @param microServicePolicy The micro service policy * @return The payload used to send deploy closed loop request */ public static String getDeployPayload(Loop loop, MicroServicePolicy microServicePolicy) { JsonObject globalProp = loop.getGlobalPropertiesJson(); - JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServicePolicy.getName()); + JsonObject deploymentProp = + globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServicePolicy.getName()); String serviceTypeId = microServicePolicy.getDcaeBlueprintId(); diff --git a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java index e6ba9815..47960598 100644 --- a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java +++ b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java @@ -43,13 +43,13 @@ import org.hibernate.annotations.SortNatural; import org.onap.clamp.loop.common.AuditEntity; /** - * This class represents a micro service model for a loop template. + * This class represents a micro service/operational/... model for a loop template. + * So it's an element in the flow (a box shown in the loop). */ @Entity @Table(name = "loop_element_models") public class LoopElementModel extends AuditEntity implements Serializable { - public static final String DEFAULT_GROUP_NAME = "DEFAULT"; /** * The serial version id. */ @@ -65,11 +65,13 @@ public class LoopElementModel extends AuditEntity implements Serializable { private String dcaeBlueprintId; /** - * Here we store the blueprint coming from DCAE. + * Here we store the blueprint coming from DCAE, it can be null if this is not a micro service model. */ @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml") private String blueprint; + public static String MICRO_SERVICE_TYPE = "MICRO_SERVICE_TYPE"; + public static String OPERATIONAL_POLICY_TYPE = "OPERATIONAL_POLICY_TYPE"; /** * The type of element. */ @@ -89,14 +91,14 @@ public class LoopElementModel extends AuditEntity implements Serializable { */ @Expose @ManyToMany( - fetch = FetchType.EAGER, - cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) + fetch = FetchType.EAGER, + cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) @JoinTable( - name = "loopelementmodels_to_policymodels", - joinColumns = @JoinColumn(name = "loop_element_name", referencedColumnName = "name"), - inverseJoinColumns = { - @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), - @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) + name = "loopelementmodels_to_policymodels", + joinColumns = @JoinColumn(name = "loop_element_name", referencedColumnName = "name"), + inverseJoinColumns = { + @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), + @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) @SortNatural private SortedSet<PolicyModel> policyModels = new TreeSet<>(); @@ -228,10 +230,10 @@ public class LoopElementModel extends AuditEntity implements Serializable { /** * Constructor. * - * @param name The name id + * @param name The name id * @param loopElementType The type of loop element - * @param blueprint The blueprint defined for dcae that contains the - * policy type to use + * @param blueprint The blueprint defined for dcae that contains the + * policy type to use */ public LoopElementModel(String name, String loopElementType, String blueprint) { this.name = name; diff --git a/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java b/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java index 54096cb8..a7bbd9dd 100644 --- a/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java +++ b/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java @@ -39,6 +39,7 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.SortNatural; +import org.onap.clamp.clds.util.drawing.SvgLoopGenerator; import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.service.Service; @@ -73,17 +74,17 @@ public class LoopTemplate extends AuditEntity implements Serializable { @Expose @OneToMany( - cascade = CascadeType.ALL, - fetch = FetchType.EAGER, - mappedBy = "loopTemplate", - orphanRemoval = true) + cascade = CascadeType.ALL, + fetch = FetchType.EAGER, + mappedBy = "loopTemplate", + orphanRemoval = true) @SortNatural private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>(); @Expose @ManyToOne( - fetch = FetchType.EAGER, - cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) + fetch = FetchType.EAGER, + cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) @JoinColumn(name = "service_uuid") private Service modelService; @@ -244,10 +245,7 @@ public class LoopTemplate extends AuditEntity implements Serializable { * @param loopElementModel The loopElementModel to add */ public void addLoopElementModel(LoopElementModel loopElementModel) { - LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, - loopElementModel, this.loopElementModelsUsed.size()); - this.loopElementModelsUsed.add(jointEntry); - loopElementModel.getUsedByLoopTemplates().add(jointEntry); + this.addLoopElementModel(loopElementModel,this.loopElementModelsUsed.size()); } /** @@ -255,13 +253,14 @@ public class LoopTemplate extends AuditEntity implements Serializable { * specified manually. * * @param loopElementModel The loopElementModel to add - * @param listPosition The position in the flow + * @param listPosition The position in the flow */ public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) { LoopTemplateLoopElementModel jointEntry = - new LoopTemplateLoopElementModel(this, loopElementModel, listPosition); + new LoopTemplateLoopElementModel(this, loopElementModel, listPosition); this.loopElementModelsUsed.add(jointEntry); loopElementModel.getUsedByLoopTemplates().add(jointEntry); + this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this)); } /** @@ -301,16 +300,16 @@ public class LoopTemplate extends AuditEntity implements Serializable { /** * Constructor. * - * @param name The loop template name id - * @param blueprint The blueprint containing all microservices (legacy - * case) - * @param svgRepresentation The svg representation of that loop template + * @param name The loop template name id + * @param blueprint The blueprint containing all microservices (legacy + * case) + * @param svgRepresentation The svg representation of that loop template * @param maxInstancesAllowed The maximum number of instances that can be - * created from that template - * @param service The service associated to that loop template + * created from that template + * @param service The service associated to that loop template */ public LoopTemplate(String name, String blueprint, String svgRepresentation, - Integer maxInstancesAllowed, Service service) { + Integer maxInstancesAllowed, Service service) { this.name = name; this.setBlueprint(blueprint); this.svgRepresentation = svgRepresentation; @@ -352,17 +351,17 @@ public class LoopTemplate extends AuditEntity implements Serializable { /** * Generate the loop template name. * - * @param serviceName The service name - * @param serviceVersion The service version - * @param resourceName The resource name + * @param serviceName The service name + * @param serviceVersion The service version + * @param resourceName The resource name * @param blueprintFileName The blueprint file name * @return The generated loop template name */ public static String generateLoopTemplateName(String serviceName, String serviceVersion, - String resourceName, String blueprintFileName) { + String resourceName, String blueprintFileName) { StringBuilder buffer = new StringBuilder("LOOP_TEMPLATE_").append(serviceName).append("_v") - .append(serviceVersion).append("_").append(resourceName).append("_") - .append(blueprintFileName.replaceAll(".yaml", "")); + .append(serviceVersion).append("_").append(resourceName).append("_") + .append(blueprintFileName.replaceAll(".yaml", "")); return buffer.toString().replace('.', '_').replaceAll(" ", ""); } } diff --git a/src/main/java/org/onap/clamp/loop/template/LoopTemplatesService.java b/src/main/java/org/onap/clamp/loop/template/LoopTemplatesService.java index 279d602c..29382137 100644 --- a/src/main/java/org/onap/clamp/loop/template/LoopTemplatesService.java +++ b/src/main/java/org/onap/clamp/loop/template/LoopTemplatesService.java @@ -24,11 +24,7 @@ package org.onap.clamp.loop.template; import java.util.List; -import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException; -import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; -import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser; import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator; -import org.onap.clamp.clds.util.drawing.SvgFacade; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -40,9 +36,6 @@ public class LoopTemplatesService { @Autowired ChainGenerator chainGenerator; - @Autowired - private SvgFacade svgFacade; - /** * Constructor. */ @@ -56,24 +49,6 @@ public class LoopTemplatesService { return loopTemplatesRepository.save(loopTemplate); } - /** - * Saves or updates loop template Object. - * - * @param templateName the loop template name - * @param loopTemplate the loop template object - * @return the loop template - * @throws BlueprintParserException In case of issues with the blueprint - * parsing - */ - public LoopTemplate saveOrUpdateLoopTemplateByName(String templateName, - LoopTemplate loopTemplate) throws BlueprintParserException { - - if (getLoopTemplate(templateName) != null) { - loopTemplate.setName(getLoopTemplate(templateName).getName()); - } - return saveOrUpdateLoopTemplate(createTemplateFromBlueprint(templateName, loopTemplate)); - } - public List<String> getLoopTemplateNames() { return loopTemplatesRepository.getAllLoopTemplateNames(); } @@ -89,23 +64,4 @@ public class LoopTemplatesService { public void deleteLoopTemplate(String name) { loopTemplatesRepository.deleteById(name); } - - private LoopTemplate createTemplateFromBlueprint(String templateName, LoopTemplate loopTemplate) - throws BlueprintParserException { - - String blueprintYaml = loopTemplate.getBlueprint(); - List<BlueprintMicroService> microServicesChain = - chainGenerator.getChainOfMicroServices(BlueprintParser.getMicroServices(blueprintYaml)); - if (microServicesChain.isEmpty()) { - microServicesChain = BlueprintParser.fallbackToOneMicroService(); - } - loopTemplate.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain)); - loopTemplate.setName(templateName); - - LoopTemplate existingTemplate = getLoopTemplate(templateName); - if (existingTemplate != null) { - loopTemplate.setName(existingTemplate.getName()); - } - return loopTemplate; - } } diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index c6568205..e1c08eea 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -42,7 +42,7 @@ import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.template.LoopElementModel; @MappedSuperclass -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +@TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public abstract class Policy extends AuditEntity { @Expose @@ -55,6 +55,10 @@ public abstract class Policy extends AuditEntity { @Column(columnDefinition = "json", name = "configurations_json") private JsonObject configurationsJson; + /** + * This attribute can be null when the user add a policy on the loop instance, not the template. + * When null, It therefore indicates that this policy is not by default in the loop template. + */ @Expose @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "loop_element_model_id") @@ -68,20 +72,19 @@ public abstract class Policy extends AuditEntity { /** * Name getter. - * + * * @return the name */ public abstract String getName(); /** * Name setter. - * */ public abstract void setName(String name); /** * jsonRepresentation getter. - * + * * @return the jsonRepresentation */ public JsonObject getJsonRepresentation() { @@ -90,7 +93,7 @@ public abstract class Policy extends AuditEntity { /** * jsonRepresentation setter. - * + * * @param jsonRepresentation The jsonRepresentation to set */ public void setJsonRepresentation(JsonObject jsonRepresentation) { @@ -99,7 +102,7 @@ public abstract class Policy extends AuditEntity { /** * configurationsJson getter. - * + * * @return The configurationsJson */ public JsonObject getConfigurationsJson() { @@ -108,7 +111,7 @@ public abstract class Policy extends AuditEntity { /** * configurationsJson setter. - * + * * @param configurationsJson the configurationsJson to set */ public void setConfigurationsJson(JsonObject configurationsJson) { @@ -117,7 +120,7 @@ public abstract class Policy extends AuditEntity { /** * loopElementModel getter. - * + * * @return the loopElementModel */ public LoopElementModel getLoopElementModel() { @@ -126,7 +129,7 @@ public abstract class Policy extends AuditEntity { /** * loopElementModel setter. - * + * * @param loopElementModel the loopElementModel to set */ public void setLoopElementModel(LoopElementModel loopElementModel) { @@ -135,7 +138,7 @@ public abstract class Policy extends AuditEntity { /** * pdpGroup getter. - * + * * @return the pdpGroup */ public String getPdpGroup() { @@ -144,7 +147,7 @@ public abstract class Policy extends AuditEntity { /** * pdpGroup setter. - * + * * @param pdpGroup the pdpGroup to set */ public void setPdpGroup(String pdpGroup) { @@ -162,7 +165,7 @@ public abstract class Policy extends AuditEntity { * @return The generated policy name */ public static String generatePolicyName(String policyType, String serviceName, String serviceVersion, - String resourceName, String blueprintFilename) { + String resourceName, String blueprintFilename) { StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v") .append(serviceVersion).append("_").append(resourceName).append("_") .append(blueprintFilename.replaceAll(".yaml", "")); diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 43c8d6e0..8d9017ea 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -30,20 +30,20 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; - import java.io.Serializable; import java.util.HashSet; import java.util.Map; import java.util.Set; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; - import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.json.JSONObject; @@ -51,12 +51,13 @@ import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.Policy; import org.yaml.snakeyaml.Yaml; @Entity @Table(name = "micro_service_policies") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +@TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public class MicroServicePolicy extends Policy implements Serializable { /** * The serial version ID. @@ -72,10 +73,6 @@ public class MicroServicePolicy extends Policy implements Serializable { private String name; @Expose - @Column(nullable = false, name = "policy_model_type") - private String modelType; - - @Expose @Column(name = "context") private String context; @@ -87,9 +84,6 @@ public class MicroServicePolicy extends Policy implements Serializable { @Column(name = "shared", nullable = false) private Boolean shared; - @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false) - private String policyTosca; - @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER) private Set<Loop> usedByLoops = new HashSet<>(); @@ -105,6 +99,12 @@ public class MicroServicePolicy extends Policy implements Serializable { @Column(name = "dcae_blueprint_id") private String dcaeBlueprintId; + @Expose + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), + @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) + private PolicyModel policyModel; + public MicroServicePolicy() { // serialization } @@ -114,24 +114,23 @@ public class MicroServicePolicy extends Policy implements Serializable { * using the ToscaYamlToJsonConvertor. * * @param name The name of the MicroService - * @param modelType The model type of the MicroService - * @param policyTosca The policy Tosca of the MicroService + * @param policyModel The policy model of the MicroService * @param shared The flag indicate whether the MicroService is shared * @param usedByLoops The list of loops that uses this MicroService */ - public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared, - Set<Loop> usedByLoops) { + public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, + Set<Loop> usedByLoops) { this.name = name; - this.modelType = modelType; - this.policyTosca = policyTosca; + this.policyModel = policyModel; this.shared = shared; this.setJsonRepresentation(JsonUtils.GSON_JPA_MODEL - .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyTosca, modelType), JsonObject.class)); + .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyModel.getPolicyModelTosca(), + policyModel.getPolicyModelType()), JsonObject.class)); this.usedByLoops = usedByLoops; } private JsonObject createJsonFromPolicyTosca() { - Map<String, Object> map = new Yaml().load(this.getPolicyTosca()); + Map<String, Object> map = new Yaml().load(this.getPolicyModel().getPolicyModelTosca()); JSONObject jsonObject = new JSONObject(map); return new Gson().fromJson(jsonObject.toString(), JsonObject.class); } @@ -141,18 +140,16 @@ public class MicroServicePolicy extends Policy implements Serializable { * the jsonRepresentation instead. * * @param name The name of the MicroService - * @param modelType The model type of the MicroService - * @param policyTosca The policy Tosca of the MicroService + * @param policyModel The policy model type of the MicroService * @param shared The flag indicate whether the MicroService is * shared * @param jsonRepresentation The UI representation in json format * @param usedByLoops The list of loops that uses this MicroService */ - public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared, - JsonObject jsonRepresentation, Set<Loop> usedByLoops) { + public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, + JsonObject jsonRepresentation, Set<Loop> usedByLoops) { this.name = name; - this.modelType = modelType; - this.policyTosca = policyTosca; + this.policyModel = policyModel; this.shared = shared; this.usedByLoops = usedByLoops; this.setJsonRepresentation(jsonRepresentation); @@ -165,7 +162,7 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * name setter. - * + * * @param name the name to set */ @Override @@ -173,14 +170,6 @@ public class MicroServicePolicy extends Policy implements Serializable { this.name = name; } - public String getModelType() { - return modelType; - } - - void setModelType(String modelType) { - this.modelType = modelType; - } - public Boolean getShared() { return shared; } @@ -189,14 +178,6 @@ public class MicroServicePolicy extends Policy implements Serializable { this.shared = shared; } - public String getPolicyTosca() { - return policyTosca; - } - - void setPolicyTosca(String policyTosca) { - this.policyTosca = policyTosca; - } - public Set<Loop> getUsedByLoops() { return usedByLoops; } @@ -221,9 +202,17 @@ public class MicroServicePolicy extends Policy implements Serializable { this.deviceTypeScope = deviceTypeScope; } + public PolicyModel getPolicyModel() { + return policyModel; + } + + public void setPolicyModel(PolicyModel policyModel) { + this.policyModel = policyModel; + } + /** * dcaeDeploymentId getter. - * + * * @return the dcaeDeploymentId */ public String getDcaeDeploymentId() { @@ -232,7 +221,7 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * dcaeDeploymentId setter. - * + * * @param dcaeDeploymentId the dcaeDeploymentId to set */ public void setDcaeDeploymentId(String dcaeDeploymentId) { @@ -241,7 +230,7 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * dcaeDeploymentStatusUrl getter. - * + * * @return the dcaeDeploymentStatusUrl */ public String getDcaeDeploymentStatusUrl() { @@ -250,7 +239,7 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * dcaeDeploymentStatusUrl setter. - * + * * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set */ public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) { @@ -259,7 +248,7 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * dcaeBlueprintId getter. - * + * * @return the dcaeBlueprintId */ public String getDcaeBlueprintId() { @@ -268,7 +257,7 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * dcaeBlueprintId setter. - * + * * @param dcaeBlueprintId the dcaeBlueprintId to set */ void setDcaeBlueprintId(String dcaeBlueprintId) { @@ -306,7 +295,9 @@ public class MicroServicePolicy extends Policy implements Serializable { } private String getMicroServicePropertyNameFromTosca(JsonObject object) { - return object.getAsJsonObject("policy_types").getAsJsonObject(this.modelType).getAsJsonObject("properties") + return object.getAsJsonObject("policy_types").getAsJsonObject(this.getPolicyModel().getPolicyModelType()) + .getAsJsonObject( + "properties") .keySet().toArray(new String[1])[0]; } @@ -329,8 +320,8 @@ public class MicroServicePolicy extends Policy implements Serializable { JsonObject policyDetails = new JsonObject(); thisPolicy.add(this.getName(), policyDetails); - policyDetails.addProperty("type", this.getModelType()); - policyDetails.addProperty("version", "1.0.0"); + policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType()); + policyDetails.addProperty("version", this.getPolicyModel().getVersion()); JsonObject policyMetadata = new JsonObject(); policyDetails.add("metadata", policyMetadata); diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java index 29a4e56d..b17bf1ac 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java @@ -65,7 +65,7 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli public MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) { return repository.save( repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop)) - .orElse(new MicroServicePolicy(policy.getName(), policy.getModelType(), policy.getPolicyTosca(), + .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyModel(), policy.getShared(), policy.getJsonRepresentation(), Sets.newHashSet(loop)))); } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index a1c8cdbd..0825ea9e 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -62,7 +62,7 @@ import org.yaml.snakeyaml.Yaml; @Entity @Table(name = "operational_policies") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +@TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public class OperationalPolicy extends Policy implements Serializable { /** * The serial version ID. @@ -83,8 +83,8 @@ public class OperationalPolicy extends Policy implements Serializable { @Expose @ManyToOne(fetch = FetchType.EAGER) - @JoinColumns({ @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), - @JoinColumn(name = "policy_model_version", referencedColumnName = "version") }) + @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), + @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) private PolicyModel policyModel; public OperationalPolicy() { @@ -98,10 +98,12 @@ public class OperationalPolicy extends Policy implements Serializable { * @param loop The loop that uses this operational policy * @param configurationsJson The operational policy property in the format of * json + * @param policyModel The policy model associated if any, can be null */ - public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) { + public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson, PolicyModel policyModel) { this.name = name; this.loop = loop; + this.setPolicyModel(policyModel); this.setConfigurationsJson(configurationsJson); LegacyOperationalPolicy.preloadConfiguration(configurationsJson, loop); try { @@ -128,7 +130,7 @@ public class OperationalPolicy extends Policy implements Serializable { /** * name setter. - * + * * @param name the name to set */ @Override @@ -138,7 +140,7 @@ public class OperationalPolicy extends Policy implements Serializable { /** * policyModel getter. - * + * * @return the policyModel */ public PolicyModel getPolicyModel() { @@ -147,7 +149,7 @@ public class OperationalPolicy extends Policy implements Serializable { /** * policyModel setter. - * + * * @param policyModel the policyModel to set */ public void setPolicyModel(PolicyModel policyModel) { @@ -186,7 +188,7 @@ public class OperationalPolicy extends Policy implements Serializable { /** * Create policy Yaml from json defined here. - * + * * @return A string containing Yaml */ public String createPolicyPayloadYaml() { @@ -260,7 +262,6 @@ public class OperationalPolicy extends Policy implements Serializable { /** * Regenerate the Operational Policy Json Representation. - * */ public void updateJsonRepresentation() { try { diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java index 95f4f7be..174912b2 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java @@ -35,28 +35,30 @@ import org.springframework.stereotype.Service; @Service public class OperationalPolicyService implements PolicyService<OperationalPolicy> { - private final OperationalPolicyRepository repository; + private final OperationalPolicyRepository operationalPolicyRepository; @Autowired public OperationalPolicyService(OperationalPolicyRepository repository) { - this.repository = repository; + this.operationalPolicyRepository = repository; } @Override public Set<OperationalPolicy> updatePolicies(Loop loop, List<OperationalPolicy> operationalPolicies) { return operationalPolicies - .stream() - .map(policy -> - repository - .findById(policy.getName()) - .map(p -> setConfigurationJson(p, policy.getConfigurationsJson())) - .orElse(new OperationalPolicy(policy.getName(), loop, policy.getConfigurationsJson()))) - .collect(Collectors.toSet()); + .parallelStream() + .map(policy -> + operationalPolicyRepository + .findById(policy.getName()) + .map(p -> setConfigurationJson(p, policy.getConfigurationsJson())) + .orElse(new OperationalPolicy(policy.getName(), loop, + policy.getConfigurationsJson(), + policy.getPolicyModel()))) + .collect(Collectors.toSet()); } @Override public boolean isExisting(String policyName) { - return repository.existsById(policyName); + return operationalPolicyRepository.existsById(policyName); } private OperationalPolicy setConfigurationJson(OperationalPolicy policy, JsonObject configurationsJson) { diff --git a/src/main/java/org/onap/clamp/util/SemanticVersioning.java b/src/main/java/org/onap/clamp/util/SemanticVersioning.java index 10228449..8852e2a4 100644 --- a/src/main/java/org/onap/clamp/util/SemanticVersioning.java +++ b/src/main/java/org/onap/clamp/util/SemanticVersioning.java @@ -26,8 +26,6 @@ package org.onap.clamp.util; /** * This class is the base class for object that requires semantic versioning. * ... This class supports also a.b.c.d... etc ... as a version. - * - * */ public class SemanticVersioning { public static final int BEFORE = -1; @@ -41,7 +39,7 @@ public class SemanticVersioning { * @param arg0 A version in string for semantic versioning (a.b.c.d...) * @param arg1 A version in string for semantic versioning (a.b.c.d...) * @return objects (arg0, arg1) given as parameters. It returns the value: 0: if - * (arg0==arg1) -1: if (arg0 < arg1) 1: if (arg0 > arg1) + * (arg0==arg1) -1: if (arg0 < arg1) 1: if (arg0 > arg1) */ public static int compare(String arg0, String arg1) { @@ -60,12 +58,12 @@ public class SemanticVersioning { int smalestStringLength = Math.min(arg0Array.length, arg1Array.length); for (int currentVersionIndex = - 0; currentVersionIndex < smalestStringLength; ++currentVersionIndex) { + 0; currentVersionIndex < smalestStringLength; ++currentVersionIndex) { if (Integer.parseInt(arg0Array[currentVersionIndex]) < Integer - .parseInt(arg1Array[currentVersionIndex])) { + .parseInt(arg1Array[currentVersionIndex])) { return BEFORE; } else if (Integer.parseInt(arg0Array[currentVersionIndex]) > Integer - .parseInt(arg1Array[currentVersionIndex])) { + .parseInt(arg1Array[currentVersionIndex])) { return AFTER; } // equals, so do not return anything, continue @@ -88,6 +86,6 @@ public class SemanticVersioning { return DEFAULT_VERSION; } String[] versionArray = currentVersion.split("\\."); - return String.valueOf(Integer.parseInt(versionArray[0]) + 1)+".0.0"; + return String.valueOf(Integer.parseInt(versionArray[0]) + 1) + ".0.0"; } } |