Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / HardcodedYangStoreService.java
1 /*
2  * Copyright (c) 2013 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.it;
9
10 import static org.junit.Assert.assertNotNull;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.List;
19
20 import org.apache.commons.io.IOUtils;
21 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
22 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreService;
23 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
24 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28
29 public class HardcodedYangStoreService implements YangStoreService {
30
31     private final List<InputStream> byteArrayInputStreams;
32
33     public HardcodedYangStoreService(
34             Collection<? extends InputStream> inputStreams)
35             throws YangStoreException, IOException {
36         byteArrayInputStreams = new ArrayList<>();
37         for (InputStream inputStream : inputStreams) {
38             assertNotNull(inputStream);
39             byte[] content = IOUtils.toByteArray(inputStream);
40             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
41                     content);
42             byteArrayInputStreams.add(byteArrayInputStream);
43         }
44     }
45
46     @Override
47     public YangStoreSnapshot getYangStoreSnapshot() throws YangStoreException {
48         for (InputStream inputStream : byteArrayInputStreams) {
49             try {
50                 inputStream.reset();
51             } catch (IOException e) {
52                 throw new RuntimeException(e);
53             }
54         }
55
56         YangParserImpl yangParser = new YangParserImpl();
57         final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(byteArrayInputStreams).values()));
58         YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
59             @Override
60             public SchemaContext getSchemaContext() {
61                 return schemaContext ;
62             }
63         });
64         return yangStoreService.getYangStoreSnapshot();
65     }
66 }