A1 Policy Management 85/126385/3
authorPatrikBuhr <patrik.buhr@est.tech>
Tue, 28 Dec 2021 12:07:14 +0000 (13:07 +0100)
committerPatrikBuhr <patrik.buhr@est.tech>
Wed, 29 Dec 2021 10:55:32 +0000 (11:55 +0100)
Sorting of compatible policy type IDs

Issue-ID: CCSDK-3495
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Change-Id: I9b092b82a24e951f0ac1f446b37cc0db2d644e3a

a1-policy-management/api/pms-api.json
a1-policy-management/api/pms-api.yaml
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyType.java
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java
docs/offeredapis/swagger/pms-api.json
docs/offeredapis/swagger/pms-api.yaml

index a8a444f..0ba147b 100644 (file)
                     "schema": {"type": "string"},
                     "in": "query",
                     "name": "compatible_with_version",
-                    "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around.",
+                    "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching types will be returned sorted in ascending order.",
                     "required": false
                 }
             ],
index c827468..9aa945e 100644 (file)
@@ -304,7 +304,8 @@ paths:
         in: query
         description: Select types that are compatible with the given version. This
           parameter is only applicable in conjunction with type_name. As an example
-          version 1.9.1 is compatible with 1.0.0 but not the other way around.
+          version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching
+          types will be returned sorted in ascending order.
         required: false
         style: form
         explode: true
index 9945e0a..5f2f619 100644 (file)
@@ -141,7 +141,8 @@ public class PolicyController {
             @Parameter(name = Consts.COMPATIBLE_WITH_VERSION_PARAM, required = false, //
                     description = "Select types that are compatible with the given version. This parameter is only applicable in conjunction with "
                             + Consts.TYPE_NAME_PARAM
-                            + ". As an example version 1.9.1 is compatible with 1.0.0 but not the other way around.") //
+                            + ". As an example version 1.9.1 is compatible with 1.0.0 but not the other way around."
+                            + " Matching types will be returned sorted in ascending order.") //
             @RequestParam(name = Consts.COMPATIBLE_WITH_VERSION_PARAM, required = false) String compatibleWithVersion
 
     ) throws ServiceException {
index 3a5bdd9..14c9f58 100644 (file)
@@ -62,6 +62,18 @@ public class PolicyType {
                 throw new ServiceException("Syntax error in " + version, HttpStatus.BAD_REQUEST);
             }
         }
+
+        public int compareTo(Version other) {
+            if (major != other.major)
+                return major - other.major;
+            if (minor != other.minor)
+                return minor - other.minor;
+            return patch - other.patch;
+        }
+
+        public boolean isCompatibleWith(Version other) {
+            return (major == other.major && minor >= other.minor);
+        }
     }
 
     @Getter
@@ -98,4 +110,12 @@ public class PolicyType {
         return TypeId.ofString(getId());
     }
 
+    public Version getVersion() {
+        try {
+            return Version.ofString(getTypeId().getVersion());
+        } catch (ServiceException e) {
+            return new Version(0, 0, 0);
+        }
+    }
+
 }
index 53dc55d..14acca9 100644 (file)
@@ -33,6 +33,7 @@ import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
@@ -89,7 +90,9 @@ public class PolicyTypes {
      * @param types the types to select from
      * @param typeName select types with given type name
      * @param compatibleWithVersion select types that are compatible with given
-     *        version string (major.minor.patch)
+     *        version string (major.minor.patch).
+     *        Matching types will be sorted in ascending
+     *        order.
      * @return the types that matches given criterias
      * @throws ServiceException if there are errors in the given input
      */
@@ -172,26 +175,16 @@ public class PolicyTypes {
         return result;
     }
 
-    private static boolean isTypeCompatibleWithVersion(PolicyType type, PolicyType.Version version) {
-        try {
-            PolicyType.TypeId typeId = type.getTypeId();
-            PolicyType.Version typeVersion = PolicyType.Version.ofString(typeId.getVersion());
-            return (typeVersion.major == version.major && typeVersion.minor >= version.minor);
-        } catch (Exception e) {
-            logger.warn("Ignoring type with syntactically incorrect type ID: {}", type.getId());
-            return false;
-        }
-    }
-
     private static Collection<PolicyType> filterCompatibleWithVersion(Collection<PolicyType> types, String versionStr)
             throws ServiceException {
-        Collection<PolicyType> result = new ArrayList<>();
-        PolicyType.Version otherVersion = PolicyType.Version.ofString(versionStr);
+        List<PolicyType> result = new ArrayList<>();
+        PolicyType.Version requestedVersion = PolicyType.Version.ofString(versionStr);
         for (PolicyType type : types) {
-            if (isTypeCompatibleWithVersion(type, otherVersion)) {
+            if (type.getVersion().isCompatibleWith(requestedVersion)) {
                 result.add(type);
             }
         }
+        result.sort((left, right) -> left.getVersion().compareTo(right.getVersion()));
         return result;
     }
 
index a8a444f..0ba147b 100644 (file)
                     "schema": {"type": "string"},
                     "in": "query",
                     "name": "compatible_with_version",
-                    "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around.",
+                    "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching types will be returned sorted in ascending order.",
                     "required": false
                 }
             ],
index c827468..9aa945e 100644 (file)
@@ -304,7 +304,8 @@ paths:
         in: query
         description: Select types that are compatible with the given version. This
           parameter is only applicable in conjunction with type_name. As an example
-          version 1.9.1 is compatible with 1.0.0 but not the other way around.
+          version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching
+          types will be returned sorted in ascending order.
         required: false
         style: form
         explode: true