summaryrefslogtreecommitdiffstats
path: root/actn-interface-tools/yang-utils/src/main/java/org/onap/integration/actninterfacetools/yangutils/ResourceIdUtil.java
blob: f70ee51c2c6f6b9c31876b997351ee92894fdfe2 (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
/*
 *   ============LICENSE_START=======================================================
 *   Actn Interface Tools
 *   ================================================================================
 *   Copyright (C) 2023 Huawei Canada Limited.
 *   ================================================================================
 *   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.integration.actninterfacetools.yangutils;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.onosproject.yang.compiler.datamodel.YangSchemaNode;
import org.onosproject.yang.model.KeyLeaf;
import org.onosproject.yang.model.LeafListKey;
import org.onosproject.yang.model.ListKey;
import org.onosproject.yang.model.NodeKey;
import org.onosproject.yang.model.ResourceData;
import org.onosproject.yang.model.ResourceId;
import org.onosproject.yang.runtime.AnnotatedNodeInfo;
import org.onosproject.yang.runtime.Annotation;
import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
import org.onosproject.yang.runtime.DefaultAnnotation;
import org.onosproject.yang.runtime.DefaultRuntimeContext;
import org.onosproject.yang.runtime.DefaultYangSerializerContext;
import org.onosproject.yang.runtime.RuntimeContext;
import org.onosproject.yang.runtime.SerializerHelper;
import org.onosproject.yang.runtime.YangSerializerContext;
import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry;
import org.onosproject.yang.serializers.utils.SerializerUtilException;
import org.slf4j.Logger;

import javax.ws.rs.core.UriBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * The class services as a placeholder for utilities related to
 * generic ResourceId operations.
 */
public final class ResourceIdUtil {
    public static final String ROOT_NAME = "/";
    public static final String ROOT_NS = null;
    public static final ResourceId ROOT_ID = ResourceId.builder().addBranchPointSchema(ROOT_NAME, ROOT_NS).build();

    private static final Logger log = getLogger(ResourceIdUtil.class);
    private static final String JSON_FORMAT = "JSON";
    private static final Splitter SLASH_SPLITTER = Splitter.on('/');
    private static final Splitter COMMA_SPLITTER = Splitter.on(',');
    private static final String QUOTES = "\"";
    private static final String ROOT_ELEMENT_START = "<root ";
    private static final String ROOT_ELEMENT_END = "</root>";
    private static final String URI_ENCODING_CHAR_SET = "ISO-8859-1";
    private static final String UTF8_ENCODING = "utf-8";
    private static final String ERROR_LIST_MSG = "List/Leaf-list node should be " +
            "in format \"nodeName=key\"or \"nodeName=instance-value\"";
    private static final String EQUAL = "=";
    private static final String COMMA = ",";
    private static final String COLON = ":";
    private static final String SLASH = "/";
    private static final String URI_ENCODED_SLASH = "%2F";
    private static final String URI_ENCODED_COLON = "%3A";


    // no instantiation
    private ResourceIdUtil() {
    }

    /**
     * Converts XML atrtibutes into annotated node info.
     *
     * @param element XML element
     * @param id      resource id of an element
     * @return annotated node info
     */
    public static AnnotatedNodeInfo convertXmlAttributesToAnnotations(Element element,
                                                                      ResourceId id) {
        Iterator iter = element.attributeIterator();
        if (!iter.hasNext()) {
            // element does not have any attributes
            return null;
        }
        AnnotatedNodeInfo.Builder builder = DefaultAnnotatedNodeInfo.builder();
        builder = builder.resourceId(id);
        while (iter.hasNext()) {
            Attribute attr = (Attribute) iter.next();
            DefaultAnnotation annotation = new DefaultAnnotation(
                    attr.getQualifiedName(), attr.getValue());
            builder = builder.addAnnotation(annotation);
        }
        return builder.build();
    }


    /**
     * Appends the XML data with root element.
     *
     * @param inputStream        XML data
     * @param protocolAnnotation list of annoations for root element
     * @return XML with root element
     * @throws DocumentException if root element cannot be created
     * @throws IOException       if input data cannot be read
     */
    public static String addRootElementWithAnnotation(InputStream inputStream,
                                                      List<Annotation>
                                                              protocolAnnotation)
            throws DocumentException, IOException {
        BufferedReader br;
        StringBuilder sb = new StringBuilder();
        String xmlData;
        // Parse composite stream resourceData
        br = new BufferedReader(new InputStreamReader(inputStream));
        while ((xmlData = br.readLine()) != null) {
            sb.append(xmlData);
        }

        StringBuilder rootElement = new StringBuilder(ROOT_ELEMENT_START);
        if (protocolAnnotation != null) {
            for (Annotation annotation : protocolAnnotation) {
                rootElement.append(annotation.name()).append(EQUAL)
                        .append(QUOTES).append(annotation.value()).append(QUOTES);
            }
        }
        rootElement.append(">").append(sb.toString()).append(ROOT_ELEMENT_END);
        return rootElement.toString();
    }

    public static ResourceId convertUriToRid(URI uri) {
        if (uri == null) {
            return null;
        }

        return convertUriStrToRid(getRawUriPath(uri));
    }

    public static ResourceId convertUriStrToRid(String uriStr) {
        if (uriStr == null || uriStr.isEmpty()) {
            return null;
        }

        ResourceData resourceData = CodecConverter.objectNodeToResourceData(uriStr, null);
        return resourceData.resourceId();
    }

    /**
     * Converts a URI string to resource identifier.
     *
     * @param uriString given URI
     * @param context   YANG schema context information
     * @return resource ID
     */
    @Deprecated
    public static ResourceId.Builder convertUriStrToRid(String uriString,
                                                        YangSerializerContext context) {
        if (uriString == null || uriString.isEmpty()) {
            return null;
        }
        List<String> paths = Arrays.asList(uriString.split(SLASH));

        if (!paths.isEmpty()) {
            ResourceId.Builder ridBuilder =
                    SerializerHelper.initializeResourceId(context);
            processPathSegments(paths, ridBuilder);
            return ridBuilder;
        }

        return null;
    }

    /**
     * Converts a list of path from the original format to ISO-8859-1 code.
     *
     * @param paths the original paths
     * @return list of decoded paths
     */
    @Deprecated
    public static List<String> urlPathArgsDecode(Iterable<String> paths) {
        try {
            List<String> decodedPathArgs = new ArrayList<>();
            for (String pathArg : paths) {
                String decode = URLDecoder.decode(pathArg,
                                                  URI_ENCODING_CHAR_SET);
                decodedPathArgs.add(decode);
            }
            return decodedPathArgs;
        } catch (UnsupportedEncodingException e) {
            throw new SerializerUtilException("Invalid URL path arg '" +
                                                      paths + "': ", e);
        }
    }

    private static ResourceId.Builder processPathSegments(List<String> paths,
                                                          ResourceId.Builder builder) {
        if (paths.isEmpty()) {
            return builder;
        }

        boolean isLastSegment = paths.size() == 1;

        String segment = paths.iterator().next();
        processSinglePathSegment(segment, builder);

        if (isLastSegment) {
            // We have hit the base case of recursion.
            return builder;
        }

        /*
         * Chop off the first segment, and recursively process the rest
         * of the path segments.
         */
        List<String> remainPaths = paths.subList(1, paths.size());
        processPathSegments(remainPaths, builder);

        return builder;
    }

    private static void processSinglePathSegment(String pathSegment,
                                                 ResourceId.Builder builder) {
        if (pathSegment.contains(COLON)) {
            processPathSegmentWithNamespace(pathSegment, builder);
        } else {
            processPathSegmentWithoutNamespace(pathSegment, builder);
        }
    }

    private static void processPathSegmentWithNamespace(String pathSegment,
                                                        ResourceId.Builder builder) {

        String nodeName = getLatterSegment(pathSegment, COLON);
        String namespace = getPreSegment(pathSegment, COLON);
        addNodeNameToRid(nodeName, namespace, builder);
    }

    private static void processPathSegmentWithoutNamespace(String nodeName,
                                                           ResourceId.Builder builder) {
        addNodeNameToRid(nodeName, null, builder);
    }

    private static void addNodeNameToRid(String nodeName,
                                         String namespace,
                                         ResourceId.Builder builder) {
        if (nodeName.contains(EQUAL)) {
            addListOrLeafList(nodeName, namespace, builder);
        } else {
            addLeaf(nodeName, namespace, builder);
        }
    }

    private static void addListOrLeafList(String path,
                                          String namespace,
                                          ResourceId.Builder builder) {
        String nodeName = getPreSegment(path, EQUAL);
        String keyStr = getLatterSegment(path, EQUAL);
        if (keyStr == null) {
            throw new SerializerUtilException(ERROR_LIST_MSG);
        }

        List<String> keys = uriDecodedKeys(keyStr);
        SerializerHelper.addToResourceId(builder, nodeName, namespace, keys);
    }

    private static List<String> uriDecodedKeys(String keyStr) {
        List<String> decodedKeys = Lists.newArrayList();

        if (keyStr.contains(COMMA)) {
            List<String> encodedKeys = Lists.newArrayList(COMMA_SPLITTER.split(keyStr));
            for (String encodedKey : encodedKeys) {
                decodedKeys.add(uriDecodedString(encodedKey));
            }
        } else {
            decodedKeys.add(uriDecodedString(keyStr));
        }

        return decodedKeys;
    }


    private static String uriDecodedString(String keyStr) {
        try {
            keyStr = URLDecoder.decode(keyStr, UTF8_ENCODING);
        } catch (UnsupportedEncodingException ex) {
            throw new SerializerUtilException("UnsupportedEncodingException: " + ex.getMessage());
        }

        return keyStr;
    }

    private static void addLeaf(String nodeName,
                                String namespace,
                                ResourceId.Builder builder) {
        checkNotNull(nodeName);
        String value = null;
        SerializerHelper.addToResourceId(builder, nodeName, namespace, value);
    }

    /**
     * Returns the previous segment of a path which is separated by a split char.
     * For example:
     * <pre>
     * "foo:bar", ":" to "foo"
     * </pre>
     *
     * @param path      the original path string
     * @param splitChar char used to split the path
     * @return the previous segment of the path
     */
    public static String getPreSegment(String path, String splitChar) {
        int idx = path.lastIndexOf(splitChar);
        if (idx == -1) {
            return null;
        }
        return path.substring(0, idx);
    }

    /**
     * Returns the latter segment of a path which is separated by a split char.
     * For example:
     * <pre>
     * "foo:bar", ":" to "bar"
     * </pre>
     *
     * @param path      the original path string
     * @param splitChar char used to split the path
     * @return the latter segment of the path
     */
    public static String getLatterSegment(String path, String splitChar) {
        int idx = path.lastIndexOf(splitChar);
        if (idx == -1) {
            return path;
        }

        return path.substring(idx + 1);
    }

    /**
     * Converts a resource identifier to URI string.
     *
     * @param rid resource identifier
     * @return URI
     */
    public static String convertRidToUriStr(ResourceId rid) {
        if (rid == null) {
            return null;
        }

        RuntimeContext.Builder runtimeContextBuilder = new DefaultRuntimeContext.Builder();
        runtimeContextBuilder.setDataFormat(JSON_FORMAT);
        RuntimeContext rc = runtimeContextBuilder.build();
        YangSerializerContext context = new DefaultYangSerializerContext(null,
                                                                         rc.getProtocolAnnotations());

        StringBuilder uriBuilder = new StringBuilder();
        List<NodeKey> nodeKeyList = rid.nodeKeys();
        String curNameSpace = null;
        for (NodeKey key : nodeKeyList) {
            curNameSpace = addNodeKeyToUri(key, curNameSpace, uriBuilder, context);
        }
        return trimAtLast(uriBuilder.toString(), SLASH);
    }

    private static String addNodeKeyToUri(NodeKey key,
                                          String curNameSpace,
                                          StringBuilder uriBuilder,
                                          YangSerializerContext context) {
        String newNameSpace = null;
        if (key instanceof LeafListKey) {
            newNameSpace = addLeafListNodeToUri((LeafListKey) key,
                                                curNameSpace, uriBuilder, context);
        } else if (key instanceof ListKey) {
            newNameSpace = addListNodeToUri((ListKey) key, curNameSpace,
                                            uriBuilder, context);
        } else {
            String name = key.schemaId().name();
            if (!name.equals(SLASH)) {
                newNameSpace = addNodeNameToUri(key, curNameSpace,
                                                uriBuilder, context);
            }
        }
        return newNameSpace;
    }

    private static String addLeafListNodeToUri(LeafListKey key,
                                               String curNameSpace,
                                               StringBuilder uriBuilder,
                                               YangSerializerContext context) {

        String newNameSpace = addNodeNameToUri(key, curNameSpace, uriBuilder,
                                               context);
        uriBuilder.append(EQUAL);
        uriBuilder.append(key.asString());
        return newNameSpace;
    }

    private static String addListNodeToUri(ListKey key,
                                           String curNameSpace,
                                           StringBuilder uriBuilder,
                                           YangSerializerContext context) {
        String newNameSpace = addNodeNameToUri(key, curNameSpace, uriBuilder,
                                               context);
        uriBuilder.append(EQUAL);
        String prefix = "";
        for (KeyLeaf keyLeaf : key.keyLeafs()) {
            uriBuilder.append(prefix);
            prefix = COMMA;
            uriBuilder.append(keyLeaf.leafValue().toString());
        }

        return newNameSpace;
    }

    private static String addNodeNameToUri(NodeKey key,
                                           String curNameSpace,
                                           StringBuilder uriBuilder,
                                           YangSerializerContext context) {
        String newNameSpace = key.schemaId().namespace();
        if (newNameSpace == null) {
            return curNameSpace;
        }

        if (!newNameSpace.equals(curNameSpace)) {
            uriBuilder.append(getModuleNameFromNameSpace(context, newNameSpace));
            uriBuilder.append(COLON);
        }
        uriBuilder.append(key.schemaId().name());
        uriBuilder.append(SLASH);

        return newNameSpace;
    }

    public static String trimAtLast(String valueString, String...
            removalString) {
        StringBuilder stringBuilder = new StringBuilder(valueString);
        String midString;
        int index;
        for (String remove : removalString) {
            midString = stringBuilder.toString();
            index = midString.lastIndexOf(remove);
            if (index != -1) {
                stringBuilder.deleteCharAt(index);
            }
        }
        return stringBuilder.toString();
    }

    public static String getModuleNameFromNameSpace(YangSerializerContext c,
                                                    String ns) {

        YangSchemaNode schemaNode = ((DefaultYangModelRegistry) c.getContext())
                .getForNameSpace(ns, false);
        if (schemaNode != null) {
            return schemaNode.getName();
        }
        return null;
    }

    public static URI rmLastPathSegment(URI uri) {
        if (uri == null) {
            return null;
        }

        UriBuilder builder = UriBuilder.fromUri(uri);
        String newPath = rmLastPathSegmentStr(uri.getRawPath());
        builder.replacePath(newPath);

        return builder.build();
    }

    public static String rmLastPathSegmentStr(String rawPath) {
        if (rawPath == null) {
            return null;
        }
        int pos = rawPath.lastIndexOf(SLASH);
        if (pos <= 0) {
            return null;
        }

        return rawPath.substring(0, pos);
    }

    public static ResourceId parentOf(ResourceId path) {
        try {
            return path.copyBuilder().removeLastKey().build();
        } catch (CloneNotSupportedException e) {
            log.error("ERROR: parentOf: Could not copy {}", path, e);
            throw new IllegalArgumentException("Could not copy " + path, e);
        }
    }

    public static boolean isRootRid(ResourceId path) {
        if (path == null) {
            // null is a valid presentation of root resource ID
            return true;
        }

        if ((path.nodeKeys().size() == 1) && (path.nodeKeys().get(0).schemaId().name().equals("/"))) {
            return true;
        }

        return false;
    }

    public static boolean isRidLeafListEntry(ResourceId rId) {
        if (rId == null) {
            return false;
        }

        List<NodeKey> nodeKeyList = rId.nodeKeys();
        if (CollectionUtils.isEmpty(nodeKeyList)) {
            return false;
        }

        NodeKey lastNodeKey = nodeKeyList.get(nodeKeyList.size() - 1);
        if (lastNodeKey instanceof LeafListKey) {
            return true;
        }

        return false;
    }

    public static String getKeyStrFromLeafListKey(LeafListKey key) {
        return key.asString();
    }

    public static String getNodeNameFromNodeKey(NodeKey key) {
        return key.schemaId().name();
    }

    public static String getNameSpaceFromNodeKey(NodeKey key) {
        return key.schemaId().namespace();
    }

    public static String getRawUriPath(URI uri) {
        if (uri == null) {
            return null;
        }

        String path = uri.getRawPath();
        if (path.equals("/restconf/data")) {
            return null;
        }

        return path.replaceAll("^/restconf/data/", "")
                .replaceAll("^/restconf/configdata/", "")
                .replaceAll("^/restconf/operations/", "");
    }

    public static ResourceId getResourceIdInRange(ResourceId rsId, int fromIdx, int toIdx) {
        checkArgument(rsId.nodeKeys().size() >= toIdx,
                      "%s path must be deeper than base prefix %d", rsId, toIdx);

        // FIXME waiting for Yang tools 2.2.0-b4 or later
        // return ResourceId.builder().append(child.nodeKeys().subList(fromIdx, toIdx+1).build();

        ResourceId.Builder builder = ResourceId.builder();
        for (NodeKey nodeKey : rsId.nodeKeys().subList(fromIdx, toIdx + 1)) {
            if (nodeKey instanceof ListKey) {
                ListKey listKey = (ListKey) nodeKey;
                builder.addBranchPointSchema(nodeKey.schemaId().name(),
                                             nodeKey.schemaId().namespace());
                for (KeyLeaf keyLeaf : listKey.keyLeafs()) {
                    builder.addKeyLeaf(keyLeaf.leafSchema().name(),
                                       keyLeaf.leafSchema().namespace(),
                                       keyLeaf.leafValAsString());
                }
            } else if (nodeKey instanceof LeafListKey) {
                LeafListKey llKey = (LeafListKey) nodeKey;
                builder.addLeafListBranchPoint(llKey.schemaId().name(),
                                               llKey.schemaId().namespace(),
                                               llKey.value());

            } else {
                builder.addBranchPointSchema(nodeKey.schemaId().name(),
                                             nodeKey.schemaId().namespace());
            }
        }
        return builder.build();
    }

    public static List<String> getResourceIdTokens(ResourceId rId) {
        if (rId == null) {
            return null;
        }
        List<NodeKey> nodeKeys = rId.nodeKeys();
        if (CollectionUtils.isEmpty(nodeKeys)) {
            return null;
        }
        List<String> res = Lists.newArrayList();
        for (NodeKey nodeKey : nodeKeys) {
            res.add(nodeKey.schemaId().name());
        }

        return res;
    }

    public static boolean nodeKeysMatchPrefix(ResourceId rid, List<String> nodeKeyPrefix) {
        if (rid == null || CollectionUtils.isEmpty(rid.nodeKeys())) {
            return false;
        }
        return nodeKeysMatchPrefix(rid.nodeKeys(), nodeKeyPrefix);
    }

    public static boolean nodeKeysMatchPrefix(List<NodeKey> nodeKeys, List<String> nodeKeyPrefix) {
        if (nodeKeyPrefix == null || nodeKeyPrefix.isEmpty()) {
            return true;   // no prefix, so match is true
        }
        if (nodeKeys == null || nodeKeys.isEmpty() || nodeKeys.size() < nodeKeyPrefix.size()) {
            return false;
        }

        for (int i = 0; i < nodeKeyPrefix.size(); i++) {
            if (!nodeKeys.get(i).schemaId().name().equals(nodeKeyPrefix.get(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean nodeKeysMatch(List<NodeKey> nodeKeys, List<String> nodeKeyNames) {
        int size = nodeKeyNames.size();
        if (nodeKeys == null || nodeKeys.isEmpty() || nodeKeys.size() != size) {
            return false;
        }

        for (int i = 0; i < size; i++) {
            if (!nodeKeys.get(i).schemaId().name().equals(nodeKeyNames.get(i))) {
                return false;
            }
        }
        return true;
    }

    private static boolean listKeySchemaMatch(ListKey listKeyA, ListKey listKeyB) {
        List<KeyLeaf> keyLeavesA = listKeyA.keyLeafs();
        List<KeyLeaf> keyLeavesB = listKeyB.keyLeafs();
        if (keyLeavesA.size() != keyLeavesB.size()) {
            return false;
        }

        for (int i = 0; i < keyLeavesA.size(); i++) {
            KeyLeaf keyLeafA = keyLeavesA.get(i);
            KeyLeaf keyLeafB = keyLeavesB.get(i);
            if (!keyLeafA.leafSchema().name().equals(keyLeafB.leafSchema().name())) {
                //should we compare keyLeaf.leafSchema().namespace()?
                return false;
            }
        }
        return true;
    }

    public static String getPrintableForm(ResourceId rid) {
        if (rid == null || CollectionUtils.isEmpty(rid.nodeKeys())) {
            return "null";
        }

        StringBuilder builder = new StringBuilder();
        List<NodeKey> nodeKeys = rid.nodeKeys();
        for (NodeKey nodeKey : nodeKeys) {
            if (nodeKey instanceof ListKey) {
                builder.append("/").append(nodeKey.schemaId().name());
                builder.append("=");
                List<KeyLeaf> keyLeafList = ((ListKey) nodeKey).keyLeafs();
                int i = 0;
                for (KeyLeaf keyLeaf : keyLeafList) {
                    if (i > 0) {
                        builder.append(",");
                    }
                    builder.append(keyLeaf.leafValue());
                    builder.append("(").append(keyLeaf.leafSchema().name()).append(")");
                    i++;
                }
            } else if (nodeKey instanceof LeafListKey) {
                builder.append("/")
                        .append(((LeafListKey) nodeKey).value())
                        .append("(").append(nodeKey.schemaId().name()).append(")");

            } else if (nodeKey instanceof NodeKey) {
                if (!nodeKey.schemaId().name().equals("/")) {
                    builder.append("/").append(nodeKey.schemaId().name());
                }
            } else {
                builder.append("ERROR: ").append(nodeKey.toString());
            }
        }
        return builder.toString();
    }

    public static boolean ridSchemaMatchPrefix(ResourceId ridA, ResourceId prefixRid) {
        if (ridA == null || prefixRid == null) {
            return false;
        }

        List<NodeKey> nodeKeysA = ridA.nodeKeys();
        List<NodeKey> nodeKeysB = prefixRid.nodeKeys();

        if (nodeKeysA.size() < nodeKeysB.size()) {
            return false;
        }

        for (int i = 0; i < nodeKeysB.size(); i++) {
            NodeKey nodeKeyA = nodeKeysA.get(i);
            NodeKey nodeKeyB = nodeKeysB.get(i);

            if (nodeKeyA instanceof ListKey) {
                if (!(nodeKeyB instanceof ListKey)) {
                    return false;
                }
                if (!listKeySchemaMatch((ListKey) nodeKeyA, (ListKey) nodeKeyB)) {
                    return false;
                }

            } else if (nodeKeyA instanceof LeafListKey) {
                if (!(nodeKeyB instanceof LeafListKey)) {
                    return false;
                }
                LeafListKey llKeyA = (LeafListKey) nodeKeyA;
                LeafListKey llKeyB = (LeafListKey) nodeKeyB;

                if (!llKeyA.schemaId().name().equals(llKeyB.schemaId().name())) {
                    //should we also compare nodeKey.schemaId().namespace()?
                    return false;
                }
            } else {
                if (nodeKeyB instanceof ListKey || nodeKeyB instanceof LeafListKey) {
                    return false;
                }
                if (!nodeKeyA.schemaId().name().equals(nodeKeyB.schemaId().name())) {
                    //should we also compare nodeKey.schemaId().namespace()?
                    return false;
                }
            }
        }
        return true;
    }

    public static String convertRidToString(ResourceId rid) {
        return getPrintableForm(rid);
    }

    public static boolean ridSchemaMatch(ResourceId ridA, ResourceId ridB) {
        if (ridA == null || ridB == null) {
            return false;
        }

        List<NodeKey> nodeKeysA = ridA.nodeKeys();
        List<NodeKey> nodeKeysB = ridB.nodeKeys();

        if (nodeKeysA.size() != nodeKeysB.size()) {
            return false;
        }

        for (int i = 0; i < nodeKeysA.size(); i++) {
            NodeKey nodeKeyA = nodeKeysA.get(i);
            NodeKey nodeKeyB = nodeKeysB.get(i);

            if (nodeKeyA instanceof ListKey) {
                if (!(nodeKeyB instanceof ListKey)) {
                    return false;
                }
                if (!listKeySchemaMatch((ListKey) nodeKeyA, (ListKey) nodeKeyB)) {
                    return false;
                }

            } else if (nodeKeyA instanceof LeafListKey) {
                if (!(nodeKeyB instanceof LeafListKey)) {
                    return false;
                }
                LeafListKey llKeyA = (LeafListKey) nodeKeyA;
                LeafListKey llKeyB = (LeafListKey) nodeKeyB;

                if (!llKeyA.schemaId().name().equals(llKeyB.schemaId().name())) {
                    //should we also compare nodeKey.schemaId().namespace()?
                    return false;
                }
            } else {
                if (nodeKeyB instanceof ListKey || nodeKeyB instanceof LeafListKey) {
                    return false;
                }
                if (!nodeKeyA.schemaId().name().equals(nodeKeyB.schemaId().name())) {
                    //should we also compare nodeKey.schemaId().namespace()?
                    return false;
                }
            }
        }
        return true;
    }

    public static ResourceId getGenericRidForNetworkLink() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("networks", "generic-ns")
                .addBranchPointSchema("network", "generic-ns")
                .addKeyLeaf("network-id", "generic-ns", "generic-network-id")
                .addBranchPointSchema("link", "generic-ns")
                .addKeyLeaf("link-id", "generic-ns", "generic-link-id")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForNetworkNode() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("networks", "generic-ns")
                .addBranchPointSchema("network", "generic-ns")
                .addKeyLeaf("network-id", "generic-ns", "generic-network-id")
                .addBranchPointSchema("node", "generic-ns")
                .addKeyLeaf("node-id", "generic-ns", "generic-node-id")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForNetworkNodeTp() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("networks", "generic-ns")
                .addBranchPointSchema("network", "generic-ns")
                .addKeyLeaf("network-id", "generic-ns", "generic-network-id")
                .addBranchPointSchema("node", "generic-ns")
                .addKeyLeaf("node-id", "generic-ns", "generic-node-id")
                .addBranchPointSchema("termination-point", "generic-ns")
                .addKeyLeaf("tp-id", "generic-ns", "generic-tp-id")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForNetworkNodeTtp() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("networks", "generic-ns")
                .addBranchPointSchema("network", "generic-ns")
                .addKeyLeaf("network-id", "generic-ns", "generic-network-id")
                .addBranchPointSchema("node", "generic-ns")
                .addKeyLeaf("node-id", "generic-ns", "generic-node-id")
                .addBranchPointSchema("te", "generic-ns")
                .addBranchPointSchema("tunnel-termination-point", "generic-ns")
                .addKeyLeaf("tunnel-tp-id", "generic-ns", "generic-ttp-id")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForJuniperConfigGroups() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("configuration", "generic-ns")
                .addBranchPointSchema("groups", "generic-ns")
                .addKeyLeaf("name", "generic-ns", "generic-name")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForEthPmStateParam() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("performance-monitoring", "generic-ns")
                .addBranchPointSchema("service-pm", "generic-ns")
                .addKeyLeaf("service-name", "generic-ns", "generic-list-key")
                .addBranchPointSchema("service-pm-state", "generic-ns")
                .addBranchPointSchema("performance-data", "generic-ns")
                .addKeyLeaf("parameter-name", "generic-ns", "generic-list-key")
                .addBranchPointSchema("parameter-value", "generic-ns")
                .addKeyLeaf("index", "generic-ns", "generic-list-key")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForTeTunnel() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("te", "generic-ns")
                .addBranchPointSchema("tunnels", "generic-ns")
                .addBranchPointSchema("tunnel", "generic-ns")
                .addKeyLeaf("name", "generic-ns", "generic-tunnel-name")
                .build();
        return rid;
    }

    public static ResourceId getGenericRidForEthtSvcInstances() {
        ResourceId rid = ResourceId.builder()
                .addBranchPointSchema("/", null)
                .addBranchPointSchema("etht-svc", "generic-ns")
                .addBranchPointSchema("etht-svc-instances", "generic-ns")
                .addKeyLeaf("etht-svc-name", "generic-ns", "generic-key-id")
                .build();
        return rid;
    }

    public static ResourceId convertToGenericRid(ResourceId rid) {
        if (rid == null) {
            return null;
        }

        ResourceId genericRid = null;

        genericRid = getGenericRidForEthPmStateParam();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForNetworkLink();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForTeTunnel();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForNetworkNode();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForEthtSvcInstances();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForNetworkNodeTp();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForNetworkNodeTtp();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }

        genericRid = getGenericRidForJuniperConfigGroups();
        if (ridSchemaMatch(rid, genericRid)) {
            return genericRid;
        }
        //TODO: HENRY: add other generic RIDs.

        return null;
    }

    public static String convertRid2Uri(ResourceId rid) {
        if (rid == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        List<NodeKey> nodeKeys = rid.nodeKeys();

        for (int i = 0; i < nodeKeys.size(); i++) {
            NodeKey nodeKey = nodeKeys.get(i);
            String nodeName = nodeKey.schemaId().name();

            if (nodeKey instanceof ListKey) {
                sb.append("/");
                sb.append(nodeName);
                sb.append("=");
                sb.append(listKeyValues2UriString((ListKey) nodeKey));
            } else if (nodeKey instanceof LeafListKey) {
                sb.append("/");
                sb.append(nodeName);
                sb.append("=");
                sb.append(leafListKey2UriString((LeafListKey) nodeKey));
            } else {
                if (!nodeName.equals("/")) {
                    sb.append("/");
                    sb.append(nodeName);
                    //should we also compare nodeKey.schemaId().namespace()?
                }
            }
        }

        return sb.toString();
    }

    private static String leafListKey2UriString(LeafListKey leafListKey) {
        String decodedValue = leafListKey.asString();
        try {
            return URLEncoder.encode(decodedValue, "utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error("ERROR: leafListKey2UriString: {}", e.getMessage());
        }

        return "";
    }

    private static String listKeyValues2UriString(ListKey listKey) {
        if (listKey == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        List<KeyLeaf> keyLeaves = listKey.keyLeafs();

        for (int i = 0; i < keyLeaves.size(); i++) {
            sb.append(",");
            KeyLeaf keyLeaf = keyLeaves.get(i);
            String rawValue = keyLeaf.leafValue().toString();
            try {
                sb.append(URLEncoder.encode(rawValue, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                log.error("ERROR: listKey2UriString: {}", e.getMessage());
            }
        }

        sb.deleteCharAt(sb.lastIndexOf(","));
        return sb.toString();
    }
}