2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Copyright (C) 2019 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfValidationResult;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
45 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.yaml.snakeyaml.Yaml;
51 * Test serialization of monitoring policies.
53 * @author Liam Fallon (liam.fallon@est.tech)
54 * @author Chenfei Gao (cgao@research.att.com)
56 public class MonitoringPolicySerializationTest {
58 private static final String VERSION = "version";
60 private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0";
62 private static final String DEFINITION_VERSION = "tosca_definitions_version";
64 private static final String TOPOLOGY_TEMPLATE = "topology_template";
66 private static final String TCA_POLICY = "tca_policy";
68 private static final String PROPERTIES2 = "properties";
70 private static final String POLICY_ID = "policy-id";
72 private static final String POLICIES = "policies";
74 private static final String POLICY3 = "onap.vfirewall.tca";
76 private static final String POLICY2 = "onap.scaleout.tca";
78 private static final String POLICY1 = "onap.restart.tca";
80 private static final String TYPE1 = "onap.policies.monitoring.cdap.tca.hi.lo.app";
82 private static final String METADATA = "metadata";
84 private static final String VERSION_100 = "1.0.0";
86 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
88 private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
89 private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
90 private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
91 private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
92 private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
93 private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
95 private StandardCoder standardCoder = new StandardCoder();
96 private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
99 public void testDeserialization() throws Exception {
100 String policyTypeInputJson =
101 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.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 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
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 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
125 serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
126 mergedServiceTemplate =
127 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
128 verifyVfwMonitoringInputDeserialization(mergedServiceTemplate);
129 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
130 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
134 public void testSerialization() {
137 JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
138 String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
139 verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
142 serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
143 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
144 verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
147 serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
148 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
149 verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
151 } catch (Exception e) {
152 LOGGER.warn("No exception should be thrown", e);
153 fail("No exception should be thrown");
157 private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
159 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
160 ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
161 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
162 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
163 return jpaToscaServiceTemplate;
166 private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
168 Yaml yaml = new Yaml();
169 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
170 Object yamlObject = yaml.load(policyYaml);
171 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
172 ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
174 JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
175 jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
176 return jpaToscaServiceTemplate;
179 private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
180 return standardCoder.encode(serviceTemplate.toAuthorative());
183 private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
185 // Sanity check the entire structure
186 assertNotNull(serviceTemplate);
187 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
188 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
190 // Check tosca_definitions_version
191 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
193 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
194 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
197 assertTrue(policiesConceptMap.size() == 1);
198 assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
199 assertEquals("onap.restart.tca:1.0.0",
200 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
202 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
205 assertTrue(policyVal.getMetadata().size() == 2);
206 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
207 assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
210 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
211 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
212 assertNotNull(policyVal.getProperties().values().iterator().next());
215 private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
217 // Sanity check the entire structure
218 assertNotNull(serviceTemplate);
219 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
220 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
222 // Check tosca_definitions_version
223 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
225 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
226 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
229 assertTrue(policiesConceptMap.size() == 1);
230 assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
231 assertEquals("onap.scaleout.tca:1.0.0",
232 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
234 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
237 assertTrue(policyVal.getMetadata().size() == 2);
238 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
239 assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
242 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
243 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
244 assertNotNull(policyVal.getProperties().values().iterator().next());
247 private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
249 // Sanity check the entire structure
250 assertNotNull(serviceTemplate);
251 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
252 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
254 // Check tosca_definitions_version
255 assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
257 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
258 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
261 assertTrue(policiesConceptMap.size() == 1);
262 assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
263 assertEquals("onap.vfirewall.tca:1.0.0",
264 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
266 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
269 assertTrue(policyVal.getMetadata().size() == 2);
270 assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
271 assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
274 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
275 assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
276 assertNotNull(policyVal.getProperties().values().iterator().next());
279 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
281 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
282 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
283 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
284 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
285 assertTrue(policiesJsonArray.size() == 1);
286 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
287 assertNotNull(policy.get(POLICY1));
288 JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
289 assertEquals(TYPE1, policyVal.get("type").getAsString());
290 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
291 assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
292 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
293 assertNotNull(properties.get(TCA_POLICY));
296 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
298 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
299 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
300 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
301 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
302 assertTrue(policiesJsonArray.size() == 1);
303 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
304 assertNotNull(policy.get(POLICY2));
305 JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
306 assertEquals(TYPE1, policyVal.get("type").getAsString());
307 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
308 assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
309 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
310 assertNotNull(properties.get(TCA_POLICY));
313 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
315 JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
316 assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
317 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
318 JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
319 assertTrue(policiesJsonArray.size() == 1);
320 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
321 assertNotNull(policy.get(POLICY3));
322 JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
323 assertEquals(TYPE1, policyVal.get("type").getAsString());
324 assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
325 assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
326 JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
327 assertNotNull(properties.get(TCA_POLICY));