2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.policymodel.handling;
23 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
24 import org.onap.policy.apex.model.policymodel.concepts.AxLogic;
25 import org.onap.policy.apex.model.policymodel.concepts.AxLogicReader;
26 import org.onap.policy.apex.model.policymodel.concepts.PolicyRuntimeException;
27 import org.onap.policy.common.utils.resources.ResourceUtils;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class is used to read Task Logic and Task Selection Logic from files into a string. A
33 * {@link PolicyLogicReader} can then be used to provide the logic on a {@link AxLogic} class
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class PolicyLogicReader implements AxLogicReader {
39 private static final String DOT_JAVA = ".java.";
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyModelSplitter.class);
43 // The path of the logic package
44 private String logicPackage = "";
46 // Flag indicating if default logic should be returned
47 private String defaultLogic;
53 public String getLogicPackage() {
61 public AxLogicReader setLogicPackage(final String incomingLogicPackage) {
62 this.logicPackage = incomingLogicPackage;
70 public String getDefaultLogic() {
78 public AxLogicReader setDefaultLogic(final String incomingDefaultLogic) {
79 this.defaultLogic = incomingDefaultLogic;
87 public String readLogic(final AxLogic axLogic) {
88 // Java uses compiled logic, other executor types run scripts
89 if ("JAVA".equals(axLogic.getLogicFlavour())) {
90 // Check if we're using the default logic
91 if (defaultLogic != null) {
92 // Return the java class name for the default logic
93 return logicPackage + DOT_JAVA + defaultLogic;
95 // Return the java class name for the logic
96 if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
97 return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
98 + axLogic.getKey().getLocalName();
100 return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
101 + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
105 // Now, we read in the script
107 // Get the package name of the current package and convert dots to slashes for the file path
108 String fullLogicFilePath = logicPackage.replaceAll("\\.", "/");
110 // Now, the logic should be in a sub directory for the logic executor type
111 fullLogicFilePath += "/" + axLogic.getLogicFlavour().toLowerCase();
113 // Check if we're using the default logic
114 if (defaultLogic != null) {
116 fullLogicFilePath += "/" + defaultLogic;
118 if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
119 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName() + axLogic.getKey().getLocalName();
121 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName()
122 + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
126 // Now get the type of executor to find the extension of the file
127 fullLogicFilePath += "." + axLogic.getLogicFlavour().toLowerCase();
129 final String logicString = ResourceUtils.getResourceAsString(fullLogicFilePath);
131 // Check if the logic was found
132 if (logicString == null || logicString.length() == 0) {
133 String errorMessage = "logic not found for logic \"" + fullLogicFilePath + "\"";
134 LOGGER.warn(errorMessage);
135 throw new PolicyRuntimeException(errorMessage);
138 // Return the right trimmed logic string
139 return logicString.replaceAll("\\s+$", "");