56ef8ea6fc2042c45d0558eb59e8fe2e22dc2f7c
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / AugmentationIdentifierGenerator.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 com.google.common.base.Splitter;
14 import java.util.HashSet;
15 import java.util.Set;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20
21 public class AugmentationIdentifierGenerator {
22     private static final Pattern PATTERN = Pattern.compile("AugmentationIdentifier\\Q{\\EchildNames=\\Q[\\E(.*)\\Q]}\\E");
23     private static final Splitter COMMA_SPLITTER = Splitter.on(',').trimResults();
24
25     private final String id;
26     private final Matcher matcher;
27     private final boolean doesMatch;
28
29     public AugmentationIdentifierGenerator(String id) {
30         this.id = id;
31         matcher = PATTERN.matcher(this.id);
32         doesMatch = matcher.matches();
33     }
34
35     public boolean matches() {
36         return doesMatch;
37     }
38
39     public YangInstanceIdentifier.AugmentationIdentifier getPathArgument() {
40         final String childQNames = matcher.group(1);
41
42         final Set<QName> childNames = new HashSet<>();
43         for (String name : COMMA_SPLITTER.split(childQNames)) {
44             childNames.add(QNameFactory.create(name));
45         }
46
47         return new YangInstanceIdentifier.AugmentationIdentifier(childNames);
48     }
49
50 }