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.netconf.cli;
10 import static org.junit.Assert.assertNotNull;
11 import static org.opendaylight.netconf.cli.io.IOUtil.PROMPT_SUFIX;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.ArrayDeque;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Deque;
21 import java.util.HashMap;
22 import java.util.List;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.opendaylight.netconf.cli.reader.ReadingException;
27 import org.opendaylight.netconf.cli.writer.WriteException;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
32 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
35 public class NetconfCliTest {
37 private final static YangContextParser parser = new YangParserImpl();
39 private static SchemaContext loadSchemaContext(final String resourceDirectory) throws IOException,
41 final URI uri = NetconfCliTest.class.getResource(resourceDirectory).toURI();
42 final File testDir = new File(uri);
43 final String[] fileList = testDir.list();
44 final List<File> testFiles = new ArrayList<File>();
45 if (fileList == null) {
46 throw new FileNotFoundException(resourceDirectory);
48 for (final String fileName : fileList) {
49 if (new File(testDir, fileName).isDirectory() == false) {
50 testFiles.add(new File(testDir, fileName));
53 return parser.parseFiles(testFiles);
57 public void cliTest() throws ReadingException, IOException, WriteException, URISyntaxException {
59 final SchemaContext schemaContext = loadSchemaContext("/schema-context");
60 assertNotNull(schemaContext);
62 final DataSchemaNode cont1 = findTopLevelElement("ns:model1", "2014-05-14", "cont1", schemaContext);
63 final Map<String, Deque<String>> values = new HashMap<>();
65 values.put(prompt("/cont1/cont11/lst111/[entry]/lf1111"), value("55", "32"));
66 values.put(prompt("/cont1/cont11/lst111/[entry]"), value("Y", "Y"));
67 values.put(prompt("/cont1/cont11/lst111/[entry]/cont111/lf1112"),
68 value("value for lf1112", "2value for lf1112"));
69 values.put(prompt("/cont1/cont11/lst111/[entry]/cont111/lflst1111"), value("Y", "N", "Y", "N"));
70 values.put(prompt("/cont1/cont11/lst111/[entry]/cont111/lflst1111/[entry]"), value("10", "15", "20", "30"));
72 values.put(prompt("/cont1/cont11/lst111"), value("Y", "N"));
74 values.put(prompt("/cont1/cont12/chcA"), value("AB"));
75 values.put(prompt("/cont1/cont12/chcA/cont12AB1/lf12AB1"), value("value for lf12AB1"));
77 values.put(prompt("/cont1/cont12/lst121/[entry]/lf1211"), value("value for lf12112", "2value for lf12112"));
78 values.put(prompt("/cont1/cont12/lst121/[entry]"), value("Y", "Y"));
79 values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211"), value("Y", "N", "Y", "N"));
80 values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211/[entry]"), value("Y", "Y", "Y", "Y"));
81 values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211/[entry]/lf12111"), value("5", "10", "21", "50"));
82 values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211/[entry]/lf12112"),
83 value("value for lf12112", "2value for lf12112", "3value for lf12112", "4value for lf12112"));
85 values.put(prompt("/cont1/cont12/lst121"), value("Y", "N"));
87 values.put(prompt("/cont1/cont12/lst122"), value("Y", "N"));
89 values.put(prompt("/cont1/lst11"), value("Y", "Y", "N"));
90 values.put(prompt("/cont1/lst11/[entry]"), value("Y", "Y", "Y"));
91 values.put(prompt("/cont1/lst11/[entry]/lf111"),
92 value("1value for lf111", "2value for lf111", "3value for lf111"));
94 values.put(prompt("/cont1/cont12/data"), value("<el1><el11>value</el11><el12>value1</el12></el1>"));
96 final List<ValueForMessage> valuesForMessages = new ArrayList<>();
97 valuesForMessages.add(new ValueForMessage("Y", "lst111", "[Y|N]"));
98 valuesForMessages.add(new ValueForMessage("Y", "lst121", "[Y|N]"));
99 valuesForMessages.add(new ValueForMessage("Y", "lst11", "[Y|N]"));
101 final ConsoleIOTestImpl console = new ConsoleIOTestImpl(values, valuesForMessages);
103 // final List<Node<?>> redData = new GenericReader(console, new CommandArgHandlerRegistry(console,
104 // new SchemaContextRegistry(schemaContext)), schemaContext).read(cont1);
105 // assertNotNull(redData);
106 // assertEquals(1, redData.size());
108 // assertTrue(redData.get(0) instanceof CompositeNode);
109 // final CompositeNode redTopLevelNode = (CompositeNode) redData.get(0);
111 //new NormalizedNodeWriter(console, new OutFormatter()).write(cont1, redData);
115 private static Deque<String> value(final String... values) {
116 return new ArrayDeque<>(Arrays.asList(values));
119 private static String prompt(final String path) {
120 return "/localhost" + path + PROMPT_SUFIX;
123 private static DataSchemaNode findTopLevelElement(final String namespace, final String revision,
124 final String topLevelElement, final SchemaContext schemaContext) {
125 final QName requiredElement = QName.create(namespace, revision, topLevelElement);
126 for (final DataSchemaNode dataSchemaNode : schemaContext.getChildNodes()) {
127 if (dataSchemaNode.getQName().equals(requiredElement)) {
128 return dataSchemaNode;