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