Fix simultaneous write access to policy provider
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyToscaPersistenceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import java.net.URISyntaxException;
28 import java.util.Base64;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
35
36 import lombok.NonNull;
37
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.provider.PolicyModelsProvider;
47 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
48 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * Test persistence of monitoring policies to and from the database.
58  *
59  * @author Liam Fallon (liam.fallon@est.tech)
60  */
61 public class PolicyToscaPersistenceTest {
62     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyToscaPersistenceTest.class);
63
64     private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
65     private StandardCoder standardCoder = new StandardCoder();
66
67     private PolicyModelsProvider databaseProvider;
68
69     /**
70      * Initialize provider.
71      *
72      * @throws PfModelException on exceptions in the tests
73      * @throws CoderException on JSON encoding and decoding errors
74      */
75     @Before
76     public void setupParameters() throws Exception {
77         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
78
79         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
80         parameters.setDatabaseDriver("org.h2.Driver");
81         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
82         parameters.setDatabaseUser("policy");
83         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
84         parameters.setPersistenceUnit("ToscaConceptTest");
85
86         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
87
88         createPolicyTypes();
89     }
90
91     @After
92     public void teardown() throws Exception {
93         databaseProvider.close();
94     }
95
96     @Test
97     public void testToscaPolicyPersistence() throws Exception {
98         Set<String> policyResources = ResourceUtils.getDirectoryContents("policies");
99
100         for (String policyResource : policyResources) {
101             if (!policyResource.contains("\\.tosca\\.")) {
102                 continue;
103             }
104
105             String policyString = ResourceUtils.getResourceAsString(policyResource);
106
107             if (policyResource.endsWith("yaml")) {
108                 testPolicyPersistence(yamlJsonTranslator.fromYaml(policyString, ToscaServiceTemplate.class));
109             } else {
110                 testPolicyPersistence(standardCoder.decode(policyString, ToscaServiceTemplate.class));
111             }
112         }
113     }
114
115     @Test
116     public void testHpaPolicyTypeGet() throws PfModelException {
117         long getStartTime = System.currentTimeMillis();
118         ToscaServiceTemplate hpaServiceTemplate =
119             databaseProvider.getPolicyTypes("onap.policies.optimization.resource.HpaPolicy", "1.0.0");
120         LOGGER.trace("HPA policy normal get time (ms): {}", System.currentTimeMillis() - getStartTime);
121
122         assertEquals(3, hpaServiceTemplate.getPolicyTypesAsMap().size());
123         assertEquals(5, hpaServiceTemplate.getDataTypesAsMap().size());
124
125         getStartTime = System.currentTimeMillis();
126         ToscaPolicyTypeFilter hpaFilter = ToscaPolicyTypeFilter.builder()
127             .name("onap.policies.optimization.resource.HpaPolicy").version("1.0.0").build();
128         hpaServiceTemplate = databaseProvider.getFilteredPolicyTypes(hpaFilter);
129         LOGGER.trace("HPA policy filter name version get time (ms): {}", System.currentTimeMillis() - getStartTime);
130
131         assertEquals(3, hpaServiceTemplate.getPolicyTypesAsMap().size());
132         assertEquals(5, hpaServiceTemplate.getDataTypesAsMap().size());
133
134         getStartTime = System.currentTimeMillis();
135         hpaFilter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.resource.HpaPolicy").build();
136         hpaServiceTemplate = databaseProvider.getFilteredPolicyTypes(hpaFilter);
137         LOGGER.trace("HPA policy filter name only get time (ms): {}", System.currentTimeMillis() - getStartTime);
138
139         assertEquals(3, hpaServiceTemplate.getPolicyTypesAsMap().size());
140         assertEquals(5, hpaServiceTemplate.getDataTypesAsMap().size());
141     }
142
143     @Test
144     public void testNamingPolicyGet() throws PfModelException {
145         String policyYamlString = ResourceUtils.getResourceAsString("policies/sdnc.policy.naming.input.tosca.yaml");
146         ToscaServiceTemplate serviceTemplate =
147             yamlJsonTranslator.fromYaml(policyYamlString, ToscaServiceTemplate.class);
148
149         long createStartTime = System.currentTimeMillis();
150         databaseProvider.createPolicies(serviceTemplate);
151         LOGGER.trace("Naming policy create time (ms): {}", System.currentTimeMillis() - createStartTime);
152
153         long getStartTime = System.currentTimeMillis();
154         ToscaServiceTemplate namingServiceTemplate =
155             databaseProvider.getPolicies("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", "1.0.0");
156         LOGGER.trace("Naming policy normal get time (ms): {}", System.currentTimeMillis() - getStartTime);
157
158         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
159         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
160         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
161
162         getStartTime = System.currentTimeMillis();
163         ToscaPolicyFilter filter =
164             ToscaPolicyFilter.builder().name("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP").version("1.0.0").build();
165         namingServiceTemplate = databaseProvider.getFilteredPolicies(filter);
166         LOGGER.trace("Naming policy filtered get time (ms): {}", System.currentTimeMillis() - getStartTime);
167
168         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
169         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
170         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
171
172         getStartTime = System.currentTimeMillis();
173         filter = ToscaPolicyFilter.builder().name("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP").build();
174         namingServiceTemplate = databaseProvider.getFilteredPolicies(filter);
175         LOGGER.trace("Naming policy filtered name only get time (ms): {}", System.currentTimeMillis() - getStartTime);
176
177         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
178         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
179         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
180
181         long deleteStartTime = System.currentTimeMillis();
182         databaseProvider.deletePolicy("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", "1.0.0");
183         LOGGER.trace("Naming policy delete time (ms): {}", System.currentTimeMillis() - deleteStartTime);
184     }
185
186     @Test
187     public void testNamingPolicyVersions() throws PfModelException {
188         String policyYamlString = ResourceUtils.getResourceAsString("policies/sdnc.policy.naming.input.tosca.yaml");
189         ToscaServiceTemplate serviceTemplate =
190             yamlJsonTranslator.fromYaml(policyYamlString, ToscaServiceTemplate.class);
191
192         // Create policy types and data types
193         List<Map<String, ToscaPolicy>> policyMapList = serviceTemplate.getToscaTopologyTemplate().getPolicies();
194         databaseProvider.createPolicies(serviceTemplate);
195
196         // Clear the policy map list so we start from afresh with versions
197         ToscaPolicy namingPolicy = policyMapList.get(0).values().iterator().next();
198         policyMapList.clear();
199
200         // Create 21 more versions of the policy
201         for (int i = 2; i < 22; i++) {
202             ToscaPolicy clonedNamingPolicy = new ToscaPolicy(namingPolicy);
203             clonedNamingPolicy.setVersion(i + ".0.0");
204             Map<String, ToscaPolicy> policyMap = new LinkedHashMap<>(1);
205             policyMap.put(clonedNamingPolicy.getName(), clonedNamingPolicy);
206             policyMapList.add(policyMap);
207         }
208
209         databaseProvider.createPolicies(serviceTemplate);
210
211         for (int i = 1; i < 22; i++) {
212             ToscaServiceTemplate namingServiceTemplate =
213                 databaseProvider.getPolicies("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", i + ".0.0");
214             assertEquals(i + ".0.0", namingServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).values()
215                 .iterator().next().getVersion());
216
217             ToscaPolicyFilter policyFilter =
218                 ToscaPolicyFilter.builder().name("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP").version(i + ".0.0").build();
219             namingServiceTemplate = databaseProvider.getFilteredPolicies(policyFilter);
220             assertEquals(i + ".0.0", namingServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).values()
221                 .iterator().next().getVersion());
222         }
223
224         ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP")
225             .version(ToscaPolicyFilter.LATEST_VERSION).build();
226         ToscaServiceTemplate namingServiceTemplate = databaseProvider.getFilteredPolicies(policyFilter);
227         assertEquals("21.0.0", namingServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).values().iterator()
228             .next().getVersion());
229
230         for (int i = 1; i < 22; i++) {
231             databaseProvider.deletePolicy("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", i + ".0.0");
232         }
233     }
234
235     /**
236      * Check persistence of a policy.
237      *
238      * @param serviceTemplate the service template containing the policy
239      * @throws Exception any exception thrown
240      */
241     public void testPolicyPersistence(@NonNull final ToscaServiceTemplate serviceTemplate) throws Exception {
242         assertNotNull(serviceTemplate);
243
244         CountDownLatch threadCountDownLatch = new CountDownLatch(10);
245
246         for (int i = 0; i < 10; i++) {
247             new Thread() {
248                 public void run() {
249                     assertThatCode(() -> databaseProvider.createPolicies(serviceTemplate)).doesNotThrowAnyException();
250                     assertThatCode(() -> databaseProvider.updatePolicies(serviceTemplate)).doesNotThrowAnyException();
251                     threadCountDownLatch.countDown();
252                 }
253             }.start();
254         }
255
256         threadCountDownLatch.await(10, TimeUnit.SECONDS);
257
258         for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
259             for (ToscaPolicy policy : policyMap.values()) {
260                 ToscaServiceTemplate gotToscaServiceTemplate =
261                     databaseProvider.getPolicies(policy.getName(), policy.getVersion());
262
263                 assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
264                     .get(policy.getName()).getType());
265
266                 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build());
267
268                 assertEquals(policy.getType(),
269                     getToscaPolicyFromMapList(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies(),
270                         policy.getName()).getType());
271
272                 gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(
273                     ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build());
274
275                 assertEquals(policy.getType(), gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
276                     .get(policy.getName()).getType());
277             }
278         }
279     }
280
281     private ToscaPolicy getToscaPolicyFromMapList(List<Map<String, ToscaPolicy>> toscaPolicyMapList,
282         String policyName) {
283         ToscaPolicy toscaPolicy = new ToscaPolicy();
284         for (Map<String, ToscaPolicy> policyMap : toscaPolicyMapList) {
285             toscaPolicy = policyMap.get(policyName);
286             if (toscaPolicy != null) {
287                 break;
288             }
289         }
290         return toscaPolicy;
291     }
292
293     private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
294         Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
295
296         for (String policyTypeResource : policyTypeResources) {
297             String policyTypeYamlString = ResourceUtils.getResourceAsString(policyTypeResource);
298             ToscaServiceTemplate toscaServiceTemplatePolicyType =
299                 yamlJsonTranslator.fromYaml(policyTypeYamlString, ToscaServiceTemplate.class);
300
301             assertNotNull(toscaServiceTemplatePolicyType);
302             databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
303         }
304     }
305 }