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.binding.data.codec.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.HashMap;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 class ChoiceNodeCodecContext extends DataContainerCodecContext<ChoiceNode> {
22 private final YangInstanceIdentifier.PathArgument yangArgument;
23 private final ImmutableMap<QName, ChoiceCaseNode> caseChildToCase;
25 ChoiceNodeCodecContext(final Class<?> cls, final ChoiceNode nodeSchema, final CodecContextFactory context) {
26 super(cls, nodeSchema.getQName().getModule(), nodeSchema, context);
27 Map<QName, ChoiceCaseNode> childToCase = new HashMap<>();
28 yangArgument = new YangInstanceIdentifier.NodeIdentifier(nodeSchema.getQName());
29 for (ChoiceCaseNode caseNode : nodeSchema.getCases()) {
30 for (DataSchemaNode caseChild : caseNode.getChildNodes()) {
31 childToCase.put(caseChild.getQName(), caseNode);
34 caseChildToCase = ImmutableMap.copyOf(childToCase);
38 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
43 protected DataContainerCodecContext<?> loadChild(final Class<?> childClass) {
45 ChoiceCaseNode childSchema = factory.getRuntimeContext().getCaseSchemaDefinition(schema, childClass);
46 return new CaseNodeCodecContext(childClass, childSchema, factory);
50 protected NodeCodecContext getYangIdentifierChild(final YangInstanceIdentifier.PathArgument arg) {
52 QName childQName = arg.getNodeType();
53 ChoiceCaseNode caze = caseChildToCase.get(childQName);
54 Preconditions.checkArgument(caze != null, "Argument %s is not valid child of %s", arg, schema);
56 Class<?> cazeClass = factory.getRuntimeContext().getClassForSchema(caze);
57 return getStreamChild(cazeClass).getYangIdentifierChild(arg);