2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
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;
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).
27 public abstract class AbstractStringInstanceIdentifierCodec extends AbstractNamespaceCodec implements InstanceIdentifierCodec<String> {
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);
36 if(current.isMixin()) {
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.
49 appendQName(sb, arg.getNodeType());
51 if (arg instanceof NodeIdentifierWithPredicates) {
52 for (Map.Entry<QName, Object> entry : ((NodeIdentifierWithPredicates) arg).getKeyValues().entrySet()) {
54 appendQName(sb, entry.getKey());
56 sb.append(String.valueOf(entry.getValue()));
59 } else if (arg instanceof NodeWithValue) {
61 sb.append(((NodeWithValue) arg).getValue());
70 * Returns DataSchemaContextTree associated with SchemaContext for which
71 * serialization / deserialization occurs.
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.
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.
82 * @return DataSchemaContextTree associated with SchemaContext for which
83 * serialization / deserialization occurs.
85 protected abstract @Nonnull DataSchemaContextTree getDataContextTree();
87 protected Object deserializeKeyValue(DataSchemaNode schemaNode, String value) {
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());