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