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, null, null);
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
142 * @param pdpGroup The Pdp Group info
143 * @param pdpSubgroup The Pdp Subgrouop info
145 public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared,
146 JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) {
148 this.policyModel = policyModel;
149 this.shared = shared;
150 this.setJsonRepresentation(jsonRepresentation);
151 this.setLoopElementModel(loopElementModel);
152 this.setPdpGroup(pdpGroup);
153 this.setPdpSubgroup(pdpSubgroup);
157 public String getName() {
164 * @param name the name to set
167 public void setName(String name) {
171 public Boolean getShared() {
175 void setShared(Boolean shared) {
176 this.shared = shared;
179 public Set<Loop> getUsedByLoops() {
183 void setUsedByLoops(Set<Loop> usedBy) {
184 this.usedByLoops = usedBy;
187 public String getContext() {
191 public void setContext(String context) {
192 this.context = context;
195 public String getDeviceTypeScope() {
196 return deviceTypeScope;
199 public void setDeviceTypeScope(String deviceTypeScope) {
200 this.deviceTypeScope = deviceTypeScope;
203 public PolicyModel getPolicyModel() {
207 public void setPolicyModel(PolicyModel policyModel) {
208 this.policyModel = policyModel;
212 * dcaeDeploymentId getter.
214 * @return the dcaeDeploymentId
216 public String getDcaeDeploymentId() {
217 return dcaeDeploymentId;
221 * dcaeDeploymentId setter.
223 * @param dcaeDeploymentId the dcaeDeploymentId to set
225 public void setDcaeDeploymentId(String dcaeDeploymentId) {
226 this.dcaeDeploymentId = dcaeDeploymentId;
230 * dcaeDeploymentStatusUrl getter.
232 * @return the dcaeDeploymentStatusUrl
234 public String getDcaeDeploymentStatusUrl() {
235 return dcaeDeploymentStatusUrl;
239 * dcaeDeploymentStatusUrl setter.
241 * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
243 public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
244 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
248 * dcaeBlueprintId getter.
250 * @return the dcaeBlueprintId
252 public String getDcaeBlueprintId() {
253 return dcaeBlueprintId;
257 * dcaeBlueprintId setter.
259 * @param dcaeBlueprintId the dcaeBlueprintId to set
261 void setDcaeBlueprintId(String dcaeBlueprintId) {
262 this.dcaeBlueprintId = dcaeBlueprintId;
266 public int hashCode() {
267 final int prime = 31;
269 result = prime * result + ((name == null) ? 0 : name.hashCode());
274 public boolean equals(Object obj) {
281 if (getClass() != obj.getClass()) {
284 MicroServicePolicy other = (MicroServicePolicy) obj;
286 if (other.name != null) {
289 } else if (!name.equals(other.name)) {
294 private String getMicroServicePropertyNameFromTosca(JsonObject object) {
295 return object.getAsJsonObject("policy_types").getAsJsonObject(this.getPolicyModel().getPolicyModelType())
298 .keySet().toArray(new String[1])[0];
302 public String createPolicyPayload() {
303 JsonObject toscaJson = createJsonFromPolicyTosca();
305 JsonObject policyPayloadResult = new JsonObject();
307 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
309 JsonObject topologyTemplateNode = new JsonObject();
310 policyPayloadResult.add("topology_template", topologyTemplateNode);
312 JsonArray policiesArray = new JsonArray();
313 topologyTemplateNode.add("policies", policiesArray);
315 JsonObject thisPolicy = new JsonObject();
316 policiesArray.add(thisPolicy);
318 JsonObject policyDetails = new JsonObject();
319 thisPolicy.add(this.getName(), policyDetails);
320 policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
321 policyDetails.addProperty("version", this.getPolicyModel().getVersion());
323 JsonObject policyMetadata = new JsonObject();
324 policyDetails.add("metadata", policyMetadata);
325 policyMetadata.addProperty("policy-id", this.getName());
327 JsonObject policyProperties = new JsonObject();
328 policyDetails.add("properties", policyProperties);
329 policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson());
330 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
331 logger.info("Micro service policy payload: " + policyPayload);
332 return policyPayload;