Set default and check existance of Policy Type
[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 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 import org.junit.Before;
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.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfValidationResult;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
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 Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
57
58     private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
59     private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
60     private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
61     private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
62     private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
63     private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
64
65     private StandardCoder standardCoder;
66
67     @Before
68     public void setUp() {
69         standardCoder = new StandardCoder();
70     }
71
72     @Test
73     public void testDeserialization() {
74         try {
75             // vCPE
76             JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
77             verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson);
78             JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
79             assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
80
81             // vDNS
82             serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
83             verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson);
84             serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
85             assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
86
87             // vFirewall
88             serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
89             verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson);
90             serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
91             assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
92
93         } catch (Exception e) {
94             LOGGER.warn("No exception should be thrown", e);
95             fail("No exception should be thrown");
96         }
97     }
98
99     @Test
100     public void testSerialization() {
101         try {
102             // vCPE
103             JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
104             String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
105             verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
106
107             // vDNS
108             serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
109             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
110             verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
111
112             // vFirewall
113             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
114             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
115             verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
116
117         } catch (Exception e) {
118             LOGGER.warn("No exception should be thrown", e);
119             fail("No exception should be thrown");
120         }
121     }
122
123     private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath)
124             throws Exception {
125
126         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
127         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
128         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
129         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
130         return jpaToscaServiceTemplate;
131     }
132
133     private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath)
134             throws Exception {
135
136         Yaml yaml = new Yaml();
137         String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
138         Object yamlObject = yaml.load(policyYaml);
139         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
140         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
141
142         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
143         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
144         return jpaToscaServiceTemplate;
145     }
146
147     private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
148         return standardCoder.encode(serviceTemplate.toAuthorative());
149     }
150
151     private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
152
153         // Sanity check the entire structure
154         assertNotNull(serviceTemplate);
155         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
156         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
157
158         // Check tosca_definitions_version
159         assertEquals("tosca_simple_yaml_1_0_0",
160                 serviceTemplate.getToscaDefinitionsVersion());
161
162         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
163                 .getPolicies().getConceptMap();
164
165         // Check policies
166         assertTrue(policiesConceptMap.size() == 1);
167         assertEquals("onap.restart.tca", policiesConceptMap.keySet().iterator().next().getName());
168         assertEquals("onap.restart.tca:1.0.0",
169                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
170
171         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
172
173         // Check metadata
174         assertTrue(policyVal.getMetadata().size() == 2);
175         assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
176         assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
177
178         // Check properties
179         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
180         assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
181         assertNotNull(policyVal.getProperties().values().iterator().next());
182     }
183
184     private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
185
186         // Sanity check the entire structure
187         assertNotNull(serviceTemplate);
188         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
189         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
190
191         // Check tosca_definitions_version
192         assertEquals("tosca_simple_yaml_1_0_0",
193                 serviceTemplate.getToscaDefinitionsVersion());
194
195         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
196                 .getPolicies().getConceptMap();
197
198         // Check policies
199         assertTrue(policiesConceptMap.size() == 1);
200         assertEquals("onap.scaleout.tca", policiesConceptMap.keySet().iterator().next().getName());
201         assertEquals("onap.scaleout.tca:1.0.0",
202                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.scaleout.tca").getId());
203
204         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
205
206         // Check metadata
207         assertTrue(policyVal.getMetadata().size() == 2);
208         assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
209         assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
210
211         // Check properties
212         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
213         assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
214         assertNotNull(policyVal.getProperties().values().iterator().next());
215     }
216
217     private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
218
219         // Sanity check the entire structure
220         assertNotNull(serviceTemplate);
221         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
222         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
223
224         // Check tosca_definitions_version
225         assertEquals("tosca_simple_yaml_1_0_0",
226                 serviceTemplate.getToscaDefinitionsVersion());
227
228         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
229                 .getPolicies().getConceptMap();
230
231         // Check policies
232         assertTrue(policiesConceptMap.size() == 1);
233         assertEquals("onap.vfirewall.tca", policiesConceptMap.keySet().iterator().next().getName());
234         assertEquals("onap.vfirewall.tca:1.0.0",
235                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vfirewall.tca").getId());
236
237         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
238
239         // Check metadata
240         assertTrue(policyVal.getMetadata().size() == 2);
241         assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey());
242         assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue());
243
244         // Check properties
245         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
246         assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next());
247         assertNotNull(policyVal.getProperties().values().iterator().next());
248     }
249
250     private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
251
252         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
253         assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
254                 .getAsString());
255         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template")
256                 .getAsJsonObject();
257         JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
258         assertTrue(policiesJsonArray.size() == 1);
259         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
260         assertNotNull(policy.get("onap.restart.tca"));
261         JsonObject policyVal = policy.get("onap.restart.tca").getAsJsonObject();
262         assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
263         assertEquals("1.0.0", policyVal.get("version").getAsString());
264         assertEquals("onap.restart.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
265                 .getAsString());
266         JsonObject properties = policyVal.get("properties").getAsJsonObject();
267         assertNotNull(properties.get("tca_policy"));
268     }
269
270     private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
271
272         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
273         assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
274                 .getAsString());
275         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
276         JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
277         assertTrue(policiesJsonArray.size() == 1);
278         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
279         assertNotNull(policy.get("onap.scaleout.tca"));
280         JsonObject policyVal = policy.get("onap.scaleout.tca").getAsJsonObject();
281         assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
282         assertEquals("1.0.0", policyVal.get("version").getAsString());
283         assertEquals("onap.scaleout.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
284                 .getAsString());
285         JsonObject properties = policyVal.get("properties").getAsJsonObject();
286         assertNotNull(properties.get("tca_policy"));
287     }
288
289     private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
290
291         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
292         assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version")
293                 .getAsString());
294         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject();
295         JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray();
296         assertTrue(policiesJsonArray.size() == 1);
297         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
298         assertNotNull(policy.get("onap.vfirewall.tca"));
299         JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject();
300         assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
301         assertEquals("1.0.0", policyVal.get("version").getAsString());
302         assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
303                 .getAsString());
304         JsonObject properties = policyVal.get("properties").getAsJsonObject();
305         assertNotNull(properties.get("tca_policy"));
306     }
307 }