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.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.service.engine.main.ApexMain;
32 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This class instantiates the Apex Engine based on instruction from PAP.
41 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43 public class ApexEngineHandler {
44 private static final Logger LOGGER = LoggerFactory.getLogger(ApexEngineHandler.class);
46 private ApexMain apexMain;
49 * Constructs the object. Extracts the apex config and model files and instantiates the apex engine.
51 * @param properties the properties which contains the policies and configurations received from pap
52 * @throws ApexStarterException if the apex engine instantiation failed using the properties passed
54 public ApexEngineHandler(final Object properties) throws ApexStarterException {
55 final StandardCoder standardCoder = new StandardCoder();
59 JsonObject body = standardCoder.decode(standardCoder.encode(properties), JsonObject.class);
60 final JsonObject engineServiceParameters = body.get("engineServiceParameters").getAsJsonObject();
61 policyModel = standardCoder.encode(engineServiceParameters.get("policy_type_impl"));
62 engineServiceParameters.remove("policy_type_impl");
63 apexConfig = standardCoder.encode(body);
64 } catch (final CoderException e) {
65 throw new ApexStarterException(e);
68 final String modelFilePath = createFile(policyModel, "modelFile");
70 final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile");
71 final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath };
72 LOGGER.debug("Starting apex engine.");
73 apexMain = new ApexMain(apexArgs);
77 * Method to create the policy model file.
79 * @param fileContent the content of the file
80 * @param fileName the name of the file
81 * @throws ApexStarterException if the file creation failed
83 private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
85 final Path path = Files.createTempFile(fileName, ".json");
86 Files.write(path, fileContent.getBytes(StandardCharsets.UTF_8));
87 return path.toAbsolutePath().toString();
88 } catch (final IOException e) {
89 final String errorMessage = "error creating from the properties received in PdpUpdate.";
90 LOGGER.error(errorMessage, e);
91 throw new ApexStarterException(errorMessage, e);
96 * Method to check whether the apex engine is running or not.
98 public boolean isApexEngineRunning() {
99 return null != apexMain;
103 * Method to shut down the apex engine.
105 public void shutdown() throws ApexStarterException {
107 LOGGER.debug("Shutting down apex engine.");
110 } catch (final ApexException e) {
111 throw new ApexStarterException(e);