Merge "Add period after inheritDoc for Sonar"
[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      * (non-Javadoc)
51      *
52      * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#getLogicPackage()
53      */
54     @Override
55     public String getLogicPackage() {
56         return logicPackage;
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#setLogicPackage(java.lang.
63      * String)
64      */
65     @Override
66     public AxLogicReader setLogicPackage(final String incomingLogicPackage) {
67         this.logicPackage = incomingLogicPackage;
68         return this;
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#getDefaultLogic()
75      */
76     @Override
77     public String getDefaultLogic() {
78         return defaultLogic;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#setDefaultLogic(boolean)
85      */
86     @Override
87     public AxLogicReader setDefaultLogic(final String incomingDefaultLogic) {
88         this.defaultLogic = incomingDefaultLogic;
89         return this;
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see
96      * org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#readLogic(.policymodel.concepts
97      * .AxLogic)
98      */
99     @Override
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;
107             } else {
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();
112                 } else {
113                     return logicPackage + DOT_JAVA + axLogic.getKey().getParentKeyName()
114                             + axLogic.getKey().getParentLocalName()  + axLogic.getKey().getLocalName();
115                 }
116             }
117         }
118         // Now, we read in the script
119
120         // Get the package name of the current package and convert dots to slashes for the file path
121         String fullLogicFilePath = logicPackage.replaceAll("\\.", "/");
122
123         // Now, the logic should be in a sub directory for the logic executor type
124         fullLogicFilePath += "/" + axLogic.getLogicFlavour().toLowerCase();
125
126         // Check if we're using the default logic
127         if (defaultLogic != null) {
128             // Default logic
129             fullLogicFilePath += "/" + defaultLogic;
130         } else {
131             if (axLogic.getKey().getParentLocalName().equals(AxKey.NULL_KEY_NAME)) {
132                 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName() + axLogic.getKey().getLocalName();
133             } else {
134                 fullLogicFilePath += "/" + axLogic.getKey().getParentKeyName()
135                         + axLogic.getKey().getParentLocalName() + axLogic.getKey().getLocalName();
136             }
137         }
138
139         // Now get the type of executor to find the extension of the file
140         fullLogicFilePath += "." + axLogic.getLogicFlavour().toLowerCase();
141
142         final String logicString = ResourceUtils.getResourceAsString(fullLogicFilePath);
143
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);
149         }
150
151         // Return the right trimmed logic string
152         return logicString.replaceAll("\\s+$", "");
153     }
154 }