2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.runtime.impl;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.concurrent.BlockingQueue;
41 import org.onap.policy.apex.context.ContextException;
42 import org.onap.policy.apex.context.ContextRuntimeException;
43 import org.onap.policy.apex.context.SchemaHelper;
44 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
45 import org.onap.policy.apex.core.engine.engine.ApexEngine;
46 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
47 import org.onap.policy.apex.core.engine.event.EnEvent;
48 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
51 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
52 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
53 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
54 import org.onap.policy.apex.model.basicmodel.service.ModelService;
55 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
56 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
57 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
58 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
59 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
60 import org.onap.policy.apex.service.engine.event.ApexEvent;
61 import org.onap.policy.apex.service.engine.event.impl.enevent.ApexEvent2EnEventConverter;
62 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
63 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
64 import org.onap.policy.apex.service.engine.runtime.EngineService;
65 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
66 import org.slf4j.ext.XLogger;
67 import org.slf4j.ext.XLoggerFactory;
70 * The Class EngineWorker encapsulates a core {@link ApexEngine} instance, which runs policies defined in the
71 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxModelAxModel}. Each policy is triggered by an Apex event, and
72 * when the policy is triggered it runs through to completion in the ApexEngine.
74 * <p>This class acts as a container for an {@link ApexEngine}, running it in a thread, sending it events, and receiving
77 * @author Liam Fallon (liam.fallon@ericsson.com)
79 final class EngineWorker implements EngineService {
80 // Logger for this class
81 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngineService.class);
83 // Recurring string constants
84 private static final String ENGINE_FOR_KEY_PREFIX = "apex engine for engine key ";
85 private static final String ENGINE_SUFFIX = " of this engine";
86 private static final String BAD_KEY_MATCH_TAG = " does not match the key";
87 private static final String ENGINE_KEY_PREFIX = "engine key ";
89 // The ID of this engine
90 private final AxArtifactKey engineWorkerKey;
92 // The Apex engine which is running the policies in this worker
93 private final ApexEngine engine;
95 // The event processor is an inner class, an instance of which runs as a thread that reads
96 // incoming events from a queue and forwards them to the Apex engine
97 private EventProcessor processor = null;
99 // Thread handling for the worker
100 private final ApplicationThreadFactory threadFactory;
101 private Thread processorThread;
103 // Converts ApexEvent instances to and from EnEvent instances
104 private ApexEvent2EnEventConverter apexEnEventConverter = null;
107 private boolean isSubsequentInstance;
110 * Constructor that creates an Apex engine, an event processor for events to be sent to that engine, and an
111 * {@link ApexModelReader} instance to read Apex models using JAXB.
113 * @param engineWorkerKey the engine worker key
114 * @param queue the queue on which events for this Apex worker will come
115 * @param threadFactory the thread factory to use for creating the event processing thread
116 * @throws ApexException thrown on errors on worker instantiation
118 protected EngineWorker(final AxArtifactKey engineWorkerKey, final BlockingQueue<ApexEvent> queue,
119 final ApplicationThreadFactory threadFactory) {
120 LOGGER.entry(engineWorkerKey);
122 this.engineWorkerKey = engineWorkerKey;
123 this.threadFactory = threadFactory;
125 // Create the Apex engine
126 engine = new ApexEngineFactory().createApexEngine(engineWorkerKey);
128 // Create and run the event processor
129 processor = new EventProcessor(queue);
131 // Set the Event converter up
132 apexEnEventConverter = new ApexEvent2EnEventConverter(engine);
141 public void registerActionListener(final String listenerName, final ApexEventListener apexEventListener) {
142 engine.addEventListener(listenerName, new EnEventListenerImpl(apexEventListener, apexEnEventConverter));
149 public void deregisterActionListener(final String listenerName) {
150 engine.removeEventListener(listenerName);
157 public EngineServiceEventInterface getEngineServiceEventInterface() {
158 throw new UnsupportedOperationException(
159 "getEngineServiceEventInterface() call is not allowed on an Apex Engine Worker");
166 public AxArtifactKey getKey() {
167 return engineWorkerKey;
174 public Collection<AxArtifactKey> getEngineKeys() {
175 return Arrays.asList(engineWorkerKey);
182 public AxArtifactKey getApexModelKey() {
183 if (ModelService.existsModel(AxPolicyModel.class)) {
184 return ModelService.getModel(AxPolicyModel.class).getKey();
194 public void updateModel(final AxArtifactKey engineKey, final String engineModel, final boolean forceFlag)
195 throws ApexException {
196 LOGGER.entry(engineKey);
198 // Read the Apex model into memory using the Apex Model Reader
199 AxPolicyModel apexPolicyModel = null;
201 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
202 apexPolicyModel = modelReader.read(new ByteArrayInputStream(engineModel.getBytes()));
203 } catch (final ApexModelException e) {
204 throw new ApexException("failed to unmarshal the apex model on engine " + engineKey.getId(), e);
207 // Update the Apex model in the Apex engine
208 updateModel(engineKey, apexPolicyModel, forceFlag);
217 public void updateModel(final AxArtifactKey engineKey, final AxPolicyModel apexModel, final boolean forceFlag)
218 throws ApexException {
219 LOGGER.entry(engineKey);
221 // Check if the key on the update request is correct
222 if (!engineWorkerKey.equals(engineKey)) {
224 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX;
225 throw new ApexException(message);
228 // Check model compatibility
229 if (ModelService.existsModel(AxPolicyModel.class)) {
230 // The current policy model may or may not be defined
231 final AxPolicyModel currentModel = ModelService.getModel(AxPolicyModel.class);
232 if (!currentModel.getKey().isCompatible(apexModel.getKey())) {
235 "apex model update forced, supplied model with key \"{}\" is not a compatible model update "
236 + "from the existing engine model with key \"{}\"",
237 apexModel.getKey().getId(), currentModel.getKey().getId());
239 throw new ContextException(
240 "apex model update failed, supplied model with key \"" + apexModel.getKey().getId()
241 + "\" is not a compatible model update from the existing engine model with key \""
242 + currentModel.getKey().getId() + "\"");
246 // Update the Apex model in the Apex engine
247 engine.updateModel(apexModel, isSubsequentInstance);
249 LOGGER.debug("engine model {} added to the engine-{}", apexModel.getKey().getId(), engineWorkerKey);
257 public AxEngineState getState() {
258 return engine.getState();
265 public void startAll() throws ApexException {
266 start(this.getKey());
273 public void start(final AxArtifactKey engineKey) throws ApexException {
274 LOGGER.entry(engineKey);
276 // Check if the key on the start request is correct
277 if (!engineWorkerKey.equals(engineKey)) {
278 throw new ApexException(ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId()
282 // Starts the event processing thread that handles incoming events
283 if (processorThread != null && processorThread.isAlive()) {
285 ENGINE_FOR_KEY_PREFIX + engineWorkerKey.getId() + " is already running with state " + getState();
286 LOGGER.error(message);
287 throw new ApexException(message);
293 // Start a thread to process events for the engine
294 processorThread = threadFactory.newThread(processor);
295 processorThread.start();
297 LOGGER.exit(engineKey);
304 public void stop() throws ApexException {
312 public void stop(final AxArtifactKey engineKey) throws ApexException {
313 // Check if the key on the start request is correct
314 if (!engineWorkerKey.equals(engineKey)) {
315 LOGGER.warn(ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId()
317 throw new ApexException(ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId()
321 // Interrupt the worker to stop its thread
322 if (processorThread == null || !processorThread.isAlive()) {
323 processorThread = null;
326 ENGINE_FOR_KEY_PREFIX + engineWorkerKey.getId() + " is already stopped with state " + getState());
330 // Interrupt the thread that is handling events toward the engine
331 processorThread.interrupt();
332 processorThread = null;
337 LOGGER.exit(engineKey);
344 public void clear() throws ApexException {
345 clear(this.getKey());
352 public void clear(final AxArtifactKey engineKey) throws ApexException {
353 // Check if the key on the start request is correct
354 if (!engineWorkerKey.equals(engineKey)) {
355 LOGGER.warn(ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId()
357 throw new ApexException(ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId()
361 // Interrupt the worker to stop its thread
362 if (processorThread != null && !processorThread.isAlive()) {
363 LOGGER.warn(ENGINE_FOR_KEY_PREFIX + engineWorkerKey.getId() + " is not stopped with state " + getState());
370 LOGGER.exit(engineKey);
377 public boolean isStarted() {
378 return isStarted(this.getKey());
385 public boolean isStarted(final AxArtifactKey engineKey) {
386 final AxEngineState engstate = getState();
394 return processorThread != null && processorThread.isAlive() && !processorThread.isInterrupted();
405 public boolean isStopped() {
406 return isStopped(this.getKey());
413 public boolean isStopped(final AxArtifactKey engineKey) {
414 final AxEngineState engstate = getState();
422 return processorThread == null || !processorThread.isAlive();
433 public void startPeriodicEvents(final long period) {
434 throw new UnsupportedOperationException("startPeriodicEvents() call is not allowed on an Apex Engine Worker");
441 public void stopPeriodicEvents() {
442 throw new UnsupportedOperationException("stopPeriodicEvents() call is not allowed on an Apex Engine Worker");
449 public String getStatus(final AxArtifactKey engineKey) {
450 // Get the information from the engine that we want to return
451 final AxEngineModel apexEngineModel = engine.getEngineStatus();
452 apexEngineModel.getKeyInformation().generateKeyInfo(apexEngineModel);
454 // Convert that information into a string
456 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
457 final ApexModelWriter<AxEngineModel> modelWriter = new ApexModelWriter<>(AxEngineModel.class);
458 modelWriter.setJsonOutput(true);
459 modelWriter.write(apexEngineModel, baOutputStream);
460 return baOutputStream.toString();
461 } catch (final Exception e) {
462 LOGGER.warn("error outputting runtime information for engine {}", engineWorkerKey, e);
471 public List<AxEngineModel> getEngineStats() {
472 List<AxEngineModel> engineStats = new ArrayList<>();
473 engineStats.add(engine.getEngineStatus());
481 public String getRuntimeInfo(final AxArtifactKey engineKey) {
482 // We'll build up the JSON string for runtime information bit by bit
483 final StringBuilder runtimeJsonStringBuilder = new StringBuilder();
485 // Get the engine information
486 final AxEngineModel engineModel = engine.getEngineStatus();
487 final Map<AxArtifactKey, Map<String, Object>> engineContextAlbums = engine.getEngineContext();
489 // Use GSON to convert our context information into JSON
490 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
492 // Get context into a JSON string
493 runtimeJsonStringBuilder.append("{\"TimeStamp\":");
494 runtimeJsonStringBuilder.append(engineModel.getTimestamp());
495 runtimeJsonStringBuilder.append(",\"State\":");
496 runtimeJsonStringBuilder.append(engineModel.getState());
497 runtimeJsonStringBuilder.append(",\"Stats\":");
498 runtimeJsonStringBuilder.append(gson.toJson(engineModel.getStats()));
500 // Get context into a JSON string
501 runtimeJsonStringBuilder.append(",\"ContextAlbums\":[");
503 boolean firstAlbum = true;
504 for (final Entry<AxArtifactKey, Map<String, Object>> contextAlbumEntry : engineContextAlbums.entrySet()) {
508 runtimeJsonStringBuilder.append(",");
511 runtimeJsonStringBuilder.append("{\"AlbumKey\":");
512 runtimeJsonStringBuilder.append(gson.toJson(contextAlbumEntry.getKey()));
513 runtimeJsonStringBuilder.append(",\"AlbumContent\":[");
515 // Get the schema helper to use to marshal context album objects to JSON
516 final AxContextAlbum axContextAlbum =
517 ModelService.getModel(AxContextAlbums.class).get(contextAlbumEntry.getKey());
518 SchemaHelper schemaHelper = null;
521 // Get a schema helper to manage the translations between objects on the album map
523 schemaHelper = new SchemaHelperFactory().createSchemaHelper(axContextAlbum.getKey(),
524 axContextAlbum.getItemSchema());
525 } catch (final ContextRuntimeException e) {
526 final String resultString =
527 "could not find schema helper to marshal context album \"" + axContextAlbum + "\" to JSON";
528 LOGGER.warn(resultString, e);
530 // End of context album entry
531 runtimeJsonStringBuilder.append(resultString);
532 runtimeJsonStringBuilder.append("]}");
537 boolean firstEntry = true;
538 for (final Entry<String, Object> contextEntry : contextAlbumEntry.getValue().entrySet()) {
542 runtimeJsonStringBuilder.append(",");
544 runtimeJsonStringBuilder.append("{\"EntryName\":");
545 runtimeJsonStringBuilder.append(gson.toJson(contextEntry.getKey()));
546 runtimeJsonStringBuilder.append(",\"EntryContent\":");
547 runtimeJsonStringBuilder.append(gson.toJson(schemaHelper.marshal2String(contextEntry.getValue())));
549 // End of context entry
550 runtimeJsonStringBuilder.append("}");
553 // End of context album entry
554 runtimeJsonStringBuilder.append("]}");
557 runtimeJsonStringBuilder.append("]}");
559 // Tidy up the JSON string
560 final JsonElement jsonElement = JsonParser.parseString(runtimeJsonStringBuilder.toString());
561 final String tidiedRuntimeString = gson.toJson(jsonElement);
563 LOGGER.debug("runtime information={}", tidiedRuntimeString);
565 return tidiedRuntimeString;
569 * This is an event processor thread, this class decouples the events handling logic from core business logic. This
570 * class runs its own thread and continuously querying the blocking queue for the events that have been sent to the
571 * worker for processing by the Apex engine.
573 * @author Liam Fallon (liam.fallon@ericsson.com)
575 private class EventProcessor implements Runnable {
576 private final boolean debugEnabled = LOGGER.isDebugEnabled();
578 private BlockingQueue<ApexEvent> eventProcessingQueue = null;
581 * Constructor accepts {@link ApexEngine} and {@link BlockingQueue} type objects.
583 * @param eventProcessingQueue is reference of {@link BlockingQueue} which contains trigger events.
585 EventProcessor(final BlockingQueue<ApexEvent> eventProcessingQueue) {
586 this.eventProcessingQueue = eventProcessingQueue;
594 LOGGER.debug("Engine {} processing ... ", engineWorkerKey);
596 // Take events from the event processing queue of the worker and pass them to the engine
598 boolean stopFlag = false;
599 while (processorThread != null && !processorThread.isInterrupted() && !stopFlag) {
600 ApexEvent event = null;
602 event = eventProcessingQueue.take();
603 } catch (final InterruptedException e) {
604 // restore the interrupt status
605 Thread.currentThread().interrupt();
606 LOGGER.debug("Engine {} processing interrupted ", engineWorkerKey);
609 boolean executedResult = false;
612 debugEventIfDebugEnabled(event);
614 final EnEvent enevent = apexEnEventConverter.fromApexEvent(event);
615 executedResult = engine.handleEvent(enevent);
617 } catch (final ApexException e) {
618 LOGGER.warn("Engine {} failed to process event {}", engineWorkerKey, event.toString(), e);
619 } catch (final Exception e) {
620 LOGGER.warn("Engine {} terminated processing event {}", engineWorkerKey, event.toString(), e);
623 ApexPolicyStatisticsManager apexPolicyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry();
624 if (!stopFlag && apexPolicyCounter != null) {
625 apexPolicyCounter.updatePolicyExecutedCounter(executedResult);
628 LOGGER.debug("Engine {} completed processing", engineWorkerKey);
632 * Debug the event if debug is enabled.
634 * @param event the event to debug
636 private void debugEventIfDebugEnabled(ApexEvent event) {
638 LOGGER.debug("Trigger Event {} forwarded to the Apex engine", event);