aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/yangschema/YangFileProvider.java
blob: c6758e240fe015e4fd292716191302244d3cbfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class YangFileProvider {

    private static final Logger LOG = LoggerFactory.getLogger(YangFileProvider.class);

    private static final FilenameFilter yangFilenameFilter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".yang");
        }
    };

    private static final int BUFFER_SIZE = 1024;

    private final Path mainSourcePath;
    private final List<Path> additionalSources;

    public YangFileProvider(String path) {
        this.mainSourcePath = new File(path).toPath();
        this.additionalSources = new ArrayList<>();
    }

    public boolean hasFileForModule(String module, String version) {
        return this.mainSourcePath.resolve(YangFilename.createFilename(module, version)).toFile().exists();
    }

    public boolean hasFileForModule(String module) {
        return this.findYangFiles(module).size() > 0;
    }

    private List<YangFilename> findYangFiles(String module) {
        LOG.debug("try to find yang files for {}", module);
        List<YangFilename> list = new ArrayList<>();
        String[] files = this.mainSourcePath.toFile().list(yangFilenameFilter);
        YangFilename yangfile;
        for (String fn : files) {
            try {
                yangfile = new YangFilename(this.mainSourcePath.resolve(fn).toString());
                if (yangfile.getModule().equals(module)) {
                    list.add(yangfile);
                }
            } catch (ParseException e) {
                LOG.warn("unable to handle yangfile {}: {}", fn, e);
            }
        }

        for (Path addPath : this.additionalSources) {
            files = addPath.toFile().list(yangFilenameFilter);
            for (String file : files) {
                try {
                    yangfile = new YangFilename(addPath.resolve(file).toString());
                    if (yangfile.getModule().equals(module)) {
                        list.add(yangfile);
                    }
                } catch (ParseException e) {
                    LOG.warn("unable to handle yangfile {}: {}", file, e);
                }
            }
        }
        return list;
    }

    /**
     * get yang file from source with specified version or least newer one if version is null then the latest one
     *
     * @param module
     * @param version
     * @return
     * @throws ParseException
     */
    private @Nullable YangFilename getYangFile(@Nonnull String module, @Nullable String version) throws ParseException {
        YangFilename f = null;
        List<YangFilename> list = this.findYangFiles(module);

        list.sort(SortByDateAscComparator.getInstance());

        // find specific version or nearest oldest
        if (version != null) {
            Date rev = YangFilename.parseRevision(version);
            for (YangFilename item : list) {
                if (rev.equals(item.getRevision())) {
                    f = item;
                    break;
                }
                if (item.getRevision().after(rev)) {
                    f = item;
                    break;
                }
            }
        }
        // get latest
        else {
            f = list.get(list.size() - 1);
        }
        return f;
    }

    /**
     * write filestream directly to output stream easier for http handling
     *
     * @param module
     * @param version
     * @param outputStream
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public int writeOutput(@Nonnull String module, @Nullable String version, @Nonnull OutputStream outputStream)
            throws IOException, ParseException {
        YangFilename fn = this.getYangFile(module, version);
        if (fn == null) {
            return 0;
        }
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        int sumlen = 0;
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(fn.getFilename());

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
                sumlen += bytesRead;
            }
        } catch (IOException e) {
            LOG.warn("problem sending {}: {}", fn.getFilename(), e);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return sumlen;
    }

    private static class SortByDateAscComparator implements Comparator<YangFilename> {

        private static SortByDateAscComparator instance;

        @Override
        public int compare(YangFilename o1, YangFilename o2) {
            return o1.getRevision().compareTo(o2.getRevision());
        }

        public static Comparator<YangFilename> getInstance() {
            if (instance == null) {
                instance = new SortByDateAscComparator();
            }
            return instance;
        }

    }

    public YangFilename getFileForModule(String module, String rev) throws ParseException {
        return this.getYangFile(module, rev);
    }

    public YangFilename getFileForModule(String module) throws ParseException {
        return this.getFileForModule(module, null);
    }

    public boolean hasFileOrNewerForModule(String module, String version) throws ParseException {
        return this.getYangFile(module, version) != null;
    }

}