85d666c2b797afd38e0f7ad9e01bd33091c514fe
[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 the properties which contains the policies and configurations received from pap
53      * @throws ApexStarterException if the apex engine instantiation failed using the properties passed
54      */
55
56     public ApexEngineHandler(final String properties) throws ApexStarterException {
57         final StandardCoder standardCoder = new StandardCoder();
58         JsonObject body;
59         try {
60             body = standardCoder.decode(new StringReader(properties), JsonObject.class);
61         } catch (final CoderException e) {
62             throw new ApexStarterException(e);
63         }
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();
68
69         final String modelFilePath = createFile(policyModel, "modelFile");
70
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);
75     }
76
77     /**
78      * Method to create the policy model file.
79      *
80      * @param fileContent the content of the file
81      * @param fileName the name of the file
82      * @throws ApexStarterException if the file creation failed
83      */
84     private String createFile(final String fileContent, final String fileName) throws ApexStarterException {
85         try {
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);
93         }
94     }
95
96     /**
97      * Method to check whether the apex engine is running or not.
98      */
99     public boolean isApexEngineRunning() {
100         return null != apexMain;
101     }
102
103     /**
104      * Method to shut down the apex engine.
105      */
106     public void shutdown() throws ApexStarterException {
107         try {
108             LOGGER.debug("Shutting down apex engine.");
109             apexMain.shutdown();
110             apexMain = null;
111         } catch (final ApexException e) {
112             throw new ApexStarterException(e);
113         }
114     }
115 }