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.base.PfValidationResult;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
43 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
49 * Test serialization of monitoring policies.
51 * @author Liam Fallon (liam.fallon@est.tech)
52 * @author Chenfei Gao (cgao@research.att.com)
54 public class MonitoringPolicySerializationTest {
56 private static final String VERSION = "version";
58 private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0";
60 private static final String DEFINITION_VERSION = "tosca_definitions_version";
62 private static final String TOPOLOGY_TEMPLATE = "topology_template";
64 private static final String TCA_POLICY = "tca.policy";
66 private static final String PROPERTIES2 = "properties";
68 private static final String POLICY_ID = "policy-id";
70 private static final String POLICIES = "policies";
72 private static final String POLICY3 = "onap.vfirewall.tca";
74 private static final String POLICY2 = "onap.scaleout.tca";
76 private static final String POLICY1 = "onap.restart.tca";
78 private static final String TYPE1 = "onap.policies.monitoring.tcagen2";
80 private static final String METADATA = "metadata";
82 private static final String VERSION_100 = "1.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";
93 private StandardCoder standardCoder = new StandardCoder();
94 private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
97 public void testDeserialization() throws Exception {
98 String policyTypeInputJson =
99 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
100 ToscaServiceTemplate plainPolicyTypes =
101 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
103 JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
104 policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
107 JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
108 JpaToscaServiceTemplate mergedServiceTemplate =
109 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
110 verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate);
111 JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
112 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
115 serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
116 mergedServiceTemplate =
117 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
118 verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate);
119 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
120 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
123 serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
124 mergedServiceTemplate =
125 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
126 verifyVfwMonitoringInputDeserialization(mergedServiceTemplate);
127 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
128 assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
132 public void testSerialization() {
133 assertThatCode(() -> {
135 JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
136 String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
137 verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
140 serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
141 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
142 verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
145 serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
146 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
147 verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
149 }).as("No exception should be thrown").doesNotThrowAnyException();
152 private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
154 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
155 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
156 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
157 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
158 return jpaToscaServiceTemplate;
161 private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
163 Yaml yaml = new Yaml();
164 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
165 Object yamlObject = yaml.load(policyYaml);
166 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
167 ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
169 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
170 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
171 return jpaToscaServiceTemplate;
174 private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
175 return standardCoder.encode(serviceTemplate.toAuthorative());
178 private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
180 // Sanity check the entire structure
181 assertNotNull(serviceTemplate);
182 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
183 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
185 // Check tosca_definitions_version
186 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
188 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
189 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
192 assertEquals(1, policiesConceptMap.size());
193 assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
194 assertEquals("onap.restart.tca:1.0.0",
195 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
197 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
200 assertEquals(2, policyVal.getMetadata().size());
201 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
202 assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
205 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
206 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
207 assertNotNull(policyVal.getProperties().values().iterator().next());
210 private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
212 // Sanity check the entire structure
213 assertNotNull(serviceTemplate);
214 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
215 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
217 // Check tosca_definitions_version
218 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
220 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
221 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
224 assertEquals(1, policiesConceptMap.size());
225 assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
226 assertEquals("onap.scaleout.tca:1.0.0",
227 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
229 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
232 assertEquals(2, policyVal.getMetadata().size());
233 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
234 assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
237 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
238 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
239 assertNotNull(policyVal.getProperties().values().iterator().next());
242 private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
244 // Sanity check the entire structure
245 assertNotNull(serviceTemplate);
246 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
247 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
249 // Check tosca_definitions_version
250 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
252 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
253 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
256 assertEquals(1, policiesConceptMap.size());
257 assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
258 assertEquals("onap.vfirewall.tca:1.0.0",
259 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
261 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
264 assertEquals(2, policyVal.getMetadata().size());
265 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
266 assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
269 assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
270 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
271 assertNotNull(policyVal.getProperties().values().iterator().next());
274 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
276 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
277 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
278 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
279 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
280 assertEquals(1, policiesJsonArray.size());
281 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
282 assertNotNull(policy.get(POLICY1));
283 JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
284 assertEquals(TYPE1, policyVal.get("type").getAsString());
285 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
286 assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
287 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
288 assertNotNull(properties.get(TCA_POLICY));
291 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
293 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
294 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
295 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
296 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
297 assertEquals(1, policiesJsonArray.size());
298 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
299 assertNotNull(policy.get(POLICY2));
300 JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
301 assertEquals(TYPE1, policyVal.get("type").getAsString());
302 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
303 assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
304 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
305 assertNotNull(properties.get(TCA_POLICY));
308 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
310 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
311 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
312 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
313 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
314 assertEquals(1, policiesJsonArray.size());
315 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
316 assertNotNull(policy.get(POLICY3));
317 JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
318 assertEquals(TYPE1, policyVal.get("type").getAsString());
319 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
320 assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
321 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
322 assertNotNull(properties.get(TCA_POLICY));