BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-protocolbuffer-encoding / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / NodeIdentifierFactory.java
1 package org.opendaylight.controller.cluster.datastore.node.utils;
2
3 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
4
5 import java.util.HashMap;
6 import java.util.Map;
7
8 public class NodeIdentifierFactory {
9     private static final Map<String, YangInstanceIdentifier.PathArgument> cache = new HashMap<>();
10     public static YangInstanceIdentifier.PathArgument getArgument(String id){
11         YangInstanceIdentifier.PathArgument value = cache.get(id);
12         if(value == null){
13             synchronized (cache){
14                 value = cache.get(id);
15                 if(value == null) {
16                     value = createPathArgument(id);
17                     cache.put(id, value);
18                 }
19             }
20         }
21         return value;
22     }
23
24     private static YangInstanceIdentifier.PathArgument createPathArgument(String id){
25         final NodeIdentifierWithPredicatesGenerator
26             nodeIdentifierWithPredicatesGenerator = new NodeIdentifierWithPredicatesGenerator(id);
27         if(nodeIdentifierWithPredicatesGenerator.matches()){
28             return nodeIdentifierWithPredicatesGenerator.getPathArgument();
29         }
30
31         final NodeIdentifierWithValueGenerator
32             nodeWithValueGenerator = new NodeIdentifierWithValueGenerator(id);
33         if(nodeWithValueGenerator.matches()){
34             return nodeWithValueGenerator.getPathArgument();
35         }
36
37         final AugmentationIdentifierGenerator augmentationIdentifierGenerator = new AugmentationIdentifierGenerator(id);
38         if(augmentationIdentifierGenerator.matches()){
39             return augmentationIdentifierGenerator.getPathArgument();
40         }
41
42         return new NodeIdentifierGenerator(id).getArgument();
43     }
44 }