* ONAP
* ================================================================================
* Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
throw new IllegalArgumentException("The target type is null");
}
- switch (targetType) {
- case PNF:
- return detmPnfTarget();
- case VM:
- case VNF:
- case VFMODULE:
- return detmVfModuleTarget();
- default:
- throw new IllegalArgumentException("The target type is not supported");
- }
+ return switch (targetType) {
+ case PNF -> detmPnfTarget();
+ case VM, VNF, VFMODULE -> detmVfModuleTarget();
+ default -> throw new IllegalArgumentException("The target type is not supported");
+ };
}
/**
* ONAP
* ================================================================================
* Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.controlloop.eventmanager;
+import java.io.Serial;
import java.util.Deque;
import java.util.LinkedList;
import java.util.UUID;
/**
* Manager for a single control loop event, with operation outcomes.
*/
+@Getter
public abstract class ClEventManagerWithOutcome<T extends Step> extends ClEventManagerWithSteps<T>
implements StepContext {
+ @Serial
private static final long serialVersionUID = -1216568161322872641L;
/**
* Number of attempts, so far, for the current step.
*/
- @Getter
private int attempts;
/**
* Full history of operations that have been processed by the rules. This includes the
* items in {@link #partialHistory}.
*/
- @Getter
private final transient Deque<OperationOutcome2> fullHistory = new LinkedList<>();
/**
* When a step is started, its "start" outcome is added. However, once it completes,
* its "start" outcome is removed and the "completed" outcome is added.
*/
- @Getter
private final transient Deque<OperationOutcome2> partialHistory = new LinkedList<>();
* ONAP
* ================================================================================
* Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.controlloop.eventmanager;
+import java.io.Serial;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedHashMap;
public abstract class ClEventManagerWithSteps<T extends Step> extends ControlLoopEventManager implements StepContext {
private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithSteps.class);
+ @Serial
private static final long serialVersionUID = -1216568161322872641L;
/**
/**
* Result of the last policy operation. This is just a place where the rules can store
- * the value for passing to {@link #loadNextPolicy()}.
+ * the value for passing to {@link #loadNextPolicy(OperationResult)}.
*/
@Getter
@Setter
}
// initialize the step so we can query its properties
+ assert getSteps().peek() != null;
getSteps().peek().init();
}
* Takes records from {@link #operations} and stores them in the queue. Continues to
* run until {@link #stop()} is invoked, or the thread is interrupted.
*
- * @param emfactory entity manager factory
+ * @param factory entity manager factory
*/
- private void run(EntityManagerFactory emfactory) {
- try {
+ private void run(EntityManagerFactory factory) {
+ try (factory) {
// store records until stopped, continuing if an exception occurs
while (!stopped) {
try {
Record triple = operations.take();
- storeBatch(emfactory.createEntityManager(), triple);
+ storeBatch(factory.createEntityManager(), triple);
} catch (RuntimeException e) {
logger.error("failed to save data to operation history table", e);
}
}
- storeRemainingRecords(emfactory);
+ storeRemainingRecords(factory);
} finally {
synchronized (this) {
stopped = true;
}
- emfactory.close();
}
}
/**
* Store any remaining records, but stop at the first exception.
*
- * @param emfactory entity manager factory
+ * @param factory entity manager factory
*/
- private void storeRemainingRecords(EntityManagerFactory emfactory) {
+ private void storeRemainingRecords(EntityManagerFactory factory) {
try {
while (!operations.isEmpty()) {
- storeBatch(emfactory.createEntityManager(), operations.poll());
+ try (var em = factory.createEntityManager()) {
+ storeBatch(em, operations.poll());
+ }
}
} catch (RuntimeException e) {
private void storeBatch(EntityManager entityManager, Record firstRecord) {
logger.info("store operation history record batch");
- try (var emc = new EntityMgrCloser(entityManager);
- var trans = new EntityTransCloser(entityManager.getTransaction())) {
+ try (var ignored = new EntityMgrCloser(entityManager);
+ var trans = new EntityTransCloser(entityManager.getTransaction())) {
var nrecords = 0;
var rec = firstRecord;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import java.io.Serial;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
-import org.onap.policy.controlloop.actorserviceprovider.Operator;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.drools.core.lock.LockCallback;
import org.onap.policy.drools.core.lock.LockImpl;
private final PolicyEngine engineMgr = mock(PolicyEngine.class);
private final WorkingMemory workMem = mock(WorkingMemory.class);
private final InternalFactHandle factHandle = mock(InternalFactHandle.class);
- private final Operator policyOperator = mock(Operator.class);
private final Operation policyOperation = mock(Operation.class);
- private final Actor policyActor = mock(Actor.class);
private final ExecutorService executor = mock(ExecutorService.class);
private final EventManagerServices services = mock(EventManagerServices.class);
private final ActorService actors = mock(ActorService.class);
private final MyStep stepb = mock(MyStep.class);
private List<LockImpl> locks;
- private ToscaPolicy tosca;
private ControlLoopParams params;
private ClEventManagerWithSteps<MyStep> mgr;
}
@Test
- public void testLoadNextPolicy() throws Exception {
+ void testLoadNextPolicy() throws Exception {
loadPolicy(EVENT_MGR_MULTI_YAML);
mgr = new MyManager(services, params, REQ_ID, workMem);
// start and load step for first policy
mgr.start();
- assertEquals("OperationA", mgr.getSteps().poll().getOperationName());
+ assertEquals("OperationA", Objects.requireNonNull(mgr.getSteps().poll()).getOperationName());
assertNull(mgr.getFinalResult());
// indicate success and load next policy
mgr.loadNextPolicy(OperationResult.SUCCESS);
- assertEquals("OperationB", mgr.getSteps().poll().getOperationName());
+ assertEquals("OperationB", Objects.requireNonNull(mgr.getSteps().poll()).getOperationName());
assertNull(mgr.getFinalResult());
// indicate failure - should go to final failure
private void loadPolicy(String fileName) throws CoderException {
var template = yamlCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
- tosca = template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
+ ToscaPolicy tosca = template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
params.setToscaPolicy(tosca);
}
private class MyManager extends ClEventManagerWithSteps<MyStep> {
+ @Serial
private static final long serialVersionUID = 1L;
public MyManager(EventManagerServices services, ControlLoopParams params, UUID requestId, WorkingMemory workMem)
private static class RealManager extends ClEventManagerWithSteps<MyStep> {
+ @Serial
private static final long serialVersionUID = 1L;
public RealManager(EventManagerServices services, ControlLoopParams params, UUID requestId,
</description>
<properties>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
+ <maven.compiler.release>17</maven.compiler.release>
</properties>
<build>