699f26c6663b1654e59399196e2aa0d47d6f7414
[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.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29
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;
37
38 /**
39  * This class instantiates the Apex Engine based on instruction from PAP.
40  *
41  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
42  */
43 public class ApexEngineHandler {
44     private static final Logger LOGGER = LoggerFactory.getLogger(ApexEngineHandler.class);
45
46     private ApexMain apexMain;
47
48     /**
49      * Constructs the object. Extracts the apex config and model files and instantiates the apex engine.
50      *
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
53      */
54     public ApexEngineHandler(final Object properties) throws ApexStarterException {
55         final StandardCoder standardCoder = new StandardCoder();
56         String policyModel;
57         String apexConfig;
58         try {
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);
66         }
67
68         final String modelFilePath = createFile(policyModel, "modelFile");
69
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);
74     }
75
76     /**
77      * Method to create the policy model file.
78      *
79      * @param fileContent the content of the file
80      * @param fileName the name of the file
81      * @throws ApexStarterException if the file creation failed
82      */
83     private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
84         try {
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);
92         }
93     }
94
95     /**
96      * Method to check whether the apex engine is running or not.
97      */
98     public boolean isApexEngineRunning() {
99         return null != apexMain;
100     }
101
102     /**
103      * Method to shut down the apex engine.
104      */
105     public void shutdown() throws ApexStarterException {
106         try {
107             LOGGER.debug("Shutting down apex engine.");
108             apexMain.shutdown();
109             apexMain = null;
110         } catch (final ApexException e) {
111             throw new ApexStarterException(e);
112         }
113     }
114 }