02884b63b42a5207d35e456cb9f3ec085d0106f4
[policy/xacml-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.naming;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import com.att.research.xacml.api.Response;
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Collection;
36 import java.util.Iterator;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import java.util.Properties;
40 import java.util.ServiceLoader;
41 import org.apache.commons.lang3.tuple.Pair;
42 import org.assertj.core.api.Condition;
43 import org.junit.BeforeClass;
44 import org.junit.ClassRule;
45 import org.junit.Test;
46 import org.junit.rules.TemporaryFolder;
47 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.common.utils.resources.TextFileUtils;
52 import org.onap.policy.models.decisions.concepts.DecisionRequest;
53 import org.onap.policy.models.decisions.concepts.DecisionResponse;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
55 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
56 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
57 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
58 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 public class NamingPdpApplicationTest {
63     private static final Logger LOGGER = LoggerFactory.getLogger(NamingPdpApplicationTest.class);
64     private static Properties properties = new Properties();
65     private static File propertiesFile;
66     private static XacmlApplicationServiceProvider service;
67     private static StandardCoder gson = new StandardCoder();
68     private static DecisionRequest baseRequest;
69     private static RestServerParameters clientParams;
70
71     @ClassRule
72     public static final TemporaryFolder policyFolder = new TemporaryFolder();
73
74     /**
75      * Copies the xacml.properties and policies files into
76      * temporary folder and loads the service provider saving
77      * instance of provider off for other tests to use.
78      */
79     @BeforeClass
80     public static void setUp() throws Exception {
81         clientParams = mock(RestServerParameters.class);
82         when(clientParams.getHost()).thenReturn("localhost");
83         when(clientParams.getPort()).thenReturn(6969);
84         //
85         // Load Single Decision Request
86         //
87         baseRequest = gson.decode(
88                 TextFileUtils
89                     .getTextFileAsString(
90                             "src/test/resources/decision.naming.input.json"),
91                     DecisionRequest.class);
92         //
93         // Setup our temporary folder
94         //
95         // TODO use lambda policyFolder::newFile
96         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
97         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
98                 properties, myCreator);
99         //
100         // Copy the test policy types into data area
101         //
102         String policy = "onap.policies.Naming";
103         String policyType = ResourceUtils.getResourceAsString("policytypes/" + policy + ".yaml");
104         LOGGER.info("Copying {}", policyType);
105         // TODO investigate UTF-8
106         Files.write(Paths.get(policyFolder.getRoot().getAbsolutePath(), policy + "-1.0.0.yaml"),
107                 policyType.getBytes());
108         //
109         // Load service
110         //
111         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
112                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
113         //
114         // Iterate through Xacml application services and find
115         // the optimization service. Save it for use throughout
116         // all the Junit tests.
117         //
118         StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
119         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
120         while (iterator.hasNext()) {
121             XacmlApplicationServiceProvider application = iterator.next();
122             //
123             // Is it our service?
124             //
125             if (application instanceof NamingPdpApplication) {
126                 //
127                 // Should be the first and only one
128                 //
129                 assertThat(service).isNull();
130                 service = application;
131             }
132             strDump.append(application.applicationName());
133             strDump.append(" supports ");
134             strDump.append(application.supportedPolicyTypes());
135             strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
136         }
137         LOGGER.debug("{}", strDump);
138         assertThat(service).isNotNull();
139         //
140         // Tell it to initialize based on the properties file
141         // we just built for it.
142         //
143         service.initialize(propertiesFile.toPath().getParent(), clientParams);
144     }
145
146     @Test
147     public void test01Basics() {
148         //
149         // Make sure there's an application name
150         //
151         assertThat(service.applicationName()).isNotEmpty();
152         //
153         // Decisions
154         //
155         assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
156         assertThat(service.actionDecisionsSupported()).contains("naming");
157         //
158         // Ensure it has the supported policy types and
159         // can support the correct policy types.
160         //
161         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
162                 "onap.policies.Naming", "1.0.0"))).isTrue();
163         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
164                 "onap.foobar", "1.0.0"))).isFalse();
165     }
166
167     @Test
168     public void test02NoPolicies() throws CoderException {
169         //
170         // Ask for a decision when there are no policies loaded
171         //
172         LOGGER.info("Request {}", gson.encode(baseRequest));
173         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
174         LOGGER.info("Decision {}", decision.getKey());
175
176         assertThat(decision.getKey()).isNotNull();
177         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
178     }
179
180     @Test
181     public void test03Naming() throws CoderException, FileNotFoundException, IOException,
182         XacmlApplicationException {
183         //
184         // Now load all the optimization policies
185         //
186         TestUtils.loadPolicies("policies/sdnc.policy.naming.input.tosca.yaml", service);
187         //
188         // Ask for a decision for available default policies
189         //
190         DecisionResponse response = makeDecision();
191
192         assertThat(response).isNotNull();
193         assertThat(response.getPolicies().size()).isEqualTo(1);
194         //
195         // Validate it
196         //
197         validateDecision(response, baseRequest);
198     }
199
200     private DecisionResponse makeDecision() {
201         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
202         LOGGER.info("Request Resources {}", baseRequest.getResource());
203         LOGGER.info("Decision {}", decision.getKey());
204         for (Entry<String, Object> entrySet : decision.getKey().getPolicies().entrySet()) {
205             LOGGER.info("Policy {}", entrySet.getKey());
206         }
207         return decision.getKey();
208     }
209
210     @SuppressWarnings("unchecked")
211     private void validateDecision(DecisionResponse decision, DecisionRequest request) {
212         for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
213             LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
214             assertThat(entrySet.getValue()).isInstanceOf(Map.class);
215             Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
216             assertThat(policyContents.containsKey("properties")).isTrue();
217             assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
218             Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
219
220             validateMatchable((Collection<String>) request.getResource().get("nfRole"),
221                     (Collection<String>) policyProperties.get("nfRole"));
222
223             validateMatchable((Collection<String>) request.getResource().get("naming-type"),
224                     (Collection<String>) policyProperties.get("naming-type"));
225
226             validateMatchable((Collection<String>) request.getResource().get("property-name"),
227                     (Collection<String>) policyProperties.get("property-name"));
228         }
229     }
230
231     private void validateMatchable(Collection<String> requestList, Collection<String> policyProperties) {
232         LOGGER.info("Validating matchable: {} with {}", policyProperties, requestList);
233         //
234         // Null or empty implies '*' - that is any value is acceptable
235         // for this policy.
236         //
237         if (policyProperties == null || policyProperties.isEmpty()) {
238             return;
239         }
240         Condition<String> condition = new Condition<>(
241                 requestList::contains,
242                 "Request list is contained");
243         assertThat(policyProperties).haveAtLeast(1, condition);
244
245     }
246 }