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.codec.gson;
10 import static java.util.Objects.requireNonNull;
12 import com.google.gson.stream.JsonWriter;
13 import java.io.IOException;
14 import java.util.Iterator;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
18 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
26 abstract class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
27 implements JSONCodec<YangInstanceIdentifier> {
28 private final DataSchemaContextTree dataContextTree;
29 private final JSONCodecFactory codecFactory;
30 private final EffectiveModelContext context;
32 JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
33 this.context = requireNonNull(context);
34 dataContextTree = DataSchemaContextTree.from(context);
35 codecFactory = requireNonNull(jsonCodecFactory);
39 protected final Module moduleForPrefix(final String prefix) {
40 final Iterator<? extends Module> modules = context.findModules(prefix).iterator();
41 return modules.hasNext() ? modules.next() : null;
45 protected final String prefixForNamespace(final XMLNamespace namespace) {
46 final Iterator<? extends Module> modules = context.findModules(namespace).iterator();
47 return modules.hasNext() ? modules.next().getName() : null;
51 protected final DataSchemaContextTree getDataContextTree() {
52 return dataContextTree;
56 protected final Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
58 requireNonNull(schemaNode, "schemaNode cannot be null");
59 if (schemaNode instanceof LeafSchemaNode) {
60 return codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver).parseValue(null, value);
61 } else if (schemaNode instanceof LeafListSchemaNode) {
62 return codecFactory.codecFor((LeafListSchemaNode) schemaNode, resolver).parseValue(null, value);
64 throw new IllegalArgumentException("schemaNode " + schemaNode
65 + " must be of type LeafSchemaNode or LeafListSchemaNode");
69 public final Class<YangInstanceIdentifier> getDataType() {
70 return YangInstanceIdentifier.class;
74 public final YangInstanceIdentifier parseValue(final Object ctx, final String str) {
75 return deserialize(str);
79 public final void writeValue(final JsonWriter ctx, final YangInstanceIdentifier value) throws IOException {
80 ctx.value(serialize(value));