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.policy.microservice;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonArray;
31 import com.google.gson.JsonObject;
32 import com.google.gson.annotations.Expose;
34 import java.io.Serializable;
35 import java.util.HashSet;
39 import javax.persistence.Column;
40 import javax.persistence.Entity;
41 import javax.persistence.FetchType;
42 import javax.persistence.Id;
43 import javax.persistence.ManyToMany;
44 import javax.persistence.Table;
45 import javax.persistence.Transient;
47 import org.hibernate.annotations.TypeDef;
48 import org.hibernate.annotations.TypeDefs;
49 import org.json.JSONObject;
50 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
51 import org.onap.clamp.clds.util.JsonUtils;
52 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
53 import org.onap.clamp.loop.Loop;
54 import org.onap.clamp.policy.Policy;
55 import org.yaml.snakeyaml.Yaml;
58 @Table(name = "micro_service_policies")
59 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
60 public class MicroServicePolicy extends Policy implements Serializable {
62 * The serial version ID.
64 private static final long serialVersionUID = 6271238288583332616L;
67 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class);
71 @Column(nullable = false, name = "name", unique = true)
75 @Column(nullable = false, name = "policy_model_type")
76 private String modelType;
79 @Column(name = "context")
80 private String context;
83 @Column(name = "device_type_scope")
84 private String deviceTypeScope;
87 @Column(name = "shared", nullable = false)
88 private Boolean shared;
90 @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
91 private String policyTosca;
93 @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
94 private Set<Loop> usedByLoops = new HashSet<>();
97 @Column(name = "dcae_deployment_id")
98 private String dcaeDeploymentId;
101 @Column(name = "dcae_deployment_status_url")
102 private String dcaeDeploymentStatusUrl;
105 @Column(name = "dcae_blueprint_id")
106 private String dcaeBlueprintId;
108 public MicroServicePolicy() {
113 * The constructor that create the json representation from the policyTosca
114 * using the ToscaYamlToJsonConvertor.
116 * @param name The name of the MicroService
117 * @param modelType The model type of the MicroService
118 * @param policyTosca The policy Tosca of the MicroService
119 * @param shared The flag indicate whether the MicroService is shared
120 * @param usedByLoops The list of loops that uses this MicroService
122 public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
123 Set<Loop> usedByLoops) {
125 this.modelType = modelType;
126 this.policyTosca = policyTosca;
127 this.shared = shared;
128 this.setJsonRepresentation(JsonUtils.GSON_JPA_MODEL
129 .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyTosca, modelType), JsonObject.class));
130 this.usedByLoops = usedByLoops;
133 private JsonObject createJsonFromPolicyTosca() {
134 Map<String, Object> map = new Yaml().load(this.getPolicyTosca());
135 JSONObject jsonObject = new JSONObject(map);
136 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
140 * The constructor that does not make use of ToscaYamlToJsonConvertor but take
141 * the jsonRepresentation instead.
143 * @param name The name of the MicroService
144 * @param modelType The model type of the MicroService
145 * @param policyTosca The policy Tosca of the MicroService
146 * @param shared The flag indicate whether the MicroService is
148 * @param jsonRepresentation The UI representation in json format
149 * @param usedByLoops The list of loops that uses this MicroService
151 public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
152 JsonObject jsonRepresentation, Set<Loop> usedByLoops) {
154 this.modelType = modelType;
155 this.policyTosca = policyTosca;
156 this.shared = shared;
157 this.usedByLoops = usedByLoops;
158 this.setJsonRepresentation(jsonRepresentation);
162 public String getName() {
169 * @param name the name to set
172 public void setName(String name) {
176 public String getModelType() {
180 void setModelType(String modelType) {
181 this.modelType = modelType;
184 public Boolean getShared() {
188 void setShared(Boolean shared) {
189 this.shared = shared;
192 public String getPolicyTosca() {
196 void setPolicyTosca(String policyTosca) {
197 this.policyTosca = policyTosca;
200 public Set<Loop> getUsedByLoops() {
204 void setUsedByLoops(Set<Loop> usedBy) {
205 this.usedByLoops = usedBy;
208 public String getContext() {
212 public void setContext(String context) {
213 this.context = context;
216 public String getDeviceTypeScope() {
217 return deviceTypeScope;
220 public void setDeviceTypeScope(String deviceTypeScope) {
221 this.deviceTypeScope = deviceTypeScope;
225 * dcaeDeploymentId getter.
227 * @return the dcaeDeploymentId
229 public String getDcaeDeploymentId() {
230 return dcaeDeploymentId;
234 * dcaeDeploymentId setter.
236 * @param dcaeDeploymentId the dcaeDeploymentId to set
238 public void setDcaeDeploymentId(String dcaeDeploymentId) {
239 this.dcaeDeploymentId = dcaeDeploymentId;
243 * dcaeDeploymentStatusUrl getter.
245 * @return the dcaeDeploymentStatusUrl
247 public String getDcaeDeploymentStatusUrl() {
248 return dcaeDeploymentStatusUrl;
252 * dcaeDeploymentStatusUrl setter.
254 * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
256 public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
257 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
261 * dcaeBlueprintId getter.
263 * @return the dcaeBlueprintId
265 public String getDcaeBlueprintId() {
266 return dcaeBlueprintId;
270 * dcaeBlueprintId setter.
272 * @param dcaeBlueprintId the dcaeBlueprintId to set
274 void setDcaeBlueprintId(String dcaeBlueprintId) {
275 this.dcaeBlueprintId = dcaeBlueprintId;
279 public int hashCode() {
280 final int prime = 31;
282 result = prime * result + ((name == null) ? 0 : name.hashCode());
287 public boolean equals(Object obj) {
294 if (getClass() != obj.getClass()) {
297 MicroServicePolicy other = (MicroServicePolicy) obj;
299 if (other.name != null) {
302 } else if (!name.equals(other.name)) {
308 private String getMicroServicePropertyNameFromTosca(JsonObject object) {
309 return object.getAsJsonObject("policy_types").getAsJsonObject(this.modelType).getAsJsonObject("properties")
310 .keySet().toArray(new String[1])[0];
314 public String createPolicyPayload() {
315 JsonObject toscaJson = createJsonFromPolicyTosca();
317 JsonObject policyPayloadResult = new JsonObject();
319 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
321 JsonObject topologyTemplateNode = new JsonObject();
322 policyPayloadResult.add("topology_template", topologyTemplateNode);
324 JsonArray policiesArray = new JsonArray();
325 topologyTemplateNode.add("policies", policiesArray);
327 JsonObject thisPolicy = new JsonObject();
328 policiesArray.add(thisPolicy);
330 JsonObject policyDetails = new JsonObject();
331 thisPolicy.add(this.getName(), policyDetails);
332 policyDetails.addProperty("type", this.getModelType());
333 policyDetails.addProperty("version", "1.0.0");
335 JsonObject policyMetadata = new JsonObject();
336 policyDetails.add("metadata", policyMetadata);
337 policyMetadata.addProperty("policy-id", this.getName());
339 JsonObject policyProperties = new JsonObject();
340 policyDetails.add("properties", policyProperties);
341 policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson());
342 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
343 logger.info("Micro service policy payload: " + policyPayload);
344 return policyPayload;