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.Gson;
30 import com.google.gson.JsonArray;
31 import com.google.gson.JsonObject;
32 import com.google.gson.JsonParser;
33 import com.google.gson.JsonSyntaxException;
35 import java.io.IOException;
37 import org.junit.Before;
38 import org.junit.Test;
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.simple.concepts.ToscaPolicy;
43 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.yaml.snakeyaml.Yaml;
50 * Test serialization of monitoring policies.
52 * @author Liam Fallon (liam.fallon@est.tech)
53 * @author Chenfei Gao (cgao@research.att.com)
55 public class MonitoringPolicySerializationTest {
57 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
59 private static final String VCPE_MONITORING_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
60 private static final String VCPE_MONITORING_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
61 private static final String VDNS_MONITORING_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
62 private static final String VDNS_MONITORING_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
63 private static final String VFW_MONITORING_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
64 private static final String VFW_MONITORING_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
70 gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
74 public void testDeserialization() {
77 ToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MONITORING_INPUT_JSON);
78 verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson);
79 ToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MONITORING_INPUT_YAML);
80 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
83 serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MONITORING_INPUT_JSON);
84 verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson);
85 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MONITORING_INPUT_YAML);
86 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
89 serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MONITORING_INPUT_JSON);
90 verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson);
91 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MONITORING_INPUT_YAML);
92 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
94 } catch (Exception e) {
95 fail("No exception should be thrown");
100 public void testSerialization() {
103 ToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MONITORING_INPUT_JSON);
104 String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
105 verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
108 serviceTemplate = deserializeMonitoringInputJson(VDNS_MONITORING_INPUT_JSON);
109 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
110 verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
113 serviceTemplate = deserializeMonitoringInputJson(VFW_MONITORING_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 ToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath)
124 throws JsonSyntaxException, IOException {
126 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
127 ToscaServiceTemplate serviceTemplate = gson.fromJson(policyJson, ToscaServiceTemplate.class);
128 return serviceTemplate;
131 private ToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath)
132 throws JsonSyntaxException, IOException {
134 Yaml yaml = new Yaml();
135 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
136 Object yamlObject = yaml.load(policyYaml);
137 String yamlAsJsonString = new Gson().toJson(yamlObject);
138 ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class);
139 return serviceTemplate;
142 private String serializeMonitoringServiceTemplate(ToscaServiceTemplate serviceTemplate) {
143 return gson.toJson(serviceTemplate);
146 private void verifyVcpeMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) {
148 // Sanity check the entire structure
149 assertNotNull(serviceTemplate);
150 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
151 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
153 // Check tosca_definitions_version
154 assertEquals("tosca_simple_yaml_1_0_0",
155 serviceTemplate.getToscaDefinitionsVersion());
157 Map<PfConceptKey, ToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
158 .getPolicies().getConceptMap();
161 assertTrue(policiesConceptMap.size() == 1);
162 assertEquals("onap.restart.tca", policiesConceptMap.keySet().iterator().next().getName());
163 assertEquals("onap.restart.tca:1.0.0",
164 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
166 ToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
169 assertTrue(policyVal.getMetadata().size() == 1);
170 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
171 assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
174 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
175 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
176 assertNotNull(policyVal.getProperties().values().iterator().next());
179 private void verifyVdnsMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) {
181 // Sanity check the entire structure
182 assertNotNull(serviceTemplate);
183 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
184 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
186 // Check tosca_definitions_version
187 assertEquals("tosca_simple_yaml_1_0_0",
188 serviceTemplate.getToscaDefinitionsVersion());
190 Map<PfConceptKey, ToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
191 .getPolicies().getConceptMap();
194 assertTrue(policiesConceptMap.size() == 1);
195 assertEquals("onap.scaleout.tca", policiesConceptMap.keySet().iterator().next().getName());
196 assertEquals("onap.scaleout.tca:1.0.0",
197 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.scaleout.tca").getId());
199 ToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
202 assertTrue(policyVal.getMetadata().size() == 1);
203 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
204 assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
207 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
208 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
209 assertNotNull(policyVal.getProperties().values().iterator().next());
212 private void verifyVfwMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) {
214 // Sanity check the entire structure
215 assertNotNull(serviceTemplate);
216 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
217 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
219 // Check tosca_definitions_version
220 assertEquals("tosca_simple_yaml_1_0_0",
221 serviceTemplate.getToscaDefinitionsVersion());
223 Map<PfConceptKey, ToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
224 .getPolicies().getConceptMap();
227 assertTrue(policiesConceptMap.size() == 1);
228 assertEquals("onap.vfirewall.tca", policiesConceptMap.keySet().iterator().next().getName());
229 assertEquals("onap.vfirewall.tca:1.0.0",
230 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vfirewall.tca").getId());
232 ToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
235 assertTrue(policyVal.getMetadata().size() == 1);
236 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
237 assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
240 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
241 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
242 assertNotNull(policyVal.getProperties().values().iterator().next());
245 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
247 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
248 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
250 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template")
252 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
253 assertTrue(policiesJsonArray.size() == 1);
254 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
255 assertNotNull(policy.get("onap.restart.tca"));
256 JsonObject policyVal = policy.get("onap.restart.tca").getAsJsonObject();
257 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
258 assertEquals("1.0.0", policyVal.get("version").getAsString());
259 assertEquals("onap.restart.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
261 JsonObject properties = policyVal.get("properties").getAsJsonObject();
262 assertNotNull(properties.get("tca_policy"));
265 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
267 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
268 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
270 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
271 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
272 assertTrue(policiesJsonArray.size() == 1);
273 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
274 assertNotNull(policy.get("onap.scaleout.tca"));
275 JsonObject policyVal = policy.get("onap.scaleout.tca").getAsJsonObject();
276 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
277 assertEquals("1.0.0", policyVal.get("version").getAsString());
278 assertEquals("onap.scaleout.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
280 JsonObject properties = policyVal.get("properties").getAsJsonObject();
281 assertNotNull(properties.get("tca_policy"));
284 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
286 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
287 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
289 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
290 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
291 assertTrue(policiesJsonArray.size() == 1);
292 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
293 assertNotNull(policy.get("onap.vfirewall.tca"));
294 JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject();
295 assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
296 assertEquals("1.0.0", policyVal.get("version").getAsString());
297 assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
299 JsonObject properties = policyVal.get("properties").getAsJsonObject();
300 assertNotNull(properties.get("tca_policy"));