2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Copyright (C) 2019 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.Arrays;
32 import java.util.Collection;
34 import java.util.Map.Entry;
35 import java.util.concurrent.BlockingQueue;
37 import org.onap.policy.apex.context.ContextException;
38 import org.onap.policy.apex.context.ContextRuntimeException;
39 import org.onap.policy.apex.context.SchemaHelper;
40 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
41 import org.onap.policy.apex.core.engine.engine.ApexEngine;
42 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
43 import org.onap.policy.apex.core.engine.event.EnEvent;
44 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
47 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
48 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
49 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
50 import org.onap.policy.apex.model.basicmodel.service.ModelService;
51 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
52 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
53 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
54 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
55 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
56 import org.onap.policy.apex.service.engine.event.ApexEvent;
57 import org.onap.policy.apex.service.engine.event.impl.enevent.ApexEvent2EnEventConverter;
58 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
59 import org.onap.policy.apex.service.engine.runtime.EngineService;
60 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
61 import org.slf4j.ext.XLogger;
62 import org.slf4j.ext.XLoggerFactory;
65 * The Class EngineWorker encapsulates a core {@link ApexEngine} instance, which runs policies defined in the
66 * {@link org.onap.policy.apex.model.basicmodel.concepts.AxModelAxModel}. Each policy is triggered by an Apex event, and
67 * when the policy is triggered it runs through to completion in the ApexEngine.
69 * <p>This class acts as a container for an {@link ApexEngine}, running it in a thread, sending it events, and receiving
72 * @author Liam Fallon (liam.fallon@ericsson.com)
74 final class EngineWorker implements EngineService {
75 // Logger for this class
76 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngineService.class);
78 // Recurring string constants
79 private static final String ENGINE_FOR_KEY_PREFIX = "apex engine for engine key ";
80 private static final String ENGINE_SUFFIX = " of this engine";
81 private static final String BAD_KEY_MATCH_TAG = " does not match the key";
82 private static final String ENGINE_KEY_PREFIX = "engine key ";
84 // The ID of this engine
85 private final AxArtifactKey engineWorkerKey;
87 // The Apex engine which is running the policies in this worker
88 private final ApexEngine engine;
90 // The event processor is an inner class, an instance of which runs as a thread that reads
91 // incoming events from a queue and forwards them to the Apex engine
92 private EventProcessor processor = null;
94 // Thread handling for the worker
95 private final ApplicationThreadFactory threadFactory;
96 private Thread processorThread;
98 // Converts ApexEvent instances to and from EnEvent instances
99 private ApexEvent2EnEventConverter apexEnEventConverter = null;
102 * Constructor that creates an Apex engine, an event processor for events to be sent to that engine, and an
103 * {@link ApexModelReader} instance to read Apex models using JAXB.
105 * @param engineWorkerKey the engine worker key
106 * @param queue the queue on which events for this Apex worker will come
107 * @param threadFactory the thread factory to use for creating the event processing thread
108 * @throws ApexException thrown on errors on worker instantiation
110 protected EngineWorker(final AxArtifactKey engineWorkerKey, final BlockingQueue<ApexEvent> queue,
111 final ApplicationThreadFactory threadFactory) {
112 LOGGER.entry(engineWorkerKey);
114 this.engineWorkerKey = engineWorkerKey;
115 this.threadFactory = threadFactory;
117 // Create the Apex engine
118 engine = new ApexEngineFactory().createApexEngine(engineWorkerKey);
120 // Create and run the event processor
121 processor = new EventProcessor(queue);
123 // Set the Event converter up
124 apexEnEventConverter = new ApexEvent2EnEventConverter(engine);
133 public void registerActionListener(final String listenerName, final ApexEventListener apexEventListener) {
134 engine.addEventListener(listenerName, new EnEventListenerImpl(apexEventListener, apexEnEventConverter));
141 public void deregisterActionListener(final String listenerName) {
142 engine.removeEventListener(listenerName);
149 public EngineServiceEventInterface getEngineServiceEventInterface() {
150 throw new UnsupportedOperationException(
151 "getEngineServiceEventInterface() call is not allowed on an Apex Engine Worker");
158 public AxArtifactKey getKey() {
159 return engineWorkerKey;
166 public Collection<AxArtifactKey> getEngineKeys() {
167 return Arrays.asList(engineWorkerKey);
174 public AxArtifactKey getApexModelKey() {
175 if (ModelService.existsModel(AxPolicyModel.class)) {
176 return ModelService.getModel(AxPolicyModel.class).getKey();
186 public void updateModel(final AxArtifactKey engineKey, final String engineModel, final boolean forceFlag)
187 throws ApexException {
188 LOGGER.entry(engineKey);
190 // Read the Apex model into memory using the Apex Model Reader
191 AxPolicyModel apexPolicyModel = null;
193 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
194 apexPolicyModel = modelReader.read(new ByteArrayInputStream(engineModel.getBytes()));
195 } catch (final ApexModelException e) {
196 LOGGER.error("failed to unmarshal the apex model on engine " + engineKey.getId(), e);
197 throw new ApexException("failed to unmarshal the apex model on engine " + engineKey.getId(), e);
200 // Update the Apex model in the Apex engine
201 updateModel(engineKey, apexPolicyModel, forceFlag);
210 public void updateModel(final AxArtifactKey engineKey, final AxPolicyModel apexModel, final boolean forceFlag)
211 throws ApexException {
212 LOGGER.entry(engineKey);
214 // Check if the key on the update request is correct
215 if (!engineWorkerKey.equals(engineKey)) {
216 String message = ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId()
218 LOGGER.warn(message);
219 throw new ApexException(message);
222 // Check model compatibility
223 if (ModelService.existsModel(AxPolicyModel.class)) {
224 // The current policy model may or may not be defined
225 final AxPolicyModel currentModel = ModelService.getModel(AxPolicyModel.class);
226 if (!currentModel.getKey().isCompatible(apexModel.getKey())) {
228 LOGGER.warn("apex model update forced, supplied model with key \"" + apexModel.getKey().getId()
229 + "\" is not a compatible model update from the existing engine model with key \""
230 + currentModel.getKey().getId() + "\"");
232 throw new ContextException(
233 "apex model update failed, supplied model with key \"" + apexModel.getKey().getId()
234 + "\" is not a compatible model update from the existing engine model with key \""
235 + currentModel.getKey().getId() + "\"");
240 // Update the Apex model in the Apex engine
241 engine.updateModel(apexModel);
243 LOGGER.debug("engine model {} added to the engine-{}", apexModel.getKey().getId(), engineWorkerKey);
251 public AxEngineState getState() {
252 return engine.getState();
259 public void startAll() throws ApexException {
260 start(this.getKey());
267 public void start(final AxArtifactKey engineKey) throws ApexException {
268 LOGGER.entry(engineKey);
270 // Check if the key on the start request is correct
271 if (!engineWorkerKey.equals(engineKey)) {
273 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX);
274 throw new ApexException(
275 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX);
278 // Starts the event processing thread that handles incoming events
279 if (processorThread != null && processorThread.isAlive()) {
280 String message = ENGINE_FOR_KEY_PREFIX + engineWorkerKey.getId() + " is already running with state "
282 LOGGER.error(message);
283 throw new ApexException(message);
289 // Start a thread to process events for the engine
290 processorThread = threadFactory.newThread(processor);
291 processorThread.start();
293 LOGGER.exit(engineKey);
300 public void stop() throws ApexException {
308 public void stop(final AxArtifactKey engineKey) throws ApexException {
309 // Check if the key on the start request is correct
310 if (!engineWorkerKey.equals(engineKey)) {
312 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX);
313 throw new ApexException(
314 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX);
317 // Interrupt the worker to stop its thread
318 if (processorThread == null || !processorThread.isAlive()) {
319 processorThread = null;
322 .warn(ENGINE_FOR_KEY_PREFIX + engineWorkerKey.getId() + " is already stopped with state " + getState());
326 // Interrupt the thread that is handling events toward the engine
327 processorThread.interrupt();
328 processorThread = null;
333 LOGGER.exit(engineKey);
340 public void clear() throws ApexException {
341 clear(this.getKey());
348 public void clear(final AxArtifactKey engineKey) throws ApexException {
349 // Check if the key on the start request is correct
350 if (!engineWorkerKey.equals(engineKey)) {
352 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX);
353 throw new ApexException(
354 ENGINE_KEY_PREFIX + engineKey.getId() + BAD_KEY_MATCH_TAG + engineWorkerKey.getId() + ENGINE_SUFFIX);
357 // Interrupt the worker to stop its thread
358 if (processorThread != null && !processorThread.isAlive()) {
359 LOGGER.warn(ENGINE_FOR_KEY_PREFIX + engineWorkerKey.getId() + " is not stopped with state " + getState());
366 LOGGER.exit(engineKey);
373 public boolean isStarted() {
374 return isStarted(this.getKey());
381 public boolean isStarted(final AxArtifactKey engineKey) {
382 final AxEngineState engstate = getState();
390 return processorThread != null && processorThread.isAlive() && !processorThread.isInterrupted();
401 public boolean isStopped() {
402 return isStopped(this.getKey());
409 public boolean isStopped(final AxArtifactKey engineKey) {
410 final AxEngineState engstate = getState();
418 return processorThread == null || !processorThread.isAlive();
429 public void startPeriodicEvents(final long period) {
430 throw new UnsupportedOperationException("startPeriodicEvents() call is not allowed on an Apex Engine Worker");
437 public void stopPeriodicEvents() {
438 throw new UnsupportedOperationException("stopPeriodicEvents() call is not allowed on an Apex Engine Worker");
445 public String getStatus(final AxArtifactKey engineKey) {
446 // Get the information from the engine that we want to return
447 final AxEngineModel apexEngineModel = engine.getEngineStatus();
448 apexEngineModel.getKeyInformation().generateKeyInfo(apexEngineModel);
450 // Convert that information into a string
452 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
453 final ApexModelWriter<AxEngineModel> modelWriter = new ApexModelWriter<>(AxEngineModel.class);
454 modelWriter.setJsonOutput(true);
455 modelWriter.write(apexEngineModel, baOutputStream);
456 return baOutputStream.toString();
457 } catch (final Exception e) {
458 LOGGER.warn("error outputting runtime information for engine {}", engineWorkerKey, e);
467 public String getRuntimeInfo(final AxArtifactKey engineKey) {
468 // We'll build up the JSON string for runtime information bit by bit
469 final StringBuilder runtimeJsonStringBuilder = new StringBuilder();
471 // Get the engine information
472 final AxEngineModel engineModel = engine.getEngineStatus();
473 final Map<AxArtifactKey, Map<String, Object>> engineContextAlbums = engine.getEngineContext();
475 // Use GSON to convert our context information into JSON
476 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
478 // Get context into a JSON string
479 runtimeJsonStringBuilder.append("{\"TimeStamp\":");
480 runtimeJsonStringBuilder.append(engineModel.getTimestamp());
481 runtimeJsonStringBuilder.append(",\"State\":");
482 runtimeJsonStringBuilder.append(engineModel.getState());
483 runtimeJsonStringBuilder.append(",\"Stats\":");
484 runtimeJsonStringBuilder.append(gson.toJson(engineModel.getStats()));
486 // Get context into a JSON string
487 runtimeJsonStringBuilder.append(",\"ContextAlbums\":[");
489 boolean firstAlbum = true;
490 for (final Entry<AxArtifactKey, Map<String, Object>> contextAlbumEntry : engineContextAlbums.entrySet()) {
494 runtimeJsonStringBuilder.append(",");
497 runtimeJsonStringBuilder.append("{\"AlbumKey\":");
498 runtimeJsonStringBuilder.append(gson.toJson(contextAlbumEntry.getKey()));
499 runtimeJsonStringBuilder.append(",\"AlbumContent\":[");
501 // Get the schema helper to use to marshal context album objects to JSON
502 final AxContextAlbum axContextAlbum = ModelService.getModel(AxContextAlbums.class)
503 .get(contextAlbumEntry.getKey());
504 SchemaHelper schemaHelper = null;
507 // Get a schema helper to manage the translations between objects on the album map
509 schemaHelper = new SchemaHelperFactory().createSchemaHelper(axContextAlbum.getKey(),
510 axContextAlbum.getItemSchema());
511 } catch (final ContextRuntimeException e) {
512 final String resultString = "could not find schema helper to marshal context album \"" + axContextAlbum
514 LOGGER.warn(resultString, e);
516 // End of context album entry
517 runtimeJsonStringBuilder.append(resultString);
518 runtimeJsonStringBuilder.append("]}");
523 boolean firstEntry = true;
524 for (final Entry<String, Object> contextEntry : contextAlbumEntry.getValue().entrySet()) {
528 runtimeJsonStringBuilder.append(",");
530 runtimeJsonStringBuilder.append("{\"EntryName\":");
531 runtimeJsonStringBuilder.append(gson.toJson(contextEntry.getKey()));
532 runtimeJsonStringBuilder.append(",\"EntryContent\":");
533 runtimeJsonStringBuilder.append(gson.toJson(schemaHelper.marshal2String(contextEntry.getValue())));
535 // End of context entry
536 runtimeJsonStringBuilder.append("}");
539 // End of context album entry
540 runtimeJsonStringBuilder.append("]}");
543 runtimeJsonStringBuilder.append("]}");
545 // Tidy up the JSON string
546 final JsonParser jsonParser = new JsonParser();
547 final JsonElement jsonElement = jsonParser.parse(runtimeJsonStringBuilder.toString());
548 final String tidiedRuntimeString = gson.toJson(jsonElement);
550 LOGGER.debug("runtime information={}", tidiedRuntimeString);
552 return tidiedRuntimeString;
556 * This is an event processor thread, this class decouples the events handling logic from core business logic. This
557 * class runs its own thread and continuously querying the blocking queue for the events that have been sent to the
558 * worker for processing by the Apex engine.
560 * @author Liam Fallon (liam.fallon@ericsson.com)
562 private class EventProcessor implements Runnable {
563 private final boolean debugEnabled = LOGGER.isDebugEnabled();
565 private BlockingQueue<ApexEvent> eventProcessingQueue = null;
568 * Constructor accepts {@link ApexEngine} and {@link BlockingQueue} type objects.
570 * @param eventProcessingQueue is reference of {@link BlockingQueue} which contains trigger events.
572 EventProcessor(final BlockingQueue<ApexEvent> eventProcessingQueue) {
573 this.eventProcessingQueue = eventProcessingQueue;
581 LOGGER.debug("Engine {} processing ... ", engineWorkerKey);
583 // Take events from the event processing queue of the worker and pass them to the engine
585 boolean stopFlag = false;
586 while (processorThread != null && !processorThread.isInterrupted() && !stopFlag) {
587 ApexEvent event = null;
589 event = eventProcessingQueue.take();
590 } catch (final InterruptedException e) {
591 // restore the interrupt status
592 Thread.currentThread().interrupt();
593 LOGGER.debug("Engine {} processing interrupted ", engineWorkerKey);
599 debugEventIfDebugEnabled(event);
601 final EnEvent enevent = apexEnEventConverter.fromApexEvent(event);
602 engine.handleEvent(enevent);
604 } catch (final ApexException e) {
605 LOGGER.warn("Engine {} failed to process event {}", engineWorkerKey, event.toString(), e);
606 } catch (final Exception e) {
607 LOGGER.warn("Engine {} terminated processing event {}", engineWorkerKey, event.toString(), e);
611 LOGGER.debug("Engine {} completed processing", engineWorkerKey);
615 * Debug the event if debug is enabled.
617 * @param event the event to debug
619 private void debugEventIfDebugEnabled(ApexEvent event) {
621 LOGGER.debug("Trigger Event {} forwarded to the Apex engine", event);