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