Merge "BUG-190 Simplify reconnect logic in protocol-framework."
[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 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
5
6 import java.util.HashMap;
7 import java.util.Map;
8
9 public class NodeIdentifierFactory {
10     private static final Map<String, YangInstanceIdentifier.PathArgument> cache = new HashMap<>();
11     public static YangInstanceIdentifier.PathArgument getArgument(String id){
12         YangInstanceIdentifier.PathArgument value = cache.get(id);
13         if(value == null){
14             synchronized (cache){
15                 value = cache.get(id);
16                 if(value == null) {
17                     value = createPathArgument(id, null);
18                     cache.put(id, value);
19                 }
20             }
21         }
22         return value;
23     }
24
25     public static YangInstanceIdentifier.PathArgument getArgument(String id, DataSchemaNode schemaNode){
26         YangInstanceIdentifier.PathArgument value = cache.get(id);
27         if(value == null){
28             synchronized (cache){
29                 value = cache.get(id);
30                 if(value == null) {
31                     value = createPathArgument(id, schemaNode);
32                     cache.put(id, value);
33                 }
34             }
35         }
36         return value;
37     }
38
39     public static YangInstanceIdentifier.PathArgument createPathArgument(String id, DataSchemaNode schemaNode){
40         final NodeIdentifierWithPredicatesGenerator
41             nodeIdentifierWithPredicatesGenerator = new NodeIdentifierWithPredicatesGenerator(id, schemaNode);
42         if(nodeIdentifierWithPredicatesGenerator.matches()){
43             return nodeIdentifierWithPredicatesGenerator.getPathArgument();
44         }
45
46         final NodeIdentifierWithValueGenerator
47             nodeWithValueGenerator = new NodeIdentifierWithValueGenerator(id, schemaNode);
48         if(nodeWithValueGenerator.matches()){
49             return nodeWithValueGenerator.getPathArgument();
50         }
51
52         final AugmentationIdentifierGenerator augmentationIdentifierGenerator = new AugmentationIdentifierGenerator(id);
53         if(augmentationIdentifierGenerator.matches()){
54             return augmentationIdentifierGenerator.getPathArgument();
55         }
56
57         return new NodeIdentifierGenerator(id).getArgument();
58     }
59 }