44240b07f53e8fcbf917208435b7ffbfc21e8b2a
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.data;
19
20 import java.io.File;
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;
32
33 import javax.annotation.Nonnull;
34 import javax.annotation.Nullable;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class YangFileProvider {
40
41         private static final Logger LOG = LoggerFactory.getLogger(YangFileProvider.class);
42
43         private static final FilenameFilter yangFilenameFilter = new FilenameFilter() {
44
45                 @Override
46                 public boolean accept(File dir, String name) {
47                         return name.toLowerCase().endsWith(".yang");
48                 }
49         };
50
51         private static final int BUFFER_SIZE = 1024;
52
53         private final Path mainSourcePath;
54         private final List<Path> additionalSources;
55
56         public YangFileProvider(String path) {
57                 this.mainSourcePath = new File(path).toPath();
58                 this.additionalSources = new ArrayList<>();
59         }
60
61         public boolean hasFileForModule(String module, String version) {
62                 return this.mainSourcePath.resolve(YangFilename.createFilename(module, version)).toFile().exists();
63         }
64
65         public boolean hasFileForModule(String module) {
66                 return this.findYangFiles(module).size() > 0;
67         }
68
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) {
76                                 try {
77                                         yangfile = new YangFilename(this.mainSourcePath.resolve(fn).toString());
78                                         if (yangfile.getModule().equals(module)) {
79                                                 list.add(yangfile);
80                                         }
81                                 } catch (ParseException e) {
82                                         LOG.warn("unable to handle yangfile {}: {}", file, e);
83                                 }
84                         }
85                 }
86                 for (Path addPath : this.additionalSources) {
87                         files = addPath.toFile().list(yangFilenameFilter);
88                         for (String file : files) {
89                                 try {
90                                         yangfile = new YangFilename(addPath.resolve(file).toString());
91                                         if (yangfile.getModule().equals(module)) {
92                                                 list.add(yangfile);
93                                         }
94                                 } catch (ParseException e) {
95                                         LOG.warn("unable to handle yangfile {}: {}", file, e);
96                                 }
97                         }
98                 }
99                 return list;
100         }
101
102         /**
103          * get yang file from source with specified version or least newer one if
104          * version is null then the latest one
105          * 
106          * @param module
107          * @param version
108          * @return
109          * @throws ParseException
110          */
111         private @Nullable YangFilename getYangFile(@Nonnull String module, @Nullable String version) throws ParseException {
112                 YangFilename f = null;
113                 List<YangFilename> list = this.findYangFiles(module);
114
115                 list.sort(SortByDateAscComparator.getInstance());
116
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())) {
122                                         f = item;
123                                         break;
124                                 }
125                                 if (item.getRevision().after(rev)) {
126                                         f = item;
127                                         break;
128                                 }
129                         }
130                 }
131                 // get latest
132                 else {
133                         f = list.get(list.size() - 1);
134                 }
135                 return f;
136         }
137
138         /**
139          * write filestream directly to output stream easier for http handling
140          * 
141          * @param module
142          * @param version
143          * @param outputStream
144          * @return
145          * @throws IOException
146          * @throws ParseException
147          */
148         public int writeOutput(@Nonnull String module, @Nullable String version, @Nonnull OutputStream outputStream)
149                         throws IOException, ParseException {
150                 YangFilename fn = this.getYangFile(module, version);
151                 if(fn==null) {
152                         return 0;
153                 }
154                 byte[] buffer = new byte[BUFFER_SIZE];
155                 int bytesRead = -1;
156                 int sumlen = 0;
157                 InputStream inputStream = null ;
158                 try {
159                         inputStream= new FileInputStream(fn.getFilename());
160
161                 while ((bytesRead = inputStream.read(buffer)) != -1) {
162                         outputStream.write(buffer, 0, bytesRead);
163                         sumlen += bytesRead;
164                 }}
165                 catch(IOException e) {
166                         LOG.warn("problem sending {}: {}",fn.getFilename(),e);
167                 }
168                 finally {
169                         if(inputStream!=null) {
170                                 inputStream.close();            
171                         }
172                 }
173                 return sumlen;
174         }
175
176         private static class SortByDateAscComparator implements Comparator<YangFilename> {
177
178                 private static SortByDateAscComparator instance;
179
180                 @Override
181                 public int compare(YangFilename o1, YangFilename o2) {
182                         return o1.getRevision().compareTo(o2.getRevision());
183                 }
184
185                 public static Comparator<? super YangFilename> getInstance() {
186                         if (instance == null) {
187                                 instance = new SortByDateAscComparator();
188                         }
189                         return instance;
190                 }
191
192         }
193
194         public YangFilename getFileForModule(String module, String rev) throws ParseException {
195                 return this.getYangFile(module, rev);
196         }
197
198         public YangFilename getFileForModule(String module) throws ParseException {
199                 return this.getFileForModule(module, null);
200         }
201
202         public boolean hasFileOrNewerForModule(String module, String version) throws ParseException {
203                 return this.getYangFile(module, version) != null;
204         }
205
206 }