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