summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/api/src/main/java/org/onap/sdc/dcae/catalog/asdc/ASDCCatalog.java
blob: dfbaeaae0fbd41d741bc5c8ba0d5bb3e536eabe1 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
package org.onap.sdc.dcae.catalog.asdc;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.IOUtils;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathNotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.dcae.catalog.Catalog;
import org.onap.sdc.dcae.catalog.commons.*;
import org.onap.sdc.dcae.checker.*;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@SuppressWarnings("ALL")
public class ASDCCatalog implements Catalog {

    private
    static final String JXPATH_NOT_FOUND_EXCEPTION = "JXPathNotFoundException {}";
    private
    static final String OCCURRENCES = "occurrences";
    private
    static final String TOPOLOGY_TEMPLATE_NODE_TEMPLATES = "/topology_template/node_templates";
    private
    static final String NODES_NAME = "/nodes[name='";
    private
    static final String ITEM_ID = "itemId";
    private
    static final String LABELS = "labels";
    private
    static final String ARTIFACT_URL = "artifactURL";
    private
    static final String CAPABILITY = "capability";
    private
    static final String DATABASE = "Database";
    private
    static final String COLLECTOR = "Collector";
    private
    static final String MICROSERVICE = "Microservice";
    private
    static final String ANALYTICS = "Analytics";
    private
    static final String POLICY = "Policy";
    private
    static final String SOURCE = "Source";
    private
    static final String UTILITY = "Utility";
    private
    static final String NAME = "name";
    private
    static final String ID = "id";
    private
    static final String ARTIFACT_NAME = "artifactName";
    private
    static final String DESCRIPTION = "description";
    private
    static final String MODELS = "models";
    private
    static final String ARTIFACTS = "artifacts";
    private
    static final String ITEMS = "items";
    private
    static final String PROPERTIES = "']/properties";
    private
    static final String TOPOLOGY_TEMPLATE_NODE_TEMPLATES1 = "/topology_template/node_templates/";
    private
    static final String PROPERTIES_NAME = "']/properties[name='";
    private
    static final String CAPABILITIES = "']/capabilities";
    private
    static final String CAPABILITIES_NAME = "']/capabilities[name='";

    private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

    private ASDC asdc;

    private JSONObject folders = new JSONObject();
    private String[] folderFields = new String[] {ID, ITEM_ID, NAME};

    private ProxyBuilder proxies;
    private Map<Target, JXPathContext> contexts = new HashMap<>();

    // resource and its catalog
    private Map<UUID, org.onap.sdc.dcae.checker.Catalog> catalogs = new HashMap<>();

    public ASDCCatalog(URI theURI) {

        this.asdc = new ASDC();
        this.asdc.setUri(theURI);

        initFolders();

        this.proxies = new ProxyBuilder().withConverter(v -> v == null ? null : UUID.fromString(v.toString()), UUID.class)
                .withExtensions(
                new ImmutableMap.Builder<String, BiFunction<Proxy, Object[], Object>>().put("data", (proxy, args) -> proxy.data())
                        .build()).withContext(new ImmutableMap.Builder<String, Object>().put("catalog", this).build());
    }

    private void initFolders() {

        JSONArray labels = new JSONArray();
        labels.put("Folder");
        labels.put("DCAE");
        labels.put("Superportfolio"); // for CCD compatibility

        folders.put(DATABASE, new JSONObject().put(NAME, DATABASE).put(ID, "dcae_database")
                .put(ITEM_ID, DATABASE).put(LABELS, labels));
        folders.put(COLLECTOR, new JSONObject().put(NAME, COLLECTOR).put(ID, "dcae_collector")
                .put(ITEM_ID, COLLECTOR).put(LABELS, labels));
        folders.put(MICROSERVICE, new JSONObject().put(NAME, MICROSERVICE).put(ID, "dcae_microservice")
                .put(ITEM_ID, MICROSERVICE).put(LABELS, labels));
        folders.put(ANALYTICS, new JSONObject().put(NAME, ANALYTICS).put(ID, "dcae_analytics")
                .put(ITEM_ID, ANALYTICS).put(LABELS, labels));
        folders.put(POLICY, new JSONObject().put(NAME, POLICY).put(ID, "dcae_policy").put(ITEM_ID, POLICY)
                .put(LABELS, labels));
        folders.put(SOURCE, new JSONObject().put(NAME, SOURCE).put(ID, "dcae_source").put(ITEM_ID, SOURCE)
                .put(LABELS, labels));
        folders.put(UTILITY, new JSONObject().put(NAME, UTILITY).put(ID, "dcae_utility")
                .put(ITEM_ID, UTILITY).put(LABELS, labels));
    }

    public URI getUri() {
        return this.asdc.getUri();
    }

    public String namespace() {
        return "asdc";
    }

    public boolean same(Catalog theCatalog) {
        return true;
    }

    public <T> T proxy(JSONObject theData, Class<T> theType) {
        return proxies.build(theData, theType);
    }

    /** */
    public Future<Folders> roots() {

        Folders roots = new Folders();
        for (Iterator fi = folders.keys(); fi.hasNext();) {
            roots.add(proxies.build(folders.getJSONObject((String) fi.next()), Folder.class));
        }
        return Futures.succeededFuture(roots);
    }

