Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / serialization / MonitoringPolicySerializationTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.serialization;
23
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;
28
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
32 import java.util.Map;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfValidationResult;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
43 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
47
48 /**
49  * Test serialization of monitoring policies.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  * @author Chenfei Gao (cgao@research.att.com)
53  */
54 public class MonitoringPolicySerializationTest {
55
56     private static final String VERSION = "version";
57
58     private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0";
59
60     private static final String DEFINITION_VERSION = "tosca_definitions_version";
61
62     private static final String TOPOLOGY_TEMPLATE = "topology_template";
63
64     private static final String TCA_POLICY = "tca_policy";
65
66     private static final String PROPERTIES2 = "properties";
67
68     private static final String POLICY_ID = "policy-id";
69
70     private static final String POLICIES = "policies";
71
72     private static final String POLICY3 = "onap.vfirewall.tca";
73
74     private static final String POLICY2 = "onap.scaleout.tca";
75
76     private static final String POLICY1 = "onap.restart.tca";
77
78     private static final String TYPE1 = "onap.policies.monitoring.cdap.tca.hi.lo.app";
79
80     private static final String METADATA = "metadata";
81
82     private static final String VERSION_100 = "1.0.0";
83
84     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
85
86     private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
87     private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
88     private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
89     private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
90     private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
91     private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
92
93     private StandardCoder standardCoder = new StandardCoder();
94     private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
95
96     @Test
97     public void testDeserialization() throws Exception {
98         String policyTypeInputJson =
99                 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml");
100         ToscaServiceTemplate plainPolicyTypes =
101                 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
102
103         JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
104         policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
105
106         // vCPE
107         JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
108         JpaToscaServiceTemplate mergedServiceTemplate =
109                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
110         verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate);
111         JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
112         assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
113
114         // vDNS
115         serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
116         mergedServiceTemplate =
117                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
118         verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate);
119         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
120         assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
121
122         // vFirewall
123         serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
124         mergedServiceTemplate =
125                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
126         verifyVfwMonitoringInputDeserialization(mergedServiceTemplate);
127         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
128         assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
129     }
130
131     @Test
132     public void testSerialization() {
133         try {
134             // vCPE
135             JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
136             String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
137             verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
138
139             // vDNS
140             serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
141             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
142             verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
143
144             // vFirewall
145             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
146             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
147             verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
148
149         } catch (Exception e) {
150             LOGGER.warn("No exception should be thrown", e);
151             fail("No exception should be thrown");
152         }
153     }
154
155     private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
156
157         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
158         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
159         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
160         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
161         return jpaToscaServiceTemplate;
162     }
163
164     private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
165
166         Yaml yaml = new Yaml();
167         String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
168         Object yamlObject = yaml.load(policyYaml);
169         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
170         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
171
172         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
173         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
174         return jpaToscaServiceTemplate;
175     }
176
177     private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
178         return standardCoder.encode(serviceTemplate.toAuthorative());
179     }
180
181     private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
182
183         // Sanity check the entire structure
184         assertNotNull(serviceTemplate);
185         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
186         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
187
188         // Check tosca_definitions_version
189         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
190
191         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
192                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
193
194         // Check policies
195         assertTrue(policiesConceptMap.size() == 1);
196         assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
197         assertEquals("onap.restart.tca:1.0.0",
198                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
199
200         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
201
202         // Check metadata
203         assertTrue(policyVal.getMetadata().size() == 2);
204         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
205         assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
206
207         // Check properties
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());
211     }
212
213     private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
214
215         // Sanity check the entire structure
216         assertNotNull(serviceTemplate);
217         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
218         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
219
220         // Check tosca_definitions_version
221         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
222
223         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
224                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
225
226         // Check policies
227         assertTrue(policiesConceptMap.size() == 1);
228         assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
229         assertEquals("onap.scaleout.tca:1.0.0",
230                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
231
232         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
233
234         // Check metadata
235         assertTrue(policyVal.getMetadata().size() == 2);
236         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
237         assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
238
239         // Check properties
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());
243     }
244
245     private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
246
247         // Sanity check the entire structure
248         assertNotNull(serviceTemplate);
249         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
250         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
251
252         // Check tosca_definitions_version
253         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
254
255         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
256                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
257
258         // Check policies
259         assertTrue(policiesConceptMap.size() == 1);
260         assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
261         assertEquals("onap.vfirewall.tca:1.0.0",
262                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
263
264         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
265
266         // Check metadata
267         assertTrue(policyVal.getMetadata().size() == 2);
268         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
269         assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
270
271         // Check properties
272         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
273         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
274         assertNotNull(policyVal.getProperties().values().iterator().next());
275     }
276
277     private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
278
279         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
280         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
281         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
282         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
283         assertTrue(policiesJsonArray.size() == 1);
284         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
285         assertNotNull(policy.get(POLICY1));
286         JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
287         assertEquals(TYPE1, policyVal.get("type").getAsString());
288         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
289         assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
290         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
291         assertNotNull(properties.get(TCA_POLICY));
292     }
293
294     private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
295
296         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
297         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
298         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
299         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
300         assertTrue(policiesJsonArray.size() == 1);
301         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
302         assertNotNull(policy.get(POLICY2));
303         JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
304         assertEquals(TYPE1, policyVal.get("type").getAsString());
305         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
306         assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
307         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
308         assertNotNull(properties.get(TCA_POLICY));
309     }
310
311     private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
312
313         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
314         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
315         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
316         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
317         assertTrue(policiesJsonArray.size() == 1);
318         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
319         assertNotNull(policy.get(POLICY3));
320         JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
321         assertEquals(TYPE1, policyVal.get("type").getAsString());
322         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
323         assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
324         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
325         assertNotNull(properties.get(TCA_POLICY));
326     }
327 }