Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-deployment / src / main / java / org / onap / policy / apex / core / deployment / PeriodicEventManager.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.deployment;
22
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;
29
30 /**
31  * This utility class is used to start and stop periodic events on Apex engines over the EngDep protocol.
32  */
33 public class PeriodicEventManager {
34     // Get a reference to the logger
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(BatchDeployer.class);
36
37     private static final int NUM_ARGUMENTS = 4;
38
39     // The facade that is handling messaging to the engine service
40     private EngineServiceFacade engineServiceFacade = null;
41
42     // Host name and port of the Apex service
43     private String hostName;
44     private int port;
45
46     // Should we start or stop periodic events
47     private boolean startFlag;
48
49     // The period for periodic events
50     private long period;
51
52     /**
53      * Instantiates a new periodic event manager.
54      * 
55      * @param args the command parameters
56      * @param outputStream the output stream
57      * @throws ApexDeploymentException on messaging exceptions
58      */
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);
67         }
68
69         this.hostName = args[0];
70
71         try {
72             this.port = Integer.parseInt(args[1]);
73         } catch (NumberFormatException nfe) {
74             throw new ApexDeploymentException("argument port is invalid", nfe);
75         }
76
77         if ("start".equalsIgnoreCase(args[2])) {
78             startFlag = true;
79         } else if ("stop".equalsIgnoreCase(args[2])) {
80             startFlag = false;
81         } else {
82             throw new ApexDeploymentException("argument " + args[2] + " must be \"start\" or \"stop\"");
83         }
84
85         try {
86             this.period = Long.parseLong(args[3]);
87         } catch (NumberFormatException nfe) {
88             throw new ApexDeploymentException("argument period is invalid", nfe);
89         }
90
91         // Use an engine service facade to handle periodic event setting
92         engineServiceFacade = new EngineServiceFacade(hostName, port);
93     }
94
95     /**
96      * Initializes the manager, opens an EngDep communication session with the Apex engine.
97      *
98      * @throws ApexDeploymentException thrown on messaging and communication errors
99      */
100     public void init() throws ApexDeploymentException {
101         try {
102             engineServiceFacade.init();
103         } catch (final ApexException e) {
104             String errorMessage = "periodic event setting failed on parameters " + hostName + " " + port + " "
105                             + startFlag;
106             LOGGER.error(errorMessage, e);
107             throw new ApexDeploymentException(errorMessage);
108         }
109     }
110
111     /**
112      * Close the EngDep connection to the Apex server.
113      */
114     public void close() {
115         if (engineServiceFacade != null) {
116             engineServiceFacade.close();
117         }
118     }
119
120     /**
121      * Execute the periodic event command.
122      * 
123      * @throws ApexDeploymentException on periodic event exceptions
124      */
125     public void runCommand() throws ApexDeploymentException {
126         if (startFlag) {
127             startPerioidicEvents();
128         } else {
129             stopPerioidicEvents();
130         }
131     }
132
133     /**
134      * Start the Apex engines on the engine service.
135      *
136      * @throws ApexDeploymentException on messaging errors
137      */
138     private void startPerioidicEvents() throws ApexDeploymentException {
139         if (engineServiceFacade.getEngineKeyArray() == null) {
140             throw new ApexDeploymentException("connection to apex is not initialized");
141         }
142
143         for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) {
144             engineServiceFacade.startPerioidicEvents(engineKey, period);
145         }
146     }
147
148     /**
149      * Stop the Apex engines on the engine service.
150      *
151      * @throws ApexDeploymentException on messaging errors
152      */
153     private void stopPerioidicEvents() throws ApexDeploymentException {
154         if (engineServiceFacade.getEngineKeyArray() == null) {
155             throw new ApexDeploymentException("connection to apex is not initialized");
156         }
157
158         for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) {
159             engineServiceFacade.stopPerioidicEvents(engineKey);
160         }
161     }
162
163     /**
164      * Get the engine service facade of the event manager. This method is used for testing only.
165      * 
166      * @return the engine service facade
167      */
168     protected EngineServiceFacade getEngineServiceFacade() {
169         return engineServiceFacade;
170     }
171
172     /**
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.
175      *
176      * @param args the arguments that specify the Apex engine and the Apex model file
177      * @throws ApexDeploymentException on messaging errors
178      */
179     public static void main(final String[] args) throws ApexDeploymentException {
180         PeriodicEventManager peManager = new PeriodicEventManager(args, System.out);
181         peManager.init();
182         peManager.runCommand();
183         peManager.close();
184     }
185 }