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.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfValidationResult;
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.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
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 Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
60 private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
61 private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
62 private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
63 private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
64 private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
65 private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
71 gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
75 public void testDeserialization() {
78 JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
79 verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson);
80 JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
81 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
84 serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
85 verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson);
86 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
87 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
90 serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
91 verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson);
92 serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
93 assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
95 } catch (Exception e) {
96 fail("No exception should be thrown");
101 public void testSerialization() {
104 JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
105 String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
106 verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
109 serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
110 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
111 verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
114 serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
115 serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
116 verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
118 } catch (Exception e) {
119 LOGGER.warn("No exception should be thrown", e);
120 fail("No exception should be thrown");
124 private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath)
125 throws JsonSyntaxException, IOException {
127 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
128 JpaToscaServiceTemplate serviceTemplate = gson.fromJson(policyJson, JpaToscaServiceTemplate.class);
129 return serviceTemplate;
132 private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath)
135 Yaml yaml = new Yaml();
136 String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
137 Object yamlObject = yaml.load(policyYaml);
138 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
139 JpaToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, JpaToscaServiceTemplate.class);
140 return serviceTemplate;
143 private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) {
144 return gson.toJson(serviceTemplate);
147 private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
149 // Sanity check the entire structure
150 assertNotNull(serviceTemplate);
151 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
152 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
154 // Check tosca_definitions_version
155 assertEquals("tosca_simple_yaml_1_0_0",
156 serviceTemplate.getToscaDefinitionsVersion());
158 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
159 .getPolicies().getConceptMap();
162 assertTrue(policiesConceptMap.size() == 1);
163 assertEquals("onap.restart.tca", policiesConceptMap.keySet().iterator().next().getName());
164 assertEquals("onap.restart.tca:1.0.0",
165 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
167 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
170 assertTrue(policyVal.getMetadata().size() == 1);
171 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
172 assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
175 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
176 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
177 assertNotNull(policyVal.getProperties().values().iterator().next());
180 private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
182 // Sanity check the entire structure
183 assertNotNull(serviceTemplate);
184 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
185 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
187 // Check tosca_definitions_version
188 assertEquals("tosca_simple_yaml_1_0_0",
189 serviceTemplate.getToscaDefinitionsVersion());
191 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
192 .getPolicies().getConceptMap();
195 assertTrue(policiesConceptMap.size() == 1);
196 assertEquals("onap.scaleout.tca", policiesConceptMap.keySet().iterator().next().getName());
197 assertEquals("onap.scaleout.tca:1.0.0",
198 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.scaleout.tca").getId());
200 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
203 assertTrue(policyVal.getMetadata().size() == 1);
204 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
205 assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
208 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
209 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
210 assertNotNull(policyVal.getProperties().values().iterator().next());
213 private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
215 // Sanity check the entire structure
216 assertNotNull(serviceTemplate);
217 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
218 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
220 // Check tosca_definitions_version
221 assertEquals("tosca_simple_yaml_1_0_0",
222 serviceTemplate.getToscaDefinitionsVersion());
224 Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
225 .getPolicies().getConceptMap();
228 assertTrue(policiesConceptMap.size() == 1);
229 assertEquals("onap.vfirewall.tca", policiesConceptMap.keySet().iterator().next().getName());
230 assertEquals("onap.vfirewall.tca:1.0.0",
231 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vfirewall.tca").getId());
233 JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
236 assertTrue(policyVal.getMetadata().size() == 1);
237 assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
238 assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
241 assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
242 assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
243 assertNotNull(policyVal.getProperties().values().iterator().next());
246 private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
248 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
249 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
251 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template")
253 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
254 assertTrue(policiesJsonArray.size() == 1);
255 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
256 assertNotNull(policy.get("onap.restart.tca"));
257 JsonObject policyVal = policy.get("onap.restart.tca").getAsJsonObject();
258 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
259 assertEquals("1.0.0", policyVal.get("version").getAsString());
260 assertEquals("onap.restart.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
262 JsonObject properties = policyVal.get("properties").getAsJsonObject();
263 assertNotNull(properties.get("tca_policy"));
266 private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
268 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
269 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
271 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
272 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
273 assertTrue(policiesJsonArray.size() == 1);
274 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
275 assertNotNull(policy.get("onap.scaleout.tca"));
276 JsonObject policyVal = policy.get("onap.scaleout.tca").getAsJsonObject();
277 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
278 assertEquals("1.0.0", policyVal.get("version").getAsString());
279 assertEquals("onap.scaleout.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
281 JsonObject properties = policyVal.get("properties").getAsJsonObject();
282 assertNotNull(properties.get("tca_policy"));
285 private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
287 JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
288 assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
290 JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
291 JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
292 assertTrue(policiesJsonArray.size() == 1);
293 JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
294 assertNotNull(policy.get("onap.vfirewall.tca"));
295 JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject();
296 assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
297 assertEquals("1.0.0", policyVal.get("version").getAsString());
298 assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
300 JsonObject properties = policyVal.get("properties").getAsJsonObject();
301 assertNotNull(properties.get("tca_policy"));