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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29 import com.google.gson.annotations.Expose;
30 import java.io.Serializable;
31 import java.util.HashMap;
32 import java.util.HashSet;
35 import java.util.SortedSet;
36 import java.util.TreeSet;
37 import javax.persistence.CascadeType;
38 import javax.persistence.Column;
39 import javax.persistence.Entity;
40 import javax.persistence.EnumType;
41 import javax.persistence.Enumerated;
42 import javax.persistence.FetchType;
43 import javax.persistence.Id;
44 import javax.persistence.JoinColumn;
45 import javax.persistence.JoinTable;
46 import javax.persistence.ManyToMany;
47 import javax.persistence.ManyToOne;
48 import javax.persistence.OneToMany;
49 import javax.persistence.Table;
50 import javax.persistence.Transient;
51 import org.hibernate.annotations.SortNatural;
52 import org.hibernate.annotations.Type;
53 import org.hibernate.annotations.TypeDef;
54 import org.hibernate.annotations.TypeDefs;
55 import org.onap.clamp.clds.util.drawing.SvgLoopGenerator;
56 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
57 import org.onap.clamp.loop.common.AuditEntity;
58 import org.onap.clamp.loop.components.external.DcaeComponent;
59 import org.onap.clamp.loop.components.external.ExternalComponent;
60 import org.onap.clamp.loop.components.external.PolicyComponent;
61 import org.onap.clamp.loop.log.LoopLog;
62 import org.onap.clamp.loop.service.Service;
63 import org.onap.clamp.loop.template.LoopTemplate;
64 import org.onap.clamp.policy.microservice.MicroServicePolicy;
65 import org.onap.clamp.policy.operational.OperationalPolicy;
68 @Table(name = "loops")
69 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
70 public class Loop extends AuditEntity implements Serializable {
73 * The serial version id.
75 private static final long serialVersionUID = -286522707701388642L;
78 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
82 @Column(nullable = false, name = "name", unique = true)
86 @Column(name = "dcae_deployment_id")
87 private String dcaeDeploymentId;
90 @Column(name = "dcae_deployment_status_url")
91 private String dcaeDeploymentStatusUrl;
93 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
94 private String svgRepresentation;
98 @Column(columnDefinition = "json", name = "global_properties_json")
99 private JsonObject globalPropertiesJson;
102 @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
103 @JoinColumn(name = "service_uuid")
104 private Service modelService;
107 @Column(nullable = false, name = "last_computed_state")
108 @Enumerated(EnumType.STRING)
109 private LoopState lastComputedState;
113 private final Map<String, ExternalComponent> components = new HashMap<>();
116 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
117 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
120 @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
121 @JoinTable(name = "loops_to_microservicepolicies", joinColumns = @JoinColumn(name = "loop_name"),
122 inverseJoinColumns = @JoinColumn(name = "microservicepolicy_name"))
123 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
126 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
128 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
131 @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
132 @JoinColumn(name = "loop_template_name", nullable = false)
133 private LoopTemplate loopTemplate;
135 private void initializeExternalComponents() {
136 this.addComponent(new PolicyComponent());
137 this.addComponent(new DcaeComponent());
141 * Public constructor.
144 initializeExternalComponents();
150 public Loop(String name, String svgRepresentation) {
152 this.svgRepresentation = svgRepresentation;
153 this.lastComputedState = LoopState.DESIGN;
154 this.globalPropertiesJson = new JsonObject();
155 initializeExternalComponents();
158 public String getName() {
162 void setName(String name) {
166 public String getDcaeDeploymentId() {
167 return dcaeDeploymentId;
170 void setDcaeDeploymentId(String dcaeDeploymentId) {
171 this.dcaeDeploymentId = dcaeDeploymentId;
174 public String getDcaeDeploymentStatusUrl() {
175 return dcaeDeploymentStatusUrl;
178 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
179 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
182 public String getSvgRepresentation() {
183 return svgRepresentation;
186 void setSvgRepresentation(String svgRepresentation) {
187 this.svgRepresentation = svgRepresentation;
190 public LoopState getLastComputedState() {
191 return lastComputedState;
194 void setLastComputedState(LoopState lastComputedState) {
195 this.lastComputedState = lastComputedState;
198 public Set<OperationalPolicy> getOperationalPolicies() {
199 return operationalPolicies;
202 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
203 this.operationalPolicies = operationalPolicies;
206 public Set<MicroServicePolicy> getMicroServicePolicies() {
207 return microServicePolicies;
210 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
211 this.microServicePolicies = microServicePolicies;
214 public JsonObject getGlobalPropertiesJson() {
215 return globalPropertiesJson;
218 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
219 this.globalPropertiesJson = globalPropertiesJson;
222 public Set<LoopLog> getLoopLogs() {
226 void setLoopLogs(SortedSet<LoopLog> loopLogs) {
227 this.loopLogs = loopLogs;
231 * This method adds an operational policy to the loop.
232 * It re-computes the Svg as well.
234 * @param opPolicy the operationalPolicy to add
236 public void addOperationalPolicy(OperationalPolicy opPolicy) {
237 operationalPolicies.add(opPolicy);
238 opPolicy.setLoop(this);
239 this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this));
243 * This method adds an micro service policy to the loop.
244 * It re-computes the Svg as well.
246 * @param microServicePolicy the micro service to add
248 public void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
249 microServicePolicies.add(microServicePolicy);
250 microServicePolicy.getUsedByLoops().add(this);
251 this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this));
254 public void addLog(LoopLog log) {
256 this.loopLogs.add(log);
259 public Service getModelService() {
263 void setModelService(Service modelService) {
264 this.modelService = modelService;
267 public Map<String, ExternalComponent> getComponents() {
268 refreshDcaeComponents();
272 public ExternalComponent getComponent(String componentName) {
273 refreshDcaeComponents();
274 return this.components.get(componentName);
277 public void addComponent(ExternalComponent component) {
278 this.components.put(component.getComponentName(), component);
281 public LoopTemplate getLoopTemplate() {
285 public void setLoopTemplate(LoopTemplate loopTemplate) {
286 this.loopTemplate = loopTemplate;
289 private void refreshDcaeComponents() {
290 if (!this.loopTemplate.getUniqueBlueprint()) {
291 this.components.remove("DCAE");
292 for (MicroServicePolicy policy : this.microServicePolicies) {
293 if (!this.components.containsKey("DCAE_" + policy.getName())) {
294 this.addComponent(new DcaeComponent(policy.getName()));
301 * Generate the loop name.
303 * @param serviceName The service name
304 * @param serviceVersion The service version
305 * @param resourceName The resource name
306 * @param blueprintFileName The blueprint file name
307 * @return The generated loop name
309 public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
310 String blueprintFileName) {
311 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
312 .append("_").append(resourceName).append("_").append(blueprintFileName.replaceAll(".yaml", ""));
313 return buffer.toString().replace('.', '_').replaceAll(" ", "");
317 public int hashCode() {
318 final int prime = 31;
320 result = prime * result + ((name == null) ? 0 : name.hashCode());
325 public boolean equals(Object obj) {
332 if (getClass() != obj.getClass()) {
335 Loop other = (Loop) obj;
337 if (other.name != null) {
340 } else if (!name.equals(other.name)) {