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.LoopElementModel;
55 import org.onap.clamp.loop.template.PolicyModel;
56 import org.onap.clamp.policy.Policy;
57 import org.yaml.snakeyaml.Yaml;
60 @Table(name = "micro_service_policies")
61 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
62 public class MicroServicePolicy extends Policy implements Serializable {
64 * The serial version ID.
66 private static final long serialVersionUID = 6271238288583332616L;
69 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class);
73 @Column(nullable = false, name = "name", unique = true)
77 @Column(name = "context")
78 private String context;
81 @Column(name = "device_type_scope")
82 private String deviceTypeScope;
85 @Column(name = "shared", nullable = false)
86 private Boolean shared;
88 @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
89 private Set<Loop> usedByLoops = new HashSet<>();
92 @Column(name = "dcae_deployment_id")
93 private String dcaeDeploymentId;
96 @Column(name = "dcae_deployment_status_url")
97 private String dcaeDeploymentStatusUrl;
100 @Column(name = "dcae_blueprint_id")
101 private String dcaeBlueprintId;
104 @ManyToOne(fetch = FetchType.EAGER)
105 @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
106 @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
107 private PolicyModel policyModel;
109 public MicroServicePolicy() {
114 * The constructor that creates the json representation from the policyTosca
115 * using the ToscaYamlToJsonConvertor.
117 * @param name The name of the MicroService
118 * @param policyModel The policy model of the MicroService
119 * @param shared The flag indicate whether the MicroService is shared
121 public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, LoopElementModel loopElementModel) {
122 this(name,policyModel,shared,JsonUtils.GSON_JPA_MODEL
123 .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyModel.getPolicyModelTosca(),
124 policyModel.getPolicyModelType()), JsonObject.class),loopElementModel);
127 private JsonObject createJsonFromPolicyTosca() {
128 Map<String, Object> map = new Yaml().load(this.getPolicyModel().getPolicyModelTosca());
129 JSONObject jsonObject = new JSONObject(map);
130 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
134 * The constructor that does not make use of ToscaYamlToJsonConvertor but take
135 * the jsonRepresentation instead.
136 * @param name The name of the MicroService
137 * @param policyModel The policy model type of the MicroService
138 * @param shared The flag indicate whether the MicroService is
140 * @param jsonRepresentation The UI representation in json format
141 * @param loopElementModel The loop element model from which this instance should be created
143 public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared,
144 JsonObject jsonRepresentation, LoopElementModel loopElementModel) {
146 this.policyModel = policyModel;
147 this.shared = shared;
148 this.setJsonRepresentation(jsonRepresentation);
149 this.setLoopElementModel(loopElementModel);
153 public String getName() {
160 * @param name the name to set
163 public void setName(String name) {
167 public Boolean getShared() {
171 void setShared(Boolean shared) {
172 this.shared = shared;
175 public Set<Loop> getUsedByLoops() {
179 void setUsedByLoops(Set<Loop> usedBy) {
180 this.usedByLoops = usedBy;
183 public String getContext() {
187 public void setContext(String context) {
188 this.context = context;
191 public String getDeviceTypeScope() {
192 return deviceTypeScope;
195 public void setDeviceTypeScope(String deviceTypeScope) {
196 this.deviceTypeScope = deviceTypeScope;
199 public PolicyModel getPolicyModel() {
203 public void setPolicyModel(PolicyModel policyModel) {
204 this.policyModel = policyModel;
208 * dcaeDeploymentId getter.
210 * @return the dcaeDeploymentId
212 public String getDcaeDeploymentId() {
213 return dcaeDeploymentId;
217 * dcaeDeploymentId setter.
219 * @param dcaeDeploymentId the dcaeDeploymentId to set
221 public void setDcaeDeploymentId(String dcaeDeploymentId) {
222 this.dcaeDeploymentId = dcaeDeploymentId;
226 * dcaeDeploymentStatusUrl getter.
228 * @return the dcaeDeploymentStatusUrl
230 public String getDcaeDeploymentStatusUrl() {
231 return dcaeDeploymentStatusUrl;
235 * dcaeDeploymentStatusUrl setter.
237 * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
239 public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
240 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
244 * dcaeBlueprintId getter.
246 * @return the dcaeBlueprintId
248 public String getDcaeBlueprintId() {
249 return dcaeBlueprintId;
253 * dcaeBlueprintId setter.
255 * @param dcaeBlueprintId the dcaeBlueprintId to set
257 void setDcaeBlueprintId(String dcaeBlueprintId) {
258 this.dcaeBlueprintId = dcaeBlueprintId;
262 public int hashCode() {
263 final int prime = 31;
265 result = prime * result + ((name == null) ? 0 : name.hashCode());
270 public boolean equals(Object obj) {
277 if (getClass() != obj.getClass()) {
280 MicroServicePolicy other = (MicroServicePolicy) obj;
282 if (other.name != null) {
285 } else if (!name.equals(other.name)) {
291 private String getMicroServicePropertyNameFromTosca(JsonObject object) {
292 return object.getAsJsonObject("policy_types").getAsJsonObject(this.getPolicyModel().getPolicyModelType())
295 .keySet().toArray(new String[1])[0];
299 public String createPolicyPayload() {
300 JsonObject toscaJson = createJsonFromPolicyTosca();
302 JsonObject policyPayloadResult = new JsonObject();
304 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
306 JsonObject topologyTemplateNode = new JsonObject();
307 policyPayloadResult.add("topology_template", topologyTemplateNode);
309 JsonArray policiesArray = new JsonArray();
310 topologyTemplateNode.add("policies", policiesArray);
312 JsonObject thisPolicy = new JsonObject();
313 policiesArray.add(thisPolicy);
315 JsonObject policyDetails = new JsonObject();
316 thisPolicy.add(this.getName(), policyDetails);
317 policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
318 policyDetails.addProperty("version", this.getPolicyModel().getVersion());
320 JsonObject policyMetadata = new JsonObject();
321 policyDetails.add("metadata", policyMetadata);
322 policyMetadata.addProperty("policy-id", this.getName());
324 JsonObject policyProperties = new JsonObject();
325 policyDetails.add("properties", policyProperties);
326 policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson());
327 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
328 logger.info("Micro service policy payload: " + policyPayload);
329 return policyPayload;