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.
53 * @return ApexEngineHandler
54 * @throws ApexStarterException
57 public ApexEngineHandler(final String properties) throws ApexStarterException {
58 final StandardCoder standardCoder = new StandardCoder();
61 body = standardCoder.decode(new StringReader(properties), JsonObject.class);
62 } catch (final CoderException e) {
63 throw new ApexStarterException(e);
65 final JsonObject engineServiceParameters = body.get("engineServiceParameters").getAsJsonObject();
66 final String policyModel = engineServiceParameters.get("policy_type_impl").toString();
67 engineServiceParameters.remove("policy_type_impl");
68 final String apexConfig = body.toString();
70 final String modelFilePath = createFile(policyModel, "modelFile");
72 final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile");
73 final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath };
74 LOGGER.debug("Starting apex engine.");
75 apexMain = new ApexMain(apexArgs);
79 * Method to create the policy model file
82 * @param modelFilePath
83 * @throws ApexStarterException
85 private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
87 final Path path = Files.createTempFile(fileName, ".json");
88 Files.write(path, fileContent.getBytes(StandardCharsets.UTF_8));
89 return path.toAbsolutePath().toString();
90 } catch (final IOException e) {
91 final String errorMessage = "error creating from the properties received in PdpUpdate.";
92 LOGGER.error(errorMessage, e);
93 throw new ApexStarterException(errorMessage, e);
97 public boolean isApexEngineRunning() {
98 return null != apexMain;
101 public void shutdown() throws ApexStarterException {
103 LOGGER.debug("Shutting down apex engine.");
106 } catch (final ApexException e) {
107 throw new ApexStarterException(e);