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.monitoring;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import com.att.research.xacml.api.Response;
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.Properties;
36 import java.util.ServiceLoader;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.BeforeClass;
39 import org.junit.ClassRule;
40 import org.junit.FixMethodOrder;
41 import org.junit.Test;
42 import org.junit.rules.TemporaryFolder;
43 import org.junit.runners.MethodSorters;
44 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.common.utils.resources.TextFileUtils;
48 import org.onap.policy.models.decisions.concepts.DecisionRequest;
49 import org.onap.policy.models.decisions.concepts.DecisionResponse;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
53 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
54 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
55 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 public class MonitoringPdpApplicationTest {
62 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
63 private static Properties properties = new Properties();
64 private static File propertiesFile;
65 private static XacmlApplicationServiceProvider service;
66 private static DecisionRequest requestSinglePolicy;
67 private static DecisionRequest requestPolicyType;
69 private static StandardCoder gson = new StandardCoder();
70 private static BusTopicParams clientParams = new BusTopicParams();
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 requestSinglePolicy = gson.decode(
87 .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
88 DecisionRequest.class);
89 // Load Single Decision Request
91 requestPolicyType = gson.decode(
93 .getTextFileAsString("../../main/src/test/resources/decisions/decision.policytype.input.json"),
94 DecisionRequest.class);
96 // Setup our temporary folder
98 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
99 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
100 properties, myCreator);
102 // Load XacmlApplicationServiceProvider service
104 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
105 ServiceLoader.load(XacmlApplicationServiceProvider.class);
107 // Look for our class instance and save it
109 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
110 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
111 while (iterator.hasNext()) {
112 XacmlApplicationServiceProvider application = iterator.next();
114 // Is it our service?
116 if (application instanceof MonitoringPdpApplication) {
118 // Should be the first and only one
120 assertThat(service).isNull();
121 service = application;
123 strDump.append(application.applicationName());
124 strDump.append(" supports ");
125 strDump.append(application.supportedPolicyTypes());
126 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
128 LOGGER.debug("{}", strDump);
130 // Tell it to initialize based on the properties file
131 // we just built for it.
133 service.initialize(propertiesFile.toPath().getParent(), clientParams);
137 public void test1Basics() {
139 // Make sure there's an application name
141 assertThat(service.applicationName()).isNotEmpty();
143 // Ensure it has the supported policy types and
144 // can support the correct policy types.
146 assertThat(service.canSupportPolicyType(
147 new ToscaConceptIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"))).isTrue();
148 assertThat(service.canSupportPolicyType(
149 new ToscaConceptIdentifier(
150 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
151 assertThat(service.canSupportPolicyType(
152 new ToscaConceptIdentifier("onap.foobar", "1.0.0"))).isFalse();
154 // Ensure it supports decisions
156 assertThat(service.actionDecisionsSupported()).contains("configure");
160 public void test2NoPolicies() {
162 // Ask for a decision
164 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy, null);
165 LOGGER.info("Decision {}", decision);
167 assertThat(decision.getKey()).isNotNull();
168 assertThat(decision.getKey().getPolicies()).isEmpty();
170 // Test the branch for query params, and we have no policy anyway
172 Map<String, String[]> requestQueryParams = new HashMap<>();
173 decision = service.makeDecision(requestSinglePolicy, requestQueryParams);
174 LOGGER.info("Decision {}", decision);
176 assertThat(decision.getKey()).isNotNull();
177 assertThat(decision.getKey().getPolicies()).isEmpty();
179 // Test the branch for query params, and we have no policy anyway
181 requestQueryParams.put("abbrev", new String[] {"false"});
182 decision = service.makeDecision(requestSinglePolicy, requestQueryParams);
183 LOGGER.info("Decision {}", decision);
185 assertThat(decision.getKey()).isNotNull();
186 assertThat(decision.getKey().getPolicies()).isEmpty();
188 // Monitoring applications should not have this information returned
190 assertThat(decision.getKey().getAdvice()).isNull();
191 assertThat(decision.getKey().getObligations()).isNull();
192 assertThat(decision.getKey().getAttributes()).isNull();
195 @SuppressWarnings("unchecked")
197 public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
199 // Now load the vDNS Policy - make sure
200 // the pdp can support it and have it load
204 // Now load the monitoring policies
206 final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
209 // Ask for a decision
211 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy, null);
212 LOGGER.info("Decision {}", decision);
214 // Should have one policy returned
216 assertThat(decision.getKey()).isNotNull();
217 assertThat(decision.getKey().getPolicies()).hasSize(1);
219 // Monitoring applications should not have this information returned
221 assertThat(decision.getKey().getAdvice()).isNull();
222 assertThat(decision.getKey().getObligations()).isNull();
223 assertThat(decision.getKey().getAttributes()).isNull();
225 // Dump it out as Json
227 LOGGER.info(gson.encode(decision.getKey()));
229 // Ask for a decision based on policy-type
231 decision = service.makeDecision(requestPolicyType, null);
232 LOGGER.info("Decision {}", decision);
234 // Should have one policy returned
236 assertThat(decision.getKey()).isNotNull();
237 assertThat(decision.getKey().getPolicies()).hasSize(1);
239 // Monitoring applications should not have this information returned
241 assertThat(decision.getKey().getAdvice()).isNull();
242 assertThat(decision.getKey().getObligations()).isNull();
243 assertThat(decision.getKey().getAttributes()).isNull();
245 // Validate the full policy is returned
247 Map<String, Object> jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
248 assertThat(jsonPolicy).isNotNull();
249 assertThat(jsonPolicy.get("properties")).isNotNull();
251 // Dump it out as Json
253 LOGGER.info(gson.encode(decision.getKey()));
255 // Ask for abbreviated results
257 Map<String, String[]> requestQueryParams = new HashMap<>();
258 requestQueryParams.put("abbrev", new String[] {"true"});
259 decision = service.makeDecision(requestPolicyType, requestQueryParams);
260 LOGGER.info("Decision {}", decision);
262 // Should have one policy returned
264 assertThat(decision.getKey()).isNotNull();
265 assertThat(decision.getKey().getPolicies()).hasSize(1);
267 // Monitoring applications should not have this information returned
269 assertThat(decision.getKey().getAdvice()).isNull();
270 assertThat(decision.getKey().getObligations()).isNull();
271 assertThat(decision.getKey().getAttributes()).isNull();
273 // Validate an abbreviated policy is returned
275 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
276 assertThat(jsonPolicy).isNotNull().doesNotContainKey("properties");
278 // Don't Ask for abbreviated results
280 requestQueryParams = new HashMap<>();
281 requestQueryParams.put("abbrev", new String[] {"false"});
282 decision = service.makeDecision(requestPolicyType, requestQueryParams);
283 LOGGER.info("Decision {}", decision);
285 // Should have one policy returned
287 assertThat(decision.getKey()).isNotNull();
288 assertThat(decision.getKey().getPolicies()).hasSize(1);
290 // Monitoring applications should not have this information returned
292 assertThat(decision.getKey().getAdvice()).isNull();
293 assertThat(decision.getKey().getObligations()).isNull();
294 assertThat(decision.getKey().getAttributes()).isNull();
296 // And should have full policy returned
298 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
299 assertThat(jsonPolicy).isNotNull();
300 assertThat(jsonPolicy.get("properties")).isNotNull();
302 // Throw an unknown exception
304 requestQueryParams = new HashMap<>();
305 requestQueryParams.put("unknown", new String[] {"true"});
306 decision = service.makeDecision(requestPolicyType, requestQueryParams);
307 LOGGER.info("Decision {}", decision);
309 assertThat(decision.getKey()).isNotNull();
310 assertThat(decision.getKey().getPolicies()).hasSize(1);
311 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
312 assertThat(jsonPolicy).isNotNull();
313 assertThat(jsonPolicy.get("properties")).isNotNull();
315 // Dump it out as Json
317 LOGGER.info(gson.encode(decision.getKey()));
321 LOGGER.info("Now testing unloading of policy");
322 for (ToscaPolicy policy : loadedPolicies) {
323 assertThat(service.unloadPolicy(policy)).isTrue();
326 // Ask for a decision
328 decision = service.makeDecision(requestSinglePolicy, null);
329 LOGGER.info("Decision {}", decision.getKey());
331 assertThat(decision.getKey()).isNotNull();
332 assertThat(decision.getKey().getPolicies()).isEmpty();