    /** */
    public Future<Folders> rootsByLabel(String theLabel) {

        Folders roots = new Folders();
        for (Iterator fi = folders.keys(); fi.hasNext();) {
            JSONObject folder = folders.getJSONObject((String) fi.next());
            JSONArray labels = folder.getJSONArray(LABELS);

            for (int i = 0; i < labels.length(); i++) {
                if (labels.get(i).equals(theLabel)) {
                    roots.add(proxies.build(folder, Folder.class));
                }
            }
        }
        return Futures.succeededFuture(roots);
    }

    public Future<Mixels> lookup(JSONObject theSelector) {
        return Futures.succeededFuture(new Mixels());
    }

    public Future<Mixels> lookup(String theAnnotation, JSONObject theSelector) {
        return Futures.succeededFuture(new Mixels());
    }

    public ItemAction item(String theItemId) {
        return new ResourceAction(UUID.fromString(theItemId));
    }

    public CatalogFolderAction folder(String theFolderId) {
        return new CatalogFolderAction(theFolderId);
    }

    public CatalogTemplateAction template(String theId) {
        return new CatalogTemplateAction(theId);
    }

    public CatalogTypeAction type(String theItemId, String theName) {
        return new CatalogTypeAction(UUID.fromString(theItemId), theName);
    }

