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.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import com.google.common.base.Preconditions;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import java.io.BufferedReader;
19 import java.io.ByteArrayOutputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStreamWriter;
27 import java.net.URISyntaxException;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.ext.MessageBodyReader;
40 import javax.ws.rs.ext.MessageBodyWriter;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43 import javax.xml.parsers.ParserConfigurationException;
44 import javax.xml.transform.OutputKeys;
45 import javax.xml.transform.Transformer;
46 import javax.xml.transform.TransformerException;
47 import javax.xml.transform.TransformerFactory;
48 import javax.xml.transform.dom.DOMSource;
49 import javax.xml.transform.stream.StreamResult;
50 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
51 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
52 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
53 import org.opendaylight.controller.sal.restconf.impl.NodeWrapper;
54 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
55 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
56 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
57 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
58 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
59 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
62 import org.opendaylight.yangtools.yang.data.api.Node;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.opendaylight.yangtools.yang.data.composite.node.schema.cnsn.parser.CnSnToNormalizedNodeParserFactory;
67 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
68 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
69 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
70 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
71 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
72 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.Module;
76 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
77 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
78 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81 import org.w3c.dom.Document;
82 import org.xml.sax.SAXException;
84 public final class TestUtils {
86 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
88 private final static YangContextParser parser = new YangParserImpl();
90 private static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
91 final File testDir = new File(resourceDirectory);
92 final String[] fileList = testDir.list();
93 final List<File> testFiles = new ArrayList<File>();
94 if (fileList == null) {
95 throw new FileNotFoundException(resourceDirectory);
97 for (int i = 0; i < fileList.length; i++) {
98 String fileName = fileList[i];
99 if (new File(testDir, fileName).isDirectory() == false) {
100 testFiles.add(new File(testDir, fileName));
103 return parser.parseYangModels(testFiles);
106 public static Set<Module> loadModulesFrom(String yangPath) {
108 return TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath());
109 } catch (FileNotFoundException e) {
110 LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
116 public static SchemaContext loadSchemaContext(Set<Module> modules) {
117 return parser.resolveSchemaContext(modules);
120 public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException {
121 return parser.resolveSchemaContext(loadModulesFrom(resourceDirectory));
124 public static Module findModule(Set<Module> modules, String moduleName) {
125 for (Module module : modules) {
126 if (module.getName().equals(moduleName)) {
133 public static Document loadDocumentFrom(InputStream inputStream) {
135 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
136 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
137 return docBuilder.parse(inputStream);
138 } catch (SAXException | IOException | ParserConfigurationException e) {
139 LOG.error("Error during loading Document from XML", e);
144 public static String getDocumentInPrintableForm(Document doc) {
145 Preconditions.checkNotNull(doc);
147 ByteArrayOutputStream out = new ByteArrayOutputStream();
148 TransformerFactory tf = TransformerFactory.newInstance();
149 Transformer transformer = tf.newTransformer();
150 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
151 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
152 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
153 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
154 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
156 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
157 byte[] charData = out.toByteArray();
158 return new String(charData, "UTF-8");
159 } catch (IOException | TransformerException e) {
160 String msg = "Error during transformation of Document into String";
169 * Fill missing data (namespaces) and build correct data type in {@code compositeNode} according to
170 * {@code dataSchemaNode}. The method {@link RestconfImpl#createConfigurationData createConfigurationData} is used
171 * because it contains calling of method {code normalizeNode}
173 public static void normalizeCompositeNode(Node<?> node, Set<Module> modules, String schemaNodePath) {
174 RestconfImpl restconf = RestconfImpl.getInstance();
175 ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules));
177 prepareMocksForRestconf(modules, restconf);
178 restconf.updateConfigurationData(schemaNodePath, node);
182 * Searches module with name {@code searchedModuleName} in {@code modules}. If module name isn't specified and
183 * module set has only one element then this element is returned.
186 public static Module resolveModule(String searchedModuleName, Set<Module> modules) {
187 assertNotNull("Modules can't be null.", modules);
188 if (searchedModuleName != null) {
189 for (Module m : modules) {
190 if (m.getName().equals(searchedModuleName)) {
194 } else if (modules.size() == 1) {
195 return modules.iterator().next();
200 public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) {
201 assertNotNull("Module can't be null", module);
203 if (searchedDataSchemaName != null) {
204 for (DataSchemaNode dsn : module.getChildNodes()) {
205 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
209 } else if (module.getChildNodes().size() == 1) {
210 return module.getChildNodes().iterator().next();
215 public static QName buildQName(String name, String uri, String date, String prefix) {
217 URI u = new URI(uri);
220 dt = Date.valueOf(date);
222 return new QName(u, dt, prefix, name);
223 } catch (URISyntaxException e) {
228 public static QName buildQName(String name, String uri, String date) {
229 return buildQName(name, uri, date, null);
232 public static QName buildQName(String name) {
233 return buildQName(name, "", null);
236 private static void addDummyNamespaceToAllNodes(NodeWrapper<?> wrappedNode) throws URISyntaxException {
237 wrappedNode.setNamespace(new URI(""));
238 if (wrappedNode instanceof CompositeNodeWrapper) {
239 for (NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
240 addDummyNamespaceToAllNodes(childNodeWrapper);
245 private static void prepareMocksForRestconf(Set<Module> modules, RestconfImpl restconf) {
246 ControllerContext controllerContext = ControllerContext.getInstance();
247 BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
249 controllerContext.setSchemas(TestUtils.loadSchemaContext(modules));
251 when(mockedBrokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
252 .thenReturn(mock(CheckedFuture.class));
254 restconf.setControllerContext(controllerContext);
255 restconf.setBroker(mockedBrokerFacade);
258 public static Node<?> readInputToCnSn(String path, boolean dummyNamespaces,
259 MessageBodyReader<Node<?>> reader) throws WebApplicationException {
261 InputStream inputStream = TestUtils.class.getResourceAsStream(path);
263 final Node<?> node = reader.readFrom(null, null, null, null, null, inputStream);
264 assertTrue(node instanceof CompositeNodeWrapper);
265 if (dummyNamespaces) {
267 TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) node);
268 return ((CompositeNodeWrapper) node).unwrap();
269 } catch (URISyntaxException e) {
270 LOG.error(e.getMessage());
271 assertTrue(e.getMessage(), false);
275 } catch (IOException e) {
276 LOG.error(e.getMessage());
277 assertTrue(e.getMessage(), false);
282 // public static Node<?> readInputToCnSnNew(String path, MessageBodyReader<Node<?>> reader) throws WebApplicationException {
283 // InputStream inputStream = TestUtils.class.getResourceAsStream(path);
285 // return reader.readFrom(null, null, null, null, null, inputStream);
286 // } catch (IOException e) {
287 // LOG.error(e.getMessage());
288 // assertTrue(e.getMessage(), false);
293 public static Node<?> readInputToCnSn(String path, MessageBodyReader<Node<?>> reader) {
294 return readInputToCnSn(path, false, reader);
297 public static String writeCompNodeWithSchemaContextToOutput(Node<?> node, Set<Module> modules,
298 DataSchemaNode dataSchemaNode, MessageBodyWriter<StructuredData> messageBodyWriter) throws IOException,
299 WebApplicationException {
301 assertNotNull(dataSchemaNode);
302 assertNotNull("Composite node can't be null", node);
303 ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
305 ControllerContext.getInstance().setSchemas(loadSchemaContext(modules));
307 assertTrue(node instanceof CompositeNode);
308 messageBodyWriter.writeTo(new StructuredData((CompositeNode)node, dataSchemaNode, null), null, null, null, null,
311 return byteArrayOS.toString();
314 public static String loadTextFile(String filePath) throws IOException {
315 FileReader fileReader = new FileReader(filePath);
316 BufferedReader bufReader = new BufferedReader(fileReader);
319 StringBuilder result = new StringBuilder();
320 while ((line = bufReader.readLine()) != null) {
324 return result.toString();
327 private static Pattern patternForStringsSeparatedByWhiteChars(String... substrings) {
328 StringBuilder pattern = new StringBuilder();
329 pattern.append(".*");
330 for (String substring : substrings) {
331 pattern.append(substring);
332 pattern.append("\\s*");
334 pattern.append(".*");
335 return Pattern.compile(pattern.toString(), Pattern.DOTALL);
338 public static boolean containsStringData(String jsonOutput, String... substrings) {
339 Pattern pattern = patternForStringsSeparatedByWhiteChars(substrings);
340 Matcher matcher = pattern.matcher(jsonOutput);
341 return matcher.matches();
344 public static NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compositeNode,
345 final DataSchemaNode schema) {
346 List<Node<?>> lst = new ArrayList<Node<?>>();
347 lst.add(compositeNode);
348 if (schema instanceof ContainerSchemaNode) {
349 return CnSnToNormalizedNodeParserFactory.getInstance().getContainerNodeParser()
350 .parse(lst, (ContainerSchemaNode) schema);
351 } else if (schema instanceof ListSchemaNode) {
352 return CnSnToNormalizedNodeParserFactory.getInstance().getMapNodeParser()
353 .parse(lst, (ListSchemaNode) schema);
356 LOG.error("Top level isn't of type container, list, leaf schema node but " + schema.getClass().getSimpleName());
358 throw new RestconfDocumentedException(new RestconfError(ErrorType.APPLICATION, ErrorTag.INVALID_VALUE,
359 "It wasn't possible to translate specified data to datastore readable form."));
362 public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName, String namespace,
363 String revision) throws ParseException {
364 return new YangInstanceIdentifier.NodeIdentifier(QName.create(namespace, revision, localName));
367 public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(String localName,
368 String namespace, String revision, Map<String, Object> keys) throws ParseException {
369 Map<QName, Object> predicate = new HashMap<>();
370 for (String key : keys.keySet()) {
371 predicate.put(QName.create(namespace, revision, key), keys.get(key));
374 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(
376 QName.create(namespace, revision, localName), predicate);
379 public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(String localName,
380 String namespace, String revision, String... keysAndValues) throws ParseException {
381 java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(revision);
382 if (keysAndValues.length % 2 != 0) {
383 new IllegalArgumentException("number of keys argument have to be divisible by 2 (map)");
385 Map<QName, Object> predicate = new HashMap<>();
388 while (i < keysAndValues.length) {
389 predicate.put(QName.create(namespace, revision, keysAndValues[i++]), keysAndValues[i++]);
392 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(namespace, revision, localName),
396 public static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
397 CompositeNodeBuilder<ImmutableCompositeNode> interfaceBuilder = ImmutableCompositeNode.builder();
398 interfaceBuilder.addLeaf(buildQName("name", "dummy", "2014-07-29"), "eth0");
399 interfaceBuilder.addLeaf(buildQName("type", "dummy", "2014-07-29"), "ethernetCsmacd");
400 interfaceBuilder.addLeaf(buildQName("enabled", "dummy", "2014-07-29"), "false");
401 interfaceBuilder.addLeaf(buildQName("description", "dummy", "2014-07-29"), "some interface");
402 return interfaceBuilder.toInstance();
405 static NormalizedNode<?,?> prepareNormalizedNodeWithIetfInterfacesInterfacesData() throws ParseException {
406 String ietfInterfacesDate = "2013-07-04";
407 String namespace = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
408 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryNode = ImmutableMapEntryNodeBuilder.create();
410 Map<String, Object> predicates = new HashMap<>();
411 predicates.put("name", "eth0");
413 mapEntryNode.withNodeIdentifier(getNodeIdentifierPredicate("interface", namespace, ietfInterfacesDate,
416 .withChild(new ImmutableLeafNodeBuilder<String>()
417 .withNodeIdentifier(getNodeIdentifier("name", namespace, ietfInterfacesDate)).withValue("eth0")
419 mapEntryNode.withChild(new ImmutableLeafNodeBuilder<String>()
420 .withNodeIdentifier(getNodeIdentifier("type", namespace, ietfInterfacesDate))
421 .withValue("ethernetCsmacd").build());
422 mapEntryNode.withChild(new ImmutableLeafNodeBuilder<Boolean>()
423 .withNodeIdentifier(getNodeIdentifier("enabled", namespace, ietfInterfacesDate))
424 .withValue(Boolean.FALSE).build());
425 mapEntryNode.withChild(new ImmutableLeafNodeBuilder<String>()
426 .withNodeIdentifier(getNodeIdentifier("description", namespace, ietfInterfacesDate))
427 .withValue("some interface").build());
429 return mapEntryNode.build();