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.dao.model.jsontype.StringJsonUserType;
55 import org.onap.clamp.loop.common.AuditEntity;
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.deploy.DcaeDeployParameters;
60 import org.onap.clamp.loop.log.LoopLog;
61 import org.onap.clamp.loop.service.Service;
62 import org.onap.clamp.loop.template.LoopElementModel;
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;
79 @Column(nullable = false, name = "name", unique = true)
83 @Column(name = "dcae_deployment_id")
84 private String dcaeDeploymentId;
87 @Column(name = "dcae_deployment_status_url")
88 private String dcaeDeploymentStatusUrl;
92 @Column(columnDefinition = "json", name = "global_properties_json")
93 private JsonObject globalPropertiesJson;
96 @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
97 @JoinColumn(name = "service_uuid")
98 private Service modelService;
101 @Column(nullable = false, name = "last_computed_state")
102 @Enumerated(EnumType.STRING)
103 private LoopState lastComputedState;
107 private final Map<String, ExternalComponent> components = new HashMap<>();
110 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
111 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
114 @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
115 @JoinTable(name = "loops_to_microservicepolicies", joinColumns = @JoinColumn(name = "loop_name"),
116 inverseJoinColumns = @JoinColumn(name = "microservicepolicy_name"))
117 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
120 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
122 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
125 @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
126 @JoinColumn(name = "loop_template_name", nullable = false)
127 private LoopTemplate loopTemplate;
129 private void initializeExternalComponents() {
130 this.addComponent(new PolicyComponent());
131 this.addComponent(new DcaeComponent());
135 * Public constructor.
138 initializeExternalComponents();
144 public Loop(String name) {
146 this.lastComputedState = LoopState.DESIGN;
147 this.globalPropertiesJson = new JsonObject();
148 initializeExternalComponents();
152 * This constructor creates a loop from a loop template.
154 * @param name The loop name
155 * @param loopTemplate The loop template from which a new loop instance must be created
157 public Loop(String name, LoopTemplate loopTemplate, ToscaConverterWithDictionarySupport toscaConverter) {
159 this.setLoopTemplate(loopTemplate);
160 this.setModelService(loopTemplate.getModelService());
161 loopTemplate.getLoopElementModelsUsed().forEach(element -> {
162 if (LoopElementModel.MICRO_SERVICE_TYPE.equals(element.getLoopElementModel().getLoopElementType())) {
163 this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel()
164 .createPolicyInstance(this, toscaConverter));
166 else if (LoopElementModel.OPERATIONAL_POLICY_TYPE
167 .equals(element.getLoopElementModel().getLoopElementType())) {
168 this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel()
169 .createPolicyInstance(this, toscaConverter));
172 this.setGlobalPropertiesJson(DcaeDeployParameters.getDcaeDeploymentParametersInJson(this));
175 public String getName() {
179 void setName(String name) {
183 public String getDcaeDeploymentId() {
184 return dcaeDeploymentId;
187 void setDcaeDeploymentId(String dcaeDeploymentId) {
188 this.dcaeDeploymentId = dcaeDeploymentId;
191 public String getDcaeDeploymentStatusUrl() {
192 return dcaeDeploymentStatusUrl;
195 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
196 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
199 public LoopState getLastComputedState() {
200 return lastComputedState;
203 void setLastComputedState(LoopState lastComputedState) {
204 this.lastComputedState = lastComputedState;
207 public Set<OperationalPolicy> getOperationalPolicies() {
208 return operationalPolicies;
211 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
212 this.operationalPolicies = operationalPolicies;
215 public Set<MicroServicePolicy> getMicroServicePolicies() {
216 return microServicePolicies;
219 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
220 this.microServicePolicies = microServicePolicies;
223 public JsonObject getGlobalPropertiesJson() {
224 return globalPropertiesJson;
227 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
228 this.globalPropertiesJson = globalPropertiesJson;
231 public Set<LoopLog> getLoopLogs() {
235 void setLoopLogs(SortedSet<LoopLog> loopLogs) {
236 this.loopLogs = loopLogs;
240 * This method adds an operational policy to the loop.
242 * @param opPolicy the operationalPolicy to add
244 public void addOperationalPolicy(OperationalPolicy opPolicy) {
245 operationalPolicies.add(opPolicy);
246 opPolicy.setLoop(this);
250 * This method removes an operational policy to the loop.
252 * @param opPolicy the operationalPolicy to add
254 public void removeOperationalPolicy(OperationalPolicy opPolicy) {
255 operationalPolicies.remove(opPolicy);
259 * This method adds an micro service policy to the loop.
261 * @param microServicePolicy the micro service to add
263 public void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
264 microServicePolicies.add(microServicePolicy);
265 microServicePolicy.getUsedByLoops().add(this);
268 public void addLog(LoopLog log) {
270 this.loopLogs.add(log);
273 public Service getModelService() {
277 void setModelService(Service modelService) {
278 this.modelService = modelService;
281 public Map<String, ExternalComponent> getComponents() {
282 refreshDcaeComponents();
286 public ExternalComponent getComponent(String componentName) {
287 refreshDcaeComponents();
288 return this.components.get(componentName);
291 public void addComponent(ExternalComponent component) {
292 this.components.put(component.getComponentName(), component);
295 public LoopTemplate getLoopTemplate() {
299 public void setLoopTemplate(LoopTemplate loopTemplate) {
300 this.loopTemplate = loopTemplate;
303 private void refreshDcaeComponents() {
304 if (!this.loopTemplate.getUniqueBlueprint()) {
305 this.components.remove("DCAE");
306 for (MicroServicePolicy policy : this.microServicePolicies) {
307 if (!this.components.containsKey("DCAE_" + policy.getName())) {
308 this.addComponent(new DcaeComponent(policy.getName()));
315 * Return the operationalPolicy object with the opPolicyName.
317 * @param opPolicyName The operationalPolicy name
318 * @return The OperationalPolicy object found in loop object
320 public OperationalPolicy getOperationalPolicy(String opPolicyName) {
321 for (OperationalPolicy operationalPolicy : this.getOperationalPolicies()) {
322 if (operationalPolicy.getName().equals(opPolicyName)) {
323 return operationalPolicy;
330 * Return the microServicePolicy object with the msPolicyName.
332 * @param msPolicyName The microServicePolicy name
333 * @return The MicroServicePolicy object found in loop object
335 public MicroServicePolicy getMicroServicePolicy(String msPolicyName) {
336 for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
337 if (microServicePolicy.getName().equals(msPolicyName)) {
338 return microServicePolicy;
345 * Generate the loop name.
347 * @param serviceName The service name
348 * @param serviceVersion The service version
349 * @param resourceName The resource name
350 * @param blueprintFileName The blueprint file name
351 * @return The generated loop name
353 public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
354 String blueprintFileName) {
355 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
356 .append("_").append(resourceName).append("_").append(blueprintFileName.replaceAll(".yaml", ""));
357 return buffer.toString().replace('.', '_').replaceAll(" ", "");
361 public int hashCode() {
362 final int prime = 31;
364 result = prime * result + ((name == null) ? 0 : name.hashCode());
369 public boolean equals(Object obj) {
376 if (getClass() != obj.getClass()) {
379 Loop other = (Loop) obj;
381 if (other.name != null) {
385 else if (!name.equals(other.name)) {