Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-deployment / src / main / java / org / onap / policy / apex / core / deployment / BatchDeployer.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.policymodel.concepts.AxPolicyModel;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * The Class {@link BatchDeployer} deploys an Apex model held as an XML or Json file onto an Apex engine. It uses the
32  * EngDep protocol to communicate with the engine, with the EngDep protocol being carried on Java web sockets.
33  *
34  * <p>This deployer is a simple command line deployer that reads the communication parameters and the location of the
35  * Apex model file as arguments.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class BatchDeployer {
40     private static final int NUM_ARGUMENTS = 3;
41
42     // Get a reference to the logger
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(BatchDeployer.class);
44
45     // The facade that is handling messaging to the engine service
46     private EngineServiceFacade engineServiceFacade = null;
47
48     private String hostName;
49     private int port;
50
51     /**
52      * Instantiates a new deployer.
53      *
54      * @param hostName the apex host name
55      * @param port the apex EngDep port
56      * @param outputStream the output stream
57      */
58     public BatchDeployer(final String hostName, final int port, final PrintStream outputStream) {
59         this.hostName = hostName;
60         this.port = port;
61
62         engineServiceFacade = new EngineServiceFacade(hostName, port);
63     }
64
65     /**
66      * Initializes the deployer, opens an EngDep communication session with the Apex engine.
67      *
68      * @throws ApexDeploymentException thrown on deployment and communication errors
69      */
70     public void init() throws ApexDeploymentException {
71         try {
72             engineServiceFacade.init();
73         } catch (final ApexException e) {
74             final String errorMessage = "model deployment failed on parameters " + hostName + " " + port;
75             LOGGER.error(errorMessage, e);
76             throw new ApexDeploymentException(errorMessage);
77         }
78     }
79
80     /**
81      * Close the EngDep connection to the Apex server.
82      */
83     public void close() {
84         if (engineServiceFacade != null) {
85             engineServiceFacade.close();
86         }
87     }
88
89     /**
90      * Deploy an Apex model on the Apex server.
91      *
92      * @param modelFileName the name of the model file containing the model to deploy
93      * @param ignoreConflicts true if conflicts between context in polices is to be ignored
94      * @param force true if the model is to be applied even if it is incompatible with the existing model
95      * @throws ApexException on Apex errors
96      */
97     public void deployModel(final String modelFileName, final boolean ignoreConflicts, final boolean force)
98         throws ApexException {
99         engineServiceFacade.deployModel(modelFileName, ignoreConflicts, force);
100     }
101
102     /**
103      * Deploy an Apex model on the Apex server.
104      *
105      * @param policyModel the model to deploy
106      * @param ignoreConflicts true if conflicts between context in polices is to be ignored
107      * @param force true if the model is to be applied even if it is incompatible with the existing model
108      * @throws ApexException on Apex errors
109      */
110     public void deployModel(final AxPolicyModel policyModel, final boolean ignoreConflicts, final boolean force)
111         throws ApexException {
112         engineServiceFacade.deployModel(policyModel, ignoreConflicts, force);
113     }
114
115     /**
116      * Get the engine service facade of the event manager. This method is used for testing only.
117      *
118      * @return the engine service facade
119      */
120     protected EngineServiceFacade getEngineServiceFacade() {
121         return engineServiceFacade;
122     }
123
124     /**
125      * The main method, reads the Apex server host address, port and location of the Apex model file from the command
126      * line arguments.
127      *
128      * @param args the arguments that specify the Apex engine and the Apex model file
129      * @throws ApexException on deployment errors
130      */
131     public static void main(final String[] args) throws ApexException {
132         if (args.length != NUM_ARGUMENTS) {
133             final String message = "invalid arguments: " + Arrays.toString(args)
134                 + "\nusage: BatchDeployer <server address> <port address> <model file path";
135             LOGGER.error(message);
136             throw new ApexDeploymentException(message);
137         }
138
139         int port;
140         try {
141             port = Integer.parseInt(args[1]);
142         } catch (final NumberFormatException nfe) {
143             throw new ApexDeploymentException("argument port is invalid", nfe);
144         }
145
146         final BatchDeployer deployer = new BatchDeployer(args[0], port, System.out);
147         deployer.init();
148         deployer.deployModel(args[2], false, false);
149         deployer.close();
150     }
151 }