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;
52 * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#getLogicPackage()
55 public String getLogicPackage() {
62 * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#setLogicPackage(java.lang.
66 public AxLogicReader setLogicPackage(final String incomingLogicPackage) {
67 this.logicPackage = incomingLogicPackage;
74 * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#getDefaultLogic()
77 public String getDefaultLogic() {
84 * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#setDefaultLogic(boolean)
87 public AxLogicReader setDefaultLogic(final String incomingDefaultLogic) {
88 this.defaultLogic = incomingDefaultLogic;
96 * org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#readLogic(.policymodel.concepts
100 public String readLogic(final AxLogic axLogic) {
101 // Java uses compiled logic, other executor types run scripts
102 if ("JAVA".equals(axLogic.getLogicFlavour())) {
103 // Check if we're using the default logic
104 if (defaultLogic != null) {
105 // Return the java class name for the default logic
106 return logicPackage + DOT_JAVA + defaultLogic;
108 // Return the java class name for the logic
109 if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
110 return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
111 + axLogic.getKey().getLocalName();
113 return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
114 + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
118 // Now, we read in the script
120 // Get the package name of the current package and convert dots to slashes for the file path
121 String fullLogicFilePath = logicPackage.replaceAll("\\.", "/");
123 // Now, the logic should be in a sub directory for the logic executor type
124 fullLogicFilePath += "/" + axLogic.getLogicFlavour().toLowerCase();
126 // Check if we're using the default logic
127 if (defaultLogic != null) {
129 fullLogicFilePath += "/" + defaultLogic;
131 if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
132 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName() + axLogic.getKey().getLocalName();
134 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName()
135 + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
139 // Now get the type of executor to find the extension of the file
140 fullLogicFilePath += "." + axLogic.getLogicFlavour().toLowerCase();
142 final String logicString = ResourceUtils.getResourceAsString(fullLogicFilePath);
144 // Check if the logic was found
145 if (logicString == null || logicString.length() == 0) {
146 String errorMessage = "logic not found for logic \"" + fullLogicFilePath + "\"";
147 LOGGER.warn(errorMessage);
148 throw new PolicyRuntimeException(errorMessage);
151 // Return the right trimmed logic string
152 return logicString.replaceAll("\\s+$", "");