14c9f58092325cf6e5f66624140a91b8fbea8a38
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.repository;
22
23 import lombok.Builder;
24 import lombok.Getter;
25
26 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
27 import org.springframework.http.HttpStatus;
28
29 @Builder
30 public class PolicyType {
31     @Getter
32     private String id;
33     @Getter
34     private String schema;
35
36     @Getter
37     public static class Version {
38         public final int major;
39         public final int minor;
40         public final int patch;
41
42         public Version(int major, int minor, int patch) {
43             this.major = major;
44             this.minor = minor;
45             this.patch = patch;
46         }
47
48         public static Version ofString(String version) throws ServiceException {
49             String[] versionTokenized = version.split("\\.");
50             if (versionTokenized.length != 3) {
51                 throw new ServiceException("Version must contain major.minor.patch code: " + version,
52                         HttpStatus.BAD_REQUEST);
53             }
54
55             try {
56                 return new Version( //
57                         Integer.parseInt(versionTokenized[0]), //
58                         Integer.parseInt(versionTokenized[1]), //
59                         Integer.parseInt(versionTokenized[2]) //
60                 );
61             } catch (Exception e) {
62                 throw new ServiceException("Syntax error in " + version, HttpStatus.BAD_REQUEST);
63             }
64         }
65
66         public int compareTo(Version other) {
67             if (major != other.major)
68                 return major - other.major;
69             if (minor != other.minor)
70                 return minor - other.minor;
71             return patch - other.patch;
72         }
73
74         public boolean isCompatibleWith(Version other) {
75             return (major == other.major && minor >= other.minor);
76         }
77     }
78
79     @Getter
80     public static class TypeId {
81         private final String name;
82         private final String version;
83
84         public TypeId(String name, String version) {
85             this.name = name;
86             this.version = version;
87         }
88
89         public static TypeId ofString(String typeId) {
90             StringBuilder name = new StringBuilder();
91             String version = "";
92             String[] tokens = typeId.split("_");
93
94             if (tokens.length >= 2) {
95                 version = tokens[tokens.length - 1]; // Last token
96                 for (int i = 0; i < tokens.length - 1; ++i) {
97                     if (i != 0) {
98                         name.append("_");
99                     }
100                     name.append(tokens[i]); // All other tokens
101                 }
102                 return new TypeId(name.toString(), version);
103             } else {
104                 return new TypeId(typeId, "");
105             }
106         }
107     }
108
109     public TypeId getTypeId() {
110         return TypeId.ofString(getId());
111     }
112
113     public Version getVersion() {
114         try {
115             return Version.ofString(getTypeId().getVersion());
116         } catch (ServiceException e) {
117             return new Version(0, 0, 0);
118         }
119     }
120
121 }