2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 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;
 
  34 import org.junit.Before;
 
  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.resources.ResourceUtils;
 
  39 import org.onap.policy.models.base.PfConceptKey;
 
  40 import org.onap.policy.models.base.PfValidationResult;
 
  41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
 
  43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 
  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 Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
 
  58     private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
 
  59     private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
 
  60     private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
 
  61     private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
 
  62     private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
 
  63     private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
 
  65     private StandardCoder standardCoder;
 
  69         standardCoder = new StandardCoder();
 
  73     public void testDeserialization() {
 
  76             JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
 
  77             verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson);
 
  78             JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
 
  79             assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
 
  82             serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
 
  83             verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson);
 
  84             serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
 
  85             assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
 
  88             serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
 
  89             verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson);
 
  90             serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
 
  91             assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
 
  93         } catch (Exception e) {
 
  94             LOGGER.warn("No exception should be thrown", e);
 
  95             fail("No exception should be thrown");
 
 100     public void testSerialization() {
 
 103             JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
 
 104             String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
 
 105             verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
 
 108             serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
 
 109             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
 
 110             verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
 
 113             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
 
 114             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
 
 115             verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
 
 117         } catch (Exception e) {
 
 118             LOGGER.warn("No exception should be thrown", e);
 
 119             fail("No exception should be thrown");
 
 123     private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath)
 
 126         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
 
 127         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
 
 128         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
 
 129         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
 
 130         return jpaToscaServiceTemplate;
 
 133     private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath)
 
 136         Yaml yaml = new Yaml();
 
 137         String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
 
 138         Object yamlObject = yaml.load(policyYaml);
 
 139         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
 
 140         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
 
 142         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
 
 143         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
 
 144         return jpaToscaServiceTemplate;
 
 147     private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
 
 148         return standardCoder.encode(serviceTemplate.toAuthorative());
 
 151     private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
 
 153         // Sanity check the entire structure
 
 154         assertNotNull(serviceTemplate);
 
 155         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
 
 156         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
 
 158         // Check tosca_definitions_version
 
 159         assertEquals("tosca_simple_yaml_1_0_0",
 
 160                 serviceTemplate.getToscaDefinitionsVersion());
 
 162         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
 
 163                 .getPolicies().getConceptMap();
 
 166         assertTrue(policiesConceptMap.size() == 1);
 
 167         assertEquals("onap.restart.tca", policiesConceptMap.keySet().iterator().next().getName());
 
 168         assertEquals("onap.restart.tca:1.0.0",
 
 169                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
 
 171         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
 
 174         assertTrue(policyVal.getMetadata().size() == 1);
 
 175         assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
 
 176         assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
 
 179         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
 
 180         assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
 
 181         assertNotNull(policyVal.getProperties().values().iterator().next());
 
 184     private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
 
 186         // Sanity check the entire structure
 
 187         assertNotNull(serviceTemplate);
 
 188         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
 
 189         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
 
 191         // Check tosca_definitions_version
 
 192         assertEquals("tosca_simple_yaml_1_0_0",
 
 193                 serviceTemplate.getToscaDefinitionsVersion());
 
 195         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
 
 196                 .getPolicies().getConceptMap();
 
 199         assertTrue(policiesConceptMap.size() == 1);
 
 200         assertEquals("onap.scaleout.tca", policiesConceptMap.keySet().iterator().next().getName());
 
 201         assertEquals("onap.scaleout.tca:1.0.0",
 
 202                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.scaleout.tca").getId());
 
 204         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
 
 207         assertTrue(policyVal.getMetadata().size() == 1);
 
 208         assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
 
 209         assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
 
 212         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
 
 213         assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
 
 214         assertNotNull(policyVal.getProperties().values().iterator().next());
 
 217     private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
 
 219         // Sanity check the entire structure
 
 220         assertNotNull(serviceTemplate);
 
 221         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
 
 222         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
 
 224         // Check tosca_definitions_version
 
 225         assertEquals("tosca_simple_yaml_1_0_0",
 
 226                 serviceTemplate.getToscaDefinitionsVersion());
 
 228         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
 
 229                 .getPolicies().getConceptMap();
 
 232         assertTrue(policiesConceptMap.size() == 1);
 
 233         assertEquals("onap.vfirewall.tca", policiesConceptMap.keySet().iterator().next().getName());
 
 234         assertEquals("onap.vfirewall.tca:1.0.0",
 
 235                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vfirewall.tca").getId());
 
 237         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
 
 240         assertTrue(policyVal.getMetadata().size() == 1);
 
 241         assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
 
 242         assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
 
 245         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
 
 246         assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
 
 247         assertNotNull(policyVal.getProperties().values().iterator().next());
 
 250     private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
 
 252         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
 
 253         assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
 
 255         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template")
 
 257         JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
 
 258         assertTrue(policiesJsonArray.size() == 1);
 
 259         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
 
 260         assertNotNull(policy.get("onap.restart.tca"));
 
 261         JsonObject policyVal = policy.get("onap.restart.tca").getAsJsonObject();
 
 262         assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
 
 263         assertEquals("1.0.0", policyVal.get("version").getAsString());
 
 264         assertEquals("onap.restart.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
 
 266         JsonObject properties = policyVal.get("properties").getAsJsonObject();
 
 267         assertNotNull(properties.get("tca_policy"));
 
 270     private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
 
 272         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
 
 273         assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
 
 275         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
 
 276         JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
 
 277         assertTrue(policiesJsonArray.size() == 1);
 
 278         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
 
 279         assertNotNull(policy.get("onap.scaleout.tca"));
 
 280         JsonObject policyVal = policy.get("onap.scaleout.tca").getAsJsonObject();
 
 281         assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
 
 282         assertEquals("1.0.0", policyVal.get("version").getAsString());
 
 283         assertEquals("onap.scaleout.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
 
 285         JsonObject properties = policyVal.get("properties").getAsJsonObject();
 
 286         assertNotNull(properties.get("tca_policy"));
 
 289     private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
 
 291         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
 
 292         assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
 
 294         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
 
 295         JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
 
 296         assertTrue(policiesJsonArray.size() == 1);
 
 297         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
 
 298         assertNotNull(policy.get("onap.vfirewall.tca"));
 
 299         JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject();
 
 300         assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
 
 301         assertEquals("1.0.0", policyVal.get("version").getAsString());
 
 302         assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
 
 304         JsonObject properties = policyVal.get("properties").getAsJsonObject();
 
 305         assertNotNull(properties.get("tca_policy"));