Reduce use of yang.model.api
[yangtools.git] / codec / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / InstanceIdentifierDeserializer.java
1 /*
2  * Copyright (c) 2016 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.codec.xml;
9
10 import static java.util.Objects.requireNonNull;
11
12 import javax.xml.namespace.NamespaceContext;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
21
22 final class InstanceIdentifierDeserializer extends AbstractInstanceIdentifierCodec {
23     private final @NonNull XmlCodecFactory codecFactory;
24     private final @NonNull NamespaceContext namespaceContext;
25
26     InstanceIdentifierDeserializer(final DataSchemaContextTree dataContextTree, final XmlCodecFactory codecFactory,
27             final NamespaceContext namespaceContext) {
28         super(dataContextTree);
29         this.codecFactory = requireNonNull(codecFactory);
30         this.namespaceContext = requireNonNull(namespaceContext);
31     }
32
33     @Override
34     protected QNameModule moduleForPrefix(final String prefix) {
35         final var modules = codecFactory.getEffectiveModelContext()
36             .findModuleStatements(XMLNamespace.of(namespaceContext.getNamespaceURI(prefix)))
37             .iterator();
38         return modules.hasNext() ? modules.next().localQNameModule() : null;
39     }
40
41     @Override
42     protected String prefixForNamespace(final XMLNamespace namespace) {
43         // This is serialize() path, we do not support that in this class
44         throw new UnsupportedOperationException("Not implemented");
45     }
46
47     @Override
48     protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
49             final String value) {
50         requireNonNull(schemaNode, "schemaNode cannot be null");
51         final XmlCodec<?> objectXmlCodec;
52         if (schemaNode instanceof LeafSchemaNode leafSchemaNode) {
53             objectXmlCodec = codecFactory.codecFor(leafSchemaNode, resolver);
54         } else if (schemaNode instanceof LeafListSchemaNode leafListSchemaNode) {
55             objectXmlCodec = codecFactory.codecFor(leafListSchemaNode, resolver);
56         } else {
57             throw new IllegalArgumentException("schemaNode " + schemaNode
58                     + " must be of type LeafSchemaNode or LeafListSchemaNode");
59         }
60         return objectXmlCodec.parseValue(namespaceContext, value);
61     }
62 }