2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.node.utils;
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;
18 import java.util.HashMap;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
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;
30 public NodeIdentifierWithPredicatesGenerator(String id, DataSchemaNode schemaNode){
32 matcher = pattern.matcher(this.id);
33 doesMatch = matcher.matches();
35 if(schemaNode instanceof ListSchemaNode){
36 this.listSchemaNode = (ListSchemaNode) schemaNode;
38 this.listSchemaNode = null;
43 public boolean matches(){
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<>();
52 for(String keyValue : keyValues){
53 int eqIndex = keyValue.lastIndexOf('=');
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);
64 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QNameFactory.create(matcher.group(1)), nameValues);
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);