Upgrade to oparent 3.2.1 47/130147/2
authorpdragosh <pd1248@att.com>
Wed, 3 Aug 2022 13:27:58 +0000 (08:27 -0500)
committerPamela Dragosh <pd1248@att.com>
Wed, 3 Aug 2022 14:58:33 +0000 (14:58 +0000)
Issue-ID: POLICY-4211
Change-Id: Ic4000bacba8eec9fdeace7bc11b9937fe42cda8b
Signed-off-by: pdragosh <pd1248@att.com>
main/src/main/java/org/onap/policy/api/main/rest/provider/CommonModelProvider.java

index cb20c04..6e9a1c3 100644 (file)
-/*-\r
- * ============LICENSE_START=======================================================\r
- * ONAP Policy API\r
- * ================================================================================\r
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.\r
- * Modifications Copyright (C) 2019-2021 Nordix Foundation.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *\r
- * SPDX-License-Identifier: Apache-2.0\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package org.onap.policy.api.main.rest.provider;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.function.BiConsumer;\r
-import javax.ws.rs.core.Response;\r
-import org.apache.commons.lang3.tuple.Pair;\r
-import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
-import org.onap.policy.common.parameters.ParameterService;\r
-import org.onap.policy.models.base.PfConceptKey;\r
-import org.onap.policy.models.base.PfModelException;\r
-import org.onap.policy.models.pdp.concepts.PdpGroup;\r
-import org.onap.policy.models.pdp.concepts.PdpGroupFilter;\r
-import org.onap.policy.models.pdp.concepts.PdpSubGroup;\r
-import org.onap.policy.models.pdp.enums.PdpState;\r
-import org.onap.policy.models.provider.PolicyModelsProvider;\r
-import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;\r
-\r
-/**\r
- * Super class for providers that use a model provider.\r
- */\r
-public class CommonModelProvider implements AutoCloseable {\r
-\r
-    protected final PolicyModelsProvider modelsProvider;\r
-\r
-    /**\r
-     * Constructs the object, populating {@link #modelsProvider}.\r
-     *\r
-     * @throws PfModelException if an error occurs\r
-     */\r
-    public CommonModelProvider() throws PfModelException {\r
-\r
-        ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
-        PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
-        modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
-    }\r
-\r
-    /**\r
-     * Closes the connection to database.\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    @Override\r
-    public void close() throws PfModelException {\r
-\r
-        modelsProvider.close();\r
-    }\r
-\r
-    /**\r
-     * Collects all deployed versions of specified policy in all pdp groups.\r
-     *\r
-     * @param policyId the ID of policy\r
-     * @param policyType the concept key of policy type\r
-     * @param getter the custom generic getter Bifunction\r
-     * @param consumer the BiConsumer\r
-     * @param data the data structure storing retrieved deployed policies\r
-     *\r
-     * @return a map between pdp group and deployed versions of specified policy in that group\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    protected <T, R> Map<Pair<String, String>, T> collectDeployedPolicies(String policyId, PfConceptKey policyType,\r
-            BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {\r
-\r
-        List<PdpGroup> pdpGroups = getPolicyTypeFilteredPdpGroups(policyType);\r
-        hasActivePdpGroup(pdpGroups, policyType, policyId);\r
-        return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data);\r
-    }\r
-\r
-    @FunctionalInterface\r
-    protected interface BiFunctionWithEx<T, U, R> {\r
-        public R apply(T value1, U value2) throws PfModelException;\r
-    }\r
-\r
-    /**\r
-     * Checks if the list of pdp groups is empty. If so, throws exception saying specified policy deployment is not\r
-     * found in all existing pdp groups.\r
-     *\r
-     * @param pdpGroups the list of pdp groups to check against\r
-     * @param policyType the concept key of policy type\r
-     * @param policyId the ID of policy\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private void hasActivePdpGroup(List<PdpGroup> pdpGroups, PfConceptKey policyType, String policyId)\r
-            throws PfModelException {\r
-\r
-        if (pdpGroups.isEmpty()) {\r
-            throw new PfModelException(Response.Status.NOT_FOUND,\r
-                    constructDeploymentNotFoundMessage(policyType, policyId));\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Retrieves all pdp groups supporting specified policy type.\r
-     *\r
-     * @param policyTypeId the ID of policy type\r
-     * @param policyTypeVersion the version of policy type\r
-     *\r
-     * @return a list of pdp groups supporting specified policy type\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private List<PdpGroup> getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) throws PfModelException {\r
-\r
-        List<ToscaConceptIdentifier> policyTypes = new ArrayList<>();\r
-        policyTypes.add(new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion()));\r
-        PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes).groupState(PdpState.ACTIVE)\r
-                .pdpState(PdpState.ACTIVE).build();\r
-        return modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
-    }\r
-\r
-    /**\r
-     * Constructs the map of deployed pdp groups and deployed policies.\r
-     *\r
-     * @param pdpGroups the list of pdp groups that contain the specified policy\r
-     * @param policyId the ID of policy\r
-     * @param policyType the concept key of policy type\r
-     * @param getter the custom generic getter BiFunction\r
-     * @param consumer the BiConsumer\r
-     * @param data the data structure storing retrieved deployed policies\r
-     *\r
-     * @return the constructed map of pdp groups and deployed policies\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private <T, R> Map<Pair<String, String>, T> constructDeployedPolicyMap(List<PdpGroup> pdpGroups, String policyId,\r
-            PfConceptKey policyType, BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data)\r
-            throws PfModelException {\r
-\r
-        Map<Pair<String, String>, T> deployedPolicyMap = new HashMap<>();\r
-        for (PdpGroup pdpGroup : pdpGroups) {\r
-            List<ToscaConceptIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType);\r
-            T deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data);\r
-            deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);\r
-        }\r
-        return deployedPolicyMap;\r
-    }\r
-\r
-    /**\r
-     * Extracts policy identifiers matching specified policy ID from specified pdp group.\r
-     *\r
-     * @param policyId the ID of policy to match\r
-     * @param pdpGroup the target pdp group to search\r
-     * @param policyType the concept key of policy type\r
-     *\r
-     * @return the list of policy identifiers\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private List<ToscaConceptIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup,\r
-            PfConceptKey policyType) throws PfModelException {\r
-\r
-        List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>();\r
-        for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {\r
-            for (ToscaConceptIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {\r
-                if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {\r
-                    policyIdentifiers.add(policyIdentifier);\r
-                }\r
-            }\r
-        }\r
-        if (policyIdentifiers.isEmpty()) {\r
-            throw new PfModelException(Response.Status.NOT_FOUND,\r
-                    constructDeploymentNotFoundMessage(policyType, policyId));\r
-        }\r
-        return policyIdentifiers;\r
-    }\r
-\r
-    /**\r
-     * Retrieves deployed policies in a generic way.\r
-     *\r
-     * @param policyIdentifiers the identifiers of the policies to return\r
-     * @param policyType the concept key of current policy type\r
-     * @param getter the method reference of getting deployed policies\r
-     * @param consumer the method reference of consuming the returned policies\r
-     * @param data the data structure of deployed policies to return\r
-     *\r
-     * @return the generic type of policy data structure to return\r
-     *\r
-     * @throws PfModelException the PfModel parsing exception\r
-     */\r
-    private <T, R> T getDeployedPolicies(List<ToscaConceptIdentifier> policyIdentifiers, PfConceptKey policyType,\r
-            BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {\r
-\r
-        for (ToscaConceptIdentifier policyIdentifier : policyIdentifiers) {\r
-            R result = getter.apply(policyIdentifier.getName(),\r
-                    getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType));\r
-            consumer.accept(data, result);\r
-        }\r
-        return data;\r
-    }\r
-\r
-    /**\r
-     * Trims the version for legacy policies.\r
-     *\r
-     * @param fullVersion the full version format with major, minor, patch\r
-     * @param policyType the concept key of policy type\r
-     *\r
-     * @return the trimmed version\r
-     */\r
-    private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) {\r
-        return (policyType.getName().contains("guard") || policyType.getName().contains("Operational"))\r
-                ? fullVersion.split("\\.")[0]\r
-                : fullVersion;\r
-    }\r
-\r
-    /**\r
-     * Constructs returned message for not found policy deployment.\r
-     *\r
-     * @param policyType the concept key of policy type\r
-     * @param policyId the ID of policy\r
-     *\r
-     * @return constructed message\r
-     */\r
-    private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) {\r
-\r
-        return "could not find policy with ID " + policyId + " and type " + policyType.getName() + ":"\r
-                + policyType.getVersion() + " deployed in any pdp group";\r
-    }\r
-}\r
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy API
+ * ================================================================================
+ * Copyright (C) 2019-2020,2022 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.api.main.rest.provider;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.BiConsumer;
+import javax.ws.rs.core.Response;
+import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.enums.PdpState;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.provider.PolicyModelsProviderFactory;
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+
+/**
+ * Super class for providers that use a model provider.
+ */
+public class CommonModelProvider implements AutoCloseable {
+
+    protected final PolicyModelsProvider modelsProvider;
+
+    /**
+     * Constructs the object, populating {@link #modelsProvider}.
+     *
+     * @throws PfModelException if an error occurs
+     */
+    public CommonModelProvider() throws PfModelException {
+
+        ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
+        PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
+        modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
+    }
+
+    /**
+     * Closes the connection to database.
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    @Override
+    public void close() throws PfModelException {
+
+        modelsProvider.close();
+    }
+
+    /**
+     * Collects all deployed versions of specified policy in all pdp groups.
+     *
+     * @param policyId the ID of policy
+     * @param policyType the concept key of policy type
+     * @param getter the custom generic getter Bifunction
+     * @param consumer the BiConsumer
+     * @param data the data structure storing retrieved deployed policies
+     *
+     * @return a map between pdp group and deployed versions of specified policy in that group
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    protected <T, R> Map<Pair<String, String>, T> collectDeployedPolicies(String policyId, PfConceptKey policyType,
+            BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {
+
+        List<PdpGroup> pdpGroups = getPolicyTypeFilteredPdpGroups(policyType);
+        hasActivePdpGroup(pdpGroups, policyType, policyId);
+        return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data);
+    }
+
+    @FunctionalInterface
+    protected interface BiFunctionWithEx<T, U, R> {
+        public R apply(T value1, U value2) throws PfModelException;
+    }
+
+    /**
+     * Checks if the list of pdp groups is empty. If so, throws exception saying specified policy deployment is not
+     * found in all existing pdp groups.
+     *
+     * @param pdpGroups the list of pdp groups to check against
+     * @param policyType the concept key of policy type
+     * @param policyId the ID of policy
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    private void hasActivePdpGroup(List<PdpGroup> pdpGroups, PfConceptKey policyType, String policyId)
+            throws PfModelException {
+
+        if (pdpGroups.isEmpty()) {
+            throw new PfModelException(Response.Status.NOT_FOUND,
+                    constructDeploymentNotFoundMessage(policyType, policyId));
+        }
+    }
+
+    /**
+     * Retrieves all pdp groups supporting specified policy type.
+     *
+     * @param policyType the policy type
+     *
+     * @return a list of pdp groups supporting specified policy type
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    private List<PdpGroup> getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) throws PfModelException {
+
+        List<ToscaConceptIdentifier> policyTypes = new ArrayList<>();
+        policyTypes.add(new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion()));
+        PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes).groupState(PdpState.ACTIVE)
+                .pdpState(PdpState.ACTIVE).build();
+        return modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
+    }
+
+    /**
+     * Constructs the map of deployed pdp groups and deployed policies.
+     *
+     * @param pdpGroups the list of pdp groups that contain the specified policy
+     * @param policyId the ID of policy
+     * @param policyType the concept key of policy type
+     * @param getter the custom generic getter BiFunction
+     * @param consumer the BiConsumer
+     * @param data the data structure storing retrieved deployed policies
+     *
+     * @return the constructed map of pdp groups and deployed policies
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    private <T, R> Map<Pair<String, String>, T> constructDeployedPolicyMap(List<PdpGroup> pdpGroups, String policyId,
+            PfConceptKey policyType, BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data)
+            throws PfModelException {
+
+        Map<Pair<String, String>, T> deployedPolicyMap = new HashMap<>();
+        for (PdpGroup pdpGroup : pdpGroups) {
+            List<ToscaConceptIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType);
+            T deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data);
+            deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);
+        }
+        return deployedPolicyMap;
+    }
+
+    /**
+     * Extracts policy identifiers matching specified policy ID from specified pdp group.
+     *
+     * @param policyId the ID of policy to match
+     * @param pdpGroup the target pdp group to search
+     * @param policyType the concept key of policy type
+     *
+     * @return the list of policy identifiers
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    private List<ToscaConceptIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup,
+            PfConceptKey policyType) throws PfModelException {
+
+        List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>();
+        for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+            for (ToscaConceptIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {
+                if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {
+                    policyIdentifiers.add(policyIdentifier);
+                }
+            }
+        }
+        if (policyIdentifiers.isEmpty()) {
+            throw new PfModelException(Response.Status.NOT_FOUND,
+                    constructDeploymentNotFoundMessage(policyType, policyId));
+        }
+        return policyIdentifiers;
+    }
+
+    /**
+     * Retrieves deployed policies in a generic way.
+     *
+     * @param policyIdentifiers the identifiers of the policies to return
+     * @param policyType the concept key of current policy type
+     * @param getter the method reference of getting deployed policies
+     * @param consumer the method reference of consuming the returned policies
+     * @param data the data structure of deployed policies to return
+     *
+     * @return the generic type of policy data structure to return
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    private <T, R> T getDeployedPolicies(List<ToscaConceptIdentifier> policyIdentifiers, PfConceptKey policyType,
+            BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {
+
+        for (ToscaConceptIdentifier policyIdentifier : policyIdentifiers) {
+            R result = getter.apply(policyIdentifier.getName(),
+                    getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType));
+            consumer.accept(data, result);
+        }
+        return data;
+    }
+
+    /**
+     * Trims the version for legacy policies.
+     *
+     * @param fullVersion the full version format with major, minor, patch
+     * @param policyType the concept key of policy type
+     *
+     * @return the trimmed version
+     */
+    private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) {
+        return (policyType.getName().contains("guard") || policyType.getName().contains("Operational"))
+                ? fullVersion.split("\\.")[0]
+                : fullVersion;
+    }
+
+    /**
+     * Constructs returned message for not found policy deployment.
+     *
+     * @param policyType the concept key of policy type
+     * @param policyId the ID of policy
+     *
+     * @return constructed message
+     */
+    private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) {
+
+        return "could not find policy with ID " + policyId + " and type " + policyType.getName() + ":"
+                + policyType.getVersion() + " deployed in any pdp group";
+    }
+}