Fix CS warnings in sal-clustering-commons and enable enforcement
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / NodeIdentifierWithValueGenerator.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 java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
15 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
17
18 public class NodeIdentifierWithValueGenerator {
19     private static final Pattern PATTERN = Pattern.compile("(.*)\\Q[\\E(.*)\\Q]\\E");
20
21     private final String id;
22     private final DataSchemaNode schemaNode;
23     private final Matcher matcher;
24     private final boolean doesMatch;
25
26     public NodeIdentifierWithValueGenerator(String id, DataSchemaNode schemaNode) {
27         this.id = id;
28         this.schemaNode = schemaNode;
29         matcher = PATTERN.matcher(this.id);
30         doesMatch = matcher.matches();
31     }
32
33     public boolean matches() {
34         return doesMatch;
35     }
36
37     public YangInstanceIdentifier.PathArgument getPathArgument() {
38         final String name = matcher.group(1);
39         final String value = matcher.group(2);
40
41         return new YangInstanceIdentifier.NodeWithValue<>(QNameFactory.create(name), getValue(value));
42     }
43
44     private Object getValue(String value) {
45         if (schemaNode != null) {
46             if (schemaNode instanceof LeafListSchemaNode) {
47                 return TypeDefinitionAwareCodec.from(LeafListSchemaNode.class.cast(schemaNode).getType())
48                         .deserialize(value);
49
50             }
51         }
52         return value;
53     }
54
55 }