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