6b6547328431ffe6eeb5aba549a97ac78caaeed5
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-persistence
4  * ================================================================================
5  * Copyright (C) 2017 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.drools.controller.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.util.Date;
28 import java.util.Properties;
29
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Persistence;
34
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Ignore;
40 import org.junit.Test;
41
42 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
43 import org.openecomp.policy.common.ia.IntegrityAudit;
44 import org.openecomp.policy.common.im.AdministrativeStateException;
45 import org.openecomp.policy.common.im.IntegrityMonitor;
46 import org.openecomp.policy.common.im.StandbyStatusException;
47 import org.openecomp.policy.common.im.StateManagement;
48 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
49 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
50 import org.openecomp.policy.drools.core.PolicyContainer;
51 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
52 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
53 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
54 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
55 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
56 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
57 import org.openecomp.policy.drools.persistence.PersistenceFeature;
58 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
59 import org.openecomp.policy.drools.system.Main;
60 import org.openecomp.policy.drools.system.PolicyEngine;
61
62 /*
63  * Cloned from StandbyStateManagement.java in support of US673632.
64  * See MultiSite_v1-10.ppt, slide 38
65  */
66 public class IntegrityAuditIntegrationTest {
67                 
68         
69         public static final String INTEGRITY_MONITOR_PROPERTIES_FILE="src/test/server/config/IntegrityMonitor.properties";
70
71         /*
72          * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting 
73          * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be 
74          * sufficient to ensure that we wait for the DesignationWaiter to do its job, before 
75          * checking the results. 
76          */
77         private long sleepTime = 80000;
78                 
79         /*
80          * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
81          */
82         private long interruptRecoveryTime = 5000;
83         
84         /*
85          * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
86          */
87         @BeforeClass
88         public static void setUpClass() throws Exception {
89                 
90                 PolicyLogger.info("setUpClass: Entering");
91
92                 String userDir = System.getProperty("user.dir");
93                 PolicyLogger.debug("setUpClass: userDir=" + userDir);
94                 System.setProperty("com.sun.management.jmxremote.port", "9980");
95                 System.setProperty("com.sun.management.jmxremote.authenticate","false");
96                                 
97                 // Make sure path to config directory is set correctly in PolicyContainer.main
98                 // Also make sure we ignore HTTP server failures resulting from port conflicts.
99                 PolicyContainer.isUnitTesting = true;
100                 
101                 /*
102                  * Setting isUnitTesting to true ensures
103                  * 
104                  * 1) That we load test version of properties files
105                  * 
106                  * and
107                  * 
108                  * 2) that we use dbAuditSimulate() method, because all we care about
109                  * for this JUnit testing is that the audits are executed.
110                  */
111                 IntegrityAudit.isUnitTesting = true;
112                 
113                 initializeDb();
114                 
115                 PolicyLogger.info("setUpClass: Exiting");
116                 
117         }
118
119         @AfterClass
120         public static void tearDownClass() throws Exception {
121                                 
122         }
123
124         @Before
125         public void setUp() throws Exception {
126                 
127         }
128
129         @After
130         public void tearDown() throws Exception {
131                 
132         }
133
134         
135         /*
136          * Verifies that audit thread starts successfully.
137          */
138         //@Ignore
139         @Test
140         public void testAuditInit() throws Exception {
141                 
142                 PolicyLogger.debug("\n\ntestAuditInit: Entering\n\n");
143
144                 PolicyLogger.debug("testAuditInit: Reading IntegrityMonitorProperties");
145                 Properties integrityMonitorProperties = new Properties();
146                 integrityMonitorProperties.load(new FileInputStream(new File(
147                                 INTEGRITY_MONITOR_PROPERTIES_FILE)));
148                 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
149                 String thisPdpId = IntegrityMonitorProperties
150                                 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
151                 
152                 PolicyLogger.debug("testAuditInit: Reading xacmlPersistenceProperties");
153                 Properties xacmlPersistenceProperties = new Properties();
154                 xacmlPersistenceProperties.load(new FileInputStream(new File(
155                                 "src/test/server/config/xacmlPersistence.properties")));
156                 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
157                 
158                 PolicyLogger.debug("testAuditInit: Creating emfXacml");
159                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
160                                 "junitXacmlPU", xacmlPersistenceProperties);
161                 
162                 PolicyLogger.debug("testAuditInit: Reading droolsPersistenceProperties");
163                 Properties droolsPersistenceProperties = new Properties();
164                 droolsPersistenceProperties.load(new FileInputStream(new File(
165                                 "src/test/server/config/droolsPersistence.properties")));
166                 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
167
168                 PolicyLogger.debug("testAuditInit: Creating emfDrools");
169                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
170                                 "junitDroolsPU", droolsPersistenceProperties);
171                 
172                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
173                 
174                 PolicyLogger.debug("testAuditInit: Cleaning up tables");
175                 conn.deleteAllSessions();
176                 conn.deleteAllPdps();
177                 
178                 /*
179                  * Insert this PDP as designated.  Initial standby state will be 
180                  * either null or cold standby.   
181                  */
182                 PolicyLogger.debug("testAuditInit: Inserting PDP=" + thisPdpId + " as designated");
183                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
184                 conn.insertPdp(pdp);
185                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
186                 PolicyLogger.debug("testAuditInit: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
187                                 + droolsPdpEntity.isDesignated());
188                 assertTrue(droolsPdpEntity.isDesignated() == true);
189                 
190                 PolicyLogger.debug("testAuditInit: Instantiating stateManagement object");
191                 StateManagement sm = new StateManagement(emfXacml, "dummy");
192                 sm.deleteAllStateManagementEntities();
193                 sm = new StateManagement(emfXacml, thisPdpId);
194                 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
195                 sm.addObserver(pmStandbyStateChangeNotifier);
196                                 
197                 PolicyLogger.debug("testAuditInit: Running policy-management.Main class, designated="
198                                 + conn.getPdp(thisPdpId).isDesignated());
199                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
200                 policyManagementRunner.start();
201                 
202                 PolicyLogger.debug("testAuditInit: Runner started; Sleeping "
203                                 + interruptRecoveryTime + "ms before promoting PDP="
204                                 + thisPdpId);
205                 Thread.sleep(interruptRecoveryTime);
206                 
207                 IntegrityAudit integrityAudit = PersistenceFeature.getIntegrityAudit();
208                 PolicyLogger.debug("testAuditInit: isThreadInitialized=" + integrityAudit.isThreadInitialized());
209                 assertTrue("AuditThread not initialized!?",integrityAudit.isThreadInitialized());
210                                 
211                 PolicyLogger.debug("testAuditInit: Stopping auditThread");
212                 integrityAudit.stopAuditThread();
213                 Thread.sleep(1000);
214                 //This will interrupt thread.  However, the thread will not die.  It keeps on ticking and trying to
215                 //run the audit.
216                 assertTrue("AuditThread not still running after stopAuditThread invoked!?",integrityAudit.isThreadInitialized());
217
218                 PolicyLogger.debug("testAuditInit: Stopping policyManagementRunner");
219                 policyManagementRunner.stopRunner();
220                 
221                 PolicyLogger.debug("\n\ntestAuditInit: Exiting\n\n");
222                 Thread.sleep(interruptRecoveryTime);
223
224         }       
225         
226     /*
227      * This method initializes and cleans the DB so that PDP-D will be able to 
228      * store IntegrityAuditEntity in the DB.
229      */
230         public static void initializeDb(){
231                 
232                 PolicyLogger.debug("initializeDb: Entering");
233                 
234         Properties cleanProperties = new Properties();
235         cleanProperties.put(DroolsPersistenceProperties.DB_DRIVER,"org.h2.Driver");
236         cleanProperties.put(DroolsPersistenceProperties.DB_URL, "jdbc:h2:file:./sql/drools");
237         cleanProperties.put(DroolsPersistenceProperties.DB_USER, "sa");
238         cleanProperties.put(DroolsPersistenceProperties.DB_PWD, "");
239         //EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaPU", cleanProperties);
240         EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitDroolsPU", cleanProperties);
241                 
242                 EntityManager em = emf.createEntityManager();
243                 // Start a transaction
244                 EntityTransaction et = em.getTransaction();
245
246                 et.begin();
247
248                 // Clean up the DB
249                 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
250
251                 // commit transaction
252                 et.commit();
253                 em.close();
254                 
255                 PolicyLogger.debug("initializeDb: Exiting");
256         }       
257         
258         private class PolicyManagementRunner extends Thread {
259
260                 public void run() {
261                         PolicyLogger.info("PolicyManagementRunner.run: Entering");
262                         String args[] = { "src/main/server/config" };
263                         try {
264                                 Main.main(args);
265                         } catch (Exception e) {
266                                 PolicyLogger
267                                                 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
268                                                                 + e.getMessage());
269                         }
270                         PolicyLogger.info("PolicyManagementRunner.run: Exiting");
271                 }
272                 
273                 public void stopRunner() {
274                         PolicyEngine.manager.shutdown();
275                 }
276
277         }
278         
279 }