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.ArrayList;
33 import java.util.HashSet;
34 import java.util.List;
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.OrderBy;
49 import javax.persistence.Table;
51 import org.hibernate.annotations.Type;
52 import org.hibernate.annotations.TypeDef;
53 import org.hibernate.annotations.TypeDefs;
54 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
55 import org.onap.clamp.loop.log.LoopLog;
56 import org.onap.clamp.policy.microservice.MicroServicePolicy;
57 import org.onap.clamp.policy.operational.OperationalPolicy;
60 @Table(name = "loops")
61 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
62 public class Loop implements Serializable {
65 * The serial version id.
67 private static final long serialVersionUID = -286522707701388642L;
71 @Column(nullable = false, name = "name", unique = true)
75 @Column(name = "dcae_deployment_id")
76 private String dcaeDeploymentId;
79 @Column(name = "dcae_deployment_status_url")
80 private String dcaeDeploymentStatusUrl;
83 @Column(name = "dcae_blueprint_id")
84 private String dcaeBlueprintId;
86 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
87 private String svgRepresentation;
91 @Column(columnDefinition = "json", name = "global_properties_json")
92 private JsonObject globalPropertiesJson;
96 @Column(columnDefinition = "json", name = "model_properties_json")
97 private JsonObject modelPropertiesJson;
99 @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
100 private String blueprint;
103 @Column(nullable = false, name = "last_computed_state")
104 @Enumerated(EnumType.STRING)
105 private LoopState lastComputedState;
108 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
109 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
112 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
113 @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
114 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
117 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
118 @OrderBy("log_instant DESC")
119 private Set<LoopLog> loopLogs = new HashSet<>();
127 public Loop(String name, String blueprint, String svgRepresentation) {
129 this.svgRepresentation = svgRepresentation;
130 this.blueprint = blueprint;
131 this.lastComputedState = LoopState.DESIGN;
132 this.globalPropertiesJson = new JsonObject();
135 public String getName() {
139 void setName(String name) {
143 public String getDcaeDeploymentId() {
144 return dcaeDeploymentId;
147 void setDcaeDeploymentId(String dcaeDeploymentId) {
148 this.dcaeDeploymentId = dcaeDeploymentId;
151 public String getDcaeDeploymentStatusUrl() {
152 return dcaeDeploymentStatusUrl;
155 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
156 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
159 public String getSvgRepresentation() {
160 return svgRepresentation;
163 void setSvgRepresentation(String svgRepresentation) {
164 this.svgRepresentation = svgRepresentation;
167 public String getBlueprint() {
171 void setBlueprint(String blueprint) {
172 this.blueprint = blueprint;
175 public LoopState getLastComputedState() {
176 return lastComputedState;
179 void setLastComputedState(LoopState lastComputedState) {
180 this.lastComputedState = lastComputedState;
183 public Set<OperationalPolicy> getOperationalPolicies() {
184 return operationalPolicies;
187 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
188 this.operationalPolicies = operationalPolicies;
191 public Set<MicroServicePolicy> getMicroServicePolicies() {
192 return microServicePolicies;
195 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
196 this.microServicePolicies = microServicePolicies;
199 public JsonObject getGlobalPropertiesJson() {
200 return globalPropertiesJson;
203 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
204 this.globalPropertiesJson = globalPropertiesJson;
207 public Set<LoopLog> getLoopLogs() {
211 void setLoopLogs(Set<LoopLog> loopLogs) {
212 this.loopLogs = loopLogs;
215 void addOperationalPolicy(OperationalPolicy opPolicy) {
216 operationalPolicies.add(opPolicy);
217 opPolicy.setLoop(this);
220 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
221 microServicePolicies.add(microServicePolicy);
222 microServicePolicy.getUsedByLoops().add(this);
225 void addLog(LoopLog log) {
230 public String getDcaeBlueprintId() {
231 return dcaeBlueprintId;
234 void setDcaeBlueprintId(String dcaeBlueprintId) {
235 this.dcaeBlueprintId = dcaeBlueprintId;
238 public JsonObject getModelPropertiesJson() {
239 return modelPropertiesJson;
242 void setModelPropertiesJson(JsonObject modelPropertiesJson) {
243 this.modelPropertiesJson = modelPropertiesJson;
247 * Generate the loop name.
251 * @param serviceVersion
252 * The service version
253 * @param resourceName
255 * @param blueprintFileName
256 * The blueprint file name
257 * @return The generated loop name
259 static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
260 String blueprintFilename) {
261 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
262 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
263 return buffer.toString().replace('.', '_').replaceAll(" ", "");
267 * Generates the Json that must be sent to policy to add all policies to Active
270 * @return The json, payload to send
272 public String createPoliciesPayloadPdpGroup() {
273 JsonObject jsonObject = new JsonObject();
274 JsonArray jsonArray = new JsonArray();
275 jsonObject.add("policies", jsonArray);
277 for (String policyName : this.listPolicyNamesPdpGroup()) {
278 JsonObject policyNode = new JsonObject();
279 jsonArray.add(policyNode);
280 policyNode.addProperty("policy-id", policyName);
282 return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
286 * Generates the list of policy names that must be send/remove to/from active
289 * @return A list of policy names
291 public List<String> listPolicyNamesPdpGroup() {
292 List<String> policyNamesList = new ArrayList<>();
293 for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
294 policyNamesList.add(opPolicy.getName());
295 for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
296 policyNamesList.add(guardName);
299 for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
300 policyNamesList.add(microServicePolicy.getName());
302 return policyNamesList;
306 public int hashCode() {
307 final int prime = 31;
309 result = prime * result + ((name == null) ? 0 : name.hashCode());
314 public boolean equals(Object obj) {
321 if (getClass() != obj.getClass()) {
324 Loop other = (Loop) obj;
326 if (other.name != null) {
329 } else if (!name.equals(other.name)) {