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.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.resources.TextFileUtils;
47 import org.onap.policy.models.decisions.concepts.DecisionRequest;
48 import org.onap.policy.models.decisions.concepts.DecisionResponse;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
52 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
53 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
54 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 public class MonitoringPdpApplicationTest {
61 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
62 private static Properties properties = new Properties();
63 private static File propertiesFile;
64 private static XacmlApplicationServiceProvider service;
65 private static DecisionRequest requestSinglePolicy;
66 private static DecisionRequest requestPolicyType;
68 private static StandardCoder gson = new StandardCoder();
71 public static final TemporaryFolder policyFolder = new TemporaryFolder();
74 * Copies the xacml.properties and policies files into
75 * temporary folder and loads the service provider saving
76 * instance of provider off for other tests to use.
79 public static void setup() throws Exception {
81 // Load Single Decision Request
83 requestSinglePolicy = gson.decode(
85 .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
86 DecisionRequest.class);
87 // Load Single Decision Request
89 requestPolicyType = gson.decode(
91 .getTextFileAsString("../../main/src/test/resources/decisions/decision.policytype.input.json"),
92 DecisionRequest.class);
94 // Setup our temporary folder
96 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
97 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
98 properties, myCreator);
100 // Load XacmlApplicationServiceProvider service
102 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
103 ServiceLoader.load(XacmlApplicationServiceProvider.class);
105 // Look for our class instance and save it
107 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
108 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
109 while (iterator.hasNext()) {
110 XacmlApplicationServiceProvider application = iterator.next();
112 // Is it our service?
114 if (application instanceof MonitoringPdpApplication) {
116 // Should be the first and only one
118 assertThat(service).isNull();
119 service = application;
121 strDump.append(application.applicationName());
122 strDump.append(" supports ");
123 strDump.append(application.supportedPolicyTypes());
124 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
126 LOGGER.debug("{}", strDump);
128 // Tell it to initialize based on the properties file
129 // we just built for it.
131 service.initialize(propertiesFile.toPath().getParent(), null);
135 public void test1Basics() {
137 // Make sure there's an application name
139 assertThat(service.applicationName()).isNotEmpty();
141 // Ensure it has the supported policy types and
142 // can support the correct policy types.
144 assertThat(service.canSupportPolicyType(
145 new ToscaConceptIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"))).isTrue();
146 assertThat(service.canSupportPolicyType(
147 new ToscaConceptIdentifier(
148 "onap.policies.monitoring.foobar", "1.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 assertThat(decision.getKey().getAdvice()).isNull();
189 assertThat(decision.getKey().getObligations()).isNull();
190 assertThat(decision.getKey().getAttributes()).isNull();
193 @SuppressWarnings("unchecked")
195 public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
197 // Now load the vDNS Policy - make sure
198 // the pdp can support it and have it load
202 // Now load the monitoring policies
204 final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
207 // Ask for a decision
209 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy, null);
210 LOGGER.info("Decision {}", decision);
212 // Should have one policy returned
214 assertThat(decision.getKey()).isNotNull();
215 assertThat(decision.getKey().getPolicies()).hasSize(1);
217 // Monitoring applications should not have this information returned
219 assertThat(decision.getKey().getAdvice()).isNull();
220 assertThat(decision.getKey().getObligations()).isNull();
221 assertThat(decision.getKey().getAttributes()).isNull();
223 // Dump it out as Json
225 LOGGER.info(gson.encode(decision.getKey()));
227 // Ask for a decision based on policy-type
229 decision = service.makeDecision(requestPolicyType, null);
230 LOGGER.info("Decision {}", decision);
232 // Should have one policy returned
234 assertThat(decision.getKey()).isNotNull();
235 assertThat(decision.getKey().getPolicies()).hasSize(1);
237 // Monitoring applications should not have this information returned
239 assertThat(decision.getKey().getAdvice()).isNull();
240 assertThat(decision.getKey().getObligations()).isNull();
241 assertThat(decision.getKey().getAttributes()).isNull();
243 // Validate the full policy is returned
245 Map<String, Object> jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
246 assertThat(jsonPolicy).isNotNull();
247 assertThat(jsonPolicy.get("properties")).isNotNull();
249 // Dump it out as Json
251 LOGGER.info(gson.encode(decision.getKey()));
253 // Ask for abbreviated results
255 Map<String, String[]> requestQueryParams = new HashMap<>();
256 requestQueryParams.put("abbrev", new String[] {"true"});
257 decision = service.makeDecision(requestPolicyType, requestQueryParams);
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 assertThat(decision.getKey().getAdvice()).isNull();
268 assertThat(decision.getKey().getObligations()).isNull();
269 assertThat(decision.getKey().getAttributes()).isNull();
271 // Validate an abbreviated policy is returned
273 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
274 assertThat(jsonPolicy).isNotNull().doesNotContainKey("properties");
276 // Don't Ask for abbreviated results
278 requestQueryParams = new HashMap<>();
279 requestQueryParams.put("abbrev", new String[] {"false"});
280 decision = service.makeDecision(requestPolicyType, requestQueryParams);
281 LOGGER.info("Decision {}", decision);
283 // Should have one policy returned
285 assertThat(decision.getKey()).isNotNull();
286 assertThat(decision.getKey().getPolicies()).hasSize(1);
288 // Monitoring applications should not have this information returned
290 assertThat(decision.getKey().getAdvice()).isNull();
291 assertThat(decision.getKey().getObligations()).isNull();
292 assertThat(decision.getKey().getAttributes()).isNull();
294 // And should have full policy returned
296 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
297 assertThat(jsonPolicy).isNotNull();
298 assertThat(jsonPolicy.get("properties")).isNotNull();
300 // Throw an unknown exception
302 requestQueryParams = new HashMap<>();
303 requestQueryParams.put("unknown", new String[] {"true"});
304 decision = service.makeDecision(requestPolicyType, requestQueryParams);
305 LOGGER.info("Decision {}", decision);
307 assertThat(decision.getKey()).isNotNull();
308 assertThat(decision.getKey().getPolicies()).hasSize(1);
309 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
310 assertThat(jsonPolicy).isNotNull();
311 assertThat(jsonPolicy.get("properties")).isNotNull();
313 // Dump it out as Json
315 LOGGER.info(gson.encode(decision.getKey()));
319 LOGGER.info("Now testing unloading of policy");
320 for (ToscaPolicy policy : loadedPolicies) {
321 assertThat(service.unloadPolicy(policy)).isTrue();
324 // Ask for a decision
326 decision = service.makeDecision(requestSinglePolicy, null);
327 LOGGER.info("Decision {}", decision.getKey());
329 assertThat(decision.getKey()).isNotNull();
330 assertThat(decision.getKey().getPolicies()).isEmpty();