Bug 2766: Fixed parsing and serializing XPath Instance Identifiers
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractStringInstanceIdentifierCodec.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.yangtools.yang.data.util;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.Map;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.codec.InstanceIdentifierCodec;
20
21 /**
22  * Abstract utility class for representations which encode {@link YangInstanceIdentifier} as a
23  * prefix:name tuple. Typical uses are RESTCONF/JSON (module:name) and XML (prefix:name).
24  */
25 @Beta
26 public abstract class AbstractStringInstanceIdentifierCodec extends AbstractNamespaceCodec implements InstanceIdentifierCodec<String> {
27
28     @Override
29     public final String serialize(final YangInstanceIdentifier data) {
30         StringBuilder sb = new StringBuilder();
31         DataSchemaContextNode<?> current = getDataContextTree().getRoot();
32         for (PathArgument arg : data.getPathArguments()) {
33             current = current.getChild(arg);
34
35             if(current.isMixin()) {
36                 /*
37                  * XML/YANG instance identifier does not have concept
38                  * of augmentation identifier, or list as whole which
39                  * identifies mixin (same as paretn element),
40                  * so we can safely ignore it if it is part of path
41                  * (since child node) is identified in same fashion.
42                  *
43                  */
44                 continue;
45             }
46
47             sb.append('/');
48             appendQName(sb, arg.getNodeType());
49
50             if (arg instanceof NodeIdentifierWithPredicates) {
51                 for (Map.Entry<QName, Object> entry : ((NodeIdentifierWithPredicates) arg).getKeyValues().entrySet()) {
52                     sb.append('[');
53                     appendQName(sb, entry.getKey());
54                     sb.append("='");
55                     sb.append(String.valueOf(entry.getValue()));
56                     sb.append("']");
57                 }
58             } else if (arg instanceof NodeWithValue) {
59                 sb.append("[.='");
60                 sb.append(((NodeWithValue) arg).getValue());
61                 sb.append("']");
62             }
63         }
64         return sb.toString();
65     }
66
67     /**
68      *
69      * Returns DataSchemaContextTree associated with SchemaContext for which
70      * serialization / deserialization occurs.
71      *
72      * Implementations MUST provide non-null Data Tree context, in order
73      * for correct serialization / deserialization of PathArguments,
74      * since XML representation does not have Augmentation arguments
75      * and does not provide path arguments for cases.
76      *
77      * This effectively means same input XPath representation of Path Argument
78      * may result in different YangInstanceIdentifiers if models are different
79      * in uses of choices and cases.
80      *
81      * @return DataSchemaContextTree associated with SchemaContext for which
82      * serialization / deserialization occurs.
83      */
84     protected abstract @Nonnull DataSchemaContextTree getDataContextTree();
85
86     @Override
87     public final YangInstanceIdentifier deserialize(final String data) {
88         Preconditions.checkNotNull(data, "Data may not be null");
89         XpathStringParsingPathArgumentBuilder builder = new XpathStringParsingPathArgumentBuilder(this, data);
90         return YangInstanceIdentifier.create(builder.build());
91     }
92
93 }