Fix license header violations in sal-clustering-commons
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / NodeIdentifierWithValueGenerator.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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
15
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 public class NodeIdentifierWithValueGenerator{
20         private final String id;
21     private final DataSchemaNode schemaNode;
22     private static final Pattern pattern = Pattern.compile("(.*)\\Q[\\E(.*)\\Q]\\E");
23         private final Matcher matcher;
24         private final boolean doesMatch;
25
26         public NodeIdentifierWithValueGenerator(String id, DataSchemaNode schemaNode){
27             this.id = id;
28             this.schemaNode = schemaNode;
29             matcher = pattern.matcher(this.id);
30             doesMatch = matcher.matches();
31         }
32
33         public boolean matches(){
34             return doesMatch;
35         }
36
37         public YangInstanceIdentifier.PathArgument getPathArgument(){
38             final String name = matcher.group(1);
39             final String value = matcher.group(2);
40
41             return new YangInstanceIdentifier.NodeWithValue(
42                 QNameFactory.create(name), getValue(value));
43         }
44
45         private Object getValue(String value){
46             if(schemaNode != null){
47                 if(schemaNode instanceof LeafListSchemaNode){
48                     return TypeDefinitionAwareCodec
49                         .from(LeafListSchemaNode.class.cast(schemaNode).getType()).deserialize(value);
50
51                 }
52             }
53         return value;
54         }
55
56     }