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

import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.checker.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.onap.sdc.dcae.checker.common.ConstCommon.DEFAULT;
import static org.onap.sdc.dcae.checker.common.ConstCommon.TYPE;
import static org.onap.sdc.dcae.checker.common.ConstCommon.UNKNOWN;

public class FacetCommon extends BaseCommon {

    private static FacetCommon instance;

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

    private FacetCommon() {}
    /**
     * Given the type of a certain construct (node type for example), look up
     * in one of its facets (properties, capabilities, ..) for one of the given
     * facet type (if looking in property, one of the given data type).
     *
     * @return a map of all facets of the given type, will be empty to signal
     * none found
     * <p>
     * Should we look for a facet construct of a compatible type: any type derived
     * from the given facet's construct type??
     */
    public Map<String, Map>
    findTypeFacetByType(Construct theTypeConstruct,
                        String theTypeName,
                        Facet theFacet,
                        String theFacetType,
                        Catalog catalog) {

        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTypeFacetByType {}, {}: {} {}", theTypeName, theTypeConstruct, theFacetType, theFacet);
        Map<String, Map> res = new HashMap<>();
        Iterator<Map.Entry<String, Map>> i =
                catalog.hierarchy(theTypeConstruct, theTypeName);
        while (i.hasNext()) {
            Map.Entry<String, Map> typeSpec = i.next();
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTypeFacetByType, Checking {} type {}", theTypeConstruct, typeSpec.getKey());
            Map<String, Map> typeFacet =
                    (Map<String, Map>) typeSpec.getValue().get(theFacet.name());
            if (typeFacet == null) {
                continue;
            }
            Iterator<Map.Entry<String, Map>> fi = typeFacet.entrySet().iterator();
            while (fi.hasNext()) {
                Map.Entry<String, Map> facet = fi.next();
                String facetType = (String) facet.getValue().get("type");
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTypeFacetByType, Checking {} type {}", facet.getKey(), facetType);

                //here is the question: do we look for an exact match or ..
                //now we check that the type has a capability of a type compatible
                //(equal or derived from) the given capability type.
                if (catalog.isDerivedFrom(
                        theFacet.construct(), facetType, theFacetType)) {
                    res.putIfAbsent(facet.getKey(), facet.getValue());
                }
            }
        }
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTypeFacetByType, found {}", res);

        return res;
    }

