2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 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.util.HashMap;
31 import java.util.List;
33 import java.util.Properties;
34 import java.util.ServiceLoader;
35 import org.apache.commons.lang3.tuple.Pair;
36 import org.junit.BeforeClass;
37 import org.junit.ClassRule;
38 import org.junit.FixMethodOrder;
39 import org.junit.Test;
40 import org.junit.rules.TemporaryFolder;
41 import org.junit.runners.MethodSorters;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
45 import org.onap.policy.models.decisions.concepts.DecisionRequest;
46 import org.onap.policy.models.decisions.concepts.DecisionResponse;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
51 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
52 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
57 public class MonitoringPdpApplicationTest {
59 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
60 private static final Properties properties = new Properties();
61 private static XacmlApplicationServiceProvider service;
62 private static DecisionRequest requestSinglePolicy;
63 private static DecisionRequest requestPolicyType;
65 private static final StandardCoder gson = new StandardCoder();
68 public static final TemporaryFolder policyFolder = new TemporaryFolder();
71 * Copies the xacml.properties and policies files into
72 * temporary folder and loads the service provider saving
73 * instance of provider off for other tests to use.
76 public static void setup() throws Exception {
78 // Load Single Decision Request
80 requestSinglePolicy = gson.decode(
82 .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
83 DecisionRequest.class);
84 // Load Single Decision Request
86 requestPolicyType = gson.decode(
88 .getTextFileAsString("../../main/src/test/resources/decisions/decision.policytype.input.json"),
89 DecisionRequest.class);
91 // Setup our temporary folder
93 XacmlPolicyUtils.FileCreator myCreator = policyFolder::newFile;
94 File propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
95 properties, myCreator);
97 // Load XacmlApplicationServiceProvider service
99 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
100 ServiceLoader.load(XacmlApplicationServiceProvider.class);
102 // Look for our class instance and save it
104 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
105 for (XacmlApplicationServiceProvider application : applicationLoader) {
107 // Is it our service?
109 if (application instanceof MonitoringPdpApplication) {
111 // Should be the first and only one
113 assertThat(service).isNull();
114 service = application;
116 strDump.append(application.applicationName());
117 strDump.append(" supports ");
118 strDump.append(application.supportedPolicyTypes());
119 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
121 LOGGER.debug("{}", strDump);
123 // Tell it to initialize based on the properties file
124 // we just built for it.
126 service.initialize(propertiesFile.toPath().getParent(), null);
130 public void test1Basics() {
132 // Make sure there's an application name
134 assertThat(service.applicationName()).isNotEmpty();
136 // Ensure it has the supported policy types and
137 // can support the correct policy types.
139 assertThat(service.canSupportPolicyType(
140 new ToscaConceptIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"))).isTrue();
141 assertThat(service.canSupportPolicyType(
142 new ToscaConceptIdentifier("onap.policies.monitoring.tcagen2", "2.0.0"))).isTrue();
143 assertThat(service.canSupportPolicyType(
144 new ToscaConceptIdentifier(
145 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
146 assertThat(service.canSupportPolicyType(
147 new ToscaConceptIdentifier(
148 "onap.policies.monitoring.foobar", "2.0.1"))).isTrue();
149 assertThat(service.canSupportPolicyType(
150 new ToscaConceptIdentifier("onap.foobar", "1.0.0"))).isFalse();
152 // Ensure it supports decisions
154 assertThat(service.actionDecisionsSupported()).contains("configure");
158 public void test2NoPolicies() {
160 // Ask for a decision
162 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy, null);
163 LOGGER.info("Decision {}", decision);
165 assertThat(decision.getKey()).isNotNull();
166 assertThat(decision.getKey().getPolicies()).isEmpty();
168 // Test the branch for query params, and we have no policy anyway
170 Map<String, String[]> requestQueryParams = new HashMap<>();
171 decision = service.makeDecision(requestSinglePolicy, requestQueryParams);
172 LOGGER.info("Decision {}", decision);
174 assertThat(decision.getKey()).isNotNull();
175 assertThat(decision.getKey().getPolicies()).isEmpty();
177 // Test the branch for query params, and we have no policy anyway
179 requestQueryParams.put("abbrev", new String[] {"false"});
180 decision = service.makeDecision(requestSinglePolicy, requestQueryParams);
181 LOGGER.info("Decision {}", decision);
183 assertThat(decision.getKey()).isNotNull();
184 assertThat(decision.getKey().getPolicies()).isEmpty();
186 // Monitoring applications should not have this information returned
188 assertNoInfo(decision);
192 public void tes3AddvDnsPolicy() throws CoderException, XacmlApplicationException {
193 testAddPolicy("src/test/resources/vDNS.policy.input.yaml",
194 "onap.policies.monitoring.cdap.tca.hi.lo.app",
195 "onap.scaleout.tca");
199 public void tes4AddvFirewall1Policy() throws CoderException, XacmlApplicationException {
200 testAddPolicy("policies/vFirewall.policy.monitoring.input.tosca.yaml",
201 "onap.policies.monitoring.tcagen2",
202 "onap.vfirewall.tca");
206 public void tes5AddvFirewall2Policy() throws CoderException, XacmlApplicationException {
207 testAddPolicy("policies/vFirewall.policy.monitoring.input.tosca.v2.yaml",
208 "onap.policies.monitoring.tcagen2",
209 "onap.vfirewall.tca");
212 @SuppressWarnings("unchecked")
213 public void testAddPolicy(String policyResource, String policyType, String policyId)
214 throws CoderException, XacmlApplicationException {
216 // Now load the vDNS Policy - make sure
217 // the pdp can support it and have it load
221 // Now load the monitoring policies
223 final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies(policyResource, service);
226 // Set the policy-id for the decision request.
228 requestSinglePolicy.getResource().put("policy-id", policyId);
231 // Ask for a decision
233 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy, null);
234 LOGGER.info("Decision {}", decision);
236 // Should have one policy returned
238 assertThat(decision.getKey()).isNotNull();
239 assertThat(decision.getKey().getPolicies()).hasSize(1);
241 // Monitoring applications should not have this information returned
243 assertNoInfo(decision);
245 // Dump it out as Json
247 LOGGER.info(gson.encode(decision.getKey()));
250 // Set the policy-type for the decision request.
252 requestPolicyType.getResource().put("policy-type", policyType);
255 // Ask for a decision based on policy-type
257 decision = service.makeDecision(requestPolicyType, null);
258 LOGGER.info("Decision {}", decision);
260 // Should have one policy returned
262 assertThat(decision.getKey()).isNotNull();
263 assertThat(decision.getKey().getPolicies()).hasSize(1);
265 // Monitoring applications should not have this information returned
267 assertNoInfo(decision);
269 // Validate the full policy is returned
271 Map<String, Object> jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
272 assertThat(jsonPolicy).isNotNull();
273 assertThat(jsonPolicy.get("properties")).isNotNull();
275 // Dump it out as Json
277 LOGGER.info(gson.encode(decision.getKey()));
279 // Ask for abbreviated results
281 Map<String, String[]> requestQueryParams = new HashMap<>();
282 requestQueryParams.put("abbrev", new String[] {"true"});
283 decision = service.makeDecision(requestPolicyType, requestQueryParams);
284 LOGGER.info("Decision {}", decision);
286 // Should have one policy returned
288 assertThat(decision.getKey()).isNotNull();
289 assertThat(decision.getKey().getPolicies()).hasSize(1);
291 // Monitoring applications should not have this information returned
293 assertNoInfo(decision);
295 // Validate an abbreviated policy is returned
297 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
298 assertThat(jsonPolicy).isNotNull().doesNotContainKey("properties");
300 // Don't Ask for abbreviated results
302 requestQueryParams = new HashMap<>();
303 requestQueryParams.put("abbrev", new String[] {"false"});
304 decision = service.makeDecision(requestPolicyType, requestQueryParams);
305 LOGGER.info("Decision {}", decision);
307 // Should have one policy returned
309 assertThat(decision.getKey()).isNotNull();
310 assertThat(decision.getKey().getPolicies()).hasSize(1);
312 // Monitoring applications should not have this information returned
314 assertNoInfo(decision);
316 // And should have full policy returned
318 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
319 assertThat(jsonPolicy).isNotNull();
320 assertThat(jsonPolicy.get("properties")).isNotNull();
322 // Throw an unknown exception
324 requestQueryParams = new HashMap<>();
325 requestQueryParams.put("unknown", new String[] {"true"});
326 decision = service.makeDecision(requestPolicyType, requestQueryParams);
327 LOGGER.info("Decision {}", decision);
329 assertThat(decision.getKey()).isNotNull();
330 assertThat(decision.getKey().getPolicies()).hasSize(1);
331 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
332 assertThat(jsonPolicy).isNotNull();
333 assertThat(jsonPolicy.get("properties")).isNotNull();
335 // Dump it out as Json
337 LOGGER.info(gson.encode(decision.getKey()));
341 LOGGER.info("Now testing unloading of policy");
342 for (ToscaPolicy policy : loadedPolicies) {
343 assertThat(service.unloadPolicy(policy)).isTrue();
346 // Ask for a decision
348 decision = service.makeDecision(requestSinglePolicy, null);
349 LOGGER.info("Decision {}", decision.getKey());
351 assertThat(decision.getKey()).isNotNull();
352 assertThat(decision.getKey().getPolicies()).isEmpty();
355 private void assertNoInfo(Pair<DecisionResponse, Response> decision) {
356 assertThat(decision.getKey().getAdvice()).isNull();
357 assertThat(decision.getKey().getObligations()).isNull();
358 assertThat(decision.getKey().getAttributes()).isNull();