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;
25 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * This utility class is used to start and stop periodic events on Apex engines over the EngDep protocol.
33 public class PeriodicEventManager {
34 // Get a reference to the logger
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(BatchDeployer.class);
37 private static final int NUM_ARGUMENTS = 4;
39 // The facade that is handling messaging to the engine service
40 private EngineServiceFacade engineServiceFacade = null;
42 // Host name and port of the Apex service
43 private String hostName;
46 // Should we start or stop periodic events
47 private boolean startFlag;
49 // The period for periodic events
53 * Instantiates a new periodic event manager.
55 * @param args the command parameters
56 * @param outputStream the output stream
57 * @throws ApexDeploymentException on messaging exceptions
59 public PeriodicEventManager(final String[] args, final PrintStream outputStream) throws ApexDeploymentException {
60 if (args.length != NUM_ARGUMENTS) {
61 String message = "invalid arguments: " + Arrays.toString(args)
62 + "\nusage: PeriodicEventManager <server address> <port address> "
63 + "<start/stop> <periods in ms>";
64 LOGGER.error(message);
65 outputStream.println(message);
66 throw new ApexDeploymentException(message);
69 this.hostName = args[0];
72 this.port = Integer.parseInt(args[1]);
73 } catch (NumberFormatException nfe) {
74 throw new ApexDeploymentException("argument port is invalid", nfe);
77 if ("start".equalsIgnoreCase(args[2])) {
79 } else if ("stop".equalsIgnoreCase(args[2])) {
82 throw new ApexDeploymentException("argument " + args[2] + " must be \"start\" or \"stop\"");
86 this.period = Long.parseLong(args[3]);
87 } catch (NumberFormatException nfe) {
88 throw new ApexDeploymentException("argument period is invalid", nfe);
91 // Use an engine service facade to handle periodic event setting
92 engineServiceFacade = new EngineServiceFacade(hostName, port);
96 * Initializes the manager, opens an EngDep communication session with the Apex engine.
98 * @throws ApexDeploymentException thrown on messaging and communication errors
100 public void init() throws ApexDeploymentException {
102 engineServiceFacade.init();
103 } catch (final ApexException e) {
104 String errorMessage = "periodic event setting failed on parameters " + hostName + " " + port + " "
106 LOGGER.error(errorMessage, e);
107 throw new ApexDeploymentException(errorMessage);
112 * Close the EngDep connection to the Apex server.
114 public void close() {
115 if (engineServiceFacade != null) {
116 engineServiceFacade.close();
121 * Execute the periodic event command.
123 * @throws ApexDeploymentException on periodic event exceptions
125 public void runCommand() throws ApexDeploymentException {
127 startPerioidicEvents();
129 stopPerioidicEvents();
134 * Start the Apex engines on the engine service.
136 * @throws ApexDeploymentException on messaging errors
138 private void startPerioidicEvents() throws ApexDeploymentException {
139 if (engineServiceFacade.getEngineKeyArray() == null) {
140 throw new ApexDeploymentException("connection to apex is not initialized");
143 for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) {
144 engineServiceFacade.startPerioidicEvents(engineKey, period);
149 * Stop the Apex engines on the engine service.
151 * @throws ApexDeploymentException on messaging errors
153 private void stopPerioidicEvents() throws ApexDeploymentException {
154 if (engineServiceFacade.getEngineKeyArray() == null) {
155 throw new ApexDeploymentException("connection to apex is not initialized");
158 for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) {
159 engineServiceFacade.stopPerioidicEvents(engineKey);
164 * Get the engine service facade of the event manager. This method is used for testing only.
166 * @return the engine service facade
168 protected EngineServiceFacade getEngineServiceFacade() {
169 return engineServiceFacade;
173 * The main method, reads the Apex server host address, port and location of the Apex model XML file from the
174 * command line arguments.
176 * @param args the arguments that specify the Apex engine and the Apex model file
177 * @throws ApexDeploymentException on messaging errors
179 public static void main(final String[] args) throws ApexDeploymentException {
180 PeriodicEventManager peManager = new PeriodicEventManager(args, System.out);
182 peManager.runCommand();