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;
31 import java.io.Serializable;
32 import java.util.HashMap;
33 import java.util.HashSet;
36 import java.util.SortedSet;
37 import java.util.TreeSet;
39 import javax.persistence.CascadeType;
40 import javax.persistence.Column;
41 import javax.persistence.Entity;
42 import javax.persistence.EnumType;
43 import javax.persistence.Enumerated;
44 import javax.persistence.FetchType;
45 import javax.persistence.Id;
46 import javax.persistence.JoinColumn;
47 import javax.persistence.JoinTable;
48 import javax.persistence.ManyToMany;
49 import javax.persistence.ManyToOne;
50 import javax.persistence.OneToMany;
51 import javax.persistence.Table;
52 import javax.persistence.Transient;
54 import org.hibernate.annotations.SortNatural;
55 import org.hibernate.annotations.Type;
56 import org.hibernate.annotations.TypeDef;
57 import org.hibernate.annotations.TypeDefs;
58 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
59 import org.onap.clamp.loop.common.AuditEntity;
60 import org.onap.clamp.loop.components.external.DcaeComponent;
61 import org.onap.clamp.loop.components.external.ExternalComponent;
62 import org.onap.clamp.loop.components.external.PolicyComponent;
63 import org.onap.clamp.loop.log.LoopLog;
64 import org.onap.clamp.loop.service.Service;
65 import org.onap.clamp.loop.template.LoopTemplate;
66 import org.onap.clamp.policy.microservice.MicroServicePolicy;
67 import org.onap.clamp.policy.operational.OperationalPolicy;
70 @Table(name = "loops")
71 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
72 public class Loop extends AuditEntity implements Serializable {
75 * The serial version id.
77 private static final long serialVersionUID = -286522707701388642L;
80 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
84 @Column(nullable = false, name = "name", unique = true)
88 @Column(name = "dcae_deployment_id")
89 private String dcaeDeploymentId;
92 @Column(name = "dcae_deployment_status_url")
93 private String dcaeDeploymentStatusUrl;
95 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
96 private String svgRepresentation;
100 @Column(columnDefinition = "json", name = "global_properties_json")
101 private JsonObject globalPropertiesJson;
104 @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
105 @JoinColumn(name = "service_uuid")
106 private Service modelService;
109 @Column(nullable = false, name = "last_computed_state")
110 @Enumerated(EnumType.STRING)
111 private LoopState lastComputedState;
115 private final Map<String, ExternalComponent> components = new HashMap<>();
118 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
119 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
122 @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
123 @JoinTable(name = "loops_to_microservicepolicies", joinColumns = @JoinColumn(name = "loop_name"),
124 inverseJoinColumns = @JoinColumn(name = "microservicepolicy_name"))
125 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
128 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
130 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
133 @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
134 @JoinColumn(name = "loop_template_name")
135 private LoopTemplate loopTemplate;
137 private void initializeExternalComponents() {
138 this.addComponent(new PolicyComponent());
139 this.addComponent(new DcaeComponent());
143 * Public constructor.
146 initializeExternalComponents();
152 public Loop(String name, String svgRepresentation) {
154 this.svgRepresentation = svgRepresentation;
155 this.lastComputedState = LoopState.DESIGN;
156 this.globalPropertiesJson = new JsonObject();
157 initializeExternalComponents();
160 public String getName() {
164 void setName(String name) {
168 public String getDcaeDeploymentId() {
169 return dcaeDeploymentId;
172 void setDcaeDeploymentId(String dcaeDeploymentId) {
173 this.dcaeDeploymentId = dcaeDeploymentId;
176 public String getDcaeDeploymentStatusUrl() {
177 return dcaeDeploymentStatusUrl;
180 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
181 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
184 public String getSvgRepresentation() {
185 return svgRepresentation;
188 void setSvgRepresentation(String svgRepresentation) {
189 this.svgRepresentation = svgRepresentation;
192 public LoopState getLastComputedState() {
193 return lastComputedState;
196 void setLastComputedState(LoopState lastComputedState) {
197 this.lastComputedState = lastComputedState;
200 public Set<OperationalPolicy> getOperationalPolicies() {
201 return operationalPolicies;
204 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
205 this.operationalPolicies = operationalPolicies;
208 public Set<MicroServicePolicy> getMicroServicePolicies() {
209 return microServicePolicies;
212 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
213 this.microServicePolicies = microServicePolicies;
216 public JsonObject getGlobalPropertiesJson() {
217 return globalPropertiesJson;
220 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
221 this.globalPropertiesJson = globalPropertiesJson;
224 public Set<LoopLog> getLoopLogs() {
228 void setLoopLogs(SortedSet<LoopLog> loopLogs) {
229 this.loopLogs = loopLogs;
232 void addOperationalPolicy(OperationalPolicy opPolicy) {
233 operationalPolicies.add(opPolicy);
234 opPolicy.setLoop(this);
237 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
238 microServicePolicies.add(microServicePolicy);
239 microServicePolicy.getUsedByLoops().add(this);
242 public void addLog(LoopLog log) {
244 this.loopLogs.add(log);
247 public Service getModelService() {
251 void setModelService(Service modelService) {
252 this.modelService = modelService;
255 public Map<String, ExternalComponent> getComponents() {
259 public ExternalComponent getComponent(String componentName) {
260 return this.components.get(componentName);
263 public void addComponent(ExternalComponent component) {
264 this.components.put(component.getComponentName(), component);
267 public LoopTemplate getLoopTemplate() {
271 public void setLoopTemplate(LoopTemplate loopTemplate) {
272 this.loopTemplate = loopTemplate;
276 * Generate the loop name.
278 * @param serviceName The service name
279 * @param serviceVersion The service version
280 * @param resourceName The resource name
281 * @param blueprintFileName The blueprint file name
282 * @return The generated loop name
284 public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
285 String blueprintFilename) {
286 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
287 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
288 return buffer.toString().replace('.', '_').replaceAll(" ", "");
292 public int hashCode() {
293 final int prime = 31;
295 result = prime * result + ((name == null) ? 0 : name.hashCode());
300 public boolean equals(Object obj) {
307 if (getClass() != obj.getClass()) {
310 Loop other = (Loop) obj;
312 if (other.name != null) {
315 } else if (!name.equals(other.name)) {