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;
33 import java.io.Serializable;
34 import java.util.HashSet;
37 import javax.persistence.Column;
38 import javax.persistence.Entity;
39 import javax.persistence.FetchType;
40 import javax.persistence.Id;
41 import javax.persistence.JoinColumn;
42 import javax.persistence.JoinColumns;
43 import javax.persistence.ManyToMany;
44 import javax.persistence.ManyToOne;
45 import javax.persistence.Table;
46 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.loop.template.PolicyModel;
55 import org.onap.clamp.policy.Policy;
56 import org.yaml.snakeyaml.Yaml;
59 @Table(name = "micro_service_policies")
60 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
61 public class MicroServicePolicy extends Policy implements Serializable {
63 * The serial version ID.
65 private static final long serialVersionUID = 6271238288583332616L;
68 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class);
72 @Column(nullable = false, name = "name", unique = true)
76 @Column(name = "context")
77 private String context;
80 @Column(name = "device_type_scope")
81 private String deviceTypeScope;
84 @Column(name = "shared", nullable = false)
85 private Boolean shared;
87 @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
88 private Set<Loop> usedByLoops = new HashSet<>();
91 @Column(name = "dcae_deployment_id")
92 private String dcaeDeploymentId;
95 @Column(name = "dcae_deployment_status_url")
96 private String dcaeDeploymentStatusUrl;
99 @Column(name = "dcae_blueprint_id")
100 private String dcaeBlueprintId;
103 @ManyToOne(fetch = FetchType.EAGER)
104 @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
105 @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
106 private PolicyModel policyModel;
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 policyModel The policy model of the MicroService
118 * @param shared The flag indicate whether the MicroService is shared
119 * @param usedByLoops The list of loops that uses this MicroService
121 public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared,
122 Set<Loop> usedByLoops) {
124 this.policyModel = policyModel;
125 this.shared = shared;
126 this.setJsonRepresentation(JsonUtils.GSON_JPA_MODEL
127 .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyModel.getPolicyModelTosca(),
128 policyModel.getPolicyModelType()), JsonObject.class));
129 this.usedByLoops = usedByLoops;
132 private JsonObject createJsonFromPolicyTosca() {
133 Map<String, Object> map = new Yaml().load(this.getPolicyModel().getPolicyModelTosca());
134 JSONObject jsonObject = new JSONObject(map);
135 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
139 * The constructor that does not make use of ToscaYamlToJsonConvertor but take
140 * the jsonRepresentation instead.
142 * @param name The name of the MicroService
143 * @param policyModel The policy model type of the MicroService
144 * @param shared The flag indicate whether the MicroService is
146 * @param jsonRepresentation The UI representation in json format
147 * @param usedByLoops The list of loops that uses this MicroService
149 public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared,
150 JsonObject jsonRepresentation, Set<Loop> usedByLoops) {
152 this.policyModel = policyModel;
153 this.shared = shared;
154 this.usedByLoops = usedByLoops;
155 this.setJsonRepresentation(jsonRepresentation);
159 public String getName() {
166 * @param name the name to set
169 public void setName(String name) {
173 public Boolean getShared() {
177 void setShared(Boolean shared) {
178 this.shared = shared;
181 public Set<Loop> getUsedByLoops() {
185 void setUsedByLoops(Set<Loop> usedBy) {
186 this.usedByLoops = usedBy;
189 public String getContext() {
193 public void setContext(String context) {
194 this.context = context;
197 public String getDeviceTypeScope() {
198 return deviceTypeScope;
201 public void setDeviceTypeScope(String deviceTypeScope) {
202 this.deviceTypeScope = deviceTypeScope;
205 public PolicyModel getPolicyModel() {
209 public void setPolicyModel(PolicyModel policyModel) {
210 this.policyModel = policyModel;
214 * dcaeDeploymentId getter.
216 * @return the dcaeDeploymentId
218 public String getDcaeDeploymentId() {
219 return dcaeDeploymentId;
223 * dcaeDeploymentId setter.
225 * @param dcaeDeploymentId the dcaeDeploymentId to set
227 public void setDcaeDeploymentId(String dcaeDeploymentId) {
228 this.dcaeDeploymentId = dcaeDeploymentId;
232 * dcaeDeploymentStatusUrl getter.
234 * @return the dcaeDeploymentStatusUrl
236 public String getDcaeDeploymentStatusUrl() {
237 return dcaeDeploymentStatusUrl;
241 * dcaeDeploymentStatusUrl setter.
243 * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
245 public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
246 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
250 * dcaeBlueprintId getter.
252 * @return the dcaeBlueprintId
254 public String getDcaeBlueprintId() {
255 return dcaeBlueprintId;
259 * dcaeBlueprintId setter.
261 * @param dcaeBlueprintId the dcaeBlueprintId to set
263 void setDcaeBlueprintId(String dcaeBlueprintId) {
264 this.dcaeBlueprintId = dcaeBlueprintId;
268 public int hashCode() {
269 final int prime = 31;
271 result = prime * result + ((name == null) ? 0 : name.hashCode());
276 public boolean equals(Object obj) {
283 if (getClass() != obj.getClass()) {
286 MicroServicePolicy other = (MicroServicePolicy) obj;
288 if (other.name != null) {
291 } else if (!name.equals(other.name)) {
297 private String getMicroServicePropertyNameFromTosca(JsonObject object) {
298 return object.getAsJsonObject("policy_types").getAsJsonObject(this.getPolicyModel().getPolicyModelType())
301 .keySet().toArray(new String[1])[0];
305 public String createPolicyPayload() {
306 JsonObject toscaJson = createJsonFromPolicyTosca();
308 JsonObject policyPayloadResult = new JsonObject();
310 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
312 JsonObject topologyTemplateNode = new JsonObject();
313 policyPayloadResult.add("topology_template", topologyTemplateNode);
315 JsonArray policiesArray = new JsonArray();
316 topologyTemplateNode.add("policies", policiesArray);
318 JsonObject thisPolicy = new JsonObject();
319 policiesArray.add(thisPolicy);
321 JsonObject policyDetails = new JsonObject();
322 thisPolicy.add(this.getName(), policyDetails);
323 policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
324 policyDetails.addProperty("version", this.getPolicyModel().getVersion());
326 JsonObject policyMetadata = new JsonObject();
327 policyDetails.add("metadata", policyMetadata);
328 policyMetadata.addProperty("policy-id", this.getName());
330 JsonObject policyProperties = new JsonObject();
331 policyDetails.add("properties", policyProperties);
332 policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson());
333 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
334 logger.info("Micro service policy payload: " + policyPayload);
335 return policyPayload;