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