Merge "Bug 1372 - toString methods in generated classes"
[yangtools.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / ChoiceNodeCodecContext.java
1 /*
2  * Copyright (c) 2014 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.binding.data.codec.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.HashMap;
13 import java.util.Map;
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;
19
20 class ChoiceNodeCodecContext extends DataContainerCodecContext<ChoiceNode> {
21
22     private final YangInstanceIdentifier.PathArgument yangArgument;
23     private final ImmutableMap<QName, ChoiceCaseNode> caseChildToCase;
24
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);
32             }
33         }
34         caseChildToCase = ImmutableMap.copyOf(childToCase);
35     }
36
37     @Override
38     protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
39         return yangArgument;
40     }
41
42     @Override
43     protected DataContainerCodecContext<?> loadChild(final Class<?> childClass) {
44
45         ChoiceCaseNode childSchema = factory.getRuntimeContext().getCaseSchemaDefinition(schema, childClass);
46         return new CaseNodeCodecContext(childClass, childSchema, factory);
47     }
48
49     @Override
50     protected NodeCodecContext getYangIdentifierChild(final YangInstanceIdentifier.PathArgument arg) {
51
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);
55         ;
56         Class<?> cazeClass = factory.getRuntimeContext().getClassForSchema(caze);
57         return getStreamChild(cazeClass).getYangIdentifierChild(arg);
58     }
59
60 }