da61e6de7327c9b3adabf30bf9d621561dd7c998
[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 createPathArgument(String id, DataSchemaNode schemaNode){
36         final NodeIdentifierWithPredicatesGenerator
37             nodeIdentifierWithPredicatesGenerator = new NodeIdentifierWithPredicatesGenerator(id, schemaNode);
38         if(nodeIdentifierWithPredicatesGenerator.matches()){
39             return nodeIdentifierWithPredicatesGenerator.getPathArgument();
40         }
41
42         final NodeIdentifierWithValueGenerator
43             nodeWithValueGenerator = new NodeIdentifierWithValueGenerator(id, schemaNode);
44         if(nodeWithValueGenerator.matches()){
45             return nodeWithValueGenerator.getPathArgument();
46         }
47
48         final AugmentationIdentifierGenerator augmentationIdentifierGenerator = new AugmentationIdentifierGenerator(id);
49         if(augmentationIdentifierGenerator.matches()){
50             return augmentationIdentifierGenerator.getPathArgument();
51         }
52
53         return new NodeIdentifierGenerator(id).getArgument();
54     }
55 }