2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Copyright (C) 2019-2020 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";
83 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
85 private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
86 private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
87 private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
88 private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
89 private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
90 private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
92 private StandardCoder standardCoder = new StandardCoder();
93 private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
96 public void testDeserialization() throws Exception {
97 String policyTypeInputJson =
98 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
99 ToscaServiceTemplate plainPolicyTypes =
100 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
102 JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
103 policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
106 JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
107 JpaToscaServiceTemplate mergedServiceTemplate =
108 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
109 verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate);
110 JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
111 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
114 serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
115 mergedServiceTemplate =
116 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
117 verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate);
118 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
119 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
122 serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
123 mergedServiceTemplate =
124 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
125 verifyVfwMonitoringInputDeserialization(mergedServiceTemplate);
126 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
127 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
131 public void testSerialization() {
132 assertThatCode(() -> {
134 JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
135 String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
136 verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
139 serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
140 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
141 verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
144 serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
145 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
146 verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
148 }).as("No exception should be thrown").doesNotThrowAnyException();
151 private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
153 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
154 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
155 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
156 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
157 return jpaToscaServiceTemplate;
160 private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
162 Yaml yaml = new Yaml();
163 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
164 Object yamlObject = yaml.load(policyYaml);
165 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
166 ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
168 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
169 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
170 return jpaToscaServiceTemplate;
173 private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
174 return standardCoder.encode(serviceTemplate.toAuthorative());
177 private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
179 // Sanity check the entire structure
180 assertNotNull(serviceTemplate);
181 LOGGER.info(serviceTemplate.validate("").toString());
182 assertTrue(serviceTemplate.validate("").isValid());
184 // Check tosca_definitions_version
185 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
187 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
188 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
191 assertEquals(1, policiesConceptMap.size());
192 assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
193 assertEquals("onap.restart.tca:1.0.0",
194 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
196 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
199 assertEquals(2, policyVal.getMetadata().size());
200 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
201 assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
204 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
205 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
206 assertNotNull(policyVal.getProperties().values().iterator().next());
209 private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
211 // Sanity check the entire structure
212 assertNotNull(serviceTemplate);
213 LOGGER.info(serviceTemplate.validate("").toString());
214 assertTrue(serviceTemplate.validate("").isValid());
216 // Check tosca_definitions_version
217 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
219 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
220 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
223 assertEquals(1, policiesConceptMap.size());
224 assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
225 assertEquals("onap.scaleout.tca:1.0.0",
226 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
228 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
231 assertEquals(2, policyVal.getMetadata().size());
232 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
233 assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
236 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
237 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
238 assertNotNull(policyVal.getProperties().values().iterator().next());
241 private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
243 // Sanity check the entire structure
244 assertNotNull(serviceTemplate);
245 LOGGER.info(serviceTemplate.validate("").toString());
246 assertTrue(serviceTemplate.validate("").isValid());
248 // Check tosca_definitions_version
249 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
251 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
252 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
255 assertEquals(1, policiesConceptMap.size());
256 assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
257 assertEquals("onap.vfirewall.tca:1.0.0",
258 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
260 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
263 assertEquals(2, policyVal.getMetadata().size());
264 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
265 assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
268 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
269 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
270 assertNotNull(policyVal.getProperties().values().iterator().next());
273 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
275 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
276 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
277 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
278 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
279 assertEquals(1, policiesJsonArray.size());
280 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
281 assertNotNull(policy.get(POLICY1));
282 JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
283 assertEquals(TYPE1, policyVal.get("type").getAsString());
284 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
285 assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
286 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
287 assertNotNull(properties.get(TCA_POLICY));
290 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
292 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
293 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
294 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
295 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
296 assertEquals(1, policiesJsonArray.size());
297 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
298 assertNotNull(policy.get(POLICY2));
299 JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
300 assertEquals(TYPE1, policyVal.get("type").getAsString());
301 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
302 assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
303 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
304 assertNotNull(properties.get(TCA_POLICY));
307 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
309 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
310 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
311 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
312 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
313 assertEquals(1, policiesJsonArray.size());
314 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
315 assertNotNull(policy.get(POLICY3));
316 JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
317 assertEquals(TYPE1, policyVal.get("type").getAsString());
318 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
319 assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
320 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
321 assertNotNull(properties.get(TCA_POLICY));