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.Table;
50 import org.hibernate.annotations.Type;
51 import org.hibernate.annotations.TypeDef;
52 import org.hibernate.annotations.TypeDefs;
53 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
54 import org.onap.clamp.loop.log.LoopLog;
55 import org.onap.clamp.policy.microservice.MicroServicePolicy;
56 import org.onap.clamp.policy.operational.OperationalPolicy;
59 @Table(name = "loops")
60 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
61 public class Loop implements Serializable {
64 * The serial version id.
66 private static final long serialVersionUID = -286522707701388642L;
70 @Column(nullable = false, name = "name", unique = true)
74 @Column(name = "dcae_deployment_id")
75 private String dcaeDeploymentId;
78 @Column(name = "dcae_deployment_status_url")
79 private String dcaeDeploymentStatusUrl;
82 @Column(name = "dcae_blueprint_id")
83 private String dcaeBlueprintId;
85 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
86 private String svgRepresentation;
90 @Column(columnDefinition = "json", name = "global_properties_json")
91 private JsonObject globalPropertiesJson;
95 @Column(columnDefinition = "json", name = "model_properties_json")
96 private JsonObject modelPropertiesJson;
98 @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
99 private String blueprint;
102 @Column(nullable = false, name = "last_computed_state")
103 @Enumerated(EnumType.STRING)
104 private LoopState lastComputedState;
107 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
108 private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
111 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
112 @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
113 private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
116 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
117 private Set<LoopLog> loopLogs = new HashSet<>();
125 public Loop(String name, String blueprint, String svgRepresentation) {
127 this.svgRepresentation = svgRepresentation;
128 this.blueprint = blueprint;
129 this.lastComputedState = LoopState.DESIGN;
130 this.globalPropertiesJson = new JsonObject();
133 public String getName() {
137 void setName(String name) {
141 public String getDcaeDeploymentId() {
142 return dcaeDeploymentId;
145 void setDcaeDeploymentId(String dcaeDeploymentId) {
146 this.dcaeDeploymentId = dcaeDeploymentId;
149 public String getDcaeDeploymentStatusUrl() {
150 return dcaeDeploymentStatusUrl;
153 void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
154 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
157 public String getSvgRepresentation() {
158 return svgRepresentation;
161 void setSvgRepresentation(String svgRepresentation) {
162 this.svgRepresentation = svgRepresentation;
165 public String getBlueprint() {
169 void setBlueprint(String blueprint) {
170 this.blueprint = blueprint;
173 public LoopState getLastComputedState() {
174 return lastComputedState;
177 void setLastComputedState(LoopState lastComputedState) {
178 this.lastComputedState = lastComputedState;
181 public Set<OperationalPolicy> getOperationalPolicies() {
182 return operationalPolicies;
185 void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
186 this.operationalPolicies = operationalPolicies;
189 public Set<MicroServicePolicy> getMicroServicePolicies() {
190 return microServicePolicies;
193 void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
194 this.microServicePolicies = microServicePolicies;
197 public JsonObject getGlobalPropertiesJson() {
198 return globalPropertiesJson;
201 void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
202 this.globalPropertiesJson = globalPropertiesJson;
205 public Set<LoopLog> getLoopLogs() {
209 void setLoopLogs(Set<LoopLog> loopLogs) {
210 this.loopLogs = loopLogs;
213 void addOperationalPolicy(OperationalPolicy opPolicy) {
214 operationalPolicies.add(opPolicy);
215 opPolicy.setLoop(this);
218 void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
219 microServicePolicies.add(microServicePolicy);
220 microServicePolicy.getUsedByLoops().add(this);
223 void addLog(LoopLog log) {
228 public String getDcaeBlueprintId() {
229 return dcaeBlueprintId;
232 void setDcaeBlueprintId(String dcaeBlueprintId) {
233 this.dcaeBlueprintId = dcaeBlueprintId;
236 public JsonObject getModelPropertiesJson() {
237 return modelPropertiesJson;
240 void setModelPropertiesJson(JsonObject modelPropertiesJson) {
241 this.modelPropertiesJson = modelPropertiesJson;
245 * Generate the loop name.
249 * @param serviceVersion
250 * The service version
251 * @param resourceName
253 * @param blueprintFileName
254 * The blueprint file name
255 * @return The generated loop name
257 static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
258 String blueprintFilename) {
259 StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
260 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
261 return buffer.toString().replace('.', '_').replaceAll(" ", "");
265 * Generates the Json that must be sent to policy to add all policies to Active
268 * @return The json, payload to send
270 public String createPoliciesPayloadPdpGroup() {
271 JsonObject jsonObject = new JsonObject();
272 JsonArray jsonArray = new JsonArray();
273 jsonObject.add("policies", jsonArray);
275 for (String policyName : this.listPolicyNamesPdpGroup()) {
276 JsonObject policyNode = new JsonObject();
277 jsonArray.add(policyNode);
278 policyNode.addProperty("policy-id", policyName);
280 return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
284 * Generates the list of policy names that must be send/remove to/from active
287 * @return A list of policy names
289 public List<String> listPolicyNamesPdpGroup() {
290 List<String> policyNamesList = new ArrayList<>();
291 for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
292 policyNamesList.add(opPolicy.getName());
293 for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
294 policyNamesList.add(guardName);
297 for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
298 policyNamesList.add(microServicePolicy.getName());
300 return policyNamesList;
304 public int hashCode() {
305 final int prime = 31;
307 result = prime * result + ((name == null) ? 0 : name.hashCode());
312 public boolean equals(Object obj) {
319 if (getClass() != obj.getClass()) {
322 Loop other = (Loop) obj;
324 if (other.name != null) {
327 } else if (!name.equals(other.name)) {