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