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.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
17 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer.ChoiceNodeBaseSerializer;
18 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer.NodeSerializerDispatcher;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 final class ChoiceNodeCliSerializer extends ChoiceNodeBaseSerializer<String> {
23 private final NodeSerializerDispatcher<String> dispatcher;
24 private final OutFormatter out;
26 ChoiceNodeCliSerializer(final OutFormatter out, final NodeSerializerDispatcher<String> dispatcher) {
27 this.out = Preconditions.checkNotNull(out);
28 this.dispatcher = Preconditions.checkNotNull(dispatcher);
32 public Iterable<String> serialize(final ChoiceSchemaNode schema, final ChoiceNode node) {
33 final StringBuilder output = new StringBuilder();
35 out.addStringWithIndent(output, "choice ");
36 output.append(schema.getQName().getLocalName());
38 output.append(detectCase(schema, node));
40 out.openComposite(output);
43 for (final String childOutput : super.serialize(schema, node)) {
44 output.append(childOutput);
48 out.closeCompositeWithIndent(output);
50 return Collections.singletonList(output.toString());
53 private String detectCase(final ChoiceSchemaNode schema, final ChoiceNode node) {
54 for (final DataContainerChild<? extends PathArgument, ?> caseChild : node.getValue()) {
55 final QName presentChildQName = caseChild.getNodeType();
56 for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
57 if (choiceCaseNode.getDataChildByName(presentChildQName) != null) {
58 // Pick the first case that contains first child node
59 return choiceCaseNode.getQName().getLocalName();
64 // Should not happen, nodes should come from one of the cases
65 throw new IllegalStateException("Choice node " + node + " does not conform to choice schema " + schema);
69 protected NodeSerializerDispatcher<String> getNodeDispatcher() {