CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / netconf / tools / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / reader / impl / AnyXmlReader.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.reader.impl;
9
10 import static org.opendaylight.controller.netconf.cli.io.IOUtil.isSkipInput;
11 import static org.opendaylight.controller.netconf.cli.io.IOUtil.listType;
12
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;
35
36 public class AnyXmlReader extends AbstractReader<AnyXmlSchemaNode> {
37
38     public AnyXmlReader(final ConsoleIO console, final SchemaContext schemaContext) {
39         super(console, schemaContext);
40     }
41
42     public AnyXmlReader(final ConsoleIO console, final SchemaContext schemaContext, final boolean readConfigNode) {
43         super(console, schemaContext, readConfigNode);
44     }
45
46     @Override
47     protected List<NormalizedNode<?, ?>> readWithContext(final AnyXmlSchemaNode schemaNode) throws IOException, ReadingException {
48         console.writeLn(listType(schemaNode) + " " + schemaNode.getQName().getLocalName());
49
50         final String rawValue = console.read();
51
52         DataContainerChild<?, ?> newNode = null;
53         if (!isSkipInput(rawValue)) {
54             final Optional<DataContainerChild<?, ?>> value = tryParse(rawValue, schemaNode);
55
56             if (value.isPresent()) {
57                 newNode = ImmutableContainerNodeBuilder.create()
58                         .withNodeIdentifier(new NodeIdentifier(schemaNode.getQName()))
59                         .withChild(value.get()).build();
60             } else {
61                 newNode = ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(schemaNode.getQName())).withValue(rawValue).build();
62             }
63         }
64
65         final List<NormalizedNode<?, ?>> newNodes = new ArrayList<>();
66         newNodes.add(newNode);
67         return newNodes;
68     }
69
70     private Optional<DataContainerChild<?, ?>> tryParse(final String rawValue, final AnyXmlSchemaNode schemaNode) {
71         try {
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)
78             );
79         } catch (SAXException | IOException e) {
80             // TODO log
81             return Optional.absent();
82         }
83     }
84
85     @Override
86     protected ConsoleContext getContext(final AnyXmlSchemaNode schemaNode) {
87         return new BaseConsoleContext<>(schemaNode);
88     }
89 }