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();
189 @SuppressWarnings("unchecked")
191 public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
193 // Now load the vDNS Policy - make sure
194 // the pdp can support it and have it load
198 // Now load the monitoring policies
200 final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
203 // Ask for a decision
205 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy, null);
206 LOGGER.info("Decision {}", decision);
208 assertThat(decision.getKey()).isNotNull();
209 assertThat(decision.getKey().getPolicies()).hasSize(1);
211 // Dump it out as Json
213 LOGGER.info(gson.encode(decision.getKey()));
215 // Ask for a decision based on policy-type
217 decision = service.makeDecision(requestPolicyType, null);
218 LOGGER.info("Decision {}", decision);
220 assertThat(decision.getKey()).isNotNull();
221 assertThat(decision.getKey().getPolicies()).hasSize(1);
222 Map<String, Object> jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
223 assertThat(jsonPolicy).isNotNull();
224 assertThat(jsonPolicy.get("properties")).isNotNull();
226 // Dump it out as Json
228 LOGGER.info(gson.encode(decision.getKey()));
230 // Ask for abbreviated results
232 Map<String, String[]> requestQueryParams = new HashMap<>();
233 requestQueryParams.put("abbrev", new String[] {"true"});
234 decision = service.makeDecision(requestPolicyType, requestQueryParams);
235 LOGGER.info("Decision {}", decision);
237 assertThat(decision.getKey()).isNotNull();
238 assertThat(decision.getKey().getPolicies()).hasSize(1);
239 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
240 assertThat(jsonPolicy).isNotNull().doesNotContainKey("properties");
242 // Don't Ask for abbreviated results
244 requestQueryParams = new HashMap<>();
245 requestQueryParams.put("abbrev", new String[] {"false"});
246 decision = service.makeDecision(requestPolicyType, requestQueryParams);
247 LOGGER.info("Decision {}", decision);
249 assertThat(decision.getKey()).isNotNull();
250 assertThat(decision.getKey().getPolicies()).hasSize(1);
251 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
252 assertThat(jsonPolicy).isNotNull();
253 assertThat(jsonPolicy.get("properties")).isNotNull();
255 // Throw an unknown exception
257 requestQueryParams = new HashMap<>();
258 requestQueryParams.put("unknown", new String[] {"true"});
259 decision = service.makeDecision(requestPolicyType, requestQueryParams);
260 LOGGER.info("Decision {}", decision);
262 assertThat(decision.getKey()).isNotNull();
263 assertThat(decision.getKey().getPolicies()).hasSize(1);
264 jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
265 assertThat(jsonPolicy).isNotNull();
266 assertThat(jsonPolicy.get("properties")).isNotNull();
268 // Dump it out as Json
270 LOGGER.info(gson.encode(decision.getKey()));
274 LOGGER.info("Now testing unloading of policy");
275 for (ToscaPolicy policy : loadedPolicies) {
276 assertThat(service.unloadPolicy(policy)).isTrue();
279 // Ask for a decision
281 decision = service.makeDecision(requestSinglePolicy, null);
282 LOGGER.info("Decision {}", decision.getKey());
284 assertThat(decision.getKey()).isNotNull();
285 assertThat(decision.getKey().getPolicies()).isEmpty();