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