3a5bdd952e1e6187ee07637bde34cf5353f09083
[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
67     @Getter
68     public static class TypeId {
69         private final String name;
70         private final String version;
71
72         public TypeId(String name, String version) {
73             this.name = name;
74             this.version = version;
75         }
76
77         public static TypeId ofString(String typeId) {
78             StringBuilder name = new StringBuilder();
79             String version = "";
80             String[] tokens = typeId.split("_");
81
82             if (tokens.length >= 2) {
83                 version = tokens[tokens.length - 1]; // Last token
84                 for (int i = 0; i < tokens.length - 1; ++i) {
85                     if (i != 0) {
86                         name.append("_");
87                     }
88                     name.append(tokens[i]); // All other tokens
89                 }
90                 return new TypeId(name.toString(), version);
91             } else {
92                 return new TypeId(typeId, "");
93             }
94         }
95     }
96
97     public TypeId getTypeId() {
98         return TypeId.ofString(getId());
99     }
100
101 }