summaryrefslogtreecommitdiffstats
path: root/dcaedt_validator/checker/src/main/java/org/onap/sdc/dcae/checker/common/DataCommon.java
blob: 52ceef394caacd21a0166fbb54fa0aa8fb8a16a7 (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
package org.onap.sdc.dcae.checker.common;

import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.checker.*;

import java.util.Map;

public class DataCommon extends BaseCommon {

    private static DataCommon instance;

    public synchronized static DataCommon getInstance() {
        if (instance == null)
        {
            instance = new DataCommon();
        }
        return instance;
    }

    private DataCommon() {}

    /* the type can be:
     *   a known type: predefined or user-defined
     *   a collection (list or map) and then check that the entry_schema points to one of the first two cases (is that it?)
     */
    public boolean checkDataType(Map theSpec, Checker.CheckContext theContext, Catalog catalog) {
        TypeCommon typeCommon = TypeCommon.getInstance();
        if (!typeCommon.checkType(Construct.Data, theSpec, theContext, catalog)) {
            return false;
        }

        String type = (String) theSpec.get("type");
        if (/*isCollectionType(type)*/
                "list".equals(type) || "map".equals(type)) {
            Map entrySchema = (Map) theSpec.get("entry_schema");
            if (entrySchema == null) {
                //maybe issue a warning ?? or is 'string' the default??
                return true;
            }

            if (!catalog.hasType(Construct.Data, (String) entrySchema.get("type"))) {
                theContext.addError("Unknown entry_schema type: " + entrySchema, null);
                return false;
            }
        }
        return true;
    }

    /*
     * For inputs/properties/attributes/(parameters). It is the caller's
     * responsability to provide the value (from a 'default', inlined, ..)
     *
     * @param theDef the definition of the given construct/facet as it appears in
     * 			its enclosing type definition.
     * @param
     */
    public boolean checkDataValuation(Object theExpr,
                                       Map<String, ?> theDef,
                                       Checker.CheckContext theContext) {
        //first check if the expression is a function, if not handle it as a value assignment
        Data.Function f = Data.function(theExpr);
        if (f != null) {
            return f.evaluator()
                    .eval(theExpr, theDef, theContext);
        } else {
            Data.Type type = Data.typeByName((String) theDef.get("type"));
            if (type != null) {
                Data.Evaluator evaluator;

                evaluator = type.evaluator();
                if (evaluator == null) {
                    debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "No value evaluator available for type {}", type);
                } else {
                    if ((theExpr != null) && (!evaluator.eval(theExpr, theDef, theContext))) {
                        return false;
                    }
                }


                evaluator = type.constraintsEvaluator();
                if (evaluator == null) {
                    debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "No constraints evaluator available for type {}", type);
                } else {
                    if (theExpr != null) {
                        if (!evaluator.eval(theExpr, theDef, theContext)) {
                            return false;
                        }
                    } else {
                        //should have a null value validatorT
                    }
                }

                return true;
            } else {
                theContext.addError("Expression " + theExpr + " of " + theDef + " could not be evaluated", null);
                return false;
            }
        }
    }


}