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