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;
29 import java.io.Serializable;
30 import java.util.HashMap;
31 import java.util.HashSet;
34 import java.util.SortedSet;
35 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.OneToMany;
48 import javax.persistence.Table;
49 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.dao.model.jsontype.StringJsonUserType;
56 import org.onap.clamp.loop.components.external.DcaeComponent;
57 import org.onap.clamp.loop.components.external.ExternalComponent;
58 import org.onap.clamp.loop.components.external.PolicyComponent;
59 import org.onap.clamp.loop.log.LoopLog;
60 import org.onap.clamp.policy.microservice.MicroServicePolicy;
61 import org.onap.clamp.policy.operational.OperationalPolicy;
64 @Table(name = "loops")
65 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
66 public class Loop implements Serializable {
69 * The serial version id.
71 private static final long serialVersionUID = -286522707701388642L;
75 @Column(nullable = false, name = "name", unique = true)
79 @Column(name = "dcae_deployment_id")
80 private String dcaeDeploymentId;
83 @Column(name = "dcae_deployment_status_url")
84 private String dcaeDeploymentStatusUrl;
87 @Column(name = "dcae_blueprint_id")
88 private String dcaeBlueprintId;
90 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
91 private String svgRepresentation;
95 @Column(columnDefinition = "json", name = "global_properties_json")
96 private JsonObject globalPropertiesJson;
100 @Column(columnDefinition = "json", name = "model_properties_json")
101 private JsonObject modelPropertiesJson;
103 @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
104 private String blueprint;
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")
117 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
120 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
121 @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
122 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
125 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
127 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
129 private void initializeExternalComponents() {
130 this.addComponent(new PolicyComponent());
131 this.addComponent(new DcaeComponent());
135 initializeExternalComponents();
141 public Loop(String name, String blueprint, String svgRepresentation) {
143 this.svgRepresentation = svgRepresentation;
144 this.blueprint = blueprint;
145 this.lastComputedState = LoopState.DESIGN;
146 this.globalPropertiesJson = new JsonObject();
147 initializeExternalComponents();
150 public String getName() {
154 void setName(String name) {
158 public String getDcaeDeploymentId() {
159 return dcaeDeploymentId;
162 void setDcaeDeploymentId(String dcaeDeploymentId) {
163 this.dcaeDeploymentId = dcaeDeploymentId;
166 public String getDcaeDeploymentStatusUrl() {
167 return dcaeDeploymentStatusUrl;
170 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
171 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
174 public String getSvgRepresentation() {
175 return svgRepresentation;
178 void setSvgRepresentation(String svgRepresentation) {
179 this.svgRepresentation = svgRepresentation;
182 public String getBlueprint() {
186 void setBlueprint(String blueprint) {
187 this.blueprint = blueprint;
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;
230 void addOperationalPolicy(OperationalPolicy opPolicy) {
231 operationalPolicies.add(opPolicy);
232 opPolicy.setLoop(this);
235 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
236 microServicePolicies.add(microServicePolicy);
237 microServicePolicy.getUsedByLoops().add(this);
240 public void addLog(LoopLog log) {
242 this.loopLogs.add(log);
245 public String getDcaeBlueprintId() {
246 return dcaeBlueprintId;
249 void setDcaeBlueprintId(String dcaeBlueprintId) {
250 this.dcaeBlueprintId = dcaeBlueprintId;
253 public JsonObject getModelPropertiesJson() {
254 return modelPropertiesJson;
257 void setModelPropertiesJson(JsonObject modelPropertiesJson) {
258 this.modelPropertiesJson = modelPropertiesJson;
261 public Map<String, ExternalComponent> getComponents() {
265 public ExternalComponent getComponent(String componentName) {
266 return this.components.get(componentName);
269 public void addComponent(ExternalComponent component) {
270 this.components.put(component.getComponentName(), component);
274 * Generate the loop name.
278 * @param serviceVersion
279 * The service version
280 * @param resourceName
282 * @param blueprintFileName
283 * The blueprint file name
284 * @return The generated loop name
286 static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
287 String blueprintFilename) {
288 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
289 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
290 return buffer.toString().replace('.', '_').replaceAll(" ", "");
294 public int hashCode() {
295 final int prime = 31;
297 result = prime * result + ((name == null) ? 0 : name.hashCode());
302 public boolean equals(Object obj) {
309 if (getClass() != obj.getClass()) {
312 Loop other = (Loop) obj;
314 if (other.name != null) {
317 } else if (!name.equals(other.name)) {