aboutsummaryrefslogtreecommitdiffstats
path: root/policy-persistence/src/main/java/org/openecomp/policy/drools/persistence/PersistenceFeature.java
blob: e2c7f40255d6faccae9c00d9ebb717a999c6b0d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*-
 * ============LICENSE_START=======================================================
 * policy-persistence
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.drools.persistence;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.kie.api.KieServices;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.openecomp.policy.common.ia.IntegrityAudit;
import org.openecomp.policy.common.ia.IntegrityAuditProperties;
import org.openecomp.policy.common.im.StateManagement;
import org.openecomp.policy.common.logging.eelf.MessageCodes;
import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
import org.openecomp.policy.common.logging.flexlogger.Logger;
import org.openecomp.policy.common.logging.flexlogger.PropertyUtil;
import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
import org.openecomp.policy.drools.core.FeatureAPI;
import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
import org.openecomp.policy.drools.core.PolicyContainer;
import org.openecomp.policy.drools.core.PolicySession;
import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
import org.openecomp.policy.drools.system.PolicyEngine;

import bitronix.tm.Configuration;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;

/**
 * If this feature is supported, there is a single instance of it.
 * It adds persistence to Drools sessions, but it is also intertwined with
 * active/standby state management and IntegrityMonitor. For now, they are
 * all treated as a single feature, but it would be nice to separate them.
 *
 * The bulk of the code here was once in other classes, such as
 * 'PolicyContainer' and 'Main'. It was moved here as part of making this
 * a separate optional feature.
 */
public class PersistenceFeature implements FeatureAPI
{
  // get an instance of logger
  private static Logger logger =
	FlexLogger.getLogger(PersistenceFeature.class);

  // 'KieServices' singleton
  static private KieServices kieServices = KieServices.Factory.get();

  private static DroolsPdp myPdp;
  private static Object myPdpSync = new Object();
  private static DroolsPdpsElectionHandler electionHandler;

  // indicates whether persistence has been disabled
  private static boolean persistenceDisabled = false;

  /*
   * Used by JUnit testing to verify whether or not audit is running.
   */
  private static IntegrityAudit integrityAudit = null;

  /**
   * Lookup the adjunct for this feature that is associated with the
   * specified PolicyContainer. If not found, create one.
   *
   * @param policyContainer the container whose adjunct we are looking up,
   *	and possibly creating
   * @return the associated 'ContainerAdjunct' instance, which may be new
   */
  private ContainerAdjunct getContainerAdjunct(PolicyContainer policyContainer)
  {
	Object rval = policyContainer.getAdjunct(this);
	if (rval == null || ! (rval instanceof ContainerAdjunct))
	  {
		// adjunct does not exist, or has the wrong type (should never happen)
		rval = new ContainerAdjunct(policyContainer);
		policyContainer.setAdjunct(this, rval);
	  }
	return((ContainerAdjunct)rval);
  }

  /**************************/
  /* 'FeatureAPI' interface */
  /**************************/

  /**
   * {@inheritDoc}
   */
  @Override
	public int getSequenceNumber()
  {
	return(1);
  }

  /**
   * {@inheritDoc}
   */
  @Override
	public void globalInit(String args[], String configDir)
  {
	// Initialization code associated with 'PolicyContainer'
	DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = null;
	try
	  {
		droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.init(configDir);
	  }
	catch (Exception e)
	  {
		logger.error(MessageCodes.EXCEPTION_ERROR, e,
					 "main", "DroolsPDPIntegrityMonitor.init()");
	  }

	initializePersistence(configDir, droolsPdpIntegrityMonitor);

	// 1. Start Integrity Monitor (unless it was specifically disabled in the CORE layer
		
		
	if (persistenceDisabled) {
	  System.out.println("WARNING: Starting Engine with Persistance disabled");
	  logger.warn("Starting Engine with Persistance disabled");
	} else {
	  DroolsPDPIntegrityMonitor im = null;
	  //At this point the DroolsPDPIntegrityMonitor instance must exist
	  try {
		im = DroolsPDPIntegrityMonitor.getInstance();
	  } catch (Exception e1) {
		String msg = "policy-core startup failed to get DroolsPDPIntegrityMonitor instance:  \n" + e1;
		System.out.println(msg);
		e1.printStackTrace();
	  }
	  //Now get the StateManagement instance so we can register our observer
	  StateManagement sm = im.getStateManager();
			
	  //Create an instance of the Observer
	  PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
			
	  //Register the PMStandbyStateChangeNotifier Observer
	  sm.addObserver(pmNotifier);
	}
  }

