2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END============================================
21 * ===================================================================
25 package org.onap.clamp.policy.operational;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34 import com.google.gson.annotations.Expose;
35 import java.io.IOException;
36 import java.io.Serializable;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URLEncoder;
39 import java.nio.charset.StandardCharsets;
40 import java.util.HashMap;
42 import javax.persistence.Column;
43 import javax.persistence.Entity;
44 import javax.persistence.FetchType;
45 import javax.persistence.Id;
46 import javax.persistence.JoinColumn;
47 import javax.persistence.ManyToOne;
48 import javax.persistence.Table;
49 import javax.persistence.Transient;
50 import org.apache.commons.lang3.RandomStringUtils;
51 import org.hibernate.annotations.TypeDef;
52 import org.hibernate.annotations.TypeDefs;
53 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
54 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
55 import org.onap.clamp.loop.Loop;
56 import org.onap.clamp.loop.service.Service;
57 import org.onap.clamp.loop.template.LoopElementModel;
58 import org.onap.clamp.loop.template.PolicyModel;
59 import org.onap.clamp.policy.Policy;
60 import org.yaml.snakeyaml.DumperOptions;
61 import org.yaml.snakeyaml.Yaml;
64 @Table(name = "operational_policies")
65 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
66 public class OperationalPolicy extends Policy implements Serializable {
68 * The serial version ID.
70 private static final long serialVersionUID = 6117076450841538255L;
73 private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicy.class);
77 @Column(nullable = false, name = "name", unique = true)
80 @ManyToOne(fetch = FetchType.LAZY)
81 @JoinColumn(name = "loop_id", nullable = false)
85 * Constructor for serialization.
87 public OperationalPolicy() {
93 * @param name The name of the operational policy
94 * @param configurationsJson The operational policy property in the format of
96 * @param jsonRepresentation The jsonObject defining the json schema
97 * @param policyModel The policy model associated if any, can be null
98 * @param loopElementModel The loop element from which this instance is supposed to be created
99 * @param pdpGroup The Pdp Group info
100 * @param pdpSubgroup The Pdp Subgroup info
102 public OperationalPolicy(String name, JsonObject configurationsJson,
103 JsonObject jsonRepresentation, PolicyModel policyModel,
104 LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) {
106 this.setPolicyModel(policyModel);
107 this.setConfigurationsJson(configurationsJson);
108 this.setPdpGroup(pdpGroup);
109 this.setPdpSubgroup(pdpSubgroup);
110 this.setLoopElementModel(loopElementModel);
111 this.setJsonRepresentation(jsonRepresentation);
116 * Create an operational policy from a loop element model.
118 * @param loop The parent loop
119 * @param service The loop service
120 * @param loopElementModel The loop element model
121 * @param toscaConverter The tosca converter that must be used to create the Json representation
123 public OperationalPolicy(Loop loop, Service service, LoopElementModel loopElementModel,
124 ToscaConverterWithDictionarySupport toscaConverter) {
125 this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(),
126 loopElementModel.getPolicyModels().first().getPolicyAcronym() + '_'
127 + loopElementModel.getPolicyModels().first().getVersion(),
128 RandomStringUtils.randomAlphanumeric(3)), new JsonObject(),
129 new JsonObject(), loopElementModel.getPolicyModels().first(), loopElementModel, null, null);
131 this.updateJsonRepresentation(toscaConverter, service);
135 * Create an operational policy from a policy model.
137 * @param loop The parent loop
138 * @param service The loop service
139 * @param policyModel The policy model
140 * @param toscaConverter The tosca converter that must be used to create the Json representation
141 * @throws IOException In case of issues with the legacy files (generated from resource files
143 public OperationalPolicy(Loop loop, Service service, PolicyModel policyModel,
144 ToscaConverterWithDictionarySupport toscaConverter) throws IOException {
145 this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(),
146 policyModel.getPolicyAcronym() + '_' + policyModel.getVersion(),
147 RandomStringUtils.randomAlphanumeric(3)),
149 new JsonObject(), policyModel, null, null, null);
151 this.updateJsonRepresentation(toscaConverter, service);
154 public void setLoop(Loop loopName) {
155 this.loop = loopName;
158 public Loop getLoop() {
163 public String getName() {
170 * @param name the name to set
173 public void setName(String name) {
178 public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel) {
180 this.setJsonRepresentation(new JsonObject());
181 if (this.getPolicyModel() == null) {
184 if (this.isLegacy()) {
185 // Op policy Legacy case
186 LegacyOperationalPolicy.preloadConfiguration(this.getConfigurationsJson(), this.loop);
187 this.setJsonRepresentation(OperationalPolicyRepresentationBuilder
188 .generateOperationalPolicySchema(this.loop.getModelService()));
192 this.setJsonRepresentation(toscaConverter.convertToscaToJsonSchemaObject(
193 this.getPolicyModel().getPolicyModelTosca(),
194 this.getPolicyModel().getPolicyModelType(), serviceModel));
200 public int hashCode() {
201 final int prime = 31;
203 result = prime * result + ((name == null) ? 0 : name.hashCode());
208 public boolean equals(Object obj) {
215 if (getClass() != obj.getClass()) {
218 OperationalPolicy other = (OperationalPolicy) obj;
220 if (other.name != null) {
224 else if (!name.equals(other.name)) {
230 public Boolean isLegacy() {
231 return (this.getPolicyModel() != null) && this.getPolicyModel().getPolicyModelType().contains("legacy");
235 * Create policy Yaml from json defined here.
237 * @return A string containing Yaml
239 public String createPolicyPayloadYaml() {
240 JsonObject policyPayloadResult = new JsonObject();
242 policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
244 JsonObject topologyTemplateNode = new JsonObject();
245 policyPayloadResult.add("topology_template", topologyTemplateNode);
247 JsonArray policiesArray = new JsonArray();
248 topologyTemplateNode.add("policies", policiesArray);
250 JsonObject operationalPolicy = new JsonObject();
251 policiesArray.add(operationalPolicy);
253 JsonObject operationalPolicyDetails = new JsonObject();
254 operationalPolicy.add(this.name, operationalPolicyDetails);
255 operationalPolicyDetails.addProperty("type", "onap.policies.controlloop.Operational");
256 operationalPolicyDetails.addProperty("version", "1.0.0");
258 JsonObject metadata = new JsonObject();
259 operationalPolicyDetails.add("metadata", metadata);
260 metadata.addProperty("policy-id", this.name);
262 operationalPolicyDetails.add("properties", LegacyOperationalPolicy
263 .reworkActorAttributes(this.getConfigurationsJson().get("operational_policy").deepCopy()));
265 DumperOptions options = new DumperOptions();
266 options.setIndent(2);
267 options.setPrettyFlow(true);
268 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
269 Gson gson = new GsonBuilder().create();
271 return (new Yaml(options)).dump(gson.fromJson(gson.toJson(policyPayloadResult), Map.class));
275 public String createPolicyPayload() throws UnsupportedEncodingException {
277 // Now using the legacy payload fo Dublin
278 JsonObject payload = new JsonObject();
279 payload.addProperty("policy-id", this.getName());
280 payload.addProperty("content",
282 LegacyOperationalPolicy
283 .createPolicyPayloadYamlLegacy(
284 this.getConfigurationsJson().get("operational_policy")),
285 StandardCharsets.UTF_8.toString()));
286 String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload);
287 logger.info("Operational policy payload: " + opPayload);
291 return super.createPolicyPayload();
296 * Return a map containing all Guard policies indexed by Guard policy Name.
298 * @return The Guards map
300 public Map<String, String> createGuardPolicyPayloads() {
301 Map<String, String> result = new HashMap<>();
303 if (this.getConfigurationsJson() != null) {
304 JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
305 if (guardsList != null) {
306 for (JsonElement guardElem : guardsList.getAsJsonArray()) {
307 result.put(guardElem.getAsJsonObject().get("policy-id").getAsString(),
308 new GsonBuilder().create().toJson(guardElem));
312 logger.info("Guard policy payload: " + result);