2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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.controlloop.eventmanager;
23 import java.io.Serializable;
24 import java.util.Deque;
25 import java.util.HashMap;
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.ConcurrentLinkedDeque;
31 import java.util.concurrent.Executor;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.ForkJoinPool;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicLong;
36 import lombok.AccessLevel;
38 import lombok.ToString;
39 import org.onap.policy.controlloop.ControlLoopException;
40 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
43 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
44 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
45 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
46 import org.onap.policy.drools.core.lock.LockCallback;
47 import org.onap.policy.drools.system.PolicyEngineConstants;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Manager for a single event. Once this has been created, the event can be retracted from
53 * working memory. Invoke {@link #isActive()} to determine if the manager is active (i.e.,
54 * hasn't been replicated from another server). When the manager is no longer needed,
55 * {@link #destroy()} should be invoked.
57 @ToString(onlyExplicitlyIncluded = true)
58 public class ControlLoopEventManager implements StepContext, Serializable {
60 private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager.class);
61 private static final long serialVersionUID = -1216568161322872641L;
64 * Data manager used when the policy engine's guard.disabled property is "true".
66 private static final OperationHistoryDataManager STUB_DATA_MANAGER = new OperationHistoryDataManagerStub();
68 private static final String GUARD_DISABLED_PROPERTY = "guard.disabled";
69 private static final String EVENT_MANAGER_SERVICE_CONFIG = "event-manager";
72 * Counts the number of these objects that have been created. This is used by junit
75 private static final AtomicLong createCount = new AtomicLong(0);
78 * {@code True} if this object was created by this JVM instance, {@code false}
79 * otherwise. This will be {@code false} if this object is reconstituted from a
80 * persistent store or by transfer from another server.
82 private transient boolean createdByThisJvmInstance;
86 public final String closedLoopControlName;
89 private final UUID requestId;
92 * Time, in milliseconds, when the control loop will time out.
95 private final long endTimeMs;
97 // fields extracted from the ControlLoopParams
99 private final String policyName;
101 private final String policyVersion;
104 * Maps a target entity to its lock.
106 private final transient Map<String, LockData> target2lock = new HashMap<>();
108 @Getter(AccessLevel.PROTECTED)
109 private final ControlLoopProcessor processor;
112 * Set of properties used while processing the event.
114 private Map<String, Serializable> properties = new ConcurrentHashMap<>();
117 * Unprocessed outcomes from the operations. Outcomes are added to this each time the
118 * "start" or "complete" callback is invoked, typically by an operation running in a
119 * background thread, thus it must be thread safe.
122 private final transient Deque<OperationOutcome> outcomes = new ConcurrentLinkedDeque<>();
126 * Constructs the object.
128 * @param params control loop parameters
129 * @param requestId event request ID
130 * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
133 public ControlLoopEventManager(ControlLoopParams params, UUID requestId) throws ControlLoopException {
135 createCount.incrementAndGet();
137 this.createdByThisJvmInstance = true;
138 this.closedLoopControlName = params.getClosedLoopControlName();
139 this.requestId = requestId;
140 this.policyName = params.getPolicyName();
141 this.policyVersion = params.getPolicyVersion();
142 this.processor = new ControlLoopProcessor(params.getToscaPolicy());
143 this.endTimeMs = System.currentTimeMillis() + detmControlLoopTimeoutMs();
147 * Gets the number of manager objects that have been created.
149 * @return the number of manager objects that have been created
151 public static long getCreateCount() {
152 return createCount.get();
156 * Determines if the manager is still active.
158 * @return {@code true} if the manager is still active, {@code false} otherwise
160 public boolean isActive() {
161 return createdByThisJvmInstance;
165 * Cancels the current operation and frees all locks.
167 public void destroy() {
169 getBlockingExecutor().execute(this::freeAllLocks);
176 private void freeAllLocks() {
177 target2lock.values().forEach(LockData::free);
181 * Determines the overall control loop timeout.
183 * @return the policy timeout, in milliseconds, if specified, a default timeout
186 private long detmControlLoopTimeoutMs() {
187 // validation checks preclude null or 0 timeout values in the policy
188 Integer timeout = processor.getPolicy().getProperties().getTimeout();
189 return TimeUnit.MILLISECONDS.convert(timeout, TimeUnit.SECONDS);
193 * Requests a lock. This requests the lock for the time that remains before the
194 * timeout expires. This avoids having to extend the lock.
196 * @param targetEntity entity to be locked
197 * @return a future that can be used to await the lock
200 public synchronized CompletableFuture<OperationOutcome> requestLock(String targetEntity) {
202 long remainingMs = endTimeMs - System.currentTimeMillis();
203 int remainingSec = 15 + Math.max(0, (int) TimeUnit.SECONDS.convert(remainingMs, TimeUnit.MILLISECONDS));
205 LockData data = target2lock.computeIfAbsent(targetEntity, key -> {
206 LockData data2 = new LockData(key, requestId);
207 makeLock(targetEntity, requestId.toString(), remainingSec, data2);
209 data2.addUnavailableCallback(this::onComplete);
214 return data.getFuture();
217 public void onStart(OperationOutcome outcome) {
218 outcomes.add(outcome);
221 public void onComplete(OperationOutcome outcome) {
222 outcomes.add(outcome);
226 * Determines if the context contains a property.
228 * @param name name of the property of interest
229 * @return {@code true} if the context contains the property, {@code false} otherwise
232 public boolean contains(String name) {
233 return properties.containsKey(name);
237 * Gets a property, casting it to the desired type.
239 * @param <T> desired type
240 * @param name name of the property whose value is to be retrieved
241 * @return the property's value, or {@code null} if it does not yet have a value
244 @SuppressWarnings("unchecked")
245 public <T> T getProperty(String name) {
246 return (T) properties.get(name);
250 * Sets a property's value.
252 * @param name property name
253 * @param value new property value
256 public void setProperty(String name, Serializable value) {
257 logger.info("set property {}={} manager={}", name, value, this);
258 properties.put(name, value);
262 * Removes a property.
264 * @param name property name
267 public void removeProperty(String name) {
268 properties.remove(name);
272 * Initializes various components, on demand.
274 private static class LazyInitData {
275 private static final OperationHistoryDataManager DATA_MANAGER;
276 private static final ActorService ACTOR_SERVICE;
279 EventManagerServices services = new EventManagerServices(EVENT_MANAGER_SERVICE_CONFIG);
280 ACTOR_SERVICE = services.getActorService();
281 DATA_MANAGER = services.getDataManager();
285 // the following methods may be overridden by junit tests
287 public Executor getExecutor() {
288 return ForkJoinPool.commonPool();
291 protected ExecutorService getBlockingExecutor() {
292 return PolicyEngineConstants.getManager().getExecutorService();
295 protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
296 PolicyEngineConstants.getManager().createLock(targetEntity, requestId, holdSec, callback, false);
299 public ActorService getActorService() {
300 return LazyInitData.ACTOR_SERVICE;
303 public OperationHistoryDataManager getDataManager() {
304 boolean guardDisabled = "true".equalsIgnoreCase(getEnvironmentProperty(GUARD_DISABLED_PROPERTY));
305 return (guardDisabled ? STUB_DATA_MANAGER : LazyInitData.DATA_MANAGER);
308 protected String getEnvironmentProperty(String propName) {
309 return PolicyEngineConstants.getManager().getEnvironmentProperty(propName);