  /**
   * This is a hook to create a new persistent KieSession.
   *
   * {@inheritDoc}
   */
  @Override
	public KieSession activatePolicySession
	(PolicyContainer policyContainer, String name, String kieBaseName)
  {
	return(getContainerAdjunct(policyContainer)
		   .newPersistentKieSession(name, kieBaseName));
  }

  /**
   * {@inheritDoc}
   */
  @Override
	public void disposeKieSession(PolicySession policySession)
  {
	// TODO: There should be one data source per session
	getContainerAdjunct(policySession.getPolicyContainer())
	  .disposeKieSession();
  }

  /**
   * {@inheritDoc}
   */
  @Override
	public void destroyKieSession(PolicySession policySession)
  {
	// TODO: There should be one data source per session
	getContainerAdjunct(policySession.getPolicyContainer())
	  .destroyKieSession();
  }
  

  /**
   * {@inheritDoc}
   */
  @Override
	public void beforeStartEngine() 
   {
	 return;
   }
  
  /**
   * {@inheritDoc}
   */
  @Override
   public void afterStartEngine() 
  {
  	PolicyEngine.manager.lock();
  }

  /**
   * {@inheritDoc}
   */
  @Override
	public void beforeShutdownEngine() 
  {
	  return;
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
	public void beforeCreateController(String name, Properties properties) 
  {
	  return;
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
	public void afterCreateController(String name) 
  {
	  return;
  }

  /**
   * {@inheritDoc}
   */
  @Override
   public void afterShutdownEngine() 
  {
  	return;
  }

  /**
   * {@inheritDoc}
   */
  @Override
   public void beforeStartController(String name) 
  {
	  return;
  }

  /**
   * {@inheritDoc}
   */
  @Override
   public void afterStartController(String name) 
  {
	  return;
  }

  /**
   * {@inheritDoc}
   */
  @Override
	public boolean isPersistenceEnabled()
  {
	return(!persistenceDisabled);
  }

  /**************************/

  /**
   * @return 'true' if Drools persistence is disabled, and 'false' if not
   */
  static public boolean getPersistenceDisabled()
  {
	return(persistenceDisabled);
  }

  /**
   * Read in the persistence properties, determine whether persistence is
   * enabled or disabled, and initialize persistence if enabled.
   */
  private static void initializePersistence(String configDir, DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor)
  {
	  
		try {
			Properties pDrools = PropertyUtil.getProperties(configDir
					+ "/droolsPersistence.properties");
			DroolsPersistenceProperties.initProperties(pDrools);
			Properties pXacml = PropertyUtil.getProperties(configDir
					+ "/xacmlPersistence.properties");
			XacmlPersistenceProperties.initProperties(pXacml);
			if ("true".equals(pDrools.getProperty("persistenceDisabled"))) {
				// 'persistenceDisabled' only relates to the 'drools'
				// database. The fact that integrityMonitor/xacml depends upon
				// persistence is an implementation detail there (which can't
				// currently be disabled), and doesn't directly affect
				// 'policy-core'.
				persistenceDisabled = true;
			} 
		} catch (IOException e1) {
			logger.error(MessageCodes.MISS_PROPERTY_ERROR, e1,
					"initializePersistence");
		}
	  	  
	    /*
	     * Might as well handle the Integrity Monitor properties here, too.
	     */
	    try {
		  Properties pIm =
		    PropertyUtil.getProperties(configDir + "/IntegrityMonitor.properties");
		  IntegrityMonitorProperties.initProperties(pIm);
		  logger.info("initializePersistence: resourceName=" + IntegrityMonitorProperties.getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID));
	    } catch (IOException e1) {
		  logger.error(MessageCodes.MISS_PROPERTY_ERROR, e1, "initializePersistence");
	    }
	  

		if (persistenceDisabled) {
			// The persistence design is tied to 'DroolsPdpsElectionHandler',
			// so we should bypass that as well. This also means that we
			// won't get active/standby notifications, so we need to go
			// into the 'active' state in order to have any 'PolicySession'
			// instances.
			return;
		}

	    DroolsPdpsConnector conn = getDroolsPdpsConnector("ncompPU");
		String uniquePdpId = IntegrityMonitorProperties.getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
		if(uniquePdpId == null){
			throw new NullPointerException();
		}
		
		/*
		 * In a JUnit test environment, one or more PDPs may already have been
		 * inserted in the DB, so we need to check for this.
		 */
		DroolsPdp existingPdp = conn.getPdp(uniquePdpId);
		if (existingPdp != null) {
			System.out.println("Found existing PDP record, pdpId="
					+ existingPdp.getPdpId() + ", isDesignated="
					+ existingPdp.isDesignated() + ", updatedDate="
					+ existingPdp.getUpdatedDate());
			myPdp = existingPdp;
		}
		
	    /*
	     * Kick off integrity audit for Drools DB.
	     */
	    startIntegrityAudit(configDir);
				
		synchronized(myPdpSync){
			if(myPdp == null){

				myPdp = new DroolsPdpImpl(uniquePdpId,false,4,new Date());	
			}
			if(myPdp != null){
				String site_name = "";
				site_name = IntegrityMonitorProperties.getProperty(IntegrityMonitorProperties.SITE_NAME);
				if (site_name == null) {
					site_name = "";
				}else{
					site_name = site_name.trim();
				}
				myPdp.setSiteName(site_name);
			}
			if(electionHandler == null){
		electionHandler = new DroolsPdpsElectionHandler(conn,myPdp,droolsPdpIntegrityMonitor);
			}
		}
		Configuration bitronixConfiguration = TransactionManagerServices.getConfiguration();
		bitronixConfiguration.setJournal(null);
		bitronixConfiguration.setServerId(uniquePdpId);
		System.out.println("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n");
		logger.info("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n");
  }

	private static void startIntegrityAudit(String configDir) {

		logger.info("startIntegrityAudit: Entering, configDir='" + configDir
				+ "'");

		/*
		 * Initialize Integrity Audit properties. file.
		 */
		try {

			String resourceName = IntegrityMonitorProperties
					.getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);

			/*
			 * Load properties for auditing of Drools DB.
			 */
			Properties droolsPia = PropertyUtil.getProperties(configDir
					+ "/IntegrityMonitor.properties");

			/*
			 * Supplement properties specific to the IntegrityMonitor (e.g.
			 * site_name, node_type, resource.name) with properties specific to
			 * persisting Drools DB entities (see
			 * ../policy-core/src/main/resources/persistence.xml)
			 * 
			 * Note: integrity_audit_period_seconds is defined in
			 * IntegrityMonitor.properties, rather than creating a whole new
			 * "IntegrityAudit.properties" file for just one property.
			 */
			droolsPia
					.setProperty(
							IntegrityAuditProperties.DB_DRIVER,
							DroolsPersistenceProperties
									.getProperty(DroolsPersistenceProperties.DB_DRIVER));
			droolsPia.setProperty(IntegrityAuditProperties.DB_PWD,
					DroolsPersistenceProperties
							.getProperty(DroolsPersistenceProperties.DB_PWD));
			droolsPia.setProperty(IntegrityAuditProperties.DB_URL,
					DroolsPersistenceProperties
							.getProperty(DroolsPersistenceProperties.DB_URL));
			droolsPia.setProperty(IntegrityAuditProperties.DB_USER,
					DroolsPersistenceProperties
							.getProperty(DroolsPersistenceProperties.DB_USER));

			/*
			 * Start audit for Drools DB.
			 */
			integrityAudit = new IntegrityAudit(
					resourceName, "ncompPU", droolsPia);
			integrityAudit.startAuditThread();

		} catch (IOException e1) {
			logger.error(
					MessageCodes.MISS_PROPERTY_ERROR,
					e1,
					"initializePersistence: IntegrityAuditProperties: "
							+ e1.getMessage());
		} catch (Exception e2) {
			logger.error(
					MessageCodes.EXCEPTION_ERROR,
					e2,
					"initializePersistence: IntegrityAuditProperties: "
							+ e2.getMessage());
		}

		logger.debug("startIntegrityAudit: Exiting");

	}
  
