2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.xacml.pdp.application.naming;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import com.att.research.xacml.api.Response;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Iterator;
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.FixMethodOrder;
46 import org.junit.Test;
47 import org.junit.rules.TemporaryFolder;
48 import org.junit.runners.MethodSorters;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.common.utils.resources.TextFileUtils;
53 import org.onap.policy.models.decisions.concepts.DecisionRequest;
54 import org.onap.policy.models.decisions.concepts.DecisionResponse;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
57 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
58 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
59 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
64 public class NamingPdpApplicationTest {
65 private static final Logger LOGGER = LoggerFactory.getLogger(NamingPdpApplicationTest.class);
66 private static Properties properties = new Properties();
67 private static File propertiesFile;
68 private static XacmlApplicationServiceProvider service;
69 private static StandardCoder gson = new StandardCoder();
70 private static DecisionRequest baseRequest;
73 public static final TemporaryFolder policyFolder = new TemporaryFolder();
76 * Copies the xacml.properties and policies files into
77 * temporary folder and loads the service provider saving
78 * instance of provider off for other tests to use.
81 public static void setUp() throws Exception {
83 // Load Single Decision Request
85 baseRequest = gson.decode(
88 "src/test/resources/decision.naming.input.json"),
89 DecisionRequest.class);
91 // Setup our temporary folder
93 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
94 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
95 properties, myCreator);
97 // Copy the test policy types into data area
99 String policy = "onap.policies.Naming";
100 String policyType = ResourceUtils.getResourceAsString("policytypes/" + policy + ".yaml");
101 LOGGER.info("Copying {}", policyType);
102 Files.write(Paths.get(policyFolder.getRoot().getAbsolutePath(), policy + "-1.0.0.yaml"),
103 policyType.getBytes());
107 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
108 ServiceLoader.load(XacmlApplicationServiceProvider.class);
110 // Iterate through Xacml application services and find
111 // the optimization service. Save it for use throughout
112 // all the Junit tests.
114 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
115 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
116 while (iterator.hasNext()) {
117 XacmlApplicationServiceProvider application = iterator.next();
119 // Is it our service?
121 if (application instanceof NamingPdpApplication) {
123 // Should be the first and only one
125 assertThat(service).isNull();
126 service = application;
128 strDump.append(application.applicationName());
129 strDump.append(" supports ");
130 strDump.append(application.supportedPolicyTypes());
131 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
133 LOGGER.debug("{}", strDump);
134 assertThat(service).isNotNull();
136 // Tell it to initialize based on the properties file
137 // we just built for it.
139 service.initialize(propertiesFile.toPath().getParent(), null);
143 public void test01Basics() {
145 // Make sure there's an application name
147 assertThat(service.applicationName()).isNotEmpty();
151 assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
152 assertThat(service.actionDecisionsSupported()).contains("naming");
154 // Ensure it has the supported policy types and
155 // can support the correct policy types.
157 assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
158 "onap.policies.Naming", "1.0.0"))).isTrue();
159 assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
160 "onap.foobar", "1.0.0"))).isFalse();
164 public void test02NoPolicies() throws CoderException {
166 // Ask for a decision when there are no policies loaded
168 LOGGER.info("Request {}", gson.encode(baseRequest));
169 Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
170 LOGGER.info("Decision {}", decision.getKey());
172 assertThat(decision.getKey()).isNotNull();
173 assertThat(decision.getKey().getPolicies()).isEmpty();
175 // Naming applications should not have this information returned
177 assertThat(decision.getKey().getAdvice()).isNull();
178 assertThat(decision.getKey().getObligations()).isNull();
179 assertThat(decision.getKey().getAttributes()).isNull();
183 public void test03Naming() throws CoderException, FileNotFoundException, IOException,
184 XacmlApplicationException {
186 // Now load all the optimization policies
188 TestUtils.loadPolicies("policies/sdnc.policy.naming.input.tosca.yaml", service);
190 // Ask for a decision for available default policies
192 DecisionResponse response = makeDecision();
194 // There is no default policy
196 assertThat(response).isNotNull();
197 assertThat(response.getPolicies()).isEmpty();
201 baseRequest.getResource().put("policy-type", Arrays.asList("onap.policies.Naming"));
203 // Ask for a decision for VNF default policies
205 response = makeDecision();
206 assertThat(response).isNotNull();
207 assertThat(response.getPolicies()).hasSize(1);
209 // Naming applications should not have this information returned
211 assertThat(response.getAdvice()).isNull();
212 assertThat(response.getObligations()).isNull();
213 assertThat(response.getAttributes()).isNull();
217 validateDecision(response, baseRequest);
220 private DecisionResponse makeDecision() {
221 Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
222 LOGGER.info("Request Resources {}", baseRequest.getResource());
223 LOGGER.info("Decision {}", decision.getKey());
224 for (Entry<String, Object> entrySet : decision.getKey().getPolicies().entrySet()) {
225 LOGGER.info("Policy {}", entrySet.getKey());
227 return decision.getKey();
230 @SuppressWarnings("unchecked")
231 private void validateDecision(DecisionResponse decision, DecisionRequest request) {
232 for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
233 LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
234 assertThat(entrySet.getValue()).isInstanceOf(Map.class);
235 Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
236 assertThat(policyContents).containsKey("properties");
237 assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
238 Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
240 validateMatchable((Collection<String>) request.getResource().get("nfRole"),
241 (Collection<String>) policyProperties.get("nfRole"));
243 validateMatchable((Collection<String>) request.getResource().get("naming-type"),
244 (Collection<String>) policyProperties.get("naming-type"));
246 validateMatchable((Collection<String>) request.getResource().get("property-name"),
247 (Collection<String>) policyProperties.get("property-name"));
251 private void validateMatchable(Collection<String> requestList, Collection<String> policyProperties) {
252 LOGGER.info("Validating matchable: {} with {}", policyProperties, requestList);
254 // Null or empty implies '*' - that is any value is acceptable
257 if (policyProperties == null || policyProperties.isEmpty()) {
260 Condition<String> condition = new Condition<>(
261 requestList::contains,
262 "Request list is contained");
263 assertThat(policyProperties).haveAtLeast(1, condition);