Fixed tag for <nexus.repository.snapshot>
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / TestUtils.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.sal.restconf.impl.test;
9
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;
15
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;
20 import java.io.File;
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;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.sql.Date;
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;
34 import java.util.Map;
35 import java.util.Set;
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;
83
84 public final class TestUtils {
85
86     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
87
88     private final static YangContextParser parser = new YangParserImpl();
89
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);
96         }
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));
101             }
102         }
103         return parser.parseYangModels(testFiles);
104     }
105
106     public static Set<Module> loadModulesFrom(String yangPath) {
107         try {
108             return TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath());
109         } catch (FileNotFoundException e) {
110             LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
111         }
112
113         return null;
114     }
115
116     public static SchemaContext loadSchemaContext(Set<Module> modules) {
117         return parser.resolveSchemaContext(modules);
118     }
119
120     public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException {
121         return parser.resolveSchemaContext(loadModulesFrom(resourceDirectory));
122     }
123
124     public static Module findModule(Set<Module> modules, String moduleName) {
125         for (Module module : modules) {
126             if (module.getName().equals(moduleName)) {
127                 return module;
128             }
129         }
130         return null;
131     }
132
133     public static Document loadDocumentFrom(InputStream inputStream) {
134         try {
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);
140             return null;
141         }
142     }
143
144     public static String getDocumentInPrintableForm(Document doc) {
145         Preconditions.checkNotNull(doc);
146         try {
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");
155
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";
161             LOG.error(msg, e);
162             return msg;
163         }
164
165     }
166
167     /**
168      *
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}
172      */
173     public static void normalizeCompositeNode(Node<?> node, Set<Module> modules, String schemaNodePath) {
174         RestconfImpl restconf = RestconfImpl.getInstance();
175         ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules));
176
177         prepareMocksForRestconf(modules, restconf);
178         restconf.updateConfigurationData(schemaNodePath, node);
179     }
180
181     /**
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.
184      *
185      */
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)) {
191                     return m;
192                 }
193             }
194         } else if (modules.size() == 1) {
195             return modules.iterator().next();
196         }
197         return null;
198     }
199
200     public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) {
201         assertNotNull("Module can't be null", module);
202
203         if (searchedDataSchemaName != null) {
204             for (DataSchemaNode dsn : module.getChildNodes()) {
205                 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
206                     return dsn;
207                 }
208             }
209         } else if (module.getChildNodes().size() == 1) {
210             return module.getChildNodes().iterator().next();
211         }
212         return null;
213     }
214
215     public static QName buildQName(String name, String uri, String date, String prefix) {
216         try {
217             URI u = new URI(uri);
218             Date dt = null;
219             if (date != null) {
220                 dt = Date.valueOf(date);
221             }
222             return new QName(u, dt, prefix, name);
223         } catch (URISyntaxException e) {
224             return null;
225         }
226     }
227
228     public static QName buildQName(String name, String uri, String date) {
229         return buildQName(name, uri, date, null);
230     }
231
232     public static QName buildQName(String name) {
233         return buildQName(name, "", null);
234     }
235
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);
241             }
242         }
243     }
244
245     private static void prepareMocksForRestconf(Set<Module> modules, RestconfImpl restconf) {
246         ControllerContext controllerContext = ControllerContext.getInstance();
247         BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
248
249         controllerContext.setSchemas(TestUtils.loadSchemaContext(modules));
250
251         when(mockedBrokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
252                 .thenReturn(mock(CheckedFuture.class));
253
254         restconf.setControllerContext(controllerContext);
255         restconf.setBroker(mockedBrokerFacade);
256     }
257
258     public static Node<?> readInputToCnSn(String path, boolean dummyNamespaces,
259             MessageBodyReader<Node<?>> reader) throws WebApplicationException {
260
261         InputStream inputStream = TestUtils.class.getResourceAsStream(path);
262         try {
263             final Node<?> node = reader.readFrom(null, null, null, null, null, inputStream);
264             assertTrue(node instanceof CompositeNodeWrapper);
265             if (dummyNamespaces) {
266                 try {
267                     TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) node);
268                     return ((CompositeNodeWrapper) node).unwrap();
269                 } catch (URISyntaxException e) {
270                     LOG.error(e.getMessage());
271                     assertTrue(e.getMessage(), false);
272                 }
273             }
274             return node;
275         } catch (IOException e) {
276             LOG.error(e.getMessage());
277             assertTrue(e.getMessage(), false);
278         }
279         return null;
280     }
281
282 //    public static Node<?> readInputToCnSnNew(String path, MessageBodyReader<Node<?>> reader) throws WebApplicationException {
283 //        InputStream inputStream = TestUtils.class.getResourceAsStream(path);
284 //        try {
285 //            return reader.readFrom(null, null, null, null, null, inputStream);
286 //        } catch (IOException e) {
287 //            LOG.error(e.getMessage());
288 //            assertTrue(e.getMessage(), false);
289 //        }
290 //        return null;
291 //    }
292
293     public static Node<?> readInputToCnSn(String path, MessageBodyReader<Node<?>> reader) {
294         return readInputToCnSn(path, false, reader);
295     }
296
297     public static String writeCompNodeWithSchemaContextToOutput(Node<?> node, Set<Module> modules,
298             DataSchemaNode dataSchemaNode, MessageBodyWriter<StructuredData> messageBodyWriter) throws IOException,
299             WebApplicationException {
300
301         assertNotNull(dataSchemaNode);
302         assertNotNull("Composite node can't be null", node);
303         ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
304
305         ControllerContext.getInstance().setSchemas(loadSchemaContext(modules));
306
307         assertTrue(node instanceof CompositeNode);
308         messageBodyWriter.writeTo(new StructuredData((CompositeNode)node, dataSchemaNode, null), null, null, null, null,
309                 null, byteArrayOS);
310
311         return byteArrayOS.toString();
312     }
313
314     public static String loadTextFile(String filePath) throws IOException {
315         FileReader fileReader = new FileReader(filePath);
316         BufferedReader bufReader = new BufferedReader(fileReader);
317
318         String line = null;
319         StringBuilder result = new StringBuilder();
320         while ((line = bufReader.readLine()) != null) {
321             result.append(line);
322         }
323         bufReader.close();
324         return result.toString();
325     }
326
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*");
333         }
334         pattern.append(".*");
335         return Pattern.compile(pattern.toString(), Pattern.DOTALL);
336     }
337
338     public static boolean containsStringData(String jsonOutput, String... substrings) {
339         Pattern pattern = patternForStringsSeparatedByWhiteChars(substrings);
340         Matcher matcher = pattern.matcher(jsonOutput);
341         return matcher.matches();
342     }
343
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);
354         }
355
356         LOG.error("Top level isn't of type container, list, leaf schema node but " + schema.getClass().getSimpleName());
357
358         throw new RestconfDocumentedException(new RestconfError(ErrorType.APPLICATION, ErrorTag.INVALID_VALUE,
359                 "It wasn't possible to translate specified data to datastore readable form."));
360     }
361
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));
365     }
366
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));
372         }
373
374         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(
375
376         QName.create(namespace, revision, localName), predicate);
377     }
378
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)");
384         }
385         Map<QName, Object> predicate = new HashMap<>();
386
387         int i = 0;
388         while (i < keysAndValues.length) {
389             predicate.put(QName.create(namespace, revision, keysAndValues[i++]), keysAndValues[i++]);
390         }
391
392         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(namespace, revision, localName),
393                 predicate);
394     }
395
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();
403     }
404
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();
409
410         Map<String, Object> predicates = new HashMap<>();
411         predicates.put("name", "eth0");
412
413         mapEntryNode.withNodeIdentifier(getNodeIdentifierPredicate("interface", namespace, ietfInterfacesDate,
414                 predicates));
415         mapEntryNode
416                 .withChild(new ImmutableLeafNodeBuilder<String>()
417                         .withNodeIdentifier(getNodeIdentifier("name", namespace, ietfInterfacesDate)).withValue("eth0")
418                         .build());
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());
428
429         return mapEntryNode.build();
430     }
431 }