	/*
	 * Moved code to instantiate a JpaDroolsPdpsConnector object from main() to
	 * this method, so it can also be accessed from StandbyStateChangeNotifier
	 * class.
	 */
	public static DroolsPdpsConnector getDroolsPdpsConnector(String pu) {

		Map<String, Object> propMap = new HashMap<String, Object>();
		propMap.put("javax.persistence.jdbc.driver", DroolsPersistenceProperties
				.getProperty(DroolsPersistenceProperties.DB_DRIVER));
		propMap.put("javax.persistence.jdbc.url",
				DroolsPersistenceProperties.getProperty(DroolsPersistenceProperties.DB_URL));
		propMap.put("javax.persistence.jdbc.user", DroolsPersistenceProperties
				.getProperty(DroolsPersistenceProperties.DB_USER));
		propMap.put("javax.persistence.jdbc.password",
				DroolsPersistenceProperties.getProperty(DroolsPersistenceProperties.DB_PWD));

		EntityManagerFactory emf = Persistence.createEntityManagerFactory(
				pu, propMap);
		DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emf);

		return conn;
	}

	/*
	 * IntegrityAudit instance is needed by JUnit testing to ascertain whether
	 * or not audit is running.
	 */
	public static IntegrityAudit getIntegrityAudit() {

		return integrityAudit;

	}

  /* ============================================================ */

  /**
   * Each instance of this class is a logical extension of a 'PolicyContainer'
   * instance. It's reference is stored in the 'adjuncts' table within the
   * 'PolicyContainer', and will be garbage-collected with the container.
   */
  class ContainerAdjunct
  {
	// this is the 'PolicyContainer' instance that this adjunct is extending
	private PolicyContainer policyContainer;
	private PoolingDataSource ds = null;

	/**
	 * Constructor - initialize a new 'ContainerAdjunct'
	 *
	 * @param policyContainer the 'PolicyContainer' instance this adjunct
	 *		is extending
	 */
	ContainerAdjunct(PolicyContainer policyContainer)
	{
	  this.policyContainer = policyContainer;
	}

	/**
	 * Create a new persistent KieSession. If there is already a corresponding
	 * entry in the database, it is used to initialize the KieSession. If not,
	 * a completely new session is created.
	 *
	 * @param name the name of the KieSession (which is also the name of
	 *	the associated PolicySession)
	 * @param kieBaseName the name of the 'KieBase' instance containing
	 *	this session
	 * @return a new KieSession with persistence enabled (if persistence is
	 *	disabled, 'null' is returned
	 */
	private KieSession newPersistentKieSession(String name, String kieBaseName)
	{
	  if (persistenceDisabled)
		{
		  return(null);
		}
	  long desiredSessionId = -1;
	  synchronized (myPdpSync) {
		
	
	
		for(DroolsSession droolsSession : electionHandler.getSessions()){
		  if(droolsSession.getSessionName().equals(name)){
			desiredSessionId = droolsSession.getSessionId();
		  }
		}
	  }
	  System.out.println("\n\nThis controller is primary... coming up with session "+desiredSessionId+"\n\n");
	  logger.info("\n\nThis controller is primary... coming up with session "+desiredSessionId+"\n\n");
	  Map<String, Object> props = new HashMap<String, Object>();
	  props.put("URL", DroolsPersistenceProperties.getProperty(DroolsPersistenceProperties.DB_URL));
	  props.put("user", DroolsPersistenceProperties.getProperty(DroolsPersistenceProperties.DB_USER));
	  props.put("password", DroolsPersistenceProperties.getProperty(DroolsPersistenceProperties.DB_PWD));
	  props.put("dataSource",DroolsPersistenceProperties.getProperty(DroolsPersistenceProperties.DB_DATA_SOURCE));
	  logger.info("getPolicySession:session does not exist -- attempt to create one with name " + name);
	  // session does not exist -- attempt to create one
	  System.getProperties().put("java.naming.factory.initial","bitronix.tm.jndi.BitronixInitialContextFactory");
	  Environment env = kieServices.newEnvironment();
	  //kContainer.newKieBase(null);
	  ds = new PoolingDataSource();			
	  ds.setUniqueName("jdbc/BitronixJTADataSource"+name);
	  ds.setClassName( (String)props.remove("dataSource"));
	  //ds.setClassName( "org.h2.Driver" );
	  ds.setMaxPoolSize( 3 );
	  ds.setIsolationLevel("SERIALIZABLE");
	  ds.setAllowLocalTransactions( true );
	  //ds.getDriverProperties().put( "user", "sa" );
	  //ds.getDriverProperties().put( "password", "" );
	  //ds.getDriverProperties().put( "URL", "jdbc:h2:tcp://localhost/drools" );
	  ds.getDriverProperties().putAll(props);
	  ds.init();	
	  Properties emfProperties = new Properties();
	  emfProperties.setProperty(PersistenceUnitProperties.JTA_DATASOURCE, "jdbc/BitronixJTADataSource"+name);
	  env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, Persistence.createEntityManagerFactory("ncompsessionsPU",emfProperties));
	  env.set(EnvironmentName.TRANSACTION_MANAGER,TransactionManagerServices.getTransactionManager());
	  KieSessionConfiguration kConf = KieServices.Factory.get().newKieSessionConfiguration();
	  KieSession kieSession;
	  try{
		kieSession = kieServices.getStoreServices().loadKieSession(desiredSessionId, policyContainer.getKieContainer().getKieBase(kieBaseName), kConf, env);
		System.out.println("LOADING We can load session "+desiredSessionId+", going to create a new one");
		logger.info("LOADING We can load session "+desiredSessionId+", going to create a new one");
	  }catch(Exception e){
		System.out.println("LOADING We cannot load session "+desiredSessionId+", going to create a new one");

		logger.info("LOADING We cannot load session "+desiredSessionId+", going to create a new one");
		kieSession = kieServices.getStoreServices().newKieSession(policyContainer.getKieContainer().getKieBase(kieBaseName), null, env);
		System.out.println("LOADING CREATED "+kieSession.getIdentifier());
		logger.info("LOADING CREATED "+kieSession.getIdentifier());
	  }
	  synchronized (myPdpSync) {
		myPdp.setSessionId(name,kieSession.getIdentifier());
		electionHandler.updateMyPdp();
	  }
	  return(kieSession);
	}

	private void disposeKieSession()
	{
	if (ds != null)
	  {
		ds.close();
		ds = null;
	  }
	}

	private void destroyKieSession()
	{
	  // does the same thing as 'dispose'
	  disposeKieSession();
	}
  }
}