2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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.services.onappf.handler;
23 import com.google.gson.JsonObject;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
33 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * This class instantiates the Apex Engine based on instruction from PAP.
42 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
44 public class ApexEngineHandler {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ApexEngineHandler.class);
47 private ApexMain apexMain;
50 * Constructs the object. Extracts the apex config and model files and instantiates the apex engine.
52 * @param properties the properties which contains the policies and configurations received from pap
53 * @throws ApexStarterException if the apex engine instantiation failed using the properties passed
56 public ApexEngineHandler(final Object properties) throws ApexStarterException {
57 final StandardCoder standardCoder = new StandardCoder();
60 body = standardCoder.decode(new StringReader(properties.toString()), JsonObject.class);
61 } catch (final CoderException e) {
62 throw new ApexStarterException(e);
64 final JsonObject engineServiceParameters = body.get("engineServiceParameters").getAsJsonObject();
65 final String policyModel = engineServiceParameters.get("policy_type_impl").toString();
66 engineServiceParameters.remove("policy_type_impl");
67 final String apexConfig = body.toString();
69 final String modelFilePath = createFile(policyModel, "modelFile");
71 final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile");
72 final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath };
73 LOGGER.debug("Starting apex engine.");
74 apexMain = new ApexMain(apexArgs);
78 * Method to create the policy model file.
80 * @param fileContent the content of the file
81 * @param fileName the name of the file
82 * @throws ApexStarterException if the file creation failed
84 private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
86 final Path path = Files.createTempFile(fileName, ".json");
87 Files.write(path, fileContent.getBytes(StandardCharsets.UTF_8));
88 return path.toAbsolutePath().toString();
89 } catch (final IOException e) {
90 final String errorMessage = "error creating from the properties received in PdpUpdate.";
91 LOGGER.error(errorMessage, e);
92 throw new ApexStarterException(errorMessage, e);
97 * Method to check whether the apex engine is running or not.
99 public boolean isApexEngineRunning() {
100 return null != apexMain;
104 * Method to shut down the apex engine.
106 public void shutdown() throws ApexStarterException {
108 LOGGER.debug("Shutting down apex engine.");
111 } catch (final ApexException e) {
112 throw new ApexStarterException(e);