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.JsonSyntaxException;
30 import com.google.gson.annotations.Expose;
32 import java.io.IOException;
33 import java.io.Serializable;
34 import java.util.HashMap;
35 import java.util.HashSet;
38 import java.util.SortedSet;
39 import java.util.TreeSet;
41 import javax.persistence.CascadeType;
42 import javax.persistence.Column;
43 import javax.persistence.Entity;
44 import javax.persistence.EnumType;
45 import javax.persistence.Enumerated;
46 import javax.persistence.FetchType;
47 import javax.persistence.Id;
48 import javax.persistence.JoinColumn;
49 import javax.persistence.JoinTable;
50 import javax.persistence.ManyToMany;
51 import javax.persistence.ManyToOne;
52 import javax.persistence.OneToMany;
53 import javax.persistence.Table;
54 import javax.persistence.Transient;
56 import org.hibernate.annotations.SortNatural;
57 import org.hibernate.annotations.Type;
58 import org.hibernate.annotations.TypeDef;
59 import org.hibernate.annotations.TypeDefs;
60 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
61 import org.onap.clamp.loop.components.external.DcaeComponent;
62 import org.onap.clamp.loop.components.external.ExternalComponent;
63 import org.onap.clamp.loop.components.external.PolicyComponent;
64 import org.onap.clamp.loop.log.LoopLog;
65 import org.onap.clamp.loop.service.Service;
66 import org.onap.clamp.policy.microservice.MicroServicePolicy;
67 import org.onap.clamp.policy.operational.OperationalPolicy;
68 import org.onap.clamp.policy.operational.OperationalPolicyRepresentationBuilder;
71 @Table(name = "loops")
72 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
73 public class Loop implements Serializable {
76 * The serial version id.
78 private static final long serialVersionUID = -286522707701388642L;
81 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
85 @Column(nullable = false, name = "name", unique = true)
89 @Column(name = "dcae_deployment_id")
90 private String dcaeDeploymentId;
93 @Column(name = "dcae_deployment_status_url")
94 private String dcaeDeploymentStatusUrl;
97 @Column(name = "dcae_blueprint_id")
98 private String dcaeBlueprintId;
100 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
101 private String svgRepresentation;
105 @Column(columnDefinition = "json", name = "operational_policy_schema")
106 private JsonObject operationalPolicySchema;
110 @Column(columnDefinition = "json", name = "global_properties_json")
111 private JsonObject globalPropertiesJson;
114 @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
115 @JoinColumn(name = "service_uuid")
116 private Service modelService;
118 @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
119 private String blueprint;
122 @Column(nullable = false, name = "last_computed_state")
123 @Enumerated(EnumType.STRING)
124 private LoopState lastComputedState;
128 private final Map<String, ExternalComponent> components = new HashMap<>();
131 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
132 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
135 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
136 @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
137 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
140 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
142 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
144 private void initializeExternalComponents() {
145 this.addComponent(new PolicyComponent());
146 this.addComponent(new DcaeComponent());
150 * Public constructor.
153 initializeExternalComponents();
159 public Loop(String name, String blueprint, String svgRepresentation) {
161 this.svgRepresentation = svgRepresentation;
162 this.blueprint = blueprint;
163 this.lastComputedState = LoopState.DESIGN;
164 this.globalPropertiesJson = new JsonObject();
165 initializeExternalComponents();
168 public String getName() {
172 void setName(String name) {
176 public String getDcaeDeploymentId() {
177 return dcaeDeploymentId;
180 void setDcaeDeploymentId(String dcaeDeploymentId) {
181 this.dcaeDeploymentId = dcaeDeploymentId;
184 public String getDcaeDeploymentStatusUrl() {
185 return dcaeDeploymentStatusUrl;
188 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
189 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
192 public String getSvgRepresentation() {
193 return svgRepresentation;
196 void setSvgRepresentation(String svgRepresentation) {
197 this.svgRepresentation = svgRepresentation;
200 public String getBlueprint() {
204 void setBlueprint(String blueprint) {
205 this.blueprint = blueprint;
208 public LoopState getLastComputedState() {
209 return lastComputedState;
212 void setLastComputedState(LoopState lastComputedState) {
213 this.lastComputedState = lastComputedState;
216 public Set<OperationalPolicy> getOperationalPolicies() {
217 return operationalPolicies;
220 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
221 this.operationalPolicies = operationalPolicies;
224 public Set<MicroServicePolicy> getMicroServicePolicies() {
225 return microServicePolicies;
228 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
229 this.microServicePolicies = microServicePolicies;
232 public JsonObject getGlobalPropertiesJson() {
233 return globalPropertiesJson;
236 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
237 this.globalPropertiesJson = globalPropertiesJson;
240 public Set<LoopLog> getLoopLogs() {
244 void setLoopLogs(SortedSet<LoopLog> loopLogs) {
245 this.loopLogs = loopLogs;
248 void addOperationalPolicy(OperationalPolicy opPolicy) {
249 operationalPolicies.add(opPolicy);
250 opPolicy.setLoop(this);
253 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
254 microServicePolicies.add(microServicePolicy);
255 microServicePolicy.getUsedByLoops().add(this);
258 public void addLog(LoopLog log) {
260 this.loopLogs.add(log);
263 public String getDcaeBlueprintId() {
264 return dcaeBlueprintId;
267 void setDcaeBlueprintId(String dcaeBlueprintId) {
268 this.dcaeBlueprintId = dcaeBlueprintId;
271 public Service getModelService() {
275 void setModelService(Service modelService) {
276 this.modelService = modelService;
278 this.operationalPolicySchema = OperationalPolicyRepresentationBuilder
279 .generateOperationalPolicySchema(this.getModelService());
280 } catch (JsonSyntaxException | IOException | NullPointerException e) {
281 logger.error("Unable to generate the operational policy Schema ... ", e);
282 this.operationalPolicySchema = new JsonObject();
286 public Map<String, ExternalComponent> getComponents() {
290 public ExternalComponent getComponent(String componentName) {
291 return this.components.get(componentName);
294 public void addComponent(ExternalComponent component) {
295 this.components.put(component.getComponentName(), component);
299 * Generate the loop name.
301 * @param serviceName The service name
302 * @param serviceVersion The service version
303 * @param resourceName The resource name
304 * @param blueprintFileName The blueprint file name
305 * @return The generated loop name
307 static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
308 String blueprintFilename) {
309 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
310 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
311 return buffer.toString().replace('.', '_').replaceAll(" ", "");
315 public int hashCode() {
316 final int prime = 31;
318 result = prime * result + ((name == null) ? 0 : name.hashCode());
323 public boolean equals(Object obj) {
330 if (getClass() != obj.getClass()) {
333 Loop other = (Loop) obj;
335 if (other.name != null) {
338 } else if (!name.equals(other.name)) {