Fix issues in xacml-pdp for new sonar rules
[policy/xacml-pdp.git] / applications / optimization / src / test / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationPdpApplicationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6    Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.xacml.pdp.application.optimization;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import com.att.research.xacml.api.Response;
32 import com.google.common.collect.Lists;
33 import java.io.File;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Collection;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 import java.util.Properties;
42 import java.util.ServiceLoader;
43 import org.apache.commons.lang3.tuple.Pair;
44 import org.assertj.core.api.Condition;
45 import org.junit.BeforeClass;
46 import org.junit.ClassRule;
47 import org.junit.FixMethodOrder;
48 import org.junit.Test;
49 import org.junit.rules.TemporaryFolder;
50 import org.junit.runners.MethodSorters;
51 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.common.utils.coder.StandardYamlCoder;
55 import org.onap.policy.common.utils.resources.ResourceUtils;
56 import org.onap.policy.common.utils.resources.TextFileUtils;
57 import org.onap.policy.models.decisions.concepts.DecisionRequest;
58 import org.onap.policy.models.decisions.concepts.DecisionResponse;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
61 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
62 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
63 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
64 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
65 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
66 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69
70 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
71 public class OptimizationPdpApplicationTest {
72
73     private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTest.class);
74     private static Properties properties = new Properties();
75     private static File propertiesFile;
76     private static XacmlApplicationServiceProvider service;
77     private static StandardCoder gson = new StandardCoder();
78     private static DecisionRequest baseRequest;
79     private static RestServerParameters clientParams;
80     private static String[] listPolicyTypeFiles = {
81         "onap.policies.Optimization",
82         "onap.policies.optimization.Resource",
83         "onap.policies.optimization.Service",
84         "onap.policies.optimization.resource.AffinityPolicy",
85         "onap.policies.optimization.resource.DistancePolicy",
86         "onap.policies.optimization.resource.HpaPolicy",
87         "onap.policies.optimization.resource.OptimizationPolicy",
88         "onap.policies.optimization.resource.PciPolicy",
89         "onap.policies.optimization.service.QueryPolicy",
90         "onap.policies.optimization.service.SubscriberPolicy",
91         "onap.policies.optimization.resource.Vim_fit",
92         "onap.policies.optimization.resource.VnfPolicy"};
93
94     @ClassRule
95     public static final TemporaryFolder policyFolder = new TemporaryFolder();
96
97     /**
98      * Copies the xacml.properties and policies files into
99      * temporary folder and loads the service provider saving
100      * instance of provider off for other tests to use.
101      */
102     @BeforeClass
103     public static void setUp() throws Exception {
104         clientParams = mock(RestServerParameters.class);
105         when(clientParams.getHost()).thenReturn("localhost");
106         when(clientParams.getPort()).thenReturn(6969);
107         //
108         // Load Single Decision Request
109         //
110         baseRequest = gson.decode(
111                 TextFileUtils
112                     .getTextFileAsString(
113                             "src/test/resources/decision.optimization.input.json"),
114                     DecisionRequest.class);
115         //
116         // Setup our temporary folder
117         //
118         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
119         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
120                 properties, myCreator);
121         //
122         // Copy the test policy types into data area
123         //
124         for (String policy : listPolicyTypeFiles) {
125             String policyType = ResourceUtils.getResourceAsString("policytypes/" + policy + ".yaml");
126             LOGGER.info("Copying {}", policyType);
127             Files.write(Paths.get(policyFolder.getRoot().getAbsolutePath(), policy + "-1.0.0.yaml"),
128                     policyType.getBytes());
129         }
130         //
131         // Load service
132         //
133         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
134                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
135         //
136         // Iterate through Xacml application services and find
137         // the optimization service. Save it for use throughout
138         // all the Junit tests.
139         //
140         StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
141         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
142         while (iterator.hasNext()) {
143             XacmlApplicationServiceProvider application = iterator.next();
144             //
145             // Is it our service?
146             //
147             if (application instanceof OptimizationPdpApplication) {
148                 //
149                 // Should be the first and only one
150                 //
151                 assertThat(service).isNull();
152                 service = application;
153             }
154             strDump.append(application.applicationName());
155             strDump.append(" supports ");
156             strDump.append(application.supportedPolicyTypes());
157             strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
158         }
159         LOGGER.debug("{}", strDump);
160         assertThat(service).isNotNull();
161         //
162         // Tell it to initialize based on the properties file
163         // we just built for it.
164         //
165         service.initialize(propertiesFile.toPath().getParent(), clientParams);
166     }
167
168     /**
169      * Simply test some of the simple methods for the application.
170      */
171     @Test
172     public void test01Basics() {
173         //
174         // Make sure there's an application name
175         //
176         assertThat(service.applicationName()).isNotEmpty();
177         //
178         // Does it return the correct decisions
179         //
180         assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
181         assertThat(service.actionDecisionsSupported()).contains("optimize");
182         //
183         // Ensure it has the supported policy types and
184         // can support the correct policy types.
185         //
186         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
187                 "onap.policies.optimization.resource.AffinityPolicy", "1.0.0"))).isTrue();
188         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
189                 "onap.policies.optimization.service.SubscriberPolicy", "1.0.0"))).isTrue();
190         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
191                 "onap.policies.optimization.service.CustomUseCase", "1.0.0"))).isTrue();
192         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
193                 "onap.foobar", "1.0.0"))).isFalse();
194     }
195
196     /**
197      * With no policies loaded, there should be 0 policies returned.
198      *
199      * @throws CoderException CoderException
200      */
201     @Test
202     public void test02NoPolicies() throws CoderException {
203         //
204         // Ask for a decision when there are no policies loaded
205         //
206         LOGGER.info("Request {}", gson.encode(baseRequest));
207         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
208         LOGGER.info("Decision {}", decision.getKey());
209
210         assertThat(decision.getKey()).isNotNull();
211         assertThat(decision.getKey().getPolicies()).isEmpty();
212     }
213
214     /**
215      * Should return ONLY default policies.
216      *
217      * @throws XacmlApplicationException could not load policies
218      */
219     @Test
220     public void test03OptimizationDefault() throws XacmlApplicationException {
221         //
222         // Now load all the optimization policies
223         //
224         List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/test-optimization-policies.yaml",
225                 service);
226         assertThat(loadedPolicies).isNotNull().hasSize(14);
227         //
228         // Ask for a decision for available default policies
229         //
230         DecisionResponse response = makeDecision();
231
232         assertThat(response).isNotNull();
233         assertThat(response.getPolicies()).hasSize(2);
234         //
235         // Validate it
236         //
237         validateDecision(response, baseRequest);
238     }
239
240     /**
241      * Should only return default HPA policy type.
242      */
243     @SuppressWarnings("unchecked")
244     @Test
245     public void test04OptimizationDefaultHpa() {
246         //
247         // Add in policy type
248         //
249         List<String> policyTypes = Lists.newArrayList("onap.policies.optimization.resource.HpaPolicy");
250         baseRequest.getResource().put("policy-type", policyTypes);
251         //
252         // Ask for a decision for default HPA policy
253         //
254         DecisionResponse response = makeDecision();
255
256         assertThat(response).isNotNull();
257         assertThat(response.getPolicies()).hasSize(1);
258         response.getPolicies().forEach((key, value) -> {
259             assertThat(((Map<String, Object>) value)).containsEntry("type",
260                             "onap.policies.optimization.resource.HpaPolicy");
261         });
262         //
263         // Validate it
264         //
265         validateDecision(response, baseRequest);
266     }
267
268     /**
269      * Refine for US only policies.
270      */
271     @SuppressWarnings("unchecked")
272     @Test
273     public void test05OptimizationDefaultGeography() throws CoderException {
274         //
275         // Remove all the policy-type resources from the request
276         //
277         cleanOutResources();
278         //
279         // Add US to the geography list
280         //
281         ((List<String>) baseRequest.getResource().get("geography")).add("US");
282         //
283         // Ask for a decision for default US Policy
284         //
285         DecisionResponse response = makeDecision();
286         assertThat(response).isNotNull();
287         assertThat(response.getPolicies()).hasSize(2);
288         //
289         // Validate it
290         //
291         validateDecision(response, baseRequest);
292     }
293
294     /**
295      * Add more refinement for service.
296      */
297     @SuppressWarnings("unchecked")
298     @Test
299     public void test06OptimizationDefaultGeographyAndService() {
300         //
301         // Add vCPE to the service list
302         //
303         ((List<String>) baseRequest.getResource().get("services")).add("vCPE");
304         //
305         // Ask for a decision for default US policy for vCPE service
306         //
307         DecisionResponse response = makeDecision();
308
309         assertThat(response).isNotNull();
310         assertThat(response.getPolicies()).hasSize(3);
311         //
312         // Validate it
313         //
314         validateDecision(response, baseRequest);
315     }
316
317     /**
318      * Add more refinement for specific resource.
319      */
320     @SuppressWarnings("unchecked")
321     @Test
322     public void test07OptimizationDefaultGeographyAndServiceAndResource() {
323         //
324         // Add vG to the resource list
325         //
326         ((List<String>) baseRequest.getResource().get("resources")).add("vG");
327         //
328         // Ask for a decision for default US service vCPE resource vG policy
329         //
330         DecisionResponse response = makeDecision();
331
332         assertThat(response).isNotNull();
333         assertThat(response.getPolicies()).hasSize(6);
334         //
335         // Validate it
336         //
337         validateDecision(response, baseRequest);
338     }
339
340     /**
341      * Now we need to add in subscriberName in order to get scope for gold.
342      */
343     @SuppressWarnings("unchecked")
344     @Test
345     public void test08OptimizationGeographyAndServiceAndResourceAndScopeIsGoldSubscriber() {
346         //
347         // Add gold as a scope
348         //
349         ((List<String>) baseRequest.getContext().get("subscriberName")).add("subscriber_a");
350         //
351         // Ask for a decision for specific US vCPE vG gold
352         //
353         DecisionResponse response = makeDecision();
354
355         assertThat(response).isNotNull();
356         assertThat(response.getPolicies()).hasSize(6);
357         assertThat(response.getAdvice()).hasSize(2);
358         //
359         // Validate it
360         //
361         validateDecision(response, baseRequest);
362     }
363
364     /**
365      * Add a subscriber that should be platinum.
366      */
367     @SuppressWarnings("unchecked")
368     @Test
369     public void test09OptimizationGeographyAndServiceAndResourceAndScopeGoldOrPlatinumSubscriber() {
370         //
371         // Add platinum to the scope list: this is now gold OR platinum
372         //
373         ((List<String>) baseRequest.getResource().get("scope")).remove("gold");
374         ((List<String>) baseRequest.getContext().get("subscriberName")).add("subscriber_x");
375         //
376         // Ask for a decision for specific US vCPE vG (gold or platinum)
377         //
378         DecisionResponse response = makeDecision();
379
380         assertThat(response).isNotNull();
381         assertThat(response.getPolicies()).hasSize(8);
382         assertThat(response.getAdvice()).hasSize(2);
383         //
384         // Validate it
385         //
386         validateDecision(response, baseRequest);
387     }
388
389     /**
390      * Remove gold subscriber, keep the platinum one.
391      */
392     @SuppressWarnings("unchecked")
393     @Test
394     public void test10OptimizationGeographyAndServiceAndResourceAndScopeNotGoldStillPlatinum() {
395         //
396         // Add gold as a scope
397         //
398         ((List<String>) baseRequest.getResource().get("scope")).remove("gold");
399         ((List<String>) baseRequest.getResource().get("scope")).remove("platinum");
400         ((List<String>) baseRequest.getContext().get("subscriberName")).remove("subscriber_a");
401         //
402         // Ask for a decision for specific US vCPE vG gold
403         //
404         DecisionResponse response = makeDecision();
405
406         assertThat(response).isNotNull();
407         assertThat(response.getPolicies()).hasSize(7);
408         //
409         // Validate it
410         //
411         validateDecision(response, baseRequest);
412     }
413
414     /**
415      * Filter by Affinity policy.
416      */
417     @Test
418     public void test11OptimizationPolicyTypeDefault() {
419         //
420         // Add in policy type
421         //
422         List<String> policyTypes = Lists.newArrayList("onap.policies.optimization.resource.AffinityPolicy");
423         baseRequest.getResource().put("policy-type", policyTypes);
424         //
425         // Ask for a decision for default
426         //
427         DecisionResponse response = makeDecision();
428
429         assertThat(response).isNotNull();
430         assertThat(response.getPolicies()).hasSize(1);
431         //
432         // Validate it
433         //
434         validateDecision(response, baseRequest);
435     }
436
437     /**
438      * Now filter by HPA policy type.
439      */
440     @SuppressWarnings("unchecked")
441     @Test
442     public void test12OptimizationPolicyTypeDefault() {
443         //
444         // Add in another policy type
445         //
446         ((List<String>) baseRequest.getResource().get("policy-type"))
447             .add("onap.policies.optimization.resource.HpaPolicy");
448         //
449         // Ask for a decision for default
450         //
451         DecisionResponse response = makeDecision();
452
453         assertThat(response).isNotNull();
454         assertThat(response.getPolicies()).hasSize(2);
455         //
456         // Validate it
457         //
458         validateDecision(response, baseRequest);
459     }
460
461     @Test
462     public void test999BadSubscriberPolicies() throws Exception {
463         final StandardYamlCoder yamlCoder = new StandardYamlCoder();
464         //
465         // Decode it
466         //
467         String policyYaml = ResourceUtils.getResourceAsString("src/test/resources/bad-subscriber-policies.yaml");
468         //
469         // Serialize it into a class
470         //
471         ToscaServiceTemplate serviceTemplate;
472         try {
473             serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
474         } catch (CoderException e) {
475             throw new XacmlApplicationException("Failed to decode policy from resource file", e);
476         }
477         //
478         // Make sure all the fields are setup properly
479         //
480         JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
481         jtst.fromAuthorative(serviceTemplate);
482         ToscaServiceTemplate completedJtst = jtst.toAuthorative();
483         //
484         // Get the policies
485         //
486         for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
487             for (ToscaPolicy policy : policies.values()) {
488                 if ("missing-subscriberProperties".equals(policy.getName())) {
489                     assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() ->
490                         service.loadPolicy(policy));
491                 } else if ("missing-subscriberName".equals(policy.getName())) {
492                     assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() ->
493                         service.loadPolicy(policy));
494                 } else if ("missing-subscriberRole".equals(policy.getName())) {
495                     assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() ->
496                         service.loadPolicy(policy));
497                 }
498             }
499         }
500     }
501
502     private DecisionResponse makeDecision() {
503         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
504         LOGGER.info("Request Resources {}", baseRequest.getResource());
505         LOGGER.info("Decision {}", decision.getKey());
506         for (Entry<String, Object> entrySet : decision.getKey().getPolicies().entrySet()) {
507             LOGGER.info("Policy {}", entrySet.getKey());
508         }
509         return decision.getKey();
510     }
511
512     @SuppressWarnings("unchecked")
513     private void validateDecision(DecisionResponse decision, DecisionRequest request) {
514         for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
515             LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
516             assertThat(entrySet.getValue()).isInstanceOf(Map.class);
517             Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
518             assertThat(policyContents).containsKey("properties");
519             assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
520             Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
521
522             validateMatchable((Collection<String>) request.getResource().get("scope"),
523                     (Collection<String>) policyProperties.get("scope"));
524
525             validateMatchable((Collection<String>) request.getResource().get("services"),
526                     (Collection<String>) policyProperties.get("services"));
527
528             validateMatchable((Collection<String>) request.getResource().get("resources"),
529                     (Collection<String>) policyProperties.get("resources"));
530
531             validateMatchable((Collection<String>) request.getResource().get("geography"),
532                     (Collection<String>) policyProperties.get("geography"));
533         }
534     }
535
536     private void validateMatchable(Collection<String> requestList, Collection<String> policyProperties) {
537         LOGGER.info("Validating matchable: {} with {}", policyProperties, requestList);
538         //
539         // Null or empty implies '*' - that is any value is acceptable
540         // for this policy.
541         //
542         if (policyProperties == null || policyProperties.isEmpty()) {
543             return;
544         }
545         Condition<String> condition = new Condition<>(
546                 requestList::contains,
547                 "Request list is contained");
548         assertThat(policyProperties).haveAtLeast(1, condition);
549
550     }
551
552     @SuppressWarnings("unchecked")
553     private void cleanOutResources() {
554         ((List<String>) baseRequest.getResource().get("scope")).clear();
555         ((List<String>) baseRequest.getResource().get("services")).clear();
556         ((List<String>) baseRequest.getResource().get("resources")).clear();
557         ((List<String>) baseRequest.getResource().get("geography")).clear();
558         if (((List<String>) baseRequest.getResource().get("policy-type")) != null) {
559             baseRequest.getResource().remove("policy-type");
560         }
561     }
562 }