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