Merge "Kill Dynamic Actors when we're done with them"
[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     // @Ignore
65     @Test
66     public void cliTest() throws ReadingException, IOException, WriteException, URISyntaxException {
67
68         final SchemaContext schemaContext = loadSchemaContext("/schema-context");
69         assertNotNull(schemaContext);
70
71         final DataSchemaNode cont1 = findTopLevelElement("ns:model1", "2014-05-14", "cont1", schemaContext);
72         final Map<String, Deque<String>> values = new HashMap<>();
73
74         values.put(prompt("/cont1/cont11/lst111/[entry]/lf1111"), value("55", "32"));
75         values.put(prompt("/cont1/cont11/lst111/[entry]"), value("Y", "Y"));
76         values.put(prompt("/cont1/cont11/lst111/[entry]/cont111/lf1112"),
77                 value("value for lf1112", "2value for lf1112"));
78         values.put(prompt("/cont1/cont11/lst111/[entry]/cont111/lflst1111"), value("Y", "N", "Y", "N"));
79         values.put(prompt("/cont1/cont11/lst111/[entry]/cont111/lflst1111/[entry]"), value("10", "15", "20", "30"));
80
81         values.put(prompt("/cont1/cont11/lst111"), value("Y", "N"));
82
83         values.put(prompt("/cont1/cont12/chcA"), value("AB"));
84         values.put(prompt("/cont1/cont12/chcA/cont12AB1/lf12AB1"), value("value for lf12AB1"));
85
86         values.put(prompt("/cont1/cont12/lst121/[entry]/lf1211"), value("value for lf12112", "2value for lf12112"));
87         values.put(prompt("/cont1/cont12/lst121/[entry]"), value("Y", "Y"));
88         values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211"), value("Y", "N", "Y", "N"));
89         values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211/[entry]"), value("Y", "Y", "Y", "Y"));
90         values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211/[entry]/lf12111"), value("5", "10", "21", "50"));
91         values.put(prompt("/cont1/cont12/lst121/[entry]/lst1211/[entry]/lf12112"),
92                 value("value for lf12112", "2value for lf12112", "3value for lf12112", "4value for lf12112"));
93
94         values.put(prompt("/cont1/cont12/lst121"), value("Y", "N"));
95
96         values.put(prompt("/cont1/cont12/lst122"), value("Y", "N"));
97
98         values.put(prompt("/cont1/lst11"), value("Y", "Y", "N"));
99         values.put(prompt("/cont1/lst11/[entry]"), value("Y", "Y", "Y"));
100         values.put(prompt("/cont1/lst11/[entry]/lf111"),
101                 value("1value for lf111", "2value for lf111", "3value for lf111"));
102
103         values.put(prompt("/cont1/cont12/data"), value("<el1><el11>value</el11><el12>value1</el12></el1>"));
104
105         final List<ValueForMessage> valuesForMessages = new ArrayList<>();
106         valuesForMessages.add(new ValueForMessage("Y", "lst111", "[Y|N]"));
107         valuesForMessages.add(new ValueForMessage("Y", "lst121", "[Y|N]"));
108         valuesForMessages.add(new ValueForMessage("Y", "lst11", "[Y|N]"));
109
110         final ConsoleIOTestImpl console = new ConsoleIOTestImpl(values, valuesForMessages);
111
112         final List<Node<?>> redData = new GenericReader(console, new CommandArgHandlerRegistry(console,
113                 new SchemaContextRegistry(schemaContext)), schemaContext).read(cont1);
114         assertNotNull(redData);
115        assertEquals(1, redData.size());
116
117         assertTrue(redData.get(0) instanceof CompositeNode);
118         final CompositeNode redTopLevelNode = (CompositeNode) redData.get(0);
119
120         System.out.println("============================");
121         new NormalizedNodeWriter(console, new OutFormatter()).write(cont1, redData);
122
123     }
124
125     private Deque<String> value(final String... values) {
126         return new ArrayDeque<>(Arrays.asList(values));
127     }
128
129     private String prompt(final String path) {
130         return "/localhost" + path + PROMPT_SUFIX;
131     }
132
133     private DataSchemaNode findTopLevelElement(final String namespace, final String revision,
134             final String topLevelElement, final SchemaContext schemaContext) {
135         final QName requiredElement = QName.create(namespace, revision, topLevelElement);
136         for (final DataSchemaNode dataSchemaNode : schemaContext.getChildNodes()) {
137             if (dataSchemaNode.getQName().equals(requiredElement)) {
138                 return dataSchemaNode;
139             }
140         }
141         return null;
142
143     }
144
145 }