Replace non-Javadoc comments with inheritDocs
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / handling / PolicyLogicReader.java
1 /*-
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
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.model.policymodel.handling;
22
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;
30
31 /**
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
34  * constructor.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class PolicyLogicReader implements AxLogicReader {
39     private static final String DOT_JAVA = ".java.";
40
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyModelSplitter.class);
42
43     // The path of the logic package
44     private String logicPackage = "";
45
46     // Flag indicating if default logic should be returned
47     private String defaultLogic;
48
49     /**
50      * {@inheritDoc}.
51      */
52     @Override
53     public String getLogicPackage() {
54         return logicPackage;
55     }
56
57     /**
58      * {@inheritDoc}.
59      */
60     @Override
61     public AxLogicReader setLogicPackage(final String incomingLogicPackage) {
62         this.logicPackage = incomingLogicPackage;
63         return this;
64     }
65
66     /**
67      * {@inheritDoc}.
68      */
69     @Override
70     public String getDefaultLogic() {
71         return defaultLogic;
72     }
73
74     /**
75      * {@inheritDoc}.
76      */
77     @Override
78     public AxLogicReader setDefaultLogic(final String incomingDefaultLogic) {
79         this.defaultLogic = incomingDefaultLogic;
80         return this;
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
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;
94             } else {
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();
99                 } else {
100                     return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
101                             + axLogic.getKey().getParentLocalName()  + axLogic.getKey().getLocalName();
102                 }
103             }
104         }
105         // Now, we read in the script
106
107         // Get the package name of the current package and convert dots to slashes for the file path
108         String fullLogicFilePath = logicPackage.replaceAll("\\.", "/");
109
110         // Now, the logic should be in a sub directory for the logic executor type
111         fullLogicFilePath += "/" + axLogic.getLogicFlavour().toLowerCase();
112
113         // Check if we're using the default logic
114         if (defaultLogic != null) {
115             // Default logic
116             fullLogicFilePath += "/" + defaultLogic;
117         } else {
118             if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
119                 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName() + axLogic.getKey().getLocalName();
120             } else {
121                 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName()
122                         + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
123             }
124         }
125
126         // Now get the type of executor to find the extension of the file
127         fullLogicFilePath += "." + axLogic.getLogicFlavour().toLowerCase();
128
129         final String logicString = ResourceUtils.getResourceAsString(fullLogicFilePath);
130
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);
136         }
137
138         // Return the right trimmed logic string
139         return logicString.replaceAll("\\s+$", "");
140     }
141 }