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;
104 public MicroServicePolicy() {
109 * The constructor that create the json representation from the policyTosca
110 * using the ToscaYamlToJsonConvertor.
112 * @param name The name of the MicroService
113 * @param modelType The model type of the MicroService
114 * @param policyTosca The policy Tosca of the MicroService
115 * @param shared The flag indicate whether the MicroService is shared
116 * @param usedByLoops The list of loops that uses this MicroService
118 public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
119 Set<Loop> usedByLoops) {
121 this.modelType = modelType;
122 this.policyTosca = policyTosca;
123 this.shared = shared;
124 this.setJsonRepresentation(JsonUtils.GSON_JPA_MODEL
125 .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyTosca, modelType), JsonObject.class));
126 this.usedByLoops = usedByLoops;
129 private JsonObject createJsonFromPolicyTosca() {
130 Map<String, Object> map = new Yaml().load(this.getPolicyTosca());
131 JSONObject jsonObject = new JSONObject(map);
132 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
136 * The constructor that does not make use of ToscaYamlToJsonConvertor but take
137 * the jsonRepresentation instead.
139 * @param name The name of the MicroService
140 * @param modelType The model type of the MicroService
141 * @param policyTosca The policy Tosca of the MicroService
142 * @param shared The flag indicate whether the MicroService is
144 * @param jsonRepresentation The UI representation in json format
145 * @param usedByLoops The list of loops that uses this MicroService
147 public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
148 JsonObject jsonRepresentation, Set<Loop> usedByLoops) {
150 this.modelType = modelType;
151 this.policyTosca = policyTosca;
152 this.shared = shared;
153 this.usedByLoops = usedByLoops;
154 this.setJsonRepresentation(jsonRepresentation);
158 public String getName() {
165 * @param name the name to set
168 public void setName(String name) {
172 public String getModelType() {
176 void setModelType(String modelType) {
177 this.modelType = modelType;
180 public Boolean getShared() {
184 void setShared(Boolean shared) {
185 this.shared = shared;
188 public String getPolicyTosca() {
192 void setPolicyTosca(String policyTosca) {
193 this.policyTosca = policyTosca;
196 public Set<Loop> getUsedByLoops() {
200 void setUsedByLoops(Set<Loop> usedBy) {
201 this.usedByLoops = usedBy;
204 public String getContext() {
208 public void setContext(String context) {
209 this.context = context;
212 public String getDeviceTypeScope() {
213 return deviceTypeScope;
216 public void setDeviceTypeScope(String deviceTypeScope) {
217 this.deviceTypeScope = deviceTypeScope;
221 * dcaeDeploymentId getter.
223 * @return the dcaeDeploymentId
225 public String getDcaeDeploymentId() {
226 return dcaeDeploymentId;
230 * dcaeDeploymentId setter.
232 * @param dcaeDeploymentId the dcaeDeploymentId to set
234 public void setDcaeDeploymentId(String dcaeDeploymentId) {
235 this.dcaeDeploymentId = dcaeDeploymentId;
239 * dcaeDeploymentStatusUrl getter.
241 * @return the dcaeDeploymentStatusUrl
243 public String getDcaeDeploymentStatusUrl() {
244 return dcaeDeploymentStatusUrl;
248 * dcaeDeploymentStatusUrl setter.
250 * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
252 public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
253 this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
257 public int hashCode() {
258 final int prime = 31;
260 result = prime * result + ((name == null) ? 0 : name.hashCode());
265 public boolean equals(Object obj) {
272 if (getClass() != obj.getClass()) {
275 MicroServicePolicy other = (MicroServicePolicy) obj;
277 if (other.name != null) {
280 } else if (!name.equals(other.name)) {
286 private String getMicroServicePropertyNameFromTosca(JsonObject object) {
287 return object.getAsJsonObject("policy_types").getAsJsonObject(this.modelType).getAsJsonObject("properties")
288 .keySet().toArray(new String[1])[0];
292 public String createPolicyPayload() {
293 JsonObject toscaJson = createJsonFromPolicyTosca();
295 JsonObject policyPayloadResult = new JsonObject();
297 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
299 JsonObject topologyTemplateNode = new JsonObject();
300 policyPayloadResult.add("topology_template", topologyTemplateNode);
302 JsonArray policiesArray = new JsonArray();
303 topologyTemplateNode.add("policies", policiesArray);
305 JsonObject thisPolicy = new JsonObject();
306 policiesArray.add(thisPolicy);
308 JsonObject policyDetails = new JsonObject();
309 thisPolicy.add(this.getName(), policyDetails);
310 policyDetails.addProperty("type", this.getModelType());
311 policyDetails.addProperty("version", "1.0.0");
313 JsonObject policyMetadata = new JsonObject();
314 policyDetails.add("metadata", policyMetadata);
315 policyMetadata.addProperty("policy-id", this.getName());
317 JsonObject policyProperties = new JsonObject();
318 policyDetails.add("properties", policyProperties);
319 policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson());
320 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
321 logger.info("Micro service policy payload: " + policyPayload);
322 return policyPayload;