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.policymodel.concepts.AxPolicyModel;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
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.
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.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class BatchDeployer {
40 private static final int NUM_ARGUMENTS = 3;
42 // Get a reference to the logger
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(BatchDeployer.class);
45 // The facade that is handling messaging to the engine service
46 private EngineServiceFacade engineServiceFacade = null;
48 private String hostName;
52 * Instantiates a new deployer.
54 * @param hostName the apex host name
55 * @param port the apex EngDep port
56 * @param outputStream the output stream
58 public BatchDeployer(final String hostName, final int port, final PrintStream outputStream) {
59 this.hostName = hostName;
62 engineServiceFacade = new EngineServiceFacade(hostName, port);
66 * Initializes the deployer, opens an EngDep communication session with the Apex engine.
68 * @throws ApexDeploymentException thrown on deployment and communication errors
70 public void init() throws ApexDeploymentException {
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);
81 * Close the EngDep connection to the Apex server.
84 if (engineServiceFacade != null) {
85 engineServiceFacade.close();
90 * Deploy an Apex model on the Apex server.
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
97 public void deployModel(final String modelFileName, final boolean ignoreConflicts, final boolean force)
98 throws ApexException {
99 engineServiceFacade.deployModel(modelFileName, ignoreConflicts, force);
103 * Deploy an Apex model on the Apex server.
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
110 public void deployModel(final AxPolicyModel policyModel, final boolean ignoreConflicts, final boolean force)
111 throws ApexException {
112 engineServiceFacade.deployModel(policyModel, ignoreConflicts, force);
116 * Get the engine service facade of the event manager. This method is used for testing only.
118 * @return the engine service facade
120 protected EngineServiceFacade getEngineServiceFacade() {
121 return engineServiceFacade;
125 * The main method, reads the Apex server host address, port and location of the Apex model file from the command
128 * @param args the arguments that specify the Apex engine and the Apex model file
129 * @throws ApexException on deployment errors
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);
141 port = Integer.parseInt(args[1]);
142 } catch (final NumberFormatException nfe) {
143 throw new ApexDeploymentException("argument port is invalid", nfe);
146 final BatchDeployer deployer = new BatchDeployer(args[0], port, System.out);
148 deployer.deployModel(args[2], false, false);