Make op history classes work with generic events 98/120498/1
authorJim Hahn <jrh3@att.com>
Tue, 13 Apr 2021 18:22:19 +0000 (14:22 -0400)
committerJim Hahn <jrh3@att.com>
Tue, 13 Apr 2021 18:23:53 +0000 (14:23 -0400)
Issue-ID: POLICY-3198
Change-Id: I5b80d35fbb523094ae5464e9c058fd8f2c71ff50
Signed-off-by: Jim Hahn <jrh3@att.com>
controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManager.java
controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ophistory/OperationHistoryDataManager.java
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ophistory/OperationHistoryDataManagerImpl.java
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/ophistory/OperationHistoryDataManagerStub.java
controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ophistory/OperationHistoryDataManagerImplTest.java
controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ophistory/OperationHistoryDataManagerStubTest.java

index 0649f18..d586240 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -550,7 +550,8 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
     public void storeInDataBase(OperationOutcome2 outcome) {
         String targetEntity = getProperty(OperationProperties.AAI_TARGET_ENTITY);
 
-        getDataManager().store(requestIdStr, event, targetEntity, outcome.getClOperation());
+        getDataManager().store(requestIdStr, event.getClosedLoopControlName(), event, targetEntity,
+                        outcome.getClOperation());
     }
 
     /**
index 98758f4..7160f37 100644 (file)
@@ -523,10 +523,9 @@ public class UsecasesEventManagerTest {
      */
     @Test
     public void testLoadPreprocessorStepsNeedTargetEntity() {
-        stepa = new Step2(mgr,
-                ControlLoopOperationParams.builder()
-                        .targetType(TargetType.toTargetType(event.getTargetType()))
-                        .targetEntityIds(Map.of()).build(), event) {
+        stepa = new Step2(mgr, ControlLoopOperationParams.builder()
+                        .targetType(TargetType.toTargetType(event.getTargetType())).targetEntityIds(Map.of()).build(),
+                        event) {
             @Override
             public List<String> getPropertyNames() {
                 return List.of(OperationProperties.AAI_TARGET_ENTITY);
@@ -758,7 +757,8 @@ public class UsecasesEventManagerTest {
 
         mgr.storeInDataBase(mgr.getPartialHistory().peekLast());
 
-        verify(dataMgr).store(REQ_ID.toString(), event, null, mgr.getPartialHistory().peekLast().getClOperation());
+        verify(dataMgr).store(REQ_ID.toString(), event.getClosedLoopControlName(), event, null,
+                        mgr.getPartialHistory().peekLast().getClOperation());
     }
 
     @Test
index 091c38e..6dd372d 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -21,7 +21,6 @@
 package org.onap.policy.controlloop.ophistory;
 
 import org.onap.policy.controlloop.ControlLoopOperation;
-import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
 /**
  * Data manager for the Operation History table.
@@ -33,11 +32,13 @@ public interface OperationHistoryDataManager {
      * discarded.
      *
      * @param requestId request ID
-     * @param event event with which the operation is associated
+     * @param clName control loop name
+     * @param event event with which the operation is associated, typically just used for
+     *        logging
      * @param targetEntity target entity associated with the operation
      * @param operation operation to be stored
      */
-    void store(String requestId, VirtualControlLoopEvent event, String targetEntity, ControlLoopOperation operation);
+    void store(String requestId, String clName, Object event, String targetEntity, ControlLoopOperation operation);
 
     /**
      * Starts the background thread.
index 52a8695..7632a08 100644 (file)
@@ -38,7 +38,6 @@ import org.onap.policy.common.parameters.ValidationResult;
 import org.onap.policy.common.utils.jpa.EntityMgrCloser;
 import org.onap.policy.common.utils.jpa.EntityTransCloser;
 import org.onap.policy.controlloop.ControlLoopOperation;
-import org.onap.policy.controlloop.VirtualControlLoopEvent;
 import org.onap.policy.guard.OperationsHistory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -149,7 +148,7 @@ public class OperationHistoryDataManagerImpl implements OperationHistoryDataMana
     }
 
     @Override
-    public synchronized void store(String requestId, VirtualControlLoopEvent event, String targetEntity,
+    public synchronized void store(String requestId, String clName, Object event, String targetEntity,
                     ControlLoopOperation operation) {
 
         if (stopped) {
@@ -158,7 +157,7 @@ public class OperationHistoryDataManagerImpl implements OperationHistoryDataMana
             return;
         }
 
-        operations.add(new Record(requestId, event, targetEntity, operation));
+        operations.add(new Record(requestId, clName, event, targetEntity, operation));
 
         if (operations.size() > maxQueueLength) {
             Record discarded = operations.remove();
@@ -255,35 +254,28 @@ public class OperationHistoryDataManagerImpl implements OperationHistoryDataMana
      */
     private void storeRecord(EntityManager entityMgr, Record record) {
 
-        final VirtualControlLoopEvent event = record.getEvent();
+        final String reqId = record.getRequestId();
+        final String clName = record.getClName();
         final ControlLoopOperation operation = record.getOperation();
 
-        logger.info("store operation history record for {}", event.getRequestId());
-
-        List<OperationsHistory> results =
-            entityMgr.createQuery("select e from OperationsHistory e"
-                        + " where e.closedLoopName= ?1"
-                        + " and e.requestId= ?2"
-                        + " and e.subrequestId= ?3"
-                        + " and e.actor= ?4"
-                        + " and e.operation= ?5"
-                        + " and e.target= ?6",
-                        OperationsHistory.class)
-                .setParameter(1, event.getClosedLoopControlName())
-                .setParameter(2, record.getRequestId())
-                .setParameter(3, operation.getSubRequestId())
-                .setParameter(4, operation.getActor())
-                .setParameter(5, operation.getOperation())
-                .setParameter(6, record.getTargetEntity())
-                .getResultList();
+        logger.info("store operation history record for {}", reqId);
+
+        List<OperationsHistory> results = entityMgr
+                        .createQuery("select e from OperationsHistory e" + " where e.closedLoopName= ?1"
+                                        + " and e.requestId= ?2" + " and e.subrequestId= ?3" + " and e.actor= ?4"
+                                        + " and e.operation= ?5" + " and e.target= ?6", OperationsHistory.class)
+                        .setParameter(1, clName).setParameter(2, record.getRequestId())
+                        .setParameter(3, operation.getSubRequestId()).setParameter(4, operation.getActor())
+                        .setParameter(5, operation.getOperation()).setParameter(6, record.getTargetEntity())
+                        .getResultList();
 
         if (results.size() > 1) {
-            logger.warn("unexpected operation history record count {} for {}", results.size(), event.getRequestId());
+            logger.warn("unexpected operation history record count {} for {}", results.size(), reqId);
         }
 
         OperationsHistory entry = (results.isEmpty() ? new OperationsHistory() : results.get(0));
 
-        entry.setClosedLoopName(event.getClosedLoopControlName());
+        entry.setClosedLoopName(clName);
         entry.setRequestId(record.getRequestId());
         entry.setActor(operation.getActor());
         entry.setOperation(operation.getOperation());
@@ -303,11 +295,11 @@ public class OperationHistoryDataManagerImpl implements OperationHistoryDataMana
         }
 
         if (results.isEmpty()) {
-            logger.info("insert operation history record for {}", event.getRequestId());
+            logger.info("insert operation history record for {}", reqId);
             ++recordsInserted;
             entityMgr.persist(entry);
         } else {
-            logger.info("update operation history record for {}", event.getRequestId());
+            logger.info("update operation history record for {}", reqId);
             ++recordsUpdated;
             entityMgr.merge(entry);
         }
@@ -337,7 +329,8 @@ public class OperationHistoryDataManagerImpl implements OperationHistoryDataMana
     @ToString
     private static class Record {
         private String requestId;
-        private VirtualControlLoopEvent event;
+        private String clName;
+        private Object event;
         private String targetEntity;
         private ControlLoopOperation operation;
     }
index bd2ee04..ce5f1c1 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -21,7 +21,6 @@
 package org.onap.policy.controlloop.ophistory;
 
 import org.onap.policy.controlloop.ControlLoopOperation;
-import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
 /**
  * Stub implementation of a data manager; all methods return without doing anything.
@@ -29,7 +28,7 @@ import org.onap.policy.controlloop.VirtualControlLoopEvent;
 public class OperationHistoryDataManagerStub implements OperationHistoryDataManager {
 
     @Override
-    public void store(String requestId, VirtualControlLoopEvent event, String targetEntity,
+    public void store(String requestId, String clName, Object event, String targetEntity,
                     ControlLoopOperation operation) {
         // do nothing
     }
index a360a74..9d32a85 100644 (file)
@@ -173,7 +173,7 @@ public class OperationHistoryDataManagerImplTest {
     @Test
     public void testStore_testStop() throws InterruptedException {
         // store
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         runThread();
 
@@ -200,7 +200,7 @@ public class OperationHistoryDataManagerImplTest {
         mgr.stop();
 
         // store
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         assertEquals(0, mgr.getRecordsCommitted());
     }
@@ -212,7 +212,7 @@ public class OperationHistoryDataManagerImplTest {
     public void testStoreTooManyItems() throws InterruptedException {
         final int nextra = 5;
         for (int nitems = 0; nitems < MAX_QUEUE_LENGTH + nextra; ++nitems) {
-            mgr.store(REQ_ID, event, MY_ENTITY, operation);
+            mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
         }
 
         runThread();
@@ -233,9 +233,9 @@ public class OperationHistoryDataManagerImplTest {
         mgr = new RealThread();
         mgr.start();
 
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         waitForThread();
 
@@ -269,9 +269,9 @@ public class OperationHistoryDataManagerImplTest {
         mgr = new RealThread();
         mgr.start();
 
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         waitForThread();
 
@@ -286,7 +286,7 @@ public class OperationHistoryDataManagerImplTest {
         // arrange to throw an exception
         when(emfSpy.createEntityManager()).thenThrow(EXPECTED_EXCEPTION);
 
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         runThread();
     }
@@ -299,25 +299,25 @@ public class OperationHistoryDataManagerImplTest {
          */
 
         // no start time
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         // no end time
         operation = new ControlLoopOperation(operation);
         operation.setSubRequestId(UUID.randomUUID().toString());
         operation.setStart(Instant.now());
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         // both start and end times
         operation = new ControlLoopOperation(operation);
         operation.setSubRequestId(UUID.randomUUID().toString());
         operation.setEnd(Instant.now());
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         // only end time
         operation = new ControlLoopOperation(operation);
         operation.setSubRequestId(UUID.randomUUID().toString());
         operation.setStart(null);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         runThread();
 
@@ -341,22 +341,22 @@ public class OperationHistoryDataManagerImplTest {
 
         // no start time
         operation.setStart(null);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         // no end time
         operation = new ControlLoopOperation(operation);
         operation.setStart(Instant.now());
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         // both start and end times
         operation = new ControlLoopOperation(operation);
         operation.setEnd(Instant.now());
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         // only end time
         operation = new ControlLoopOperation(operation);
         operation.setStart(null);
-        mgr.store(REQ_ID, event, MY_ENTITY, operation);
+        mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
 
         runThread();
 
index a6f5062..4774af4 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -30,7 +30,7 @@ public class OperationHistoryDataManagerStubTest {
     public void test() {
         OperationHistoryDataManagerStub mgr = new OperationHistoryDataManagerStub();
 
-        assertThatCode(() -> mgr.store(null, null, null, null)).doesNotThrowAnyException();
+        assertThatCode(() -> mgr.store(null, null, null, null, null)).doesNotThrowAnyException();
         assertThatCode(() -> mgr.stop()).doesNotThrowAnyException();
     }
 }