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.GsonBuilder;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonObject;
29 import com.google.gson.annotations.Expose;
31 import java.io.Serializable;
32 import java.util.HashSet;
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.OneToMany;
46 import javax.persistence.Table;
48 import org.hibernate.annotations.Type;
49 import org.hibernate.annotations.TypeDef;
50 import org.hibernate.annotations.TypeDefs;
51 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
52 import org.onap.clamp.loop.log.LoopLog;
53 import org.onap.clamp.policy.microservice.MicroServicePolicy;
54 import org.onap.clamp.policy.operational.OperationalPolicy;
57 @Table(name = "loops")
58 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
59 public class Loop implements Serializable {
62 * The serial version id.
64 private static final long serialVersionUID = -286522707701388642L;
68 @Column(nullable = false, name = "name", unique = true)
72 @Column(name = "dcae_deployment_id")
73 private String dcaeDeploymentId;
76 @Column(name = "dcae_deployment_status_url")
77 private String dcaeDeploymentStatusUrl;
80 @Column(name = "dcae_blueprint_id")
81 private String dcaeBlueprintId;
83 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
84 private String svgRepresentation;
88 @Column(columnDefinition = "json", name = "global_properties_json")
89 private JsonObject globalPropertiesJson;
93 @Column(columnDefinition = "json", name = "model_properties_json")
94 private JsonObject modelPropertiesJson;
96 @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
97 private String blueprint;
100 @Column(nullable = false, name = "last_computed_state")
101 @Enumerated(EnumType.STRING)
102 private LoopState lastComputedState;
105 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
106 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
109 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
110 @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
111 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
114 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
115 private Set<LoopLog> loopLogs = new HashSet<>();
123 public Loop(String name, String blueprint, String svgRepresentation) {
125 this.svgRepresentation = svgRepresentation;
126 this.blueprint = blueprint;
127 this.lastComputedState = LoopState.DESIGN;
128 this.globalPropertiesJson = new JsonObject();
131 public String getName() {
135 void setName(String name) {
139 public String getDcaeDeploymentId() {
140 return dcaeDeploymentId;
143 void setDcaeDeploymentId(String dcaeDeploymentId) {
144 this.dcaeDeploymentId = dcaeDeploymentId;
147 public String getDcaeDeploymentStatusUrl() {
148 return dcaeDeploymentStatusUrl;
151 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
152 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
155 public String getSvgRepresentation() {
156 return svgRepresentation;
159 void setSvgRepresentation(String svgRepresentation) {
160 this.svgRepresentation = svgRepresentation;
163 public String getBlueprint() {
167 void setBlueprint(String blueprint) {
168 this.blueprint = blueprint;
171 public LoopState getLastComputedState() {
172 return lastComputedState;
175 void setLastComputedState(LoopState lastComputedState) {
176 this.lastComputedState = lastComputedState;
179 public Set<OperationalPolicy> getOperationalPolicies() {
180 return operationalPolicies;
183 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
184 this.operationalPolicies = operationalPolicies;
187 public Set<MicroServicePolicy> getMicroServicePolicies() {
188 return microServicePolicies;
191 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
192 this.microServicePolicies = microServicePolicies;
195 public JsonObject getGlobalPropertiesJson() {
196 return globalPropertiesJson;
199 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
200 this.globalPropertiesJson = globalPropertiesJson;
203 public Set<LoopLog> getLoopLogs() {
207 void setLoopLogs(Set<LoopLog> loopLogs) {
208 this.loopLogs = loopLogs;
211 void addOperationalPolicy(OperationalPolicy opPolicy) {
212 operationalPolicies.add(opPolicy);
213 opPolicy.setLoop(this);
216 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
217 microServicePolicies.add(microServicePolicy);
218 microServicePolicy.getUsedByLoops().add(this);
221 void addLog(LoopLog log) {
226 public String getDcaeBlueprintId() {
227 return dcaeBlueprintId;
230 void setDcaeBlueprintId(String dcaeBlueprintId) {
231 this.dcaeBlueprintId = dcaeBlueprintId;
234 public JsonObject getModelPropertiesJson() {
235 return modelPropertiesJson;
238 void setModelPropertiesJson(JsonObject modelPropertiesJson) {
239 this.modelPropertiesJson = modelPropertiesJson;
243 * Generate the loop name.
247 * @param serviceVersion
248 * The service version
249 * @param resourceName
251 * @param blueprintFileName
252 * The blueprint file name
253 * @return The generated loop name
255 static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
256 String blueprintFilename) {
257 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
258 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
259 return buffer.toString().replace('.', '_').replaceAll(" ", "");
262 public String createPoliciesPayloadPdpGroup() {
263 JsonObject jsonObject = new JsonObject();
264 JsonArray jsonArray = new JsonArray();
265 jsonObject.add("policies", jsonArray);
267 for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
268 JsonObject policyNode = new JsonObject();
269 jsonArray.add(policyNode);
270 policyNode.addProperty("policy-id", opPolicy.getName());
272 for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
273 JsonObject guardPolicyNode = new JsonObject();
274 jsonArray.add(guardPolicyNode);
275 guardPolicyNode.addProperty("policy-id", guardName);
278 for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
279 JsonObject policyNode = new JsonObject();
280 jsonArray.add(policyNode);
281 policyNode.addProperty("policy-id", microServicePolicy.getName());
283 return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
287 public int hashCode() {
288 final int prime = 31;
290 result = prime * result + ((name == null) ? 0 : name.hashCode());
295 public boolean equals(Object obj) {
302 if (getClass() != obj.getClass()) {
305 Loop other = (Loop) obj;
307 if (other.name != null) {
310 } else if (!name.equals(other.name)) {