2 * Copyright (c) 2013 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.controller.netconf.cli.writer.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import org.opendaylight.controller.netconf.cli.writer.OutFormatter;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer.ChoiceNodeBaseSerializer;
17 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer.NodeSerializerDispatcher;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21 final class ChoiceNodeCliSerializer extends ChoiceNodeBaseSerializer<String> {
22 private final NodeSerializerDispatcher<String> dispatcher;
23 private final OutFormatter out;
25 ChoiceNodeCliSerializer(final OutFormatter out, final NodeSerializerDispatcher<String> dispatcher) {
26 this.out = Preconditions.checkNotNull(out);
27 this.dispatcher = Preconditions.checkNotNull(dispatcher);
31 public Iterable<String> serialize(final ChoiceNode schema, final org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode node) {
32 final StringBuilder output = new StringBuilder();
34 out.addStringWithIndent(output, "choice ");
35 output.append(schema.getQName().getLocalName());
37 output.append(detectCase(schema, node));
39 out.openComposite(output);
42 for (final String childOutput : super.serialize(schema, node)) {
43 output.append(childOutput);
47 out.closeCompositeWithIndent(output);
49 return Collections.singletonList(output.toString());
52 private String detectCase(final ChoiceNode schema, final org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode node) {
53 for (final DataContainerChild<? extends PathArgument, ?> caseChild : node.getValue()) {
54 final QName presentChildQName = caseChild.getNodeType();
55 for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
56 if (choiceCaseNode.getDataChildByName(presentChildQName) != null) {
57 // Pick the first case that contains first child node
58 return choiceCaseNode.getQName().getLocalName();
63 // Should not happen, nodes should come from one of the cases
64 throw new IllegalStateException("Choice node " + node + " does not conform to choice schema " + schema);
68 protected NodeSerializerDispatcher<String> getNodeDispatcher() {