2 * ============LICENSE_START=======================================================
3 * feature-active-standby-management
4 * ================================================================================
5 * Copyright (C) 2017-2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.activestandby;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.LinkedList;
26 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.FlushModeType;
31 import javax.persistence.LockModeType;
32 import javax.persistence.Query;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class JpaDroolsPdpsConnector implements DroolsPdpsConnector {
39 // get an instance of logger
40 private static final Logger logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class);
41 private EntityManagerFactory emf;
44 //not sure if we want to use the same entity manager factory
45 //for drools session and pass it in here, or create a new one
46 public JpaDroolsPdpsConnector(EntityManagerFactory emf) {
51 public Collection<DroolsPdp> getDroolsPdps() {
52 //return a list of all the DroolsPdps in the database
53 EntityManager em = emf.createEntityManager();
55 em.getTransaction().begin();
56 Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p");
57 List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE)
58 .setFlushMode(FlushModeType.COMMIT).getResultList();
59 LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>();
60 for (Object o : droolsPdpsList) {
61 if (o instanceof DroolsPdp) {
62 //Make sure it is not a cached version
63 em.refresh((DroolsPdpEntity)o);
64 droolsPdpsReturnList.add((DroolsPdp)o);
65 if (logger.isDebugEnabled()) {
66 DroolsPdp droolsPdp = (DroolsPdp)o;
67 logger.debug("getDroolsPdps: PDP= {}"
68 + ", isDesignated= {}"
70 + ", priority= {}", droolsPdp.getPdpId(), droolsPdp.isDesignated(),
71 droolsPdp.getUpdatedDate(), droolsPdp.getPriority());
76 em.getTransaction().commit();
77 } catch (Exception e) {
78 logger.error("Cannot commit getDroolsPdps() transaction", e);
80 return droolsPdpsReturnList;
82 cleanup(em, "getDroolsPdps");
86 private boolean nullSafeEquals(Object one, Object two) {
87 if (one == null && two == null) {
90 if (one != null && two != null) {
91 return one.equals(two);
97 public void update(DroolsPdp pdp) {
99 logger.debug("update: Entering, pdpId={}", pdp.getPdpId());
101 //this is to update our own pdp in the database
102 EntityManager em = emf.createEntityManager();
104 em.getTransaction().begin();
105 Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
106 droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
107 List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE)
108 .setFlushMode(FlushModeType.COMMIT).getResultList();
109 DroolsPdpEntity droolsPdpEntity;
110 if (droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) {
111 droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
112 em.refresh(droolsPdpEntity); //Make sure we have current values
113 Date currentDate = new Date();
114 long difference = currentDate.getTime() - droolsPdpEntity.getUpdatedDate().getTime();
115 //just set some kind of default here
116 long pdpTimeout = 15000;
118 pdpTimeout = Long.parseLong(
119 ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
120 } catch (Exception e) {
121 logger.error("Could not get PDP timeout property, using default.", e);
123 boolean isCurrent = difference < pdpTimeout;
124 logger.debug("update: PDP= {}, isCurrent={}"
126 + ", pdpTimeout= {}, designated= {}",
127 pdp.getPdpId(), isCurrent, difference, pdpTimeout, droolsPdpEntity.isDesignated());
129 logger.debug("update: For PDP={}"
130 + ", instantiating new DroolsPdpEntity", pdp.getPdpId());
131 droolsPdpEntity = new DroolsPdpEntity();
132 em.persist(droolsPdpEntity);
133 droolsPdpEntity.setPdpId(pdp.getPdpId());
135 if (droolsPdpEntity.getPriority() != pdp.getPriority()) {
136 droolsPdpEntity.setPriority(pdp.getPriority());
138 if (!droolsPdpEntity.getUpdatedDate().equals(pdp.getUpdatedDate())) {
139 droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
141 if (!nullSafeEquals(droolsPdpEntity.getSiteName(),pdp.getSiteName())) {
142 droolsPdpEntity.setSiteName(pdp.getSiteName());
145 if (droolsPdpEntity.isDesignated() != pdp.isDesignated()) {
146 logger.debug("update: pdpId={}"
147 + ", pdp.isDesignated={}"
148 + ", droolsPdpEntity.pdpId= {}"
149 + ", droolsPdpEntity.isDesignated={}",
150 pdp.getPdpId(), pdp.isDesignated(),
151 droolsPdpEntity.getPdpId(), droolsPdpEntity.isDesignated());
152 droolsPdpEntity.setDesignated(pdp.isDesignated());
153 //The isDesignated value is not the same and the new one == true
154 if (pdp.isDesignated()) {
155 droolsPdpEntity.setDesignatedDate(new Date());
158 em.getTransaction().commit();
160 cleanup(em, "update");
163 logger.debug("update: Exiting");
168 * Note: A side effect of this boolean method is that if the PDP is designated but not current, the
169 * droolspdpentity.DESIGNATED column will be set to false (the PDP will be un-designated, i.e. marked as
170 * being in standby mode)
173 public boolean isPdpCurrent(DroolsPdp pdp) {
175 boolean isCurrent = isCurrent(pdp);
177 EntityManager em = emf.createEntityManager();
179 if (!isCurrent && pdp.isDesignated()) {
180 em.getTransaction().begin();
181 Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
182 droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
183 List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE)
184 .setFlushMode(FlushModeType.COMMIT).getResultList();
185 if (droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
186 logger.debug("isPdpCurrent: PDP={} designated but not current; setting designated to false",
188 DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
189 droolsPdpEntity.setDesignated(false);
190 em.getTransaction().commit();
192 logger.warn("isPdpCurrent: PDP={} is designated but not current; "
193 + "however it does not have a DB entry, so cannot set DESIGNATED to false!",
197 logger.debug("isPdpCurrent: For PDP= {}, "
198 + "designated={}, isCurrent={}", pdp.getPdpId(), pdp.isDesignated(), isCurrent);
200 } catch (Exception e) {
201 logger.error("Could not update expired record marked as designated in the database", e);
203 cleanup(em, "isPdpCurrent");
210 public void setDesignated(DroolsPdp pdp, boolean designated) {
212 logger.debug("setDesignated: Entering, pdpId={}"
213 + ", designated={}", pdp.getPdpId(), designated);
215 EntityManager em = null;
217 em = emf.createEntityManager();
218 em.getTransaction().begin();
219 Query droolsPdpsListQuery = em
220 .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
221 droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
222 List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
223 LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
224 if (droolsPdpsList.size() == 1
225 && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
226 DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList
229 logger.debug("setDesignated: PDP={}"
230 + " found, designated= {}"
231 + ", setting to {}", pdp.getPdpId(), droolsPdpEntity.isDesignated(),
233 droolsPdpEntity.setDesignated(designated);
235 em.refresh(droolsPdpEntity); //make sure we get the DB value
236 if (!droolsPdpEntity.isDesignated()) {
237 droolsPdpEntity.setDesignatedDate(new Date());
241 em.getTransaction().commit();
243 logger.error("setDesignated: PDP={}"
244 + " not in DB; cannot update designation", pdp.getPdpId());
246 } catch (Exception e) {
247 logger.error("setDesignated: Caught Exception", e);
249 cleanup(em, "setDesignated");
252 logger.debug("setDesignated: Exiting");
258 public void standDownPdp(String pdpId) {
259 logger.debug("standDownPdp: Entering, pdpId={}", pdpId);
261 EntityManager em = null;
266 em = emf.createEntityManager();
267 em.getTransaction().begin();
270 * Get droolspdpentity record for this PDP and mark DESIGNATED as
273 Query droolsPdpsListQuery = em
274 .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
275 droolsPdpsListQuery.setParameter("pdpId", pdpId);
276 List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
277 LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
278 DroolsPdpEntity droolsPdpEntity;
279 if (droolsPdpsList.size() == 1
280 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) {
281 droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
282 droolsPdpEntity.setDesignated(false);
283 em.persist(droolsPdpEntity);
284 logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId );
286 logger.error("standDownPdp: Missing record in droolspdpentity for pdpId={}"
287 + "; cannot stand down PDP", pdpId);
293 em.getTransaction().commit();
294 cleanup(em, "standDownPdp");
297 // Keep the election handler in sync with the DB
298 DroolsPdpsElectionHandler.setMyPdpDesignated(false);
300 } catch (Exception e) {
301 logger.error("standDownPdp: Unexpected Exception attempting to mark "
302 + "DESIGNATED as false for droolspdpentity, pdpId={}"
303 + ". Cannot stand down PDP; message={}", pdpId, e.getMessage(), e);
305 cleanup(em, "standDownPdp");
307 logger.debug("standDownPdp: Exiting");
312 * Determines whether or not a designated PDP has failed.
314 * Note: The update method, which is run periodically by the
315 * TimerUpdateClass, will un-designate a PDP that is stale.
318 public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps) {
320 logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size());
322 boolean failed = true;
323 boolean foundDesignatedPdp = false;
325 for (DroolsPdp pdp : pdps) {
328 * Normally, the update method will un-designate any stale PDP, but
329 * we check here to see if the PDP has gone stale since the update
332 * Even if we determine that the designated PDP is current, we keep
333 * going (we don't break), so we can get visibility into the other
334 * PDPs, when in DEBUG mode.
336 if (pdp.isDesignated() && isCurrent(pdp)) {
337 logger.debug("hasDesignatedPdpFailed: Designated PDP={} is current", pdp.getPdpId());
339 foundDesignatedPdp = true;
340 } else if (pdp.isDesignated() && !isCurrent(pdp)) {
341 logger.error("hasDesignatedPdpFailed: Designated PDP={} has failed", pdp.getPdpId());
342 foundDesignatedPdp = true;
344 logger.debug("hasDesignatedPdpFailed: PDP={} is not designated", pdp.getPdpId());
348 logger.debug("hasDesignatedPdpFailed: Exiting and returning, foundDesignatedPdp={}",
354 private boolean isCurrent(DroolsPdp pdp) {
356 logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId());
358 boolean current = false;
360 // Return if the current PDP is considered "current" based on whatever
361 // time box that may be.
362 // If the the PDP is not current, we should mark it as not primary in
364 Date currentDate = new Date();
365 long difference = currentDate.getTime()
366 - pdp.getUpdatedDate().getTime();
367 // just set some kind of default here
368 long pdpTimeout = 15000;
370 pdpTimeout = Long.parseLong(ActiveStandbyProperties
371 .getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
372 logger.debug("isCurrent: pdp.timeout={}", pdpTimeout);
373 } catch (Exception e) {
374 logger.error("isCurrent: Could not get PDP timeout property, using default.", e);
376 current = difference < pdpTimeout;
378 logger.debug("isCurrent: Exiting, difference={}, pdpTimeout={}"
379 + "; returning current={}", difference, pdpTimeout, current);
386 * Currently this method is only used in a JUnit test environment. Gets a
387 * PDP record from droolspdpentity table.
390 public DroolsPdpEntity getPdp(String pdpId) {
392 logger.debug("getPdp: Entering and getting PDP with pdpId={}", pdpId);
394 DroolsPdpEntity droolsPdpEntity = null;
396 EntityManager em = null;
398 em = emf.createEntityManager();
399 em.getTransaction().begin();
400 Query droolsPdpsListQuery = em
401 .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
402 droolsPdpsListQuery.setParameter("pdpId", pdpId);
403 List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
404 LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
405 if (droolsPdpsList.size() == 1
406 && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
407 droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
408 logger.debug("getPdp: PDP={}"
409 + " found, isDesignated={},"
410 + " updatedDate={}, "
411 + "priority={}", pdpId,
412 droolsPdpEntity.isDesignated(), droolsPdpEntity.getUpdatedDate(),
413 droolsPdpEntity.getPriority());
415 // Make sure the droolsPdpEntity is not a cached version
416 em.refresh(droolsPdpEntity);
418 em.getTransaction().commit();
420 logger.error("getPdp: PDP={} not found!?", pdpId);
422 } catch (Exception e) {
423 logger.error("getPdp: Caught Exception attempting to get PDP", e);
425 cleanup(em, "getPdp");
428 logger.debug("getPdp: Returning droolsPdpEntity={}", droolsPdpEntity);
429 return droolsPdpEntity;
434 * Normally this method should only be used in a JUnit test environment.
435 * Manually inserts a PDP record in droolspdpentity table.
438 public void insertPdp(DroolsPdp pdp) {
439 logger.debug("insertPdp: Entering and manually inserting PDP");
444 EntityManager em = emf.createEntityManager();
446 em.getTransaction().begin();
451 DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity();
452 em.persist(droolsPdpEntity);
453 droolsPdpEntity.setPdpId(pdp.getPdpId());
454 droolsPdpEntity.setDesignated(pdp.isDesignated());
455 droolsPdpEntity.setPriority(pdp.getPriority());
456 droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
457 droolsPdpEntity.setSiteName(pdp.getSiteName());
462 em.getTransaction().commit();
464 cleanup(em, "insertPdp");
466 logger.debug("insertPdp: Exiting");
471 * Normally this method should only be used in a JUnit test environment.
472 * Manually deletes all PDP records in droolspdpentity table.
475 public void deleteAllPdps() {
477 logger.debug("deleteAllPdps: Entering");
482 EntityManager em = emf.createEntityManager();
484 em.getTransaction().begin();
486 Query droolsPdpsListQuery = em
487 .createQuery("SELECT p FROM DroolsPdpEntity p");
488 @SuppressWarnings("unchecked")
489 List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode(
490 LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
491 logger.debug("deleteAllPdps: Deleting {} PDPs", droolsPdpsList.size());
492 for (DroolsPdp droolsPdp : droolsPdpsList) {
493 String pdpId = droolsPdp.getPdpId();
500 em.getTransaction().commit();
502 cleanup(em, "deleteAllPdps");
504 logger.debug("deleteAllPdps: Exiting");
509 * Normally this method should only be used in a JUnit test environment.
510 * Manually deletes a PDP record in droolspdpentity table.
513 public void deletePdp(String pdpId) {
514 logger.debug("deletePdp: Entering and manually deleting pdpId={}", pdpId);
519 EntityManager em = emf.createEntityManager();
521 em.getTransaction().begin();
526 DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId);
527 if (droolsPdpEntity != null) {
528 logger.debug("deletePdp: Removing PDP");
529 em.remove(droolsPdpEntity);
531 logger.debug("deletePdp: PDP with ID={} not currently in DB", pdpId);
537 em.getTransaction().commit();
539 cleanup(em, "deletePdp");
541 logger.debug("deletePdp: Exiting");
546 * Close the specified EntityManager, rolling back any pending transaction
548 * @param em the EntityManager to close ('null' is OK)
549 * @param method the invoking Java method (used for log messages)
551 private static void cleanup(EntityManager em, String method) {
552 if (em != null && em.isOpen()) {
553 if (em.getTransaction().isActive()) {
554 // there is an active EntityTransaction -- roll it back
556 em.getTransaction().rollback();
557 } catch (Exception e) {
558 logger.error(method + ": Caught Exception attempting to rollback EntityTransaction,", e);
562 // now, close the EntityManager
565 } catch (Exception e) {
566 logger.error(method + ": Caught Exception attempting to close EntityManager, ", e);