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 fail("No exception should be thrown");
122 private ToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath)
123 throws JsonSyntaxException, IOException {
125 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
126 ToscaServiceTemplate serviceTemplate = gson.fromJson(policyJson, ToscaServiceTemplate.class);
127 return serviceTemplate;
130 private ToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath)
131 throws JsonSyntaxException, IOException {
133 Yaml yaml = new Yaml();
134 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
135 Object yamlObject = yaml.load(policyYaml);
136 String yamlAsJsonString = new Gson().toJson(yamlObject);
137 ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class);
138 return serviceTemplate;
141 private String serializeMonitoringServiceTemplate(ToscaServiceTemplate serviceTemplate) {
142 return gson.toJson(serviceTemplate);
145 private void verifyVcpeMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) {
147 // Sanity check the entire structure
148 assertNotNull(serviceTemplate);
149 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
150 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
152 // Check tosca_definitions_version
153 assertEquals("tosca_simple_yaml_1_0_0",
154 serviceTemplate.getToscaDefinitionsVersion());
156 Map<PfConceptKey, ToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
157 .getPolicies().getConceptMap();
160 assertTrue(policiesConceptMap.size() == 1);
161 assertEquals("onap.restart.tca", policiesConceptMap.keySet().iterator().next().getName());
162 assertEquals("onap.restart.tca:1.0.0",
163 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
165 ToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
168 assertTrue(policyVal.getMetadata().size() == 1);
169 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
170 assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
173 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
174 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
175 assertNotNull(policyVal.getProperties().values().iterator().next());
178 private void verifyVdnsMonitoringInputDeserialization(ToscaServiceTemplate 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("tosca_simple_yaml_1_0_0",
187 serviceTemplate.getToscaDefinitionsVersion());
189 Map<PfConceptKey, ToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
190 .getPolicies().getConceptMap();
193 assertTrue(policiesConceptMap.size() == 1);
194 assertEquals("onap.scaleout.tca", policiesConceptMap.keySet().iterator().next().getName());
195 assertEquals("onap.scaleout.tca:1.0.0",
196 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.scaleout.tca").getId());
198 ToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
201 assertTrue(policyVal.getMetadata().size() == 1);
202 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
203 assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
206 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
207 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
208 assertNotNull(policyVal.getProperties().values().iterator().next());
211 private void verifyVfwMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) {
213 // Sanity check the entire structure
214 assertNotNull(serviceTemplate);
215 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
216 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
218 // Check tosca_definitions_version
219 assertEquals("tosca_simple_yaml_1_0_0",
220 serviceTemplate.getToscaDefinitionsVersion());
222 Map<PfConceptKey, ToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
223 .getPolicies().getConceptMap();
226 assertTrue(policiesConceptMap.size() == 1);
227 assertEquals("onap.vfirewall.tca", policiesConceptMap.keySet().iterator().next().getName());
228 assertEquals("onap.vfirewall.tca:1.0.0",
229 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vfirewall.tca").getId());
231 ToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
234 assertTrue(policyVal.getMetadata().size() == 1);
235 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
236 assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
239 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
240 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
241 assertNotNull(policyVal.getProperties().values().iterator().next());
244 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
246 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
247 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
249 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template")
251 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
252 assertTrue(policiesJsonArray.size() == 1);
253 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
254 assertNotNull(policy.get("onap.restart.tca"));
255 JsonObject policyVal = policy.get("onap.restart.tca").getAsJsonObject();
256 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
257 assertEquals("1.0.0", policyVal.get("version").getAsString());
258 assertEquals("onap.restart.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
260 JsonObject properties = policyVal.get("properties").getAsJsonObject();
261 assertNotNull(properties.get("tca_policy"));
264 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
266 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
267 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
269 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
270 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
271 assertTrue(policiesJsonArray.size() == 1);
272 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
273 assertNotNull(policy.get("onap.scaleout.tca"));
274 JsonObject policyVal = policy.get("onap.scaleout.tca").getAsJsonObject();
275 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
276 assertEquals("1.0.0", policyVal.get("version").getAsString());
277 assertEquals("onap.scaleout.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
279 JsonObject properties = policyVal.get("properties").getAsJsonObject();
280 assertNotNull(properties.get("tca_policy"));
283 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
285 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
286 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
288 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
289 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
290 assertTrue(policiesJsonArray.size() == 1);
291 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
292 assertNotNull(policy.get("onap.vfirewall.tca"));
293 JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject();
294 assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
295 assertEquals("1.0.0", policyVal.get("version").getAsString());
296 assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
298 JsonObject properties = policyVal.get("properties").getAsJsonObject();
299 assertNotNull(properties.get("tca_policy"));