    public Map<String, Object>
    findTypeFacetByName(Construct theTypeConstruct,
                        String theTypeName,
                        Facet theFacet,
                        String theFacetName,
                        Catalog catalog) {
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTypeFacetByName {} {}", theTypeConstruct, theTypeName);
        Iterator<Map.Entry<String, Map>> i =
                catalog.hierarchy(theTypeConstruct, theTypeName);
        while (i.hasNext()) {
            Map.Entry<String, Map> typeSpec = i.next();
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "findTypeFacetByName, Checking {} type {}", theTypeConstruct, typeSpec.getKey());
            Map<String, Map> typeFacet =
                    (Map<String, Map>) typeSpec.getValue().get(theFacet.name());
            if (typeFacet == null) {
                continue;
            }
            Map<String, Object> facet = typeFacet.get(theFacetName);
            if (facet != null) {
                return facet;
            }
        }
        return null;
    }

    /* Check that a particular facet (properties, attributes) of a construct type
     * (node type, capability type, etc) is correctly (consistenly) defined
     * across a type hierarchy
     */
    public boolean checkTypeConstructFacet(Construct theConstruct,
                                           String theTypeName,
                                           Map theTypeSpec,
                                           Facet theFacet,
                                           Checker.CheckContext theContext,
                                           Catalog catalog) {
        Map<String, Map> defs =
                (Map<String, Map>) theTypeSpec.get(theFacet.name());
        if (null == defs) {
            return true;
        }

        boolean res = true;

        //given that the type was cataloged there will be at least one entry
        Iterator<Map.Entry<String, Map>> i =
                catalog.hierarchy(theConstruct, theTypeName);
        if (!i.hasNext()) {
            theContext.addError(
                    "The type " + theTypeName + " needs to be cataloged before attempting 'checkTypeConstruct'", null);
            return false;
        }
        i.next(); //skip self
        while (i.hasNext()) {
            Map.Entry<String, Map> e = i.next();
            Map<String, Map> superDefs = (Map<String, Map>) e.getValue()
                    .get(theFacet.name());
            if (null == superDefs) {
                continue;
            }
            //this computes entries that appear on both collections but with different values, i.e. the re-defined properties
            Map<String, MapDifference.ValueDifference<Map>> diff = Maps.difference(defs, superDefs).entriesDiffering();

            for (Iterator<Map.Entry<String, MapDifference.ValueDifference<Map>>> di = diff.entrySet().iterator(); di.hasNext(); ) {
                Map.Entry<String, MapDifference.ValueDifference<Map>> de = di.next();
                MapDifference.ValueDifference<Map> dediff = de.getValue();
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "{} type {}: {} has been re-defined between the {} types {} and {}", theConstruct, theFacet, de.getKey(), theConstruct, e.getKey(), theTypeName);
                //for now we just check that the type is consistenly re-declared
                if (!catalog.isDerivedFrom(theFacet.construct(),
                        (String) dediff.leftValue().get("type"),
                        (String) dediff.rightValue().get("type"))) {
                    theContext.addError(
                            theConstruct + TYPE + theFacet + ", redefiniton changed its type: " + de.getKey() + " has been re-defined between the " + theConstruct + " types " + e.getKey() + " and " + theTypeName + " in an incompatible manner", null);
                    res = false;
                }
            }
        }

        return res;
    }

    /* Augmentation occurs in cases such as the declaration of capabilities within a node type.
     * In such cases the construct facets (the capabilitity's properties) can redefine (augment) the
     * specification found in the construct type.
     */
    public boolean checkFacetAugmentation(Construct theConstruct,
                                          Map theSpec,
                                          Facet theFacet,
                                          Checker.CheckContext theContext,
                                          Catalog catalog) {
        return checkFacetAugmentation(theConstruct, theSpec, null, theFacet, theContext, catalog);
    }

    public boolean checkFacetAugmentation(Construct theConstruct,
                                          Map theSpec,
                                          String theSpecType,
                                          Facet theFacet,
                                          Checker.CheckContext theContext,
                                          Catalog catalog) {

        Map<String, Map> augs = (Map<String, Map>) theSpec.get(theFacet.name());
        if (null == augs) {
            return true;
        }

        boolean res = true;
        if (theSpecType == null) {
            theSpecType = (String) theSpec.get("type");
        }
        if (theSpecType == null) {
            theContext.addError("No specification type available", null);
            return false;
        }

        for (Iterator<Map.Entry<String, Map>> ai = augs.entrySet().iterator(); ai.hasNext(); ) {
            Map.Entry<String, Map> ae = ai.next();

            //make sure it was declared by the type
            Map facetDef = catalog.getFacetDefinition(theConstruct, theSpecType, theFacet, ae.getKey());
            if (facetDef == null) {
                theContext.addError(UNKNOWN + theConstruct + " " + theFacet + " (not declared by the type " + theSpecType + ") were used: " + ae.getKey(), null);
                res = false;
                continue;
            }

            //check the compatibility of the augmentation: only the type cannot be changed
            //can the type be changed in a compatible manner ??
            if (!facetDef.get("type").equals(ae.getValue().get("type"))) {
                theContext.addError(theConstruct + " " + theFacet + " " + ae.getKey() + " has a different type than its definition: " + ae.getValue().get("type") + " instead of " + facetDef.get("type"), null);
                res = false;
                continue;
            }
            DataCommon dataCommon = DataCommon.getInstance();
            //check any valuation (here just defaults)
            Object defaultValue = ae.getValue().get(DEFAULT);
            if (defaultValue != null) {
                dataCommon.checkDataValuation(defaultValue, ae.getValue(), theContext);
            }
        }

        return res;
    }

    /*
     * Checks the validity of a certain facet of a construct
     * (properties of a node) across a type hierarchy.
     * For now the check is limited to a verifying that a a facet was declared
     * somewhere in the construct type hierarchy (a node template property has
     * been declared in the node type hierarchy).
     *
     * 2 versions with the more generic allowing the specification of the type
     * to be done explicitly.
     */
    public boolean checkFacet(Construct theConstruct,
                               Map theSpec,
                               Facet theFacet,
                               Checker.CheckContext theContext,
                               Catalog catalog) {
        return checkFacet(theConstruct, theSpec, null, theFacet, theContext, catalog);
    }

    /**
     * We walk the hierarchy and verify the assignment of a property with respect to its definition.
     * We also collect the names of those properties defined as required but for which no assignment was provided.
     */
    public boolean checkFacet(Construct theConstruct,
                               Map theSpec,
                               String theSpecType,
                               Facet theFacet,
                               Checker.CheckContext theContext,
                               Catalog catalog) {

        Map<String, Map> defs = (Map<String, Map>) theSpec.get(theFacet.name());
        if (null == defs) {
            return true;
        }
        defs = Maps.newHashMap(defs); //

        boolean res = true;
        if (theSpecType == null) {
            theSpecType = (String) theSpec.get("type");
        }
        if (theSpecType == null) {
            theContext.addError("No specification type available", null);
            return false;
        }

        Map<String, Byte> missed = new HashMap<>();  //keeps track of the missing required properties, the value is
        //false if a default was found along the hierarchy
        Iterator<Map.Entry<String, Map>> i =
                catalog.hierarchy(theConstruct, theSpecType);
        while (i.hasNext() && !defs.isEmpty()) {
            Map.Entry<String, Map> type = i.next();

            Map<String, Map> typeDefs = (Map<String, Map>) type.getValue()
                    .get(theFacet.name());
            if (null == typeDefs) {
                continue;
            }

            MapDifference<String, Map> diff = Maps.difference(defs, typeDefs);

            //this are the ones this type and the spec have in common (same key,
            //different values)
            Map<String, MapDifference.ValueDifference<Map>> facetDefs =
                    diff.entriesDiffering();
            //TODO: this assumes the definition of the facet is not cumulative, i.e.
            //subtypes 'add' something to the definition provided by the super-types
            //it considers the most specialized definition stands on its own
            for (MapDifference.ValueDifference<Map> valdef : facetDefs.values()) {
                DataCommon dataCommon = DataCommon.getInstance();
                dataCommon.checkDataValuation(valdef.leftValue(), valdef.rightValue(), theContext);
            }

            //remove from properties all those that appear in this type: unfortunately this returns an unmodifiable map ..
            defs = Maps.newHashMap(diff.entriesOnlyOnLeft());
        }

        if (!defs.isEmpty()) {
            theContext.addError(UNKNOWN + theConstruct + " " + theFacet + " (not declared by the type " + theSpecType + ") were used: " + defs, null);
            res = false;
        }

        if (!missed.isEmpty()) {
            List missedNames =
                    missed.entrySet()
                            .stream()
                            .filter(e -> e.getValue().byteValue() == (byte) 1)
                            .map(e -> e.getKey())
                            .collect(Collectors.toList());
            if (!missedNames.isEmpty()) {
                theContext.addError(theConstruct + " " + theFacet + " missing required values for: " + missedNames, null);
                res = false;
            }
        }

        return res;
    }


}