BUG 2221 : Add metering to ShardTransaction actor
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / NodeIdentifierFactory.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node.utils;
12
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 public class NodeIdentifierFactory {
20     private static final Map<String, YangInstanceIdentifier.PathArgument> cache = new HashMap<>();
21     public static YangInstanceIdentifier.PathArgument getArgument(String id){
22         YangInstanceIdentifier.PathArgument value = cache.get(id);
23         if(value == null){
24             synchronized (cache){
25                 value = cache.get(id);
26                 if(value == null) {
27                     value = createPathArgument(id, null);
28                     cache.put(id, value);
29                 }
30             }
31         }
32         return value;
33     }
34
35     public static YangInstanceIdentifier.PathArgument getArgument(String id, DataSchemaNode schemaNode){
36         YangInstanceIdentifier.PathArgument value = cache.get(id);
37         if(value == null){
38             synchronized (cache){
39                 value = cache.get(id);
40                 if(value == null) {
41                     value = createPathArgument(id, schemaNode);
42                     cache.put(id, value);
43                 }
44             }
45         }
46         return value;
47     }
48
49     public static YangInstanceIdentifier.PathArgument createPathArgument(String id, DataSchemaNode schemaNode){
50         final NodeIdentifierWithPredicatesGenerator
51             nodeIdentifierWithPredicatesGenerator = new NodeIdentifierWithPredicatesGenerator(id, schemaNode);
52         if(nodeIdentifierWithPredicatesGenerator.matches()){
53             return nodeIdentifierWithPredicatesGenerator.getPathArgument();
54         }
55
56         final NodeIdentifierWithValueGenerator
57             nodeWithValueGenerator = new NodeIdentifierWithValueGenerator(id, schemaNode);
58         if(nodeWithValueGenerator.matches()){
59             return nodeWithValueGenerator.getPathArgument();
60         }
61
62         final AugmentationIdentifierGenerator augmentationIdentifierGenerator = new AugmentationIdentifierGenerator(id);
63         if(augmentationIdentifierGenerator.matches()){
64             return augmentationIdentifierGenerator.getPathArgument();
65         }
66
67         return new NodeIdentifierGenerator(id).getArgument();
68     }
69 }