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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.handling;
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;
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
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class PolicyLogicReader implements AxLogicReader {
40 private static final String DOT_JAVA = ".java.";
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyModelSplitter.class);
44 // The path of the logic package
45 private String logicPackage = "";
47 // Flag indicating if default logic should be returned
48 private String defaultLogic;
54 public String getLogicPackage() {
62 public AxLogicReader setLogicPackage(final String incomingLogicPackage) {
63 this.logicPackage = incomingLogicPackage;
71 public String getDefaultLogic() {
79 public AxLogicReader setDefaultLogic(final String incomingDefaultLogic) {
80 this.defaultLogic = incomingDefaultLogic;
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;
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();
101 return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
102 + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
106 // Now, we read in the script
108 // Get the package name of the current package and convert dots to slashes for the file path
109 String fullLogicFilePath = logicPackage.replace(".", "/");
111 // Now, the logic should be in a sub directory for the logic executor type
112 fullLogicFilePath += "/" + axLogic.getLogicFlavour().toLowerCase();
114 // Check if we're using the default logic
115 if (defaultLogic != null) {
117 fullLogicFilePath += "/" + defaultLogic;
119 if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
120 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName() + axLogic.getKey().getLocalName();
122 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName()
123 + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
127 // Now get the type of executor to find the extension of the file
128 fullLogicFilePath += "." + axLogic.getLogicFlavour().toLowerCase();
130 final String logicString = ResourceUtils.getResourceAsString(fullLogicFilePath);
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);
139 // Return the right trimmed logic string
140 return logicString.replaceAll("\\s+$", "");