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.OneToMany;
52 import javax.persistence.Table;
53 import javax.persistence.Transient;
55 import org.hibernate.annotations.SortNatural;
56 import org.hibernate.annotations.Type;
57 import org.hibernate.annotations.TypeDef;
58 import org.hibernate.annotations.TypeDefs;
59 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
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.policy.microservice.MicroServicePolicy;
65 import org.onap.clamp.policy.operational.OperationalPolicy;
66 import org.onap.clamp.policy.operational.OperationalPolicyRepresentationBuilder;
69 @Table(name = "loops")
70 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
71 public class Loop implements Serializable {
74 * The serial version id.
76 private static final long serialVersionUID = -286522707701388642L;
79 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
83 @Column(nullable = false, name = "name", unique = true)
87 @Column(name = "dcae_deployment_id")
88 private String dcaeDeploymentId;
91 @Column(name = "dcae_deployment_status_url")
92 private String dcaeDeploymentStatusUrl;
95 @Column(name = "dcae_blueprint_id")
96 private String dcaeBlueprintId;
98 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
99 private String svgRepresentation;
103 @Column(columnDefinition = "json", name = "operational_policy_schema")
104 private JsonObject operationalPolicySchema;
108 @Column(columnDefinition = "json", name = "global_properties_json")
109 private JsonObject globalPropertiesJson;
113 @Column(columnDefinition = "json", name = "model_properties_json")
114 private JsonObject modelPropertiesJson;
116 @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
117 private String blueprint;
120 @Column(nullable = false, name = "last_computed_state")
121 @Enumerated(EnumType.STRING)
122 private LoopState lastComputedState;
126 private final Map<String, ExternalComponent> components = new HashMap<>();
129 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
130 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
133 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
134 @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
135 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
138 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
140 private SortedSet<LoopLog> loopLogs = new TreeSet<>();
142 private void initializeExternalComponents() {
143 this.addComponent(new PolicyComponent());
144 this.addComponent(new DcaeComponent());
148 * Public constructor.
151 initializeExternalComponents();
157 public Loop(String name, String blueprint, String svgRepresentation) {
159 this.svgRepresentation = svgRepresentation;
160 this.blueprint = blueprint;
161 this.lastComputedState = LoopState.DESIGN;
162 this.globalPropertiesJson = new JsonObject();
163 initializeExternalComponents();
166 public String getName() {
170 void setName(String name) {
174 public String getDcaeDeploymentId() {
175 return dcaeDeploymentId;
178 void setDcaeDeploymentId(String dcaeDeploymentId) {
179 this.dcaeDeploymentId = dcaeDeploymentId;
182 public String getDcaeDeploymentStatusUrl() {
183 return dcaeDeploymentStatusUrl;
186 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
187 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
190 public String getSvgRepresentation() {
191 return svgRepresentation;
194 void setSvgRepresentation(String svgRepresentation) {
195 this.svgRepresentation = svgRepresentation;
198 public String getBlueprint() {
202 void setBlueprint(String blueprint) {
203 this.blueprint = blueprint;
206 public LoopState getLastComputedState() {
207 return lastComputedState;
210 void setLastComputedState(LoopState lastComputedState) {
211 this.lastComputedState = lastComputedState;
214 public Set<OperationalPolicy> getOperationalPolicies() {
215 return operationalPolicies;
218 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
219 this.operationalPolicies = operationalPolicies;
222 public Set<MicroServicePolicy> getMicroServicePolicies() {
223 return microServicePolicies;
226 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
227 this.microServicePolicies = microServicePolicies;
230 public JsonObject getGlobalPropertiesJson() {
231 return globalPropertiesJson;
234 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
235 this.globalPropertiesJson = globalPropertiesJson;
238 public Set<LoopLog> getLoopLogs() {
242 void setLoopLogs(SortedSet<LoopLog> loopLogs) {
243 this.loopLogs = loopLogs;
246 void addOperationalPolicy(OperationalPolicy opPolicy) {
247 operationalPolicies.add(opPolicy);
248 opPolicy.setLoop(this);
251 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
252 microServicePolicies.add(microServicePolicy);
253 microServicePolicy.getUsedByLoops().add(this);
256 public void addLog(LoopLog log) {
258 this.loopLogs.add(log);
261 public String getDcaeBlueprintId() {
262 return dcaeBlueprintId;
265 void setDcaeBlueprintId(String dcaeBlueprintId) {
266 this.dcaeBlueprintId = dcaeBlueprintId;
269 public JsonObject getModelPropertiesJson() {
270 return modelPropertiesJson;
273 void setModelPropertiesJson(JsonObject modelPropertiesJson) {
274 this.modelPropertiesJson = modelPropertiesJson;
276 this.operationalPolicySchema = OperationalPolicyRepresentationBuilder
277 .generateOperationalPolicySchema(this.getModelPropertiesJson());
278 } catch (JsonSyntaxException | IOException | NullPointerException e) {
279 logger.error("Unable to generate the operational policy Schema ... ", e);
280 this.operationalPolicySchema = new JsonObject();
284 public Map<String, ExternalComponent> getComponents() {
288 public ExternalComponent getComponent(String componentName) {
289 return this.components.get(componentName);
292 public void addComponent(ExternalComponent component) {
293 this.components.put(component.getComponentName(), component);
297 * Generate the loop name.
299 * @param serviceName The service name
300 * @param serviceVersion The service version
301 * @param resourceName The resource name
302 * @param blueprintFileName The blueprint file name
303 * @return The generated loop name
305 static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
306 String blueprintFilename) {
307 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
308 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
309 return buffer.toString().replace('.', '_').replaceAll(" ", "");
313 public int hashCode() {
314 final int prime = 31;
316 result = prime * result + ((name == null) ? 0 : name.hashCode());
321 public boolean equals(Object obj) {
328 if (getClass() != obj.getClass()) {
331 Loop other = (Loop) obj;
333 if (other.name != null) {
336 } else if (!name.equals(other.name)) {