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);
35 Preconditions.checkArgument(current != null,
36 "Invalid input %s: schema for argument %s (after %s) not found", data, arg, sb);
38 if (current.isMixin()) {
40 * XML/YANG instance identifier does not have concept
41 * of augmentation identifier, or list as whole which
42 * identifies a mixin (same as the parent element),
43 * so we can safely ignore it if it is part of path
44 * (since child node) is identified in same fashion.
50 appendQName(sb, arg.getNodeType());
52 if (arg instanceof NodeIdentifierWithPredicates) {
53 for (Map.Entry<QName, Object> entry : ((NodeIdentifierWithPredicates) arg).getKeyValues().entrySet()) {
55 appendQName(sb, entry.getKey());
57 sb.append(String.valueOf(entry.getValue()));
60 } else if (arg instanceof NodeWithValue) {
62 sb.append(((NodeWithValue<?>) arg).getValue());
71 * Returns DataSchemaContextTree associated with SchemaContext for which
72 * serialization / deserialization occurs.
74 * Implementations MUST provide non-null Data Tree context, in order
75 * for correct serialization / deserialization of PathArguments,
76 * since XML representation does not have Augmentation arguments
77 * and does not provide path arguments for cases.
79 * This effectively means same input XPath representation of Path Argument
80 * may result in different YangInstanceIdentifiers if models are different
81 * in uses of choices and cases.
83 * @return DataSchemaContextTree associated with SchemaContext for which
84 * serialization / deserialization occurs.
86 protected abstract @Nonnull DataSchemaContextTree getDataContextTree();
88 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final String value) {
93 public final YangInstanceIdentifier deserialize(final String data) {
94 Preconditions.checkNotNull(data, "Data may not be null");
95 XpathStringParsingPathArgumentBuilder builder = new XpathStringParsingPathArgumentBuilder(this, data);
96 return YangInstanceIdentifier.create(builder.build());