f700684541174600b1a6a0807e6a79836564356b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.handling;
23
24 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
25 import org.onap.policy.apex.model.policymodel.concepts.AxLogic;
26 import org.onap.policy.apex.model.policymodel.concepts.AxLogicReader;
27 import org.onap.policy.apex.model.policymodel.concepts.PolicyRuntimeException;
28 import org.onap.policy.common.utils.resources.ResourceUtils;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 /**
33  * This class is used to read Task Logic and Task Selection Logic from files into a string. A
34  * {@link PolicyLogicReader} can then be used to provide the logic on a {@link AxLogic} class
35  * constructor.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class PolicyLogicReader implements AxLogicReader {
40     private static final String DOT_JAVA = ".java.";
41
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyModelSplitter.class);
43
44     // The path of the logic package
45     private String logicPackage = "";
46
47     // Flag indicating if default logic should be returned
48     private String defaultLogic;
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public String getLogicPackage() {
55         return logicPackage;
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public AxLogicReader setLogicPackage(final String incomingLogicPackage) {
63         this.logicPackage = incomingLogicPackage;
64         return this;
65     }
66
67     /**
68      * {@inheritDoc}.
69      */
70     @Override
71     public String getDefaultLogic() {
72         return defaultLogic;
73     }
74
75     /**
76      * {@inheritDoc}.
77      */
78     @Override
79     public AxLogicReader setDefaultLogic(final String incomingDefaultLogic) {
80         this.defaultLogic = incomingDefaultLogic;
81         return this;
82     }
83
84     /**
85      * {@inheritDoc}.
86      */
87     @Override
88     public String readLogic(final AxLogic axLogic) {
89         // Java uses compiled logic, other executor types run scripts
90         if ("JAVA".equals(axLogic.getLogicFlavour())) {
91             // Check if we're using the default logic
92             if (defaultLogic != null) {
93                 // Return the java class name for the default logic
94                 return logicPackage + DOT_JAVA + defaultLogic;
95             } else {
96                 // Return the java class name for the logic
97                 if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
98                     return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
99                             + axLogic.getKey().getLocalName();
100                 } else {
101                     return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
102                             + axLogic.getKey().getParentLocalName()  + axLogic.getKey().getLocalName();
103                 }
104             }
105         }
106         // Now, we read in the script
107
108         // Get the package name of the current package and convert dots to slashes for the file path
109         String fullLogicFilePath = logicPackage.replace(".", "/");
110
111         // Now, the logic should be in a sub directory for the logic executor type
112         fullLogicFilePath += "/" + axLogic.getLogicFlavour().toLowerCase();
113
114         // Check if we're using the default logic
115         if (defaultLogic != null) {
116             // Default logic
117             fullLogicFilePath += "/" + defaultLogic;
118         } else {
119             if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
120                 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName() + axLogic.getKey().getLocalName();
121             } else {
122                 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName()
123                         + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
124             }
125         }
126
127         // Now get the type of executor to find the extension of the file
128         fullLogicFilePath += "." + axLogic.getLogicFlavour().toLowerCase();
129
130         final String logicString = ResourceUtils.getResourceAsString(fullLogicFilePath);
131
132         // Check if the logic was found
133         if (logicString == null || logicString.length() == 0) {
134             String errorMessage = "logic not found for logic \"" + fullLogicFilePath + "\"";
135             LOGGER.warn(errorMessage);
136             throw new PolicyRuntimeException(errorMessage);
137         }
138
139         // Return the right trimmed logic string
140         return logicString.replaceAll("\\s+$", "");
141     }
142 }