96833bd5070392d802a66eb2b70aa2fcd6010cbb
[controller.git] / opendaylight / config / yang-store-impl / src / test / java / org / opendaylight / controller / config / yang / store / impl / 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.config.yang.store.impl;
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
18 import org.apache.commons.io.IOUtils;
19 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
20 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
21 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
22
23 public class HardcodedYangStoreService implements YangStoreService {
24
25     private final Collection<ByteArrayInputStream> byteArrayInputStreams;
26
27     public HardcodedYangStoreService(
28             Collection<? extends InputStream> inputStreams)
29             throws YangStoreException, IOException {
30         byteArrayInputStreams = new ArrayList<>();
31         for (InputStream inputStream : inputStreams) {
32             assertNotNull(inputStream);
33             byte[] content = IOUtils.toByteArray(inputStream);
34             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
35                     content);
36             byteArrayInputStreams.add(byteArrayInputStream);
37         }
38     }
39
40     @Override
41     public YangStoreSnapshot getYangStoreSnapshot() throws YangStoreException {
42         for (InputStream inputStream : byteArrayInputStreams) {
43             try {
44                 inputStream.reset();
45             } catch (IOException e) {
46                 throw new RuntimeException(e);
47             }
48         }
49         return new MbeParser().parseYangFiles(byteArrayInputStreams);
50     }
51 }