Added onap.policies.monitoring.tcagen2 version 2.0.0
[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-2020,2022 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.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
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.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.yaml.snakeyaml.Yaml;
46
47 /**
48  * Test serialization of monitoring policies.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  * @author Chenfei Gao (cgao@research.att.com)
52  */
53 public class MonitoringPolicySerializationTest {
54
55     private static final String VERSION = "version";
56
57     private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0";
58
59     private static final String DEFINITION_VERSION = "tosca_definitions_version";
60
61     private static final String TOPOLOGY_TEMPLATE = "topology_template";
62
63     private static final String TCA_POLICY = "tca.policy";
64
65     private static final String PROPERTIES2 = "properties";
66
67     private static final String POLICY_ID = "policy-id";
68
69     private static final String POLICIES = "policies";
70
71     private static final String POLICY3 = "onap.vfirewall.tca";
72
73     private static final String POLICY2 = "onap.scaleout.tca";
74
75     private static final String POLICY1 = "onap.restart.tca";
76
77     private static final String TYPE1 = "onap.policies.monitoring.tcagen2";
78
79     private static final String METADATA = "metadata";
80
81     private static final String VERSION_100 = "1.0.0";
82     private static final String VERSION_200 = "2.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     private static final String VFW_MON_INPUT_V2_JSON = "policies/vFirewall.policy.monitoring.input.tosca.v2.json";
93     private static final String VFW_MON_INPUT_V2_YAML = "policies/vFirewall.policy.monitoring.input.tosca.v2.yaml";
94
95     private final StandardCoder standardCoder = new StandardCoder();
96     private final YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
97
98     @Test
99     public void testDeserialization() throws Exception {
100         String policyTypeInputJson =
101                 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
102         ToscaServiceTemplate plainPolicyTypes =
103                 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
104
105         JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
106         policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
107
108         // vCPE
109         JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
110         JpaToscaServiceTemplate mergedServiceTemplate =
111                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
112         verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate);
113         JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
114         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
115
116         // vDNS
117         serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
118         mergedServiceTemplate =
119                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
120         verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate);
121         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
122         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
123
124         // vFirewall
125         serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
126         mergedServiceTemplate =
127                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
128         verifyVfwMonitoringInputDeserialization(mergedServiceTemplate, VERSION_100);
129         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
130         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
131
132         testDeserializationMonitoringV2();
133     }
134
135     @Test
136     public void testSerialization() {
137         assertThatCode(() -> {
138             // vCPE
139             JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
140             String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
141             verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
142
143             // vDNS
144             serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
145             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
146             verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
147
148             // vFirewall
149             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
150             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
151             verifyVfwMonitoringOutputserialization(serializedServiceTemplate, VERSION_100);
152
153             // vFirewall v2
154             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_V2_JSON);
155             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
156             verifyVfwMonitoringOutputserialization(serializedServiceTemplate, VERSION_200);
157         }).as("No exception should be thrown").doesNotThrowAnyException();
158     }
159
160     private void testDeserializationMonitoringV2() throws Exception {
161         String policyTypeInputJson =
162             ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.v2.yaml");
163         ToscaServiceTemplate plainPolicyTypes =
164             yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
165
166         JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
167         policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
168
169         JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_V2_JSON);
170         JpaToscaServiceTemplate mergedServiceTemplate =
171             ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
172         verifyVfwMonitoringInputDeserialization(mergedServiceTemplate, VERSION_200);
173
174         JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_V2_YAML);
175         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
176     }
177
178     private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
179         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
180         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
181         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
182         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
183         return jpaToscaServiceTemplate;
184     }
185
186     private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
187
188         Yaml yaml = new Yaml();
189         String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
190         Object yamlObject = yaml.load(policyYaml);
191         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
192         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
193
194         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
195         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
196         return jpaToscaServiceTemplate;
197     }
198
199     private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
200         return standardCoder.encode(serviceTemplate.toAuthorative());
201     }
202
203     private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
204
205         // Sanity check the entire structure
206         assertNotNull(serviceTemplate);
207         LOGGER.info(serviceTemplate.validate("").toString());
208         assertTrue(serviceTemplate.validate("").isValid());
209
210         // Check tosca_definitions_version
211         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
212
213         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
214                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
215
216         // Check policies
217         assertEquals(1, policiesConceptMap.size());
218         assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
219         assertEquals("onap.restart.tca:1.0.0",
220                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
221
222         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
223
224         // Check metadata
225         assertEquals(2, policyVal.getMetadata().size());
226         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
227         assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
228
229         // Check properties
230         assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
231         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
232         assertNotNull(policyVal.getProperties().values().iterator().next());
233     }
234
235     private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
236
237         // Sanity check the entire structure
238         assertNotNull(serviceTemplate);
239         LOGGER.info(serviceTemplate.validate("").toString());
240         assertTrue(serviceTemplate.validate("").isValid());
241
242         // Check tosca_definitions_version
243         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
244
245         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
246                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
247
248         // Check policies
249         assertEquals(1, policiesConceptMap.size());
250         assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
251         assertEquals("onap.scaleout.tca:1.0.0",
252                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
253
254         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
255
256         // Check metadata
257         assertEquals(2, policyVal.getMetadata().size());
258         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
259         assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
260
261         // Check properties
262         assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
263         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
264         assertNotNull(policyVal.getProperties().values().iterator().next());
265     }
266
267     private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate, String version) {
268
269         // Sanity check the entire structure
270         assertNotNull(serviceTemplate);
271         LOGGER.info(serviceTemplate.validate("").toString());
272         assertTrue(serviceTemplate.validate("").isValid());
273
274         // Check tosca_definitions_version
275         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
276
277         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
278                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
279
280         // Check policies
281         assertEquals(1, policiesConceptMap.size());
282         assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
283         assertEquals("onap.vfirewall.tca:" + version,
284                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
285
286         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
287
288         // Check metadata
289         assertEquals(2, policyVal.getMetadata().size());
290         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
291         assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
292
293         // Check properties
294         assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
295         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
296         assertNotNull(policyVal.getProperties().values().iterator().next());
297     }
298
299     private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
300
301         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
302         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
303         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
304         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
305         assertEquals(1, policiesJsonArray.size());
306         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
307         assertNotNull(policy.get(POLICY1));
308         JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
309         assertEquals(TYPE1, policyVal.get("type").getAsString());
310         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
311         assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
312         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
313         assertNotNull(properties.get(TCA_POLICY));
314     }
315
316     private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
317
318         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
319         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
320         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
321         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
322         assertEquals(1, policiesJsonArray.size());
323         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
324         assertNotNull(policy.get(POLICY2));
325         JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
326         assertEquals(TYPE1, policyVal.get("type").getAsString());
327         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
328         assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
329         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
330         assertNotNull(properties.get(TCA_POLICY));
331     }
332
333     private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate, String version) {
334
335         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
336         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
337         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
338         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
339         assertEquals(1, policiesJsonArray.size());
340         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
341         assertNotNull(policy.get(POLICY3));
342         JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
343         assertEquals(TYPE1, policyVal.get("type").getAsString());
344         assertEquals(version, policyVal.get(VERSION).getAsString());
345         assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
346         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
347         assertNotNull(properties.get(TCA_POLICY));
348     }
349 }