83e1b77c66d61f486af27aa0cbd788504439b7e2
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.services.onappf.handler;
22
23 import com.google.gson.JsonObject;
24
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;
30
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;
38
39 /**
40  * This class instantiates the Apex Engine based on instruction from PAP.
41  *
42  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43  */
44 public class ApexEngineHandler {
45     private static final Logger LOGGER = LoggerFactory.getLogger(ApexEngineHandler.class);
46
47     private ApexMain apexMain;
48
49     /**
50      * Constructs the object. Extracts the apex config and model files and instantiates the apex engine.
51      *
52      * @param properties
53      * @return ApexEngineHandler
54      * @throws ApexStarterException
55      */
56
57     public ApexEngineHandler(final String properties) throws ApexStarterException {
58         final StandardCoder standardCoder = new StandardCoder();
59         JsonObject body;
60         try {
61             body = standardCoder.decode(new StringReader(properties), JsonObject.class);
62         } catch (final CoderException e) {
63             throw new ApexStarterException(e);
64         }
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();
69
70         final String modelFilePath = createFile(policyModel, "modelFile");
71
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);
76     }
77
78     /**
79      * Method to create the policy model file
80      *
81      * @param policyModel
82      * @param modelFilePath
83      * @throws ApexStarterException
84      */
85     private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
86         try {
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);
94         }
95     }
96
97     public boolean isApexEngineRunning() {
98         return null != apexMain;
99     }
100
101     public void shutdown() throws ApexStarterException {
102         try {
103             LOGGER.debug("Shutting down apex engine.");
104             apexMain.shutdown();
105             apexMain = null;
106         } catch (final ApexException e) {
107             throw new ApexStarterException(e);
108         }
109     }
110 }