Change RestServerParameters to BusTopicParams
[policy/xacml-pdp.git] / applications / guard / src / test / java / org / onap / policy / xacml / pdp / application / guard / SonCoordinationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.guard;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import com.att.research.xacml.api.Response;
28 import java.io.File;
29 import java.io.IOException;
30 import java.time.Instant;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.ServiceLoader;
36 import java.util.UUID;
37 import javax.persistence.EntityManager;
38 import javax.persistence.Persistence;
39 import org.apache.commons.lang3.tuple.Pair;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.ClassRule;
44 import org.junit.FixMethodOrder;
45 import org.junit.Test;
46 import org.junit.rules.TemporaryFolder;
47 import org.junit.runners.MethodSorters;
48 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
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.TextFileUtils;
52 import org.onap.policy.guard.OperationsHistory;
53 import org.onap.policy.models.decisions.concepts.DecisionRequest;
54 import org.onap.policy.models.decisions.concepts.DecisionResponse;
55 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
56 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
57 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
58 import org.onap.policy.pdp.xacml.application.common.operationshistory.CountRecentOperationsPip;
59 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
64 public class SonCoordinationTest {
65
66     private static final Logger LOGGER = LoggerFactory.getLogger(SonCoordinationTest.class);
67     private static Properties properties = new Properties();
68     private static File propertiesFile;
69     private static BusTopicParams clientParams = new BusTopicParams();
70     private static XacmlApplicationServiceProvider service;
71     private static DecisionRequest requestVpciNode1;
72     private static DecisionRequest requestVsonhNode1;
73     private static StandardCoder gson = new StandardCoder();
74     private static EntityManager em;
75     private static final String DENY = "Deny";
76     private static final String PERMIT = "Permit";
77
78     @ClassRule
79     public static final TemporaryFolder policyFolder = new TemporaryFolder();
80
81     /**
82      * Copies the xacml.properties and policies files into
83      * temporary folder and loads the service provider saving
84      * instance of provider off for other tests to use.
85      */
86     @BeforeClass
87     public static void setup() throws Exception {
88         LOGGER.info("Setting up class");
89         //
90         // Setup our temporary folder
91         //
92         XacmlPolicyUtils.FileCreator myCreator =
93             (String filename) -> policyFolder.newFile(filename);
94         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents(
95             "src/test/resources/xacml.properties", properties, myCreator);
96         //
97         // Load service
98         //
99         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
100             ServiceLoader.load(XacmlApplicationServiceProvider.class);
101         //
102         // Find the guard service application and save for use in all the tests
103         //
104         StringBuilder strDump =
105             new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
106         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
107         while (iterator.hasNext()) {
108             XacmlApplicationServiceProvider application = iterator.next();
109             //
110             // Is it our service?
111             //
112             if (application instanceof GuardPdpApplication) {
113                 //
114                 // Should be the first and only one
115                 //
116                 assertThat(service).isNull();
117                 service = application;
118             }
119             strDump.append(application.applicationName());
120             strDump.append(" supports ");
121             strDump.append(application.supportedPolicyTypes());
122             strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
123         }
124         LOGGER.info("{}", strDump);
125         //
126         // Tell it to initialize based on the properties file
127         // we just built for it.
128         //
129         service.initialize(propertiesFile.toPath().getParent(), clientParams);
130         //
131         // Load Decision Requests
132         //
133         requestVpciNode1 = gson.decode(
134             TextFileUtils.getTextFileAsString(
135                 "src/test/resources/requests/coordination.cl.vPci.node.1.json"),
136             DecisionRequest.class);
137         requestVsonhNode1 = gson.decode(
138             TextFileUtils.getTextFileAsString(
139                 "src/test/resources/requests/coordination.cl.vSonh.node.1.json"),
140             DecisionRequest.class);
141         String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
142         em = Persistence
143             .createEntityManagerFactory(SonCoordinationTest.properties.getProperty(persistenceUnit),
144                 properties)
145             .createEntityManager();
146     }
147
148     /**
149      * Clears the database before each test.
150      *
151      */
152     @Before
153     public void startClean() throws Exception {
154         em.getTransaction().begin();
155         em.createQuery("DELETE FROM OperationsHistory").executeUpdate();
156         em.getTransaction().commit();
157     }
158
159     /**
160      * Close the entity manager.
161      */
162     @AfterClass
163     public static void cleanup() throws Exception {
164         if (em != null) {
165             em.close();
166         }
167     }
168
169     /**
170      * Check that decision matches expectation.
171      *
172      * @param expected from the response
173      * @param response received
174      *
175      **/
176     public void checkDecision(String expected, DecisionResponse response) throws CoderException {
177         LOGGER.info("Looking for {} Decision", expected);
178         assertThat(response).isNotNull();
179         assertThat(response.getStatus()).isNotNull();
180         assertThat(response.getStatus()).isEqualTo(expected);
181         //
182         // Dump it out as Json
183         //
184         LOGGER.info(gson.encode(response));
185     }
186
187     /**
188      * Request a decision and check that it matches expectation.
189      *
190      * @param request to send to Xacml PDP
191      * @param expected from the response
192      *
193      **/
194     public void requestAndCheckDecision(DecisionRequest request, String expected)
195         throws CoderException {
196
197         //
198         // Ask for a decision
199         //
200         Pair<DecisionResponse, Response> decision = service.makeDecision(request, null);
201         //
202         // Check decision
203         //
204         checkDecision(expected, decision.getKey());
205     }
206
207     @Test
208     public void test1() throws CoderException, IOException, XacmlApplicationException {
209         LOGGER.info("**************** Running vPci and vSonh Control Loops ****************");
210         //
211         // Now load the test coordination policy - make sure
212         // the pdp can support it and have it load
213         // into the PDP.
214         //
215         TestUtils.loadPolicies(
216             "src/test/resources/test.policy.guard.coordination.vPciBlocksVsonh.tosca.yaml",
217             service);
218         TestUtils.loadPolicies(
219             "src/test/resources/test.policy.guard.coordination.vSonhBlocksVpci.tosca.yaml",
220             service);
221         //
222         // vSonh doesn't have open action: vPci should get permit
223         //
224         requestAndCheckDecision(requestVpciNode1, PERMIT);
225         //
226         // vPci doesn't have open action: vSonh should get permit
227         //
228         requestAndCheckDecision(requestVsonhNode1, PERMIT);
229         //
230         // Open vSonh on node1
231         //
232         long vsonhId = insertOperationEvent(requestVsonhNode1, "Started");
233         //
234         // Under current coordination policy vPci should get a deny
235         //
236         requestAndCheckDecision(requestVpciNode1, DENY);
237         //
238         // Close vSonh on node1
239         //
240         updateOperationEvent(vsonhId, "Success");
241         //
242         // With vSonh closed on node 1, vPci now should get a permit
243         //
244         requestAndCheckDecision(requestVpciNode1, PERMIT);
245         //
246         // Open vPci on node1
247         //
248         long vpciId = insertOperationEvent(requestVpciNode1, "Started");
249         //
250         // Under current coordination policy vSonh should get a deny
251         //
252         requestAndCheckDecision(requestVsonhNode1, DENY);
253         //
254         // Close cl1 on node1
255         //
256         updateOperationEvent(vpciId, "Failed");
257         //
258         // With vPci closed on node 1, vSonh now should get a permit
259         //
260         requestAndCheckDecision(requestVsonhNode1, PERMIT);
261     }
262
263     @SuppressWarnings("unchecked")
264     private long insertOperationEvent(DecisionRequest request, String outcome) {
265         //
266         // Get the properties
267         //
268         Map<String, Object> properties = (Map<String, Object>) request.getResource().get("guard");
269         //
270         // Add an entry
271         //
272         OperationsHistory newEntry = new OperationsHistory();
273         newEntry.setActor(properties.get("actor").toString());
274         newEntry.setOperation(properties.get("operation").toString());
275         newEntry.setClosedLoopName(properties.get("clname").toString());
276         newEntry.setOutcome(outcome);
277         newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
278         newEntry.setEndtime(Date.from(Instant.now()));
279         newEntry.setRequestId(UUID.randomUUID().toString());
280         newEntry.setTarget(properties.get("target").toString());
281         em.getTransaction().begin();
282         em.persist(newEntry);
283         em.getTransaction().commit();
284         return newEntry.getId();
285     }
286
287     private void updateOperationEvent(long id, String outcome) {
288
289         OperationsHistory updateEntry = em.find(OperationsHistory.class, id);
290         updateEntry.setOutcome(outcome);
291         updateEntry.setEndtime(Date.from(Instant.now()));
292         em.getTransaction().begin();
293         em.persist(updateEntry);
294         em.getTransaction().commit();
295     }
296
297 }