Add changes for safe delete in policy-models
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
index 3a8c238..6b12395 100644 (file)
-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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 org.onap.policy.model.tosca.ToscaPolicy;
-import org.onap.policy.model.tosca.ToscaPolicyList;
-
-/**
- * Class to provide all kinds of policy operations.
- */
-public class PolicyProvider {
-
-    private static final String POST_OK = "Successfully created";
-    private static final String DELETE_OK = "Successfully deleted";
-
-    /**
-     * Retrieves a list of policies matching specified ID and version of both policy type and policy.
-     *
-     * @param policyTypeId the ID of policy type
-     * @param policyTypeVersion the version of policy type
-     * @param policyId the ID of policy
-     * @param policyVersion the version of policy
-     *
-     * @return the ToscaPolicyList object containing a list of policies matching specified fields
-     */
-    public ToscaPolicyList fetchPolicies(String policyTypeId, String policyTypeVersion,
-                                         String policyId, String policyVersion) {
-        // placeholder
-        return new ToscaPolicyList();
-    }
-
-    /**
-     * Creates a new policy for a policy type ID and version.
-     *
-     * @param policyTypeId the ID of policy type
-     * @param policyTypeVersion the version of policy type
-     * @param body the entity body of policy
-     *
-     * @return a string message indicating the operation results
-     */
-    public String createPolicy(String policyTypeId, String policyTypeVersion, ToscaPolicy body) {
-        // placeholder
-        return POST_OK;
-    }
-
-    /**
-     * Deletes the policies matching specified ID and version of both policy type and policy.
-     *
-     * @param policyTypeId the ID of policy type
-     * @param policyTypeVersion the version of policy type
-     * @param policyId the ID of policy
-     * @param policyVersion the version of policy
-     *
-     * @return a string message indicating the operation results
-     */
-    public String deletePolicies(String policyTypeId, String policyTypeVersion,
-                                 String policyId, String policyVersion) {
-        // placeholder
-        return DELETE_OK;
-    }
-}
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * ONAP Policy API\r
+ * ================================================================================\r
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.\r
+ * Modifications Copyright (C) 2020 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.List;\r
+import java.util.Map;\r
+\r
+import javax.ws.rs.core.Response;\r
+\r
+import org.apache.commons.lang3.tuple.Pair;\r
+import org.onap.policy.models.base.PfConceptKey;\r
+import org.onap.policy.models.base.PfModelException;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
+\r
+/**\r
+ * Class to provide all kinds of policy operations.\r
+ *\r
+ * @author Chenfei Gao (cgao@research.att.com)\r
+ */\r
+public class PolicyProvider extends CommonModelProvider {\r
+\r
+    /**\r
+     * Default constructor.\r
+     */\r
+    public PolicyProvider() throws PfModelException {\r
+        super();\r
+    }\r
+\r
+    /**\r
+     * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of policy\r
+     * @param policyVersion the version of policy\r
+     *\r
+     * @return the ToscaServiceTemplate object\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) throws PfModelException {\r
+\r
+        ToscaServiceTemplate serviceTemplate =\r
+                getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
+\r
+        if (!hasPolicy(serviceTemplate)) {\r
+            throw new PfModelException(Response.Status.NOT_FOUND,\r
+                    constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
+        }\r
+\r
+        return serviceTemplate;\r
+    }\r
+\r
+    /**\r
+     * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of the policy\r
+     *\r
+     * @return the ToscaServiceTemplate object\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)\r
+            throws PfModelException {\r
+\r
+        ToscaServiceTemplate serviceTemplate =\r
+                getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
+\r
+        if (!hasPolicy(serviceTemplate)) {\r
+            throw new PfModelException(Response.Status.NOT_FOUND,\r
+                    constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));\r
+        }\r
+\r
+        return serviceTemplate;\r
+    }\r
+\r
+    /**\r
+     * Retrieves a list of deployed policies in each pdp group.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of the policy\r
+     *\r
+     * @return a list of deployed policies in each pdp group\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,\r
+            String policyTypeVersion, String policyId) throws PfModelException {\r
+\r
+        return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),\r
+                modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));\r
+    }\r
+\r
+    /**\r
+     * Creates one or more new policies for the same policy type ID and version.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param body the entity body of polic(ies)\r
+     *\r
+     * @return the ToscaServiceTemplate object\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
+            throws PfModelException {\r
+\r
+        return modelsProvider.createPolicies(body);\r
+    }\r
+\r
+    /**\r
+     * Creates one or more new policies.\r
+     *\r
+     * @param body the entity body of policy\r
+     *\r
+     * @return the ToscaServiceTemplate object\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
+        return modelsProvider.createPolicies(body);\r
+    }\r
+\r
+    /**\r
+     * Deletes the policy matching specified ID and version of both policy type and policy.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of policy\r
+     * @param policyVersion the version of policy\r
+     *\r
+     * @return the ToscaServiceTemplate object\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) throws PfModelException {\r
+\r
+        ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
+\r
+        if (!hasPolicy(serviceTemplate)) {\r
+            throw new PfModelException(Response.Status.NOT_FOUND,\r
+                    constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
+        }\r
+\r
+        return serviceTemplate;\r
+    }\r
+\r
+    /**\r
+     * Retrieves the specified version of the policy.\r
+     *\r
+     * @param policyTypeName the name of the policy type\r
+     * @param policyTypeVersion the version of the policy type\r
+     * @param policyName the name of the policy\r
+     * @param policyVersion the version of the policy\r
+     *\r
+     * @return the TOSCA service template containing the specified version of the policy\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,\r
+            String policyVersion) throws PfModelException {\r
+\r
+        ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)\r
+                .type(policyTypeName).typeVersion(policyTypeVersion).build();\r
+        return modelsProvider.getFilteredPolicies(policyFilter);\r
+    }\r
+\r
+    /**\r
+     * Constructs returned message for not found resource.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of policy\r
+     * @param policyVersion the version of policy\r
+     *\r
+     * @return constructed message\r
+     */\r
+    private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) {\r
+\r
+        return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"\r
+                + policyTypeVersion + " does not exist";\r
+    }\r
+}\r