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.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonObject;
30 import com.google.gson.annotations.Expose;
32 import java.io.Serializable;
33 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.ManyToMany;
42 import javax.persistence.Table;
44 import org.hibernate.annotations.Type;
45 import org.hibernate.annotations.TypeDef;
46 import org.hibernate.annotations.TypeDefs;
47 import org.json.JSONObject;
48 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
49 import org.onap.clamp.clds.util.JsonUtils;
50 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
51 import org.onap.clamp.loop.Loop;
52 import org.onap.clamp.policy.Policy;
53 import org.yaml.snakeyaml.Yaml;
56 @Table(name = "micro_service_policies")
57 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
58 public class MicroServicePolicy implements Serializable, Policy {
60 * The serial version ID.
62 private static final long serialVersionUID = 6271238288583332616L;
66 @Column(nullable = false, name = "name", unique = true)
70 @Column(nullable = false, name = "model_type")
71 private String modelType;
75 @Column(columnDefinition = "json", name = "properties")
76 private JsonObject properties;
79 @Column(name = "shared", nullable = false)
80 private Boolean shared;
82 @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
83 private String policyTosca;
87 @Column(columnDefinition = "json", name = "json_representation", nullable = false)
88 private JsonObject jsonRepresentation;
90 @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
91 private Set<Loop> usedByLoops = new HashSet<>();
93 public MicroServicePolicy() {
98 * The constructor that create the json representation from the policyTosca
99 * using the ToscaYamlToJsonConvertor.
102 * The name of the MicroService
104 * The model type of the MicroService
106 * The policy Tosca of the MicroService
108 * The flag indicate whether the MicroService is shared
110 * The list of loops that uses this MicroService
112 public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
113 Set<Loop> usedByLoops) {
115 this.modelType = modelType;
116 this.policyTosca = policyTosca;
117 this.shared = shared;
118 this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
119 .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
120 this.usedByLoops = usedByLoops;
123 private JsonObject createJsonFromPolicyTosca() {
124 Map<String, Object> map = new Yaml().load(this.getPolicyTosca());
125 JSONObject jsonObject = new JSONObject(map);
126 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
130 * The constructor that does not make use of ToscaYamlToJsonConvertor but take
131 * the jsonRepresentation instead.
134 * The name of the MicroService
136 * The model type of the MicroService
138 * The policy Tosca of the MicroService
140 * The flag indicate whether the MicroService is shared
141 * @param jsonRepresentation
142 * The UI representation in json format
144 * The list of loops that uses this MicroService
146 public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
147 JsonObject jsonRepresentation, Set<Loop> usedByLoops) {
149 this.modelType = modelType;
150 this.policyTosca = policyTosca;
151 this.shared = shared;
152 this.usedByLoops = usedByLoops;
153 this.jsonRepresentation = jsonRepresentation;
157 public String getName() {
161 public String getModelType() {
165 void setModelType(String modelType) {
166 this.modelType = modelType;
169 public JsonObject getProperties() {
173 public void setProperties(JsonObject properties) {
174 this.properties = properties;
177 public Boolean getShared() {
181 void setShared(Boolean shared) {
182 this.shared = shared;
185 public String getPolicyTosca() {
189 void setPolicyTosca(String policyTosca) {
190 this.policyTosca = policyTosca;
194 public JsonObject getJsonRepresentation() {
195 return jsonRepresentation;
198 void setJsonRepresentation(JsonObject jsonRepresentation) {
199 this.jsonRepresentation = jsonRepresentation;
202 public Set<Loop> getUsedByLoops() {
206 void setUsedByLoops(Set<Loop> usedBy) {
207 this.usedByLoops = usedBy;
211 public int hashCode() {
212 final int prime = 31;
214 result = prime * result + ((name == null) ? 0 : name.hashCode());
219 public boolean equals(Object obj) {
226 if (getClass() != obj.getClass()) {
229 MicroServicePolicy other = (MicroServicePolicy) obj;
231 if (other.name != null) {
234 } else if (!name.equals(other.name)) {
240 private String getMicroServicePropertyNameFromTosca(JsonObject object) {
241 return object.getAsJsonObject("policy_types").getAsJsonObject(this.modelType).getAsJsonObject("properties")
242 .keySet().toArray(new String[1])[0];
246 public String createPolicyPayload() {
247 JsonObject toscaJson = createJsonFromPolicyTosca();
249 JsonObject policyPayloadResult = new JsonObject();
251 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
253 JsonObject topologyTemplateNode = new JsonObject();
254 policyPayloadResult.add("topology_template", topologyTemplateNode);
256 JsonArray policiesArray = new JsonArray();
257 topologyTemplateNode.add("policies", policiesArray);
259 JsonObject thisPolicy = new JsonObject();
260 policiesArray.add(thisPolicy);
262 JsonObject policyDetails = new JsonObject();
263 thisPolicy.add(this.getName(), policyDetails);
264 policyDetails.addProperty("type", this.getModelType());
265 policyDetails.addProperty("version", "1.0.0");
267 JsonObject policyMetadata = new JsonObject();
268 policyDetails.add("metadata", policyMetadata);
269 policyMetadata.addProperty("policy-id", this.getName());
271 JsonObject policyProperties = new JsonObject();
272 policyDetails.add("properties", policyProperties);
273 policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getProperties());
274 return new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);