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

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

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.onap.sdc.dcae.checker.common.ConstCommon.*;

public class TypeCommon extends BaseCommon {

    private static TypeCommon instance;

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

    private TypeCommon() {}

    public boolean catalogTypes(Construct theConstruct, Map<String, Map> theTypes, Checker.CheckContext theContext, Catalog catalog) {

        boolean res = true;
        for (Map.Entry<String, Map> typeEntry : theTypes.entrySet()) {
            res &= catalogType(theConstruct, typeEntry.getKey(), typeEntry.getValue(), theContext, catalog);
        }

        return res;
    }

    public boolean catalogType(Construct theConstruct,
                                String theName,
                                Map theDef,
                                Checker.CheckContext theContext,
                                Catalog catalog) {

        if (!catalog.addType(theConstruct, theName, theDef)) {
            theContext.addError(theConstruct + TYPE + theName + " re-declaration", null);
            return false;
        }
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "{} type {} has been cataloged", theConstruct, theName);

        String parentType = (String) theDef.get("derived_from");
        if (parentType != null && !catalog.hasType(theConstruct, parentType)) {
            theContext.addError(
                    theConstruct + TYPE + theName + " indicates a supertype that has not (yet) been declared: " + parentType, null);
            return false;
        }
        return true;
    }

    public boolean checkTypeReference(Construct theConstruct,
                                       Checker.CheckContext theContext,
                                       Catalog catalog,
                                       String... theTypeNames) {
        boolean res = true;
        for (String typeName : theTypeNames) {
            if (!isTypeReference(theConstruct, typeName, catalog)) {
                theContext.addError("Reference to " + theConstruct + " type '" + typeName + "' points to unknown type", null);
                res = false;
            }
        }
        return res;
    }

    public boolean isTypeReference(Construct theConstruct,
                                   String theTypeName, Catalog catalog) {
        return catalog.hasType(theConstruct, theTypeName);
    }

    //generic checking actions, not related to validation rules

    /* will check the validity of the type specification for any construct containing a 'type' entry */
    public boolean checkType(Construct theCategory, Map theSpec, Checker.CheckContext theContext, Catalog catalog) {
        String type = (String) theSpec.get("type");
        if (type == null) {
            theContext.addError("Missing type specification", null);
            return false;
        }

        if (!catalog.hasType(theCategory, type)) {
            theContext.addError(UNKNOWN + theCategory + " type: " + type, null);
            return false;
        }

        return true;
    }

    /* node or relationship templates */
    public boolean checkTemplateReference(Construct theConstruct,
                                           Checker.CheckContext theContext,
                                           Catalog catalog,
                                           String... theTemplateNames) {
        boolean res = true;
        for (String templateName : theTemplateNames) {
            if (!isTemplateReference(theConstruct, theContext, templateName, catalog)) {
                theContext.addError("Reference to " + theConstruct + " template '" + templateName + "' points to unknown template", null);
                res = false;
            }
        }
        return res;
    }

    public boolean isTemplateReference(Construct theConstruct,
                                        Checker.CheckContext theContext,
                                        String theTemplateName,
                                        Catalog catalog) {
        return catalog.hasTemplate(theContext.target(), theConstruct, theTemplateName);
    }
}