2  * ========================LICENSE_START=================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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===================================
 
  21 package org.onap.ccsdk.oran.a1policymanagementservice.repository;
 
  23 import lombok.Builder;
 
  26 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
 
  27 import org.springframework.http.HttpStatus;
 
  30 public class PolicyType {
 
  34     private String schema;
 
  37     public static class Version {
 
  38         public final int major;
 
  39         public final int minor;
 
  40         public final int patch;
 
  42         public Version(int major, int minor, int patch) {
 
  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);
 
  56                 return new Version( //
 
  57                         Integer.parseInt(versionTokenized[0]), //
 
  58                         Integer.parseInt(versionTokenized[1]), //
 
  59                         Integer.parseInt(versionTokenized[2]) //
 
  61             } catch (Exception e) {
 
  62                 throw new ServiceException("Syntax error in " + version, HttpStatus.BAD_REQUEST);
 
  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;
 
  74         public boolean isCompatibleWith(Version other) {
 
  75             return (major == other.major && minor >= other.minor);
 
  80     public static class TypeId {
 
  81         private final String name;
 
  82         private final String version;
 
  84         public TypeId(String name, String version) {
 
  86             this.version = version;
 
  89         public static TypeId ofString(String typeId) {
 
  90             StringBuilder name = new StringBuilder();
 
  92             String[] tokens = typeId.split("_");
 
  94             if (tokens.length >= 2) {
 
  95                 version = tokens[tokens.length - 1]; // Last token
 
  96                 for (int i = 0; i < tokens.length - 1; ++i) {
 
 100                     name.append(tokens[i]); // All other tokens
 
 102                 return new TypeId(name.toString(), version);
 
 104                 return new TypeId(typeId, "");
 
 109     public TypeId getTypeId() {
 
 110         return TypeId.ofString(getId());
 
 113     public Version getVersion() {
 
 115             return Version.ofString(getTypeId().getVersion());
 
 116         } catch (ServiceException e) {
 
 117             return new Version(0, 0, 0);