2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.loop;
26 import com.google.gson.JsonObject;
27 import com.google.gson.annotations.Expose;
28 import java.io.Serializable;
29 import java.util.HashMap;
30 import java.util.HashSet;
33 import java.util.SortedSet;
34 import java.util.TreeSet;
35 import javax.persistence.CascadeType;
36 import javax.persistence.Column;
37 import javax.persistence.Entity;
38 import javax.persistence.EnumType;
39 import javax.persistence.Enumerated;
40 import javax.persistence.FetchType;
41 import javax.persistence.Id;
42 import javax.persistence.JoinColumn;
43 import javax.persistence.JoinTable;
44 import javax.persistence.ManyToMany;
45 import javax.persistence.ManyToOne;
46 import javax.persistence.OneToMany;
47 import javax.persistence.Table;
48 import javax.persistence.Transient;
49 import org.hibernate.annotations.SortNatural;
50 import org.hibernate.annotations.Type;
51 import org.hibernate.annotations.TypeDef;
52 import org.hibernate.annotations.TypeDefs;
53 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
54 import org.onap.clamp.clds.util.drawing.SvgLoopGenerator;
55 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
56 import org.onap.clamp.loop.common.AuditEntity;
57 import org.onap.clamp.loop.components.external.DcaeComponent;
58 import org.onap.clamp.loop.components.external.ExternalComponent;
59 import org.onap.clamp.loop.components.external.PolicyComponent;
60 import org.onap.clamp.loop.deploy.DcaeDeployParameters;
61 import org.onap.clamp.loop.log.LoopLog;
62 import org.onap.clamp.loop.service.Service;
63 import org.onap.clamp.loop.template.LoopElementModel;
64 import org.onap.clamp.loop.template.LoopTemplate;
65 import org.onap.clamp.policy.microservice.MicroServicePolicy;
66 import org.onap.clamp.policy.operational.OperationalPolicy;
69 @Table(name = "loops")
70 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
71 public class Loop extends AuditEntity implements Serializable {
74 * The serial version id.
76 private static final long serialVersionUID = -286522707701388642L;
80 @Column(nullable = false, name = "name", unique = true)
84 @Column(name = "dcae_deployment_id")
85 private String dcaeDeploymentId;
88 @Column(name = "dcae_deployment_status_url")
89 private String dcaeDeploymentStatusUrl;
91 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
92 private String svgRepresentation;
96 @Column(columnDefinition = "json", name = "global_properties_json")
97 private JsonObject globalPropertiesJson;
100 @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
101 @JoinColumn(name = "service_uuid")
102 private Service modelService;
105 @Column(nullable = false, name = "last_computed_state")
106 @Enumerated(EnumType.STRING)
107 private LoopState lastComputedState;
111 private final Map<String, ExternalComponent> components = new HashMap<>();
114 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
115 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
118 @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
119 @JoinTable(name = "loops_to_microservicepolicies", joinColumns = @JoinColumn(name = "loop_name"),
120 inverseJoinColumns = @JoinColumn(name = "microservicepolicy_name"))
121 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
124 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
126 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
129 @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
130 @JoinColumn(name = "loop_template_name", nullable = false)
131 private LoopTemplate loopTemplate;
133 private void initializeExternalComponents() {
134 this.addComponent(new PolicyComponent());
135 this.addComponent(new DcaeComponent());
139 * Public constructor.
142 initializeExternalComponents();
148 public Loop(String name, String svgRepresentation) {
150 this.svgRepresentation = svgRepresentation;
151 this.lastComputedState = LoopState.DESIGN;
152 this.globalPropertiesJson = new JsonObject();
153 initializeExternalComponents();
157 * This constructor creates a loop from a loop template.
159 * @param name The loop name
160 * @param loopTemplate The loop template from which a new loop instance must be created
162 public Loop(String name, LoopTemplate loopTemplate, ToscaConverterWithDictionarySupport toscaConverter) {
164 this.setLoopTemplate(loopTemplate);
165 this.setModelService(loopTemplate.getModelService());
166 loopTemplate.getLoopElementModelsUsed().forEach(element -> {
167 if (LoopElementModel.MICRO_SERVICE_TYPE.equals(element.getLoopElementModel().getLoopElementType())) {
168 this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel()
169 .createPolicyInstance(this, toscaConverter));
171 else if (LoopElementModel.OPERATIONAL_POLICY_TYPE
172 .equals(element.getLoopElementModel().getLoopElementType())) {
173 this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel()
174 .createPolicyInstance(this, toscaConverter));
177 this.setGlobalPropertiesJson(DcaeDeployParameters.getDcaeDeploymentParametersInJson(this));
180 public String getName() {
184 void setName(String name) {
188 public String getDcaeDeploymentId() {
189 return dcaeDeploymentId;
192 void setDcaeDeploymentId(String dcaeDeploymentId) {
193 this.dcaeDeploymentId = dcaeDeploymentId;
196 public String getDcaeDeploymentStatusUrl() {
197 return dcaeDeploymentStatusUrl;
200 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
201 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
204 public String getSvgRepresentation() {
205 return svgRepresentation;
208 void setSvgRepresentation(String svgRepresentation) {
209 this.svgRepresentation = svgRepresentation;
212 public LoopState getLastComputedState() {
213 return lastComputedState;
216 void setLastComputedState(LoopState lastComputedState) {
217 this.lastComputedState = lastComputedState;
220 public Set<OperationalPolicy> getOperationalPolicies() {
221 return operationalPolicies;
224 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
225 this.operationalPolicies = operationalPolicies;
228 public Set<MicroServicePolicy> getMicroServicePolicies() {
229 return microServicePolicies;
232 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
233 this.microServicePolicies = microServicePolicies;
236 public JsonObject getGlobalPropertiesJson() {
237 return globalPropertiesJson;
240 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
241 this.globalPropertiesJson = globalPropertiesJson;
244 public Set<LoopLog> getLoopLogs() {
248 void setLoopLogs(SortedSet<LoopLog> loopLogs) {
249 this.loopLogs = loopLogs;
253 * This method adds an operational policy to the loop.
254 * It re-computes the Svg as well.
256 * @param opPolicy the operationalPolicy to add
258 public void addOperationalPolicy(OperationalPolicy opPolicy) {
259 operationalPolicies.add(opPolicy);
260 opPolicy.setLoop(this);
261 this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this));
265 * This method removes an operational policy to the loop.
266 * It re-computes the Svg as well.
268 * @param opPolicy the operationalPolicy to add
270 public void removeOperationalPolicy(OperationalPolicy opPolicy) {
271 operationalPolicies.remove(opPolicy);
272 this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this));
276 * This method adds an micro service policy to the loop.
277 * It re-computes the Svg as well.
279 * @param microServicePolicy the micro service to add
281 public void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
282 microServicePolicies.add(microServicePolicy);
283 microServicePolicy.getUsedByLoops().add(this);
284 this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this));
287 public void addLog(LoopLog log) {
289 this.loopLogs.add(log);
292 public Service getModelService() {
296 void setModelService(Service modelService) {
297 this.modelService = modelService;
300 public Map<String, ExternalComponent> getComponents() {
301 refreshDcaeComponents();
305 public ExternalComponent getComponent(String componentName) {
306 refreshDcaeComponents();
307 return this.components.get(componentName);
310 public void addComponent(ExternalComponent component) {
311 this.components.put(component.getComponentName(), component);
314 public LoopTemplate getLoopTemplate() {
318 public void setLoopTemplate(LoopTemplate loopTemplate) {
319 this.loopTemplate = loopTemplate;
322 private void refreshDcaeComponents() {
323 if (!this.loopTemplate.getUniqueBlueprint()) {
324 this.components.remove("DCAE");
325 for (MicroServicePolicy policy : this.microServicePolicies) {
326 if (!this.components.containsKey("DCAE_" + policy.getName())) {
327 this.addComponent(new DcaeComponent(policy.getName()));
334 * Generate the loop name.
336 * @param serviceName The service name
337 * @param serviceVersion The service version
338 * @param resourceName The resource name
339 * @param blueprintFileName The blueprint file name
340 * @return The generated loop name
342 public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
343 String blueprintFileName) {
344 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
345 .append("_").append(resourceName).append("_").append(blueprintFileName.replaceAll(".yaml", ""));
346 return buffer.toString().replace('.', '_').replaceAll(" ", "");
350 public int hashCode() {
351 final int prime = 31;
353 result = prime * result + ((name == null) ? 0 : name.hashCode());
358 public boolean equals(Object obj) {
365 if (getClass() != obj.getClass()) {
368 Loop other = (Loop) obj;
370 if (other.name != null) {
374 else if (!name.equals(other.name)) {