3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
11 package org.opendaylight.controller.cluster.datastore.node.utils;
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;
20 import java.util.HashMap;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
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;
32 public NodeIdentifierWithPredicatesGenerator(String id, DataSchemaNode schemaNode){
34 matcher = pattern.matcher(this.id);
35 doesMatch = matcher.matches();
37 if(schemaNode instanceof ListSchemaNode){
38 this.listSchemaNode = (ListSchemaNode) schemaNode;
40 this.listSchemaNode = null;
45 public boolean matches(){
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<>();
54 for(String keyValue : keyValues){
55 int eqIndex = keyValue.lastIndexOf('=');
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);
66 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QNameFactory.create(matcher.group(1)), nameValues);
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);