2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
\r
6 * Modifications Copyright (C) 2020 Nordix Foundation.
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
20 * SPDX-License-Identifier: Apache-2.0
\r
21 * ============LICENSE_END=========================================================
\r
24 package org.onap.policy.api.main.rest.provider;
\r
26 import javax.ws.rs.core.Response;
\r
28 import org.onap.policy.models.base.PfModelException;
\r
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
33 * Class to provide all kinds of policy type operations.
\r
35 * @author Chenfei Gao (cgao@research.att.com)
\r
37 public class PolicyTypeProvider extends CommonModelProvider {
\r
40 * Default constructor.
\r
42 public PolicyTypeProvider() throws PfModelException {
\r
47 * Retrieves a list of policy types matching specified policy type ID and version.
\r
49 * @param policyTypeId the ID of policy type
\r
50 * @param policyTypeVersion the version of policy type
\r
52 * @return the ToscaServiceTemplate object
\r
54 * @throws PfModelException the PfModel parsing exception
\r
56 public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)
\r
57 throws PfModelException {
\r
59 ToscaServiceTemplate serviceTemplate = getFilteredPolicyTypes(policyTypeId, policyTypeVersion);
\r
61 if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {
\r
62 throw new PfModelException(Response.Status.NOT_FOUND,
\r
63 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
66 return serviceTemplate;
\r
70 * Retrieves a list of policy types with the latest versions.
\r
72 * @param policyTypeId the ID of policy type
\r
74 * @return the ToscaServiceTemplate object
\r
76 * @throws PfModelException the PfModel parsing exception
\r
78 public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {
\r
80 ToscaServiceTemplate serviceTemplate =
\r
81 getFilteredPolicyTypes(policyTypeId, ToscaPolicyTypeFilter.LATEST_VERSION);
\r
82 if (!hasPolicyType(serviceTemplate)) {
\r
83 throw new PfModelException(Response.Status.NOT_FOUND, constructResourceNotFoundMessage(policyTypeId, null));
\r
86 return serviceTemplate;
\r
90 * Creates a new policy type.
\r
92 * @param body the entity body of policy type
\r
94 * @return the ToscaServiceTemplate object
\r
95 * @throws PfModelException the PfModel parsing exception
\r
97 public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {
\r
99 if (!hasPolicyType(body)) {
\r
100 throw new PfModelException(Response.Status.BAD_REQUEST,
\r
101 "no policy types specified in the service template");
\r
104 return modelsProvider.createPolicyTypes(body);
\r
108 * Delete the policy type matching specified policy type ID and version.
\r
110 * @param policyTypeId the ID of policy type
\r
111 * @param policyTypeVersion the version of policy type
\r
113 * @return the ToscaServiceTemplate object
\r
115 * @throws PfModelException the PfModel parsing exception
\r
117 public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)
\r
118 throws PfModelException {
\r
120 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);
\r
122 if (!hasPolicyType(serviceTemplate)) {
\r
123 throw new PfModelException(Response.Status.NOT_FOUND,
\r
124 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
127 return serviceTemplate;
\r
131 * Retrieves the specified version of the policy type.
\r
133 * @param policyTypeName the name of the policy type
\r
134 * @param policyTypeVersion the version of the policy type
\r
136 * @return the TOSCA service template containing the specified version of the policy type
\r
138 * @throws PfModelException the PfModel parsing exception
\r
140 private ToscaServiceTemplate getFilteredPolicyTypes(String policyTypeName, String policyTypeVersion)
\r
141 throws PfModelException {
\r
143 ToscaPolicyTypeFilter policyTypeFilter =
\r
144 ToscaPolicyTypeFilter.builder().name(policyTypeName).version(policyTypeVersion).build();
\r
145 return modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
\r
149 * Constructs returned message for not found resource.
\r
151 * @param policyTypeId the ID of policy type
\r
152 * @param policyTypeVersion the version of policy type
\r
154 * @return constructed message
\r
156 private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {
\r
158 return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";
\r