Merge "BUG 2799: SPI for EventSources"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / writer / impl / NormalizedNodeWriter.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.controller.netconf.cli.writer.impl;
9
10 import java.io.IOException;
11 import java.util.Iterator;
12 import java.util.List;
13 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
14 import org.opendaylight.controller.netconf.cli.writer.OutFormatter;
15 import org.opendaylight.controller.netconf.cli.writer.WriteException;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer.NodeSerializerDispatcher;
19 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class NormalizedNodeWriter extends AbstractWriter<DataSchemaNode> {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeWriter.class);
27     private final OutFormatter out;
28
29     public NormalizedNodeWriter(final ConsoleIO console, final OutFormatter out) {
30         super(console);
31         this.out = out;
32     }
33
34     public void writeInner(final DataSchemaNode dataSchemaNode, final List<NormalizedNode<?, ?>> dataNodes) throws WriteException,
35             IOException {
36         //Preconditions.checkState(dataNodes.size() == 1);
37         // TODO - add getDispatcher method to CnSnToNormalizedNodeParserFactory
38         // to be able call dispatchChildElement
39         final NormalizedNode<?, ?> dataContainerChild = dataNodes.get(0);
40
41         if (dataContainerChild != null) {
42             console.writeLn(serializeToCliOutput(dataContainerChild, dataSchemaNode));
43         }
44
45     }
46
47     private String serializeToCliOutput(final NormalizedNode<?, ?> dataContainerChild,
48             final DataSchemaNode childSchema) {
49         final CliOutputFromNormalizedNodeSerializerFactory factorySerialization = CliOutputFromNormalizedNodeSerializerFactory
50                 .getInstance(out, DomUtils.defaultValueCodecProvider());
51         final NodeSerializerDispatcher<String> dispatcher = factorySerialization.getDispatcher();
52         final Iterable<String> result = dispatcher.dispatchChildElement(childSchema, (DataContainerChild<?, ?>) dataContainerChild);
53
54         if (result == null) {
55             return "";
56         }
57
58         final Iterator<String> output = result.iterator();
59         if (!output.hasNext()) {
60             return "";
61         }
62
63         return output.next();
64     }
65
66 }