1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.data;
21 import java.io.FileInputStream;
22 import java.io.FilenameFilter;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.nio.file.Path;
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.List;
33 import javax.annotation.Nonnull;
34 import javax.annotation.Nullable;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class YangFileProvider {
41 private static final Logger LOG = LoggerFactory.getLogger(YangFileProvider.class);
43 private static final FilenameFilter yangFilenameFilter = new FilenameFilter() {
46 public boolean accept(File dir, String name) {
47 return name.toLowerCase().endsWith(".yang");
51 private static final int BUFFER_SIZE = 1024;
53 private final Path mainSourcePath;
54 private final List<Path> additionalSources;
56 public YangFileProvider(String path) {
57 this.mainSourcePath = new File(path).toPath();
58 this.additionalSources = new ArrayList<>();
61 public boolean hasFileForModule(String module, String version) {
62 return this.mainSourcePath.resolve(YangFilename.createFilename(module, version)).toFile().exists();
65 public boolean hasFileForModule(String module) {
66 return this.findYangFiles(module).size() > 0;
69 private List<YangFilename> findYangFiles(String module) {
70 List<YangFilename> list = new ArrayList<>();
71 String[] files = this.mainSourcePath.toFile().list(yangFilenameFilter);
72 YangFilename yangfile;
73 for (String file : files) {
74 files = this.mainSourcePath.toFile().list(yangFilenameFilter);
75 for (String fn : files) {
77 yangfile = new YangFilename(this.mainSourcePath.resolve(fn).toString());
78 if (yangfile.getModule().equals(module)) {
81 } catch (ParseException e) {
82 LOG.warn("unable to handle yangfile {}: {}", file, e);
86 for (Path addPath : this.additionalSources) {
87 files = addPath.toFile().list(yangFilenameFilter);
88 for (String file : files) {
90 yangfile = new YangFilename(addPath.resolve(file).toString());
91 if (yangfile.getModule().equals(module)) {
94 } catch (ParseException e) {
95 LOG.warn("unable to handle yangfile {}: {}", file, e);
103 * get yang file from source with specified version or least newer one if
104 * version is null then the latest one
109 * @throws ParseException
111 private @Nullable YangFilename getYangFile(@Nonnull String module, @Nullable String version) throws ParseException {
112 YangFilename f = null;
113 List<YangFilename> list = this.findYangFiles(module);
115 list.sort(SortByDateAscComparator.getInstance());
117 // find specific version or nearest oldest
118 if (version != null) {
119 Date rev = YangFilename.parseRevision(version);
120 for (YangFilename item : list) {
121 if (rev.equals(item.getRevision())) {
125 if (item.getRevision().after(rev)) {
133 f = list.get(list.size() - 1);
139 * write filestream directly to output stream easier for http handling
143 * @param outputStream
145 * @throws IOException
146 * @throws ParseException
148 public int writeOutput(@Nonnull String module, @Nullable String version, @Nonnull OutputStream outputStream)
149 throws IOException, ParseException {
150 YangFilename fn = this.getYangFile(module, version);
154 byte[] buffer = new byte[BUFFER_SIZE];
157 InputStream inputStream = null ;
159 inputStream= new FileInputStream(fn.getFilename());
161 while ((bytesRead = inputStream.read(buffer)) != -1) {
162 outputStream.write(buffer, 0, bytesRead);
165 catch(IOException e) {
166 LOG.warn("problem sending {}: {}",fn.getFilename(),e);
169 if(inputStream!=null) {
176 private static class SortByDateAscComparator implements Comparator<YangFilename> {
178 private static SortByDateAscComparator instance;
181 public int compare(YangFilename o1, YangFilename o2) {
182 return o1.getRevision().compareTo(o2.getRevision());
185 public static Comparator<? super YangFilename> getInstance() {
186 if (instance == null) {
187 instance = new SortByDateAscComparator();
194 public YangFilename getFileForModule(String module, String rev) throws ParseException {
195 return this.getYangFile(module, rev);
198 public YangFilename getFileForModule(String module) throws ParseException {
199 return this.getFileForModule(module, null);
202 public boolean hasFileOrNewerForModule(String module, String version) throws ParseException {
203 return this.getYangFile(module, version) != null;