2 * Copyright (c) 2014 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.reader.impl;
10 import static org.opendaylight.controller.netconf.cli.io.IOUtil.isSkipInput;
11 import static org.opendaylight.controller.netconf.cli.io.IOUtil.listType;
13 import com.google.common.base.Optional;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import org.opendaylight.controller.config.util.xml.XmlUtil;
19 import org.opendaylight.controller.netconf.cli.io.BaseConsoleContext;
20 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
21 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
22 import org.opendaylight.controller.netconf.cli.reader.AbstractReader;
23 import org.opendaylight.controller.netconf.cli.reader.ReadingException;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
30 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
31 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.w3c.dom.Document;
34 import org.xml.sax.SAXException;
36 public class AnyXmlReader extends AbstractReader<AnyXmlSchemaNode> {
38 public AnyXmlReader(final ConsoleIO console, final SchemaContext schemaContext) {
39 super(console, schemaContext);
42 public AnyXmlReader(final ConsoleIO console, final SchemaContext schemaContext, final boolean readConfigNode) {
43 super(console, schemaContext, readConfigNode);
47 protected List<NormalizedNode<?, ?>> readWithContext(final AnyXmlSchemaNode schemaNode) throws IOException, ReadingException {
48 console.writeLn(listType(schemaNode) + " " + schemaNode.getQName().getLocalName());
50 final String rawValue = console.read();
52 DataContainerChild<?, ?> newNode = null;
53 if (!isSkipInput(rawValue)) {
54 final Optional<DataContainerChild<?, ?>> value = tryParse(rawValue, schemaNode);
56 if (value.isPresent()) {
57 newNode = ImmutableContainerNodeBuilder.create()
58 .withNodeIdentifier(new NodeIdentifier(schemaNode.getQName()))
59 .withChild(value.get()).build();
61 newNode = ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(schemaNode.getQName())).withValue(rawValue).build();
65 final List<NormalizedNode<?, ?>> newNodes = new ArrayList<>();
66 newNodes.add(newNode);
70 private Optional<DataContainerChild<?, ?>> tryParse(final String rawValue, final AnyXmlSchemaNode schemaNode) {
72 final Document dom = XmlUtil.readXmlToDocument(rawValue);
73 return Optional.<DataContainerChild<?, ?>> of(
74 DomToNormalizedNodeParserFactory.
75 getInstance(DomUtils.defaultValueCodecProvider(), getSchemaContext()).
76 getAnyXmlNodeParser().
77 parse(Collections.singletonList(dom.getDocumentElement()), schemaNode)
79 } catch (SAXException | IOException e) {
81 return Optional.absent();
86 protected ConsoleContext getContext(final AnyXmlSchemaNode schemaNode) {
87 return new BaseConsoleContext<>(schemaNode);