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;
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;
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.
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.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class BatchDeployer {
41 private static final int NUM_ARGUMENTS = 3;
43 // Get a reference to the logger
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(BatchDeployer.class);
46 // The facade that is handling messaging to the engine service
47 private EngineServiceFacade engineServiceFacade = null;
49 private String hostName;
53 * Instantiates a new deployer.
55 * @param hostName the apex host name
56 * @param port the apex EngDep port
57 * @param outputStream the output stream
59 public BatchDeployer(final String hostName, final int port, final PrintStream outputStream) {
60 this.hostName = hostName;
63 engineServiceFacade = new EngineServiceFacade(hostName, port);
67 * Initializes the deployer, opens an EngDep communication session with the Apex engine.
69 * @throws ApexDeploymentException thrown on deployment and communication errors
71 public void init() throws ApexDeploymentException {
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);
82 * Close the EngDep connection to the Apex server.
85 if (engineServiceFacade != null) {
86 engineServiceFacade.close();
91 * Deploy an Apex model on the Apex server.
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
98 public void deployModel(final String modelFileName, final boolean ignoreConflicts, final boolean force)
99 throws ApexException {
100 engineServiceFacade.deployModel(modelFileName, ignoreConflicts, force);
104 * Deploy an Apex model on the Apex server.
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
111 public void deployModel(final AxPolicyModel policyModel, final boolean ignoreConflicts, final boolean force)
112 throws ApexException {
113 engineServiceFacade.deployModel(policyModel, ignoreConflicts, force);
117 * Get the engine service facade of the event manager. This method is used for testing only.
119 * @return the engine service facade
121 protected EngineServiceFacade getEngineServiceFacade() {
122 return engineServiceFacade;
126 * The main method, reads the Apex server host address, port and location of the Apex model file from the command
129 * @param args the arguments that specify the Apex engine and the Apex model file
130 * @throws ApexException on deployment errors
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);
142 port = Integer.parseInt(args[1]);
143 } catch (final NumberFormatException nfe) {
144 throw new ApexDeploymentException("argument port is invalid", nfe);
147 final BatchDeployer deployer = new BatchDeployer(args[0], port, System.out);
149 deployer.deployModel(args[2], false, false);