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