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;
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).
26 public abstract class AbstractStringInstanceIdentifierCodec extends AbstractNamespaceCodec implements InstanceIdentifierCodec<String> {
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);
35 if(current.isMixin()) {
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.
48 appendQName(sb, arg.getNodeType());
50 if (arg instanceof NodeIdentifierWithPredicates) {
51 for (Map.Entry<QName, Object> entry : ((NodeIdentifierWithPredicates) arg).getKeyValues().entrySet()) {
53 appendQName(sb, entry.getKey());
55 sb.append(String.valueOf(entry.getValue()));
58 } else if (arg instanceof NodeWithValue) {
60 sb.append(((NodeWithValue) arg).getValue());
69 * Returns DataSchemaContextTree associated with SchemaContext for which
70 * serialization / deserialization occurs.
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.
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.
81 * @return DataSchemaContextTree associated with SchemaContext for which
82 * serialization / deserialization occurs.
84 protected abstract @Nonnull DataSchemaContextTree getDataContextTree();
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());