2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.deployment;
23 import java.io.PrintStream;
24 import java.util.Arrays;
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This utility class is used to start and stop periodic events on Apex engines over the EngDep protocol.
34 public class PeriodicEventManager {
35 // Get a reference to the logger
36 private static final XLogger LOGGER = XLoggerFactory.getXLogger(BatchDeployer.class);
38 private static final int NUM_ARGUMENTS = 4;
40 // The facade that is handling messaging to the engine service
41 private EngineServiceFacade engineServiceFacade = null;
43 // Host name and port of the Apex service
44 private String hostName;
47 // Should we start or stop periodic events
48 private boolean startFlag;
50 // The period for periodic events
54 * Instantiates a new periodic event manager.
56 * @param args the command parameters
57 * @param outputStream the output stream
58 * @throws ApexDeploymentException on messaging exceptions
60 public PeriodicEventManager(final String[] args, final PrintStream outputStream) throws ApexDeploymentException {
61 if (args.length != NUM_ARGUMENTS) {
62 String message = "invalid arguments: " + Arrays.toString(args)
63 + "\nusage: PeriodicEventManager <server address> <port address> "
64 + "<start/stop> <periods in ms>";
65 LOGGER.error(message);
66 outputStream.println(message);
67 throw new ApexDeploymentException(message);
70 this.hostName = args[0];
73 this.port = Integer.parseInt(args[1]);
74 } catch (NumberFormatException nfe) {
75 throw new ApexDeploymentException("argument port is invalid", nfe);
78 if ("start".equalsIgnoreCase(args[2])) {
80 } else if ("stop".equalsIgnoreCase(args[2])) {
83 throw new ApexDeploymentException("argument " + args[2] + " must be \"start\" or \"stop\"");
87 this.period = Long.parseLong(args[3]);
88 } catch (NumberFormatException nfe) {
89 throw new ApexDeploymentException("argument period is invalid", nfe);
92 // Use an engine service facade to handle periodic event setting
93 engineServiceFacade = new EngineServiceFacade(hostName, port);
97 * Initializes the manager, opens an EngDep communication session with the Apex engine.
99 * @throws ApexDeploymentException thrown on messaging and communication errors
101 public void init() throws ApexDeploymentException {
103 engineServiceFacade.init();
104 } catch (final ApexException e) {
105 String errorMessage = "periodic event setting failed on parameters " + hostName + " " + port + " "
107 LOGGER.error(errorMessage, e);
108 throw new ApexDeploymentException(errorMessage);
113 * Close the EngDep connection to the Apex server.
115 public void close() {
116 if (engineServiceFacade != null) {
117 engineServiceFacade.close();
122 * Execute the periodic event command.
124 * @throws ApexDeploymentException on periodic event exceptions
126 public void runCommand() throws ApexDeploymentException {
128 startPerioidicEvents();
130 stopPerioidicEvents();
135 * Start the Apex engines on the engine service.
137 * @throws ApexDeploymentException on messaging errors
139 private void startPerioidicEvents() throws ApexDeploymentException {
140 if (engineServiceFacade.getEngineKeyArray() == null) {
141 throw new ApexDeploymentException("connection to apex is not initialized");
144 for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) {
145 engineServiceFacade.startPerioidicEvents(engineKey, period);
150 * Stop the Apex engines on the engine service.
152 * @throws ApexDeploymentException on messaging errors
154 private void stopPerioidicEvents() throws ApexDeploymentException {
155 if (engineServiceFacade.getEngineKeyArray() == null) {
156 throw new ApexDeploymentException("connection to apex is not initialized");
159 for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) {
160 engineServiceFacade.stopPerioidicEvents(engineKey);
165 * Get the engine service facade of the event manager. This method is used for testing only.
167 * @return the engine service facade
169 protected EngineServiceFacade getEngineServiceFacade() {
170 return engineServiceFacade;
174 * The main method, reads the Apex server host address, port and location of the Apex model XML file from the
175 * command line arguments.
177 * @param args the arguments that specify the Apex engine and the Apex model file
178 * @throws ApexDeploymentException on messaging errors
180 public static void main(final String[] args) throws ApexDeploymentException {
181 PeriodicEventManager peManager = new PeriodicEventManager(args, System.out);
183 peManager.runCommand();