    private Object resolve(Target theTarget, String thePath) {
        try {
            return contexts.get(theTarget).getValue(thePath);
        } catch (JXPathNotFoundException pnfx) {
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), JXPATH_NOT_FOUND_EXCEPTION, pnfx);
            return null;
        }
    }

    // covers common TOSCA pattern of single entry maps
    private Map.Entry<String, Map> toEntry(Object theValue) {
        return (Map.Entry<String, Map>) ((Map) theValue).entrySet().iterator().next();
    }

    private Map selectEntries(Map theOriginal, String... theKeys) {
        Arrays.sort(theKeys);
        return ((Set<Map.Entry>) theOriginal.entrySet()).stream()
                .filter(e -> Arrays.binarySearch(theKeys, e.getKey().toString()) >= 0)
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
    }

    private Map evictEntries(Map theOriginal, String... theKeys) {
        Arrays.sort(theKeys);
        return ((Set<Map.Entry>) theOriginal.entrySet()).stream()
                .filter(e -> Arrays.binarySearch(theKeys, e.getKey().toString()) < 0)
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
    }

    protected <T> Stream<T> stream(Iterator<T> theSource) {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(theSource,
                Spliterator.NONNULL | Spliterator.DISTINCT | Spliterator.IMMUTABLE), false);
    }

    private JSONArray selectModels(JSONArray theArtifacts) {
        JSONArray models = new JSONArray();
        if (theArtifacts == null) {
            return models;
        }

        for (int i = 0; i < theArtifacts.length(); i++) {
            JSONObject artifact = theArtifacts.getJSONObject(i);
            String name = artifact.optString(ARTIFACT_NAME);
            if (name != null && StringUtils.containsIgnoreCase(name, "template")) {
                models.put(new JSONObject().putOpt(NAME, artifact.optString(ARTIFACT_NAME))
                        .putOpt("version", artifact.optString("artifactVersion"))
                        .putOpt(DESCRIPTION, artifact.optString("artifactType"))
                        .putOpt(ID, artifact.optString(ARTIFACT_URL))
                        .putOpt(ITEM_ID, artifact.optString(ARTIFACT_URL)));
            }
        }
        return models;
    }

    private JSONObject patchResource(JSONObject theResource) {

        theResource.remove("resources");
        theResource.putOpt(ID, theResource.opt("uuid"));
        theResource.putOpt(ITEM_ID, theResource.opt("uuid"));

        return theResource;
    }

    private static void dumpTargets(String theDirName, Collection<Target> theTargets) {
        File targetDir = new File(theDirName);
        if (!targetDir.exists() && !targetDir.mkdirs()) {
            throw new IllegalStateException("Couldn't create dir: " + theDirName);
        }
        for (Target t : theTargets) {
            try (FileWriter dump = new FileWriter(new File(theDirName, t.getName()))) {
                IOUtils.copy(t.open(), dump);
                dump.close();
            } catch (IOException e) {
                debugLogger.log(LogLevel.DEBUG, "ASDCCatalog", "IOException {}", e);
            }
        }
    }

    private static URI asURI(String theValue) {
        try {
            return new URI(theValue);
        } catch (URISyntaxException urisx) {
            throw new IllegalArgumentException("Invalid URI", urisx);
        }
    }

    private static UUID asUUID(String theValue) {
        return UUID.fromString(theValue);
    }

    private org.onap.sdc.dcae.checker.Catalog getCatalog(UUID theResourceId) {
        return this.catalogs.get(theResourceId);
    }

    private String getArtifactVersion(JSONObject theData) {
        return theData.getString("artifactVersion");
    }

    private String getArtifactName(JSONObject theData) {
        return theData.getString(ARTIFACT_NAME);
    }

    private String getArtifactURL(JSONObject theData) {
        return theData.getString(ARTIFACT_URL);
    }

    private URI getArtifactURI(JSONObject theData) {
        return asURI(theData.getString(ARTIFACT_URL));
    }

    /** */
    public class ResourceAction implements Catalog.ItemAction<Resource> {

        private UUID iid;
        private boolean doModels;

        ResourceAction(UUID theItemId) {
            this.iid = theItemId;
        }

        public ResourceAction withModels() {
            this.doModels = true;
            return this;
        }

        public ResourceAction withAnnotations() {
            return this;
        }

        @Override
        public Future<Resource> execute() {

            return Futures.advance(asdc.getResource(this.iid, JSONObject.class), resourceData -> {
                if (doModels) {
                    resourceData.put(MODELS, selectModels(resourceData.optJSONArray(ARTIFACTS)));
                }
                return proxies.build(patchResource(resourceData), Resource.class);
            });
        }

        protected Future<JSONObject> executeRaw() {

            return Futures.advance(asdc.getResource(this.iid, JSONObject.class), resourceData -> {
                if (doModels) {
                    resourceData.put(MODELS, selectModels(resourceData.optJSONArray(ARTIFACTS)));
                }
                return resourceData;
            }, resourceError -> new RuntimeException("Failed to retrieve item " + this.iid, resourceError));
        }
    }

    public class CatalogFolderAction implements Catalog.FolderAction {

        private boolean doItemModels;
        private String folderName;

        // use the id/UUID of the folder ??
        private CatalogFolderAction(String theFolderName) {
            this.folderName = theFolderName;
        }

        public CatalogFolderAction withAnnotations() {
            return this;
        }

        public CatalogFolderAction withAnnotations(String theSelector) {
            return this;
        }

        public CatalogFolderAction withItems() {
            return this;
        }

        public CatalogFolderAction withItemAnnotations() {
            return this;
        }

        public CatalogFolderAction withItemAnnotations(String theSelector) {
            return this;
        }

        public CatalogFolderAction withItemModels() {
            doItemModels = true;
            return this;
        }

        public CatalogFolderAction withParts() {
            return this;
        }

        public CatalogFolderAction withPartAnnotations() {
            return this;
        }

        public CatalogFolderAction withPartAnnotations(String theSelector) {
            return this;
        }

        @Override
        public Future<Folder> execute() {

            JSONObject folder = folders.optJSONObject(this.folderName);
            if (folder == null) {
                return Futures.failedFuture(new RuntimeException("No such folder " + this.folderName));
            }

            final JSONObject folderView = new JSONObject(folder, folderFields);

            return Futures.advance(asdc.getResources(JSONArray.class, "DCAE Component", this.folderName),
                    resourcesData -> {

                        Actions.CompoundAction<Resource> itemsAction = new Actions.BasicCompoundAction<>();
                        for (int i = 0; i < resourcesData.length(); i++) {
                            JSONObject resource = resourcesData.getJSONObject(i);

                            if (doItemModels) {
                                itemsAction
                                        .addAction(new ResourceAction(asUUID(resource.getString("uuid"))).withModels());
                            } else {
                                folderView.append(ITEMS, patchResource(resource));
                            }
                        }

                        try {
                            List<Resource> items = itemsAction.execute().waitForResult();
                            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Number of DCAE item for : {} is {}", this.folderName, items.size());

                            for (Resource res : filterLatestVersion(items)) {
                                folderView.append(ITEMS, patchResource(res.data()));
                            }
                        } catch (Exception x) {
                            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Exception {}", x);
                            throw new RuntimeException("Failed to retrieve folder items", x);
                        }

                        return proxies.build(folderView, Folder.class);
                    }, resourcesError -> new RuntimeException("Failed to retrieve resources", resourcesError));
        }

        public Collection<Resource> filterLatestVersion(Collection<Resource> items) {
            if (items == null) {
                throw new IllegalArgumentException("null is not acceptable as a list of items");
            }
            Map<UUID, Resource> itemsMap = new HashMap<UUID, Resource>(items.size());
            for (Resource r : items) {
                if (itemsMap.containsKey(r.invariantUUID()) && isNewerVersion(itemsMap, r)) {
                    debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Avoiding adding item {} since it has a advanced version already", r.toString());
                    continue;
                }
                itemsMap.put(r.invariantUUID(), r);
            }
            return itemsMap.values();
        }

        private boolean isNewerVersion(Map<UUID, Resource> itemsMap, Resource r) {
            return Float.valueOf(itemsMap.get(r.invariantUUID()).version()) > Float.valueOf(r.version());
        }

    }

    /** */
    public class CatalogTemplateAction implements Catalog.TemplateAction {

        private String artifactId;
        private Target target;
        private org.onap.sdc.dcae.checker.Catalog catalog;
        private JXPathContext ctx = JXPathContext.newContext(new HashMap());

        private boolean doNodes, doNodeProperties, doNodePropertiesAssignments, doNodeRequirements, doNodeCapabilities,
                doNodeCapabilityProperties, doNodeCapabilityPropertyAssignments;

        /*
         * expected to be the relative url provided by asdc for the template
         * artifact
         */
        CatalogTemplateAction(String theArtifactId) {
            this.artifactId = theArtifactId;
        }

        public CatalogTemplateAction withInputs() {
            return this;
        }

        public CatalogTemplateAction withOutputs() {
            return this;
        }

        public CatalogTemplateAction withNodes() {
            this.doNodes = true;
            return this;
        }

        CatalogTemplateAction doNodes() {
            if (!this.doNodes) {
                return this;
            }

            Map nodes = (Map) resolve(this.target, TOPOLOGY_TEMPLATE_NODE_TEMPLATES);
            if (nodes == null) {
                return this;
            }

            ctx.setValue("/nodes",
                    nodes.entrySet().stream()
                            .map(nodeEntry -> new MapBuilder().put(NAME, ((Map.Entry) nodeEntry).getKey())
                                    .put(DESCRIPTION, this.artifactId)
                                    .putAll(selectEntries((Map) ((Map.Entry) nodeEntry).getValue(), "type")).build())
                            .collect(Collectors.toList()));

            return this;
        }

        // pre-requisite: a call to 'withNodes'
        public CatalogTemplateAction withNodeProperties() {
            this.doNodeProperties = true;
            return this;
        }

        protected CatalogTemplateAction doNodeProperties() {
            if (!this.doNodeProperties) {
                return this;
            }

            Map nodes = (Map) resolve(this.target, TOPOLOGY_TEMPLATE_NODE_TEMPLATES);
            if (nodes == null) {
                return this;
            }

            nodes.entrySet().stream().forEach(node -> ctx.setValue(
                    NODES_NAME + ((Map.Entry) node).getKey() + PROPERTIES,
                    stream(catalog.facets(Construct.Node, Facet.properties,
                            ((Map) ((Map.Entry) node).getValue()).get("type").toString()))
                                    .map(propEntry -> new MapBuilder().put(NAME, propEntry.getKey())
                                            .putAll((Map) propEntry.getValue()).build())
                                    .collect(Collectors.toList())));

            return this;
        }

        // pre-requisite: a call to 'withNodesProperties'
        public CatalogTemplateAction withNodePropertiesAssignments() {
            this.doNodePropertiesAssignments = true;
            return this;
        }

        CatalogTemplateAction doNodePropertiesAssignments() {
            if (!this.doNodePropertiesAssignments) {
                return this;
            }

            Map nodes = (Map) resolve(this.target, TOPOLOGY_TEMPLATE_NODE_TEMPLATES);
            if (nodes == null) {
                return this;
            }

            nodes.entrySet().forEach(node -> {
                List nodeProps;
                try {
                    nodeProps = (List) ctx.getValue(NODES_NAME + ((Map.Entry) node).getKey() + PROPERTIES);
                } catch (JXPathNotFoundException pnfx) {
                    debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), JXPATH_NOT_FOUND_EXCEPTION, pnfx);
                    return;
                }

                nodeProps.forEach(prop -> {
                    // pick from
                    String propPath = TOPOLOGY_TEMPLATE_NODE_TEMPLATES1 + ((Map.Entry) node).getKey()
                            + "/properties/" + ((Map) prop).get(NAME);
                    Object propValue = resolve(this.target, propPath);
                    // to conform with the db based api we should analyze the
                    // value for function calls
                    // dump at ..
                    propPath = NODES_NAME + ((Map.Entry) node).getKey() + PROPERTIES_NAME
                            + ((Map) prop).get(NAME) + "']";
                    if (propValue != null) {
                        ctx.setValue(propPath + "/assignment",
                                new ImmutableMap.Builder().put("value", propValue).build());
                    }
                });
            });

            return this;
        }

        Map renderRequirementDefinition(Map.Entry theReq) {
            Map def = (Map) theReq.getValue();
            return new MapBuilder().put(NAME, theReq.getKey())
                    // capability must be present
                    .put(CAPABILITY,
                            new MapBuilder().put(NAME, def.get(CAPABILITY))
                                    .put(ID, this.target.getName() + "/" + def.get(CAPABILITY)).build())
                    .putAll(evictEntries(def, CAPABILITY)).build();
        }

        // TODO: see how this comes out of neo and match it
        Map renderRequirementAssignment(Map.Entry theReq) {
            Map def = (Map) theReq.getValue();
            return new MapBuilder().put(NAME, theReq.getKey())
                    // capability must be present
                    .put(CAPABILITY,
                            new MapBuilder().put(NAME, def.get(CAPABILITY))
                                    // we provide an id only if the capability
                                    // points to a type
                                    .putOpt(ID,
                                            catalog.hasType(Construct.Capability, (String) def.get(CAPABILITY))
                                                    ? (this.target.getName() + "/" + def.get(CAPABILITY)) : null)
                                    .build())
                    .putAll(evictEntries(def, CAPABILITY)).build();
        }

        public CatalogTemplateAction withNodeRequirements() {
            this.doNodeRequirements = true;
            return this;
        }

        CatalogTemplateAction doNodeRequirements() {
            if (!this.doNodeRequirements) {
                return this;
            }

            // requirements come first from the type and then can be further
            // refined by their assignment within the
            // node template
            Map nodes = (Map) resolve(this.target, TOPOLOGY_TEMPLATE_NODE_TEMPLATES);
            if (nodes == null) {
                return this;
            }

            // type
            nodes.entrySet()
                    .forEach(
                            node -> ctx
                                    .setValue(
                                            NODES_NAME
                                                    + ((Map.Entry) node)
                                                            .getKey()
                                                    + "']/requirements",
                                            StreamSupport
                                                    .stream(Spliterators.spliteratorUnknownSize(
                                                            catalog.requirements(((Map) ((Map.Entry) node).getValue())
                                                                    .get("type").toString()),
                                                            Spliterator.NONNULL | Spliterator.DISTINCT
                                                                    | Spliterator.IMMUTABLE),
                                                            false)
                                                    .map((Map.Entry reqEntry) -> renderRequirementDefinition(reqEntry))
                                                    .collect(Collectors.toList())));

            // merge assignments on top of definitions
            nodes.entrySet().forEach(node -> {
                List nodeReqsAssigns = (List) resolve(this.target,
                        TOPOLOGY_TEMPLATE_NODE_TEMPLATES1 + ((Map.Entry) node).getKey() + "/requirements");
                if (nodeReqsAssigns == null) {
                    return;
                }
                nodeReqsAssigns.forEach(req -> {
                    Map.Entry reqAssign = toEntry(req);
                    catalog.mergeDefinitions((Map) ctx.getValue(NODES_NAME + ((Map.Entry) node).getKey()
                            + "']/requirements[name='" + reqAssign.getKey() + "']"),
                            renderRequirementAssignment(reqAssign));
                });
            });

            return this;
        }

        public CatalogTemplateAction withNodeCapabilities() {
            this.doNodeCapabilities = true;
            return this;
        }

        Map renderCapabilityDefinition(Map.Entry theCap) {
            Map def = (Map) theCap.getValue();
            return new MapBuilder().put(NAME, theCap.getKey())
                    .put("type",
                            new MapBuilder().put(NAME, def.get("type"))
                                    .put(ID, this.target.getName() + "/" + def.get("type")).build())
                    .putAll(evictEntries(def, "properties", "type")).build();
        }

        CatalogTemplateAction doNodeCapabilities() {
            if (!this.doNodeCapabilities) {
                return this;
            }

            Map nodes = (Map) resolve(this.target, TOPOLOGY_TEMPLATE_NODE_TEMPLATES);
            if (nodes == null) {
                return this;
            }

            // collect capabilities through the node type hierarchy

            // we evict the properties from the node type capability declaration
            // (when declaring a capability with the
            // node type some re-definition of capability properties can take
            // place).
            nodes.entrySet().stream()
                    .forEach(node -> ctx.setValue(NODES_NAME + ((Map.Entry) node).getKey() + CAPABILITIES,

                            stream(catalog.facets(Construct.Node, Facet.capabilities,
                                    ((Map) ((Map.Entry) node).getValue()).get("type").toString()))
                                            .map(this::renderCapabilityDefinition)
                                            .collect(Collectors.toList())));

            return this;
        }

        public CatalogTemplateAction withNodeCapabilityProperties() {
            this.doNodeCapabilityProperties = true;
            return this;
        }

        CatalogTemplateAction doNodeCapabilityProperties() {

            if (!this.doNodeCapabilityProperties) {
                return this;
            }

            Map nodes = (Map) resolve(this.target, TOPOLOGY_TEMPLATE_NODE_TEMPLATES);
            if (nodes == null) {
                return this;
            }

            // pick up all the properties from the capability type hierarchy
            // definition
            nodes.entrySet().forEach(node -> {
                List nodeCapabilities = (List) ctx
                        .getValue(NODES_NAME + ((Map.Entry) node).getKey() + CAPABILITIES);
                if (nodeCapabilities == null) {
                    return;
                }

                // collect properties from the capability type hierarchy
                nodeCapabilities.forEach(capability -> {
                    List capabilityProperties = StreamSupport
                            .stream(Spliterators.spliteratorUnknownSize(
                                    catalog.facets(Construct.Capability, Facet.properties,
                                            ((Map)((Map)capability).get("type")).get(NAME).toString()),
                                    Spliterator.NONNULL | Spliterator.DISTINCT | Spliterator.IMMUTABLE), false)
                            .map((Map.Entry capEntry) -> new MapBuilder().put(NAME, capEntry.getKey())
                                    .putAll((Map) capEntry.getValue()).build())
                            .collect(Collectors.toList());

                    if (!capabilityProperties.isEmpty()) {
                        ctx.setValue(NODES_NAME + ((Map.Entry) node).getKey() + CAPABILITIES_NAME
                                + ((Map) capability).get(NAME) + PROPERTIES, capabilityProperties);
                    }
                });

                // and go over the node type (hierarchy) and pick up any
                // re-definitions from there.
                StreamSupport
                        .stream(Spliterators.spliteratorUnknownSize(
                                catalog.facets(Construct.Node, Facet.capabilities,
                                        ((Map) ((Map.Entry) node).getValue()).get("type").toString()),
                                Spliterator.NONNULL | Spliterator.DISTINCT | Spliterator.IMMUTABLE), false)
                        .forEach((Map.Entry capability) -> {
                            // for each capability property that has some node
                            // type level re-definition
                            Map properties = (Map) ((Map) capability.getValue()).get("properties");
                            if (properties == null) {
                                return;
                            }

                            properties.entrySet().forEach(property -> {
                                String propertyLoc = NODES_NAME + ((Map.Entry) node).getKey()
                                        + CAPABILITIES_NAME + ((Map) capability).get(NAME)
                                        + PROPERTIES_NAME + ((Map.Entry) property).getKey() + "']";
                                ctx.setValue(propertyLoc, catalog.mergeDefinitions((Map) ctx.getValue(propertyLoc),
                                        (Map) ((Map.Entry) property).getValue()));
                            });
                        });
            });

            return this;
        }

        public CatalogTemplateAction withNodeCapabilityPropertyAssignments() {
            this.doNodeCapabilityPropertyAssignments = true;
            return this;
        }

        CatalogTemplateAction doNodeCapabilityPropertyAssignments() {
            if (!this.doNodeCapabilityPropertyAssignments) {
                return this;
            }

            // this is a wasteful: we go over all declared
            // nodes/capabilities/properties and check if there is an assigned
            // value in the actual template. It is optimal to approach the
            // problem from the other direction: go over delared
            // assignments and set them in the output structure ..

            List nodes = null;
            try {
                nodes = (List) ctx.getValue("/nodes");
            } catch (JXPathNotFoundException pnfx) {
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), JXPATH_NOT_FOUND_EXCEPTION, pnfx);
                return this;
            }

            nodes.forEach(node -> {
                List capabilities = (List) ctx.getValue(NODES_NAME + ((Map) node).get(NAME) + CAPABILITIES);
                if (capabilities == null) {
                    return;
                }

                capabilities.forEach(capability -> {
                    List properties;
                    try {
                        properties = (List) ctx.getValue(NODES_NAME + ((Map) node).get(NAME)
                                + CAPABILITIES_NAME + ((Map) capability).get(NAME) + PROPERTIES);
                    } catch (JXPathNotFoundException pnfx) {
                        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), JXPATH_NOT_FOUND_EXCEPTION, pnfx);
                        return;
                    }

                    properties.forEach(property -> {
                        String location = NODES_NAME + ((Map) node).get(NAME) + CAPABILITIES_NAME
                                + ((Map) capability).get(NAME) + PROPERTIES_NAME + ((Map) property).get(NAME)
                                + "']/assignment";

                        // pick the value from the original
                        try {
                            Object assignment = resolve(this.target,
                                    TOPOLOGY_TEMPLATE_NODE_TEMPLATES1 + ((Map) node).get(NAME) + "/capabilities/"
                                            + ((Map) capability).get(NAME) + "/properties/"
                                            + ((Map) property).get(NAME));
                            if (assignment != null) {
                                ctx.setValue(location, new ImmutableMap.Builder().put("value", assignment).build());
                            }
                        } catch (JXPathNotFoundException pnfx) {
                            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), JXPATH_NOT_FOUND_EXCEPTION, pnfx);
                            // it's ok, no assignment
                        }
                    });
                });
            });

            return this;
        }

        public CatalogTemplateAction withPolicies() {
            return this;
        }

        public CatalogTemplateAction withPolicyProperties() {
            return this;
        }

        public CatalogTemplateAction withPolicyPropertiesAssignments() {
            return this;
        }

        public Future<Template> execute() {

            if (this.target == null) {

                String[] parts = this.artifactId.split("/");
                if (parts.length != 8) {
                    return Futures
                            .failedFuture(new Exception("Unexpected artifact id for template " + this.artifactId));
                }

                UUID resourceId = asUUID(parts[5]);
                this.catalog = ASDCCatalog.this.catalogs.get(resourceId);

                // if we find a catalog for this resource we have to figure out
                // if it contains the required target ..

                try {
                    JSONObject resource = new ResourceAction(resourceId).executeRaw().waitForResult();

                    Checker checker = new Checker();
                    TargetLocator locator = new ASDCLocator(resource.getJSONArray(ARTIFACTS),
                            ASDCCatalog.this.catalogs.get(resourceId));
                    checker.setTargetLocator(locator);

                    Target template = locator.resolve("template");
                    if (template == null) {
                        return Futures.failedFuture(new Exception("Failed to locate template in " + resource));
                    }

                    checker.check(template);

                    for (Target t : checker.targets()) {
                        if (t.getReport().hasErrors()) {
                            dumpTargets(resourceId.toString(), checker.targets());
                            return Futures.failedFuture(new Exception("Failed template validation: " + t.getReport()));
                        }
                    }

                    this.target = template;
                    this.catalog = checker.catalog();
                    ASDCCatalog.this.catalogs.put(resourceId, this.catalog);
                    // we should only be doing this if we discovered an update
                    // (by checking timestamps). Actually, we should
                    // only do the artifact fetching if we detect an update
                    ASDCCatalog.this.contexts.put(template, JXPathContext.newContext(template.getTarget()));
                } catch (Exception x) {
                    return Futures.failedFuture(x);
                }
            }

            this.doNodes().doNodeProperties().doNodePropertiesAssignments().doNodeRequirements().doNodeCapabilities()
                    .doNodeCapabilityProperties().doNodeCapabilityPropertyAssignments();

            JSONObject pack = new JSONObject((Map) ctx.getContextBean()).put(NAME, this.target.getName())
                    .put(ID, this.target.getLocation().toString())
                    .put(ITEM_ID, this.target.getLocation().toString());
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), pack.toString(2));

            return Futures.succeededFuture(proxies.build(pack, Template.class));
        }
    }

    public class CatalogTypeAction implements Catalog.TypeAction {

        private String name;
        private UUID resourceId;
        private JXPathContext ctx;

        private boolean doHierarchy = false, doRequirements = false, doCapabilities = false;

        private CatalogTypeAction(UUID theResourceId, /* Construct theConstruct, */ String theName) {
            this.resourceId = theResourceId;
            this.name = theName;
        }

        public CatalogTypeAction withHierarchy() {
            this.doHierarchy = true;
            return this;
        }

        CatalogTypeAction doHierarchy(org.onap.sdc.dcae.checker.Catalog theCatalog) {
            if (!this.doHierarchy) {
                return this;
            }

            ctx.setValue("/hierarchy",
                    stream(theCatalog.hierarchy(Construct.Node, this.name)).skip(1) // skip
                                                                                    // self
                            .map((Map.Entry type) -> new MapBuilder()
                                    .put(NAME, type.getKey()).put(ID, resourceId + "/" + type.getKey())
                                    .putOpt(DESCRIPTION, ((Map) type.getValue()).get(DESCRIPTION)).build())
                            // renderEntry((Map.Entry)type,
                            // "description").build())
                            .collect(Collectors.toList()));
            return this;
        }

        public CatalogTypeAction withRequirements() {
            this.doRequirements = true;
            return this;
        }

        CatalogTypeAction doRequirements(org.onap.sdc.dcae.checker.Catalog theCatalog) {
            if (!this.doRequirements) {
                return this;
            }

            ctx.setValue("requirements", stream(theCatalog.requirements(this.name)).map((Map.Entry req) -> {
                String capability = (String) ((Map) req.getValue()).get(CAPABILITY),
                        node = (String) ((Map) req.getValue()).get(CAPABILITY);
                return new MapBuilder().put(NAME, req.getKey()).put(ID, resourceId + "/" + req.getKey())
                        .put(OCCURRENCES, ((Map) req.getValue()).get(OCCURRENCES))
                        .put(CAPABILITY,
                                new MapBuilder().put(NAME, capability)
                                        // if the capability points to a
                                        // capability type then encode
                                        // the type reference, else it is a name
                                        // (within a node type)
                                        .put(ID,
                                                getCatalog(resourceId).hasType(Construct.Capability, capability)
                                                        ? (resourceId + "/" + capability) : capability)
                                        .build())
                        .put("node", new MapBuilder().putOpt(NAME, node).putOpt(ID, node == null ? null
                                : (resourceId + "/" + node)).buildOpt())
                        .put("relationship", ((Map) req.getValue()).get("relationship"))
                        // renderEntry((Map.Entry)requirement, "occurrences",
                        // "node", "capability", "relationship")
                        .build();
            }).collect(Collectors.toList()));

            return this;
        }

        public CatalogTypeAction withCapabilities() {
            this.doCapabilities = true;
            return this;
        }

        CatalogTypeAction doCapabilities(org.onap.sdc.dcae.checker.Catalog theCatalog) {
            if (!this.doCapabilities) {
                return this;
            }

            ctx.setValue("capabilities",
                    stream(theCatalog
                            .facets(Construct.Node, Facet.capabilities,
                                    this.name))
                                            .map((Map.Entry capability) -> new MapBuilder()
                                                    .put(NAME, capability.getKey()).put("type",
                                                            new MapBuilder()
                                                                    .put(NAME, ((Map) capability.getValue())
                                                                            .get("type"))
                                                                    .put(ID,
                                                                            resourceId + "/"
                                                                                    + ((Map) capability.getValue())
                                                                                            .get("type"))
                                                                    .build())
                                                    .put(OCCURRENCES,
                                                            ((Map) capability.getValue()).get(OCCURRENCES))
                                                    .putOpt("validSourceTypes",
                                                            ((Map) capability.getValue()).get("validSourceTypes"))
                                                    .build()
                                            // renderEntry((Map.Entry)capability,
                                            // "occurrences",
                                            // "validSourceTypes")
                                            ).collect(Collectors.toList()));
            return this;
        }

        public Future<Type> execute() {
            org.onap.sdc.dcae.checker.Catalog catalog = ASDCCatalog.this.catalogs.get(this.resourceId);
            if (catalog == null) {
                return Futures.failedFuture(new Exception("No catalog available for resource " + this.resourceId
                        + ". You might want to fetch the model first."));
            }

            if (!catalog.hasType(Construct.Node, this.name)) {
                return Futures.failedFuture(
                        new Exception("No " + this.name + " type in catalog for resource " + this.resourceId));
            }

            this.ctx = JXPathContext
                    .newContext(new MapBuilder().put(NAME, this.name).put(ID, this.resourceId + "/" + this.name)
                            .put(ITEM_ID, this.resourceId + "/" + this.name).build());

            this.doHierarchy(catalog).doRequirements(catalog).doCapabilities(catalog);

            JSONObject pack = new JSONObject((Map) this.ctx.getContextBean());
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), pack.toString(2));

            return Futures.succeededFuture(proxies.build((Map) ctx.getContextBean(), Type.class));
        }
    }

    public interface Resource extends Catalog.Item<Resource> {

        @Override
        @Proxy.DataMap(map = "uuid")
        public String id();

        public UUID uuid();

        public UUID invariantUUID();

        public String category();

        public String subCategory();

        public String lastUpdaterFullName();

        public String version();

        @Proxy.DataMap(proxy = true, elementType = Artifact.class)
        public Artifacts artifacts();

    }

    public static class Resources extends Elements<Resource> {
    }

    public interface Artifact extends Catalog.Element<Artifact> {

        @Proxy.DataMap(map = ARTIFACT_NAME)
        String name();

        @Proxy.DataMap(map = "artifactType")
        String type();

        @Proxy.DataMap(map = "artifactDescription")
        String description();

        @Proxy.DataMap(map = "artifactUUID")
        UUID uuid();

        @Proxy.DataMap(map = "artifactVersion")
        int version();

    }

    public static class Artifacts extends Elements<Artifact> {
    }

    public class ASDCLocator implements TargetLocator {

        private JSONArray artifacts;
        private org.onap.sdc.dcae.checker.Catalog catalog;

        private ASDCLocator(JSONArray theArtifacts, org.onap.sdc.dcae.checker.Catalog theCatalog) {
            this.artifacts = theArtifacts;
            this.catalog = theCatalog;
        }

        public boolean addSearchPath(URI theURI) {
            return false;
        }

        public boolean addSearchPath(String thePath) {
            return false;
        }

        public Iterable<URI> searchPaths() {
            return Collections.emptySet();
        }

        public Target resolve(String theName) {
            JSONObject targetArtifact = null;

            for (int i = 0; i < this.artifacts.length(); i++) {
                JSONObject artifact = this.artifacts.getJSONObject(i);
                String artifactName = artifact.getString(ARTIFACT_NAME);
                if (StringUtils.containsIgnoreCase(artifactName, theName)) {
                    targetArtifact = artifact;
                }
            }

            if (targetArtifact == null) {
                return null;
            }

            ASDCTarget target;
            if (this.catalog != null) {
                // this is the caching!!
                target = (ASDCTarget) this.catalog.getTarget(ASDCCatalog.this.getArtifactURI(targetArtifact));
                if (target != null && target.getVersion().equals(ASDCCatalog.this.getArtifactVersion(targetArtifact))) {
                    return target;
                }
            }

            return new ASDCTarget(targetArtifact);
        }
    }

    public class ASDCTarget extends Target {

        private String content;
        private JSONObject artifact;

        private ASDCTarget(JSONObject theArtifact) {
            super(ASDCCatalog.this.getArtifactName(theArtifact), ASDCCatalog.this.getArtifactURI(theArtifact));
            this.artifact = theArtifact;
        }

        // here is a chance for caching within the catalog! Do not go fetch the
        // artifact if it has not been changed since the
        // last fetch.

        @Override
        public Reader open() throws IOException {
            if (this.content == null) {
                try {
                    this.content = ASDCCatalog.this.asdc
                            .fetch(ASDCCatalog.this.getArtifactURL(this.artifact), String.class).waitForResult();
                } catch (Exception x) {
                    throw new IOException("Failed to load " + ASDCCatalog.this.getArtifactURL(this.artifact), x);
                }
            }

            // should return immediately a reader blocked until content
            // available .. hard to handle errors
            return new StringReader(this.content);
        }

        public String getVersion() {
            return ASDCCatalog.this.getArtifactVersion(this.artifact);
        }

    }

    public static void main(String[] theArgs) throws Exception {

        ASDCCatalog catalog = new ASDCCatalog(new URI(theArgs[0]));

        Folder f = catalog.folder(theArgs[1]).withItems().withItemModels().execute().waitForResult();

        debugLogger.log(LogLevel.DEBUG, ASDCCatalog.class.getName(), "folder: {}", f.data());

        Resources items = f.elements(ITEMS, Resources.class);
        if (items != null) {
            for (Resource item : items) {
                executeItemsNodePropertiesAssignments(catalog, item);
            }
        }
    }

    private static void executeItemsNodePropertiesAssignments(ASDCCatalog catalog, Resource item) throws Exception {
        debugLogger.log(LogLevel.DEBUG, ASDCCatalog.class.getName(), "\titem: {} : {}",item.name(), item.data());
        Templates templates = item.elements(MODELS, Templates.class);
        if (templates != null) {
            for (Template t : templates) {
                Template ft = catalog.template(t.id()).withNodes().withNodeProperties()
                        .withNodePropertiesAssignments().execute().waitForResult();

                debugLogger.log(LogLevel.DEBUG, ASDCCatalog.class.getName(), "template data: {}", ft.data());
            }
        }
    }

}