Created sal-clustering-commons and moved
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / NodeIdentifierWithPredicatesGenerator.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.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
19
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 public class NodeIdentifierWithPredicatesGenerator{
26     private final String id;
27     private static final Pattern pattern = Pattern.compile("(.*)\\Q[{\\E(.*)\\Q}]\\E");
28     private final Matcher matcher;
29     private final boolean doesMatch;
30     private final ListSchemaNode listSchemaNode;
31
32     public NodeIdentifierWithPredicatesGenerator(String id, DataSchemaNode schemaNode){
33         this.id = id;
34         matcher = pattern.matcher(this.id);
35         doesMatch = matcher.matches();
36
37         if(schemaNode instanceof  ListSchemaNode){
38             this.listSchemaNode = (ListSchemaNode) schemaNode;
39         } else {
40             this.listSchemaNode = null;
41         }
42     }
43
44
45     public boolean matches(){
46         return doesMatch;
47     }
48
49     public YangInstanceIdentifier.NodeIdentifierWithPredicates getPathArgument(){
50         final String group = matcher.group(2);
51         final String[] keyValues = group.split(",");
52         Map<QName, Object> nameValues = new HashMap<>();
53
54         for(String keyValue : keyValues){
55             int eqIndex = keyValue.lastIndexOf('=');
56             try {
57                 final QName key = QNameFactory
58                     .create(keyValue.substring(0, eqIndex));
59                 nameValues.put(key, getValue(key, keyValue.substring(eqIndex + 1)));
60             } catch(IllegalArgumentException e){
61                 System.out.println("Error processing identifier : " + id);
62                 throw e;
63             }
64         }
65
66         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QNameFactory.create(matcher.group(1)), nameValues);
67     }
68
69
70     private Object getValue(QName key, String value){
71         if(listSchemaNode != null){
72             for(DataSchemaNode node : listSchemaNode.getChildNodes()){
73                 if(node instanceof LeafSchemaNode && node.getQName().equals(key)){
74                     return TypeDefinitionAwareCodec.from(LeafSchemaNode.class.cast(node).getType()).deserialize(value);
75                 }
76             }
77         }
78         return value;
79     }
80 }