98ced3b419f18366fba74fe9bfe46f5d4ea0487f
[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 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
83     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
84
85     private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
86     private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
87     private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
88     private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
89     private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
90     private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
91
92     private StandardCoder standardCoder = new StandardCoder();
93     private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
94
95     @Test
96     public void testDeserialization() throws Exception {
97         String policyTypeInputJson =
98                 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
99         ToscaServiceTemplate plainPolicyTypes =
100                 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
101
102         JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
103         policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
104
105         // vCPE
106         JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
107         JpaToscaServiceTemplate mergedServiceTemplate =
108                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
109         verifyVcpeMonitoringInputDeserialization(mergedServiceTemplate);
110         JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
111         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
112
113         // vDNS
114         serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
115         mergedServiceTemplate =
116                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
117         verifyVdnsMonitoringInputDeserialization(mergedServiceTemplate);
118         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
119         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
120
121         // vFirewall
122         serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
123         mergedServiceTemplate =
124                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, serviceTemplateFromJson);
125         verifyVfwMonitoringInputDeserialization(mergedServiceTemplate);
126         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
127         assertEquals(0, serviceTemplateFromJson.compareTo(serviceTemplateFromYaml));
128     }
129
130     @Test
131     public void testSerialization() {
132         assertThatCode(() -> {
133             // vCPE
134             JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
135             String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
136             verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
137
138             // vDNS
139             serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
140             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
141             verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
142
143             // vFirewall
144             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
145             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
146             verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
147
148         }).as("No exception should be thrown").doesNotThrowAnyException();
149     }
150
151     private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) throws Exception {
152
153         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
154         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
155         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
156         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
157         return jpaToscaServiceTemplate;
158     }
159
160     private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) throws Exception {
161
162         Yaml yaml = new Yaml();
163         String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
164         Object yamlObject = yaml.load(policyYaml);
165         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
166         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
167
168         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
169         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
170         return jpaToscaServiceTemplate;
171     }
172
173     private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
174         return standardCoder.encode(serviceTemplate.toAuthorative());
175     }
176
177     private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
178
179         // Sanity check the entire structure
180         assertNotNull(serviceTemplate);
181         LOGGER.info(serviceTemplate.validate("").toString());
182         assertTrue(serviceTemplate.validate("").isValid());
183
184         // Check tosca_definitions_version
185         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
186
187         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
188                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
189
190         // Check policies
191         assertEquals(1, policiesConceptMap.size());
192         assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
193         assertEquals("onap.restart.tca:1.0.0",
194                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
195
196         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
197
198         // Check metadata
199         assertEquals(2, policyVal.getMetadata().size());
200         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
201         assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
202
203         // Check properties
204         assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
205         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
206         assertNotNull(policyVal.getProperties().values().iterator().next());
207     }
208
209     private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
210
211         // Sanity check the entire structure
212         assertNotNull(serviceTemplate);
213         LOGGER.info(serviceTemplate.validate("").toString());
214         assertTrue(serviceTemplate.validate("").isValid());
215
216         // Check tosca_definitions_version
217         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
218
219         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
220                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
221
222         // Check policies
223         assertEquals(1, policiesConceptMap.size());
224         assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
225         assertEquals("onap.scaleout.tca:1.0.0",
226                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
227
228         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
229
230         // Check metadata
231         assertEquals(2, policyVal.getMetadata().size());
232         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
233         assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
234
235         // Check properties
236         assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
237         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
238         assertNotNull(policyVal.getProperties().values().iterator().next());
239     }
240
241     private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
242
243         // Sanity check the entire structure
244         assertNotNull(serviceTemplate);
245         LOGGER.info(serviceTemplate.validate("").toString());
246         assertTrue(serviceTemplate.validate("").isValid());
247
248         // Check tosca_definitions_version
249         assertEquals(YAML_VERSION, serviceTemplate.getToscaDefinitionsVersion());
250
251         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap =
252                 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
253
254         // Check policies
255         assertEquals(1, policiesConceptMap.size());
256         assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
257         assertEquals("onap.vfirewall.tca:1.0.0",
258                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
259
260         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
261
262         // Check metadata
263         assertEquals(2, policyVal.getMetadata().size());
264         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
265         assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
266
267         // Check properties
268         assertEquals(1, policiesConceptMap.values().iterator().next().getProperties().size());
269         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
270         assertNotNull(policyVal.getProperties().values().iterator().next());
271     }
272
273     private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
274
275         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
276         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
277         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
278         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
279         assertEquals(1, policiesJsonArray.size());
280         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
281         assertNotNull(policy.get(POLICY1));
282         JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
283         assertEquals(TYPE1, policyVal.get("type").getAsString());
284         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
285         assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
286         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
287         assertNotNull(properties.get(TCA_POLICY));
288     }
289
290     private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
291
292         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
293         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
294         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
295         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
296         assertEquals(1, policiesJsonArray.size());
297         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
298         assertNotNull(policy.get(POLICY2));
299         JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
300         assertEquals(TYPE1, policyVal.get("type").getAsString());
301         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
302         assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
303         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
304         assertNotNull(properties.get(TCA_POLICY));
305     }
306
307     private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
308
309         JsonObject serviceTemplateJsonObject = JsonParser.parseString(serializedServiceTemplate).getAsJsonObject();
310         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION).getAsString());
311         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
312         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
313         assertEquals(1, policiesJsonArray.size());
314         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
315         assertNotNull(policy.get(POLICY3));
316         JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
317         assertEquals(TYPE1, policyVal.get("type").getAsString());
318         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
319         assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID).getAsString());
320         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
321         assertNotNull(properties.get(TCA_POLICY));
322     }
323 }