summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/validation/ruledriven/rule/GroovyRule.java
blob: 93902c0d384ca82c50e20fcb691fc89171578c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2018-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.validation.ruledriven.rule;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.MetaMethod;
import groovy.lang.MissingMethodException;
import groovy.lang.MissingPropertyException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.InvokerInvocationException;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.validation.logging.LogHelper;
import org.onap.aai.validation.reader.data.AttributeValues;
import org.onap.aai.validation.ruledriven.configuration.GroovyConfigurationException;
import org.onap.aai.validation.ruledriven.configuration.RuleSection;
import org.onap.aai.validation.util.StringUtils;

/**
 * Rule based on a Groovy script
 *
 */
public class GroovyRule implements Rule {

    private static final Logger applicationLogger = LogHelper.INSTANCE;

    /**
     * Do not allow special characters (e.g. ?) in an attribute name. Hyphens and underscores are accepted.
     */
    private static final Pattern ATTRIBUTE_NAME_WHITELIST = Pattern.compile("^[a-zA-Z0-9-_.\\*\\[\\]]*$");
    private static final Pattern ATTRIBUTE_NAME_BLACKLIST = Pattern.compile("^(|null)$");

    private String errorCategory;
    private String errorMessage;
    private String severity;
    private List<String> attributes;
    private List<String> attributePaths; // where in the JSON entity to read the attributes from

    private String methodName;
    private GroovyObject groovyObject;
    private List<String> originalFields;
    private String originalExpression;
    private String groovyExpression; // NOSONAR stored for debugging purposes
    private boolean ruleIsValid = true;
    private String name;

    /**
     * @param ruleConfig
     * @throws GroovyConfigurationException
     *             if the Groovy expression cannot be compiled
     * @throws IOException
     *             if the Groovy class loader throws an internal exception
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public GroovyRule(RuleSection ruleConfig)
            throws GroovyConfigurationException, IOException, InstantiationException, IllegalAccessException {
        setName(ruleConfig.getName());
        setErrorCategory(ruleConfig.getCategory());
        setErrorMessage(ruleConfig.getErrorMessage());
        setSeverity(ruleConfig.getSeverity());
        setAttributes(ruleConfig.getAttributes());
        setAttributePaths(ruleConfig.getAttributes());

        this.originalFields = new ArrayList<>();
        for (String field : ruleConfig.getExpressionFieldNames()) {
            originalFields.add(StringUtils.stripPrefix(field, "."));
        }

        Class<?> groovyClass = createRule(ruleConfig.getExpressionFieldNames(), ruleConfig.getExpression());

        if (groovyClass != null) {
            groovyObject = (GroovyObject) groovyClass.newInstance();

            for (MetaMethod method : groovyObject.getMetaClass().getMethods()) {
                if (method.getName().startsWith("rule")) {
                    methodName = method.getName();
                }
            }

            try {
                executeWithSampleData();
            } catch (IllegalArgumentException e) { // NOSONAR
                if (e.getCause() instanceof InvokerInvocationException
                        && e.getCause().getCause() instanceof MissingMethodException) {
                    applicationLogger
                            .debug("WARNING: Rule \"" + getName() + "\" does not accept \"1\" for all input values");
                } else {
                    ruleIsValid = false;
                }
            }
        } else {
            ruleIsValid = false;
        }
    }

    public boolean isValid() {
        return ruleIsValid;
    }

    /**
     * Run the rule expression on the specified attribute values
     *
     * @param attributeValues
     * @return
     */
    @Override
    public RuleResult execute(AttributeValues attributeValues) {
        // Obtain the values of each of the attributes to pass into the rule
        List<Object> valueList = new ArrayList<>();
        for (String attrName : this.attributePaths) {
            valueList.add(attributeValues.get(attrName));
        }
        Object[] attrValuesArray = valueList.toArray();
        return execute(attrValuesArray);
    }

    /**
     * Apply the rule to some attribute(s)
     *
     * @param values
     *
     * @param groovyObject
     *            an instance/object of a Groovy class that implements one or more rule methods
     * @return the result of evaluating the expression
     */
    @Override
    public RuleResult execute(Object... values) {
        Object result = null;
        try {
            result = groovyObject.invokeMethod(getRuleMethod(), values);
        } catch (MissingPropertyException | MissingMethodException | InvokerInvocationException e) {
            throw new IllegalArgumentException(e);
        } catch (NullPointerException e) {
            throw new IllegalArgumentException("Argument is null", e);
        }
        return new RuleResult(result);
    }

    @Override
    public String toString() {
        return "GroovyRule \"" + name + "\" " + attributePaths + " -> " + originalFields + " {" + originalExpression
                + "}";
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getErrorCategory() {
        return errorCategory;
    }

    public void setErrorCategory(String errorCategory) {
        this.errorCategory = errorCategory;
    }

    @Override
    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    @Override
    public String getSeverity() {
        return severity;
    }

    public void setSeverity(String severity) {
        this.severity = severity;
    }

    @Override
    public List<String> getAttributePaths() {
        return attributePaths;
    }

    private String getRuleMethod() {
        return methodName;
    }

    private void setAttributePaths(List<String> attributePaths) {
        this.attributePaths = attributePaths;
    }

    private void setAttributes(List<String> attributes) {
        this.attributes = new ArrayList<>();
        for (String attribute : attributes) {
            // Strip any prefixes containing the . character
            this.attributes.add(attribute.substring(attribute.lastIndexOf('.') + 1));
        }
    }

    /**
     * Create an anonymous Java Class implementing a Groovy Rule method for the supplied attributes and rule expression.
     *
     * @param attributes
     *            the attributes that form the parameters of the Groovy method
     * @param expression
     *            a valid Groovy method expression (implementing a rule)
     * @return the Java Class for accessing the Groovy method
     * @throws GroovyConfigurationException
     *             if the Groovy expression cannot be compiled
     * @throws IOException
     *             if the Groovy class loader throws an internal exception
     */
    private Class<?> createRule(List<String> attributes, String expression)
            throws GroovyConfigurationException, IOException {
        originalExpression = expression;
        groovyExpression = expression;
        String methodParams = "";

        int i = 1;
        for (String attribute : attributes) {
            if (isValidAttributeName(attribute)) {
                String fieldName = "field" + i++;
                methodParams = appendParameter(methodParams, fieldName);
                // Strip any prefixes from the attribute name in case of JayWay expression attributes.
                attribute = StringUtils.stripPrefix(attribute, ".");
                String regex = "\\b" + attribute + "\\b(?!\\()";
                groovyExpression = groovyExpression.replaceAll(regex, fieldName);
            } else {
                ruleIsValid = false;
                return null;
            }
        }

        return loadGroovyClass("def rule(" + methodParams + ") {" + groovyExpression + "}");
    }

    private String appendParameter(String methodParams, String fieldName) {
        StringBuilder newParams = new StringBuilder();
        if (methodParams.length() > 0) {
            newParams.append(methodParams).append(", ");
        }
        newParams.append("Object ").append(fieldName);
        return newParams.toString();
    }

    private boolean isValidAttributeName(String attributeName) {
        if (ATTRIBUTE_NAME_BLACKLIST.matcher(attributeName).matches()) {
            return false;
        }

        if (!ATTRIBUTE_NAME_WHITELIST.matcher(attributeName).matches()) {
            return false;
        }

        // Make sure that the attribute name is NOT purely a number
        try (Scanner sc = new Scanner(attributeName.trim())) {
            if (sc.hasNextInt()) {
                sc.nextInt(); // Consume the integer
                return sc.hasNext(); // Name is valid if there is more content
            }
        }

        return true; // Not an integer
    }

    private void executeWithSampleData() {
        Object[] values = new Object[attributes.size()];
        int i = 0;
        for (String attribute : attributes) {
            if (attribute.contains("[*]")) {
                values[i++] = Arrays.asList("{}");
            } else {
                values[i++] = "1";
            }
        }
        execute(values);
    }

    /**
     * Load and parse a Groovy script to create an anonymous Java Class.
     *
     * @param script
     *            valid Groovy content (for the class)
     * @return the Java Class for accessing the Groovy script
     * @throws GroovyConfigurationException
     *             if the Groovy script cannot be compiled
     * @throws IOException
     *             if the Groovy class loader throws an internal exception
     */
    @SuppressWarnings("rawtypes")
    private static Class loadGroovyClass(String script) throws GroovyConfigurationException, IOException {
        ClassLoader parent = GroovyRule.class.getClassLoader();
        GroovyClassLoader loader = new GroovyClassLoader(parent);
        Class groovyClass;
        try {
            groovyClass = loader.parseClass(script);
        } catch (CompilationFailedException e) {
            throw new GroovyConfigurationException(e);
        } finally {
            loader.close();
        }
        return groovyClass;
    }

}