Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / tools / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / writer / impl / ChoiceNodeCliSerializer.java
1 /*
2  * Copyright (c) 2013 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.controller.netconf.cli.writer.impl;
9
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;
21
22 final class ChoiceNodeCliSerializer extends ChoiceNodeBaseSerializer<String> {
23     private final NodeSerializerDispatcher<String> dispatcher;
24     private final OutFormatter out;
25
26     ChoiceNodeCliSerializer(final OutFormatter out, final NodeSerializerDispatcher<String> dispatcher) {
27         this.out = Preconditions.checkNotNull(out);
28         this.dispatcher = Preconditions.checkNotNull(dispatcher);
29     }
30
31     @Override
32     public Iterable<String> serialize(final ChoiceSchemaNode schema, final ChoiceNode node) {
33         final StringBuilder output = new StringBuilder();
34         out.increaseIndent();
35         out.addStringWithIndent(output, "choice ");
36         output.append(schema.getQName().getLocalName());
37         output.append(" (");
38         output.append(detectCase(schema, node));
39         output.append(") ");
40         out.openComposite(output);
41         out.newLine(output);
42
43         for (final String childOutput : super.serialize(schema, node)) {
44             output.append(childOutput);
45             out.newLine(output);
46         }
47
48         out.closeCompositeWithIndent(output);
49         out.decreaseIndent();
50         return Collections.singletonList(output.toString());
51     }
52
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();
60                 }
61             }
62         }
63
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);
66     }
67
68     @Override
69     protected NodeSerializerDispatcher<String> getNodeDispatcher() {
70         return dispatcher;
71     }
72 }