2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Copyright (C) 2019-2020,2022 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.serialization;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.yaml.snakeyaml.Yaml;
48 * Test serialization of monitoring policies.
50 * @author Liam Fallon (liam.fallon@est.tech)
51 * @author Chenfei Gao (cgao@research.att.com)
53 public class MonitoringPolicySerializationTest {
55 private static final String VERSION = "version";
57 private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0";
59 private static final String DEFINITION_VERSION = "tosca_definitions_version";
61 private static final String TOPOLOGY_TEMPLATE = "topology_template";
63 private static final String TCA_POLICY = "tca.policy";
65 private static final String PROPERTIES2 = "properties";
67 private static final String POLICY_ID = "policy-id";
69 private static final String POLICIES = "policies";
71 private static final String POLICY3 = "onap.vfirewall.tca";
73 private static final String POLICY2 = "onap.scaleout.tca";
75 private static final String POLICY1 = "onap.restart.tca";
77 private static final String TYPE1 = "onap.policies.monitoring.tcagen2";
79 private static final String METADATA = "metadata";
81 private static final String VERSION_100 = "1.0.0";
82 private static final String VERSION_200 = "2.0.0";
84 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
86 private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
87 private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
88 private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
89 private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
90 private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
91 private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
92 private static final String VFW_MON_INPUT_V2_JSON = "policies/vFirewall.policy.monitoring.input.tosca.v2.json";
93 private static final String VFW_MON_INPUT_V2_YAML = "policies/vFirewall.policy.monitoring.input.tosca.v2.yaml";
95 private final StandardCoder standardCoder = new StandardCoder();
96 private final YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
99 public void testDeserialization() throws Exception {
100 String policyTypeInputJson =
101 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
102 ToscaServiceTemplate plainPolicyTypes =
103 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
105 JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
106 policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
109 JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
110 JpaToscaServiceTemplate mergedServiceTemplate =
111 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
112 verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate);
113 JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
114 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
117 serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
118 mergedServiceTemplate =
119 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
120 verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate);
121 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
122 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
125 serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
126 mergedServiceTemplate =
127 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
128 verifyVfwMonitoringInputDeserialization(mergedServiceTemplate, VERSION_100);
129 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
130 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
132 testDeserializationMonitoringV2();
136 public void testSerialization() {
137 assertThatCode(() -> {
139 JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
140 String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
141 verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
144 serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
145 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
146 verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
149 serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
150 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
151 verifyVfwMonitoringOutputserialization(serializedServiceTemplate, VERSION_100);
154 serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_V2_JSON);
155 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
156 verifyVfwMonitoringOutputserialization(serializedServiceTemplate, VERSION_200);
157 }).as("No exception should be thrown").doesNotThrowAnyException();
160 private void testDeserializationMonitoringV2() throws Exception {
161 String policyTypeInputJson =
162 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.v2.yaml");
163 ToscaServiceTemplate plainPolicyTypes =
164 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
166 JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
167 policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
169 JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_V2_JSON);
170 JpaToscaServiceTemplate mergedServiceTemplate =
171 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
172 verifyVfwMonitoringInputDeserialization(mergedServiceTemplate, VERSION_200);
174 JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_V2_YAML);
175 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
178 private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
179 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
180 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
181 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
182 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
183 return jpaToscaServiceTemplate;
186 private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
188 Yaml yaml = new Yaml();
189 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
190 Object yamlObject = yaml.load(policyYaml);
191 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
192 ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
194 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
195 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
196 return jpaToscaServiceTemplate;
199 private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
200 return standardCoder.encode(serviceTemplate.toAuthorative());
203 private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
205 // Sanity check the entire structure
206 assertNotNull(serviceTemplate);
207 LOGGER.info(serviceTemplate.validate("").toString());
208 assertTrue(serviceTemplate.validate("").isValid());
210 // Check tosca_definitions_version
211 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
213 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
214 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
217 assertEquals(1, policiesConceptMap.size());
218 assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
219 assertEquals("onap.restart.tca:1.0.0",
220 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
222 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
225 assertEquals(2, policyVal.getMetadata().size());
226 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
227 assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
230 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
231 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
232 assertNotNull(policyVal.getProperties().values().iterator().next());
235 private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
237 // Sanity check the entire structure
238 assertNotNull(serviceTemplate);
239 LOGGER.info(serviceTemplate.validate("").toString());
240 assertTrue(serviceTemplate.validate("").isValid());
242 // Check tosca_definitions_version
243 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
245 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
246 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
249 assertEquals(1, policiesConceptMap.size());
250 assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
251 assertEquals("onap.scaleout.tca:1.0.0",
252 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
254 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
257 assertEquals(2, policyVal.getMetadata().size());
258 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
259 assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
262 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
263 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
264 assertNotNull(policyVal.getProperties().values().iterator().next());
267 private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate, String version) {
269 // Sanity check the entire structure
270 assertNotNull(serviceTemplate);
271 LOGGER.info(serviceTemplate.validate("").toString());
272 assertTrue(serviceTemplate.validate("").isValid());
274 // Check tosca_definitions_version
275 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
277 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
278 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
281 assertEquals(1, policiesConceptMap.size());
282 assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
283 assertEquals("onap.vfirewall.tca:" + version,
284 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
286 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
289 assertEquals(2, policyVal.getMetadata().size());
290 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
291 assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
294 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
295 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
296 assertNotNull(policyVal.getProperties().values().iterator().next());
299 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
301 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
302 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
303 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
304 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
305 assertEquals(1, policiesJsonArray.size());
306 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
307 assertNotNull(policy.get(POLICY1));
308 JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
309 assertEquals(TYPE1, policyVal.get("type").getAsString());
310 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
311 assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
312 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
313 assertNotNull(properties.get(TCA_POLICY));
316 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
318 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
319 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
320 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
321 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
322 assertEquals(1, policiesJsonArray.size());
323 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
324 assertNotNull(policy.get(POLICY2));
325 JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
326 assertEquals(TYPE1, policyVal.get("type").getAsString());
327 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
328 assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
329 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
330 assertNotNull(properties.get(TCA_POLICY));
333 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate, String version) {
335 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
336 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
337 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
338 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
339 assertEquals(1, policiesJsonArray.size());
340 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
341 assertNotNull(policy.get(POLICY3));
342 JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
343 assertEquals(TYPE1, policyVal.get("type").getAsString());
344 assertEquals(version, policyVal.get(VERSION).getAsString());
345 assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
346 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
347 assertNotNull(properties.get(TCA_POLICY));