eb4961a662643d3ef9f7d73fa9f548836449da50
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema;
23
24 import java.text.ParseException;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 public class YangFilename {
31
32     private static final String REGEX = "([^\\/]*)@([0-9]{4}-[0-9]{2}-[0-9]{2}).yang";
33     private static final Pattern pattern = Pattern.compile(REGEX, Pattern.MULTILINE);
34     private final String filename;
35     private final Matcher matcher;
36     private Date revision;
37     private String module;
38
39     public static Date parseRevision(String sRevision) throws ParseException {
40         final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
41         return fmt.parse(sRevision);
42     }
43
44     public YangFilename(String fn) throws ParseException {
45         this.filename = fn;
46         matcher = pattern.matcher(this.filename);
47         if (!matcher.find()) {
48             throw new ParseException("unknown filename format", 0);
49         }
50         this.module = matcher.group(1);
51         this.revision = parseRevision(matcher.group(2));
52
53     }
54
55     public static String createFilename(String module, String rev) {
56         return String.format("%s@%s.yang", module, rev);
57     }
58
59     public YangFilename(String module, String rev) throws ParseException {
60         this(createFilename(module, rev));
61     }
62
63     public String getFilename() {
64         return this.filename;
65     }
66
67     public Date getRevision() {
68         return this.revision;
69     }
70
71     public String getModule() {
72         return this.module;
73     }
74 }