2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
\r
6 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
\r
7 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
\r
8 * ================================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
13 * http://www.apache.org/licenses/LICENSE-2.0
\r
15 * Unless required by applicable law or agreed to in writing, software
\r
16 * distributed under the License is distributed on an "AS IS" BASIS,
\r
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
18 * See the License for the specific language governing permissions and
\r
19 * limitations under the License.
\r
21 * SPDX-License-Identifier: Apache-2.0
\r
22 * ============LICENSE_END=========================================================
\r
25 package org.onap.policy.api.main.rest.provider;
\r
27 import org.onap.policy.api.main.rest.PolicyFetchMode;
\r
28 import org.onap.policy.models.base.PfModelException;
\r
29 import org.onap.policy.models.provider.PolicyModelsProvider;
\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
\r
33 import org.springframework.beans.factory.annotation.Autowired;
\r
34 import org.springframework.stereotype.Service;
\r
37 * Class to provide all kinds of policy operations.
\r
39 * @author Chenfei Gao (cgao@research.att.com)
\r
42 public class PolicyProvider extends CommonModelProvider {
\r
45 * Default constructor.
\r
48 public PolicyProvider(PolicyModelsProvider modelsProvider) throws PfModelException {
\r
49 super(modelsProvider);
\r
53 * Retrieves a list of policies matching specified ID and version of both policy type and policy.
\r
55 * @param policyTypeId the ID of policy type
\r
56 * @param policyTypeVersion the version of policy type
\r
57 * @param policyId the ID of policy
\r
58 * @param policyVersion the version of policy
\r
59 * @param mode the fetch mode for policies
\r
61 * @return the ToscaServiceTemplate object
\r
63 * @throws PfModelException the PfModel parsing exception
\r
65 public ToscaServiceTemplate fetchPolicies(final String policyTypeId, final String policyTypeVersion,
\r
66 final String policyId, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
\r
68 return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);
\r
72 * Retrieves a list of policies with the latest versions that match specified policy type id and version.
\r
74 * @param policyTypeId the ID of policy type
\r
75 * @param policyTypeVersion the version of policy type
\r
76 * @param policyId the ID of the policy
\r
77 * @param mode the fetch mode for policies
\r
78 * @return the ToscaServiceTemplate object
\r
80 * @throws PfModelException the PfModel parsing exception
\r
82 public ToscaServiceTemplate fetchLatestPolicies(final String policyTypeId, final String policyTypeVersion,
\r
83 final String policyId, final PolicyFetchMode mode) throws PfModelException {
\r
85 return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaTypedEntityFilter.LATEST_VERSION,
\r
90 * Creates one or more new policies for the same policy type ID and version.
\r
92 * @param policyTypeId the ID of policy type
\r
93 * @param policyTypeVersion the version of policy type
\r
94 * @param body the entity body of polic(ies)
\r
96 * @return the ToscaServiceTemplate object
\r
98 * @throws PfModelException the PfModel parsing exception
\r
100 public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
\r
101 throws PfModelException {
\r
103 return modelsProvider.createPolicies(body);
\r
107 * Creates one or more new policies.
\r
109 * @param body the entity body of policy
\r
111 * @return the ToscaServiceTemplate object
\r
113 * @throws PfModelException the PfModel parsing exception
\r
115 public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {
\r
116 return modelsProvider.createPolicies(body);
\r
120 * Deletes the policy matching specified ID and version of both policy type and policy.
\r
122 * @param policyTypeId the ID of policy type
\r
123 * @param policyTypeVersion the version of policy type
\r
124 * @param policyId the ID of policy
\r
125 * @param policyVersion the version of policy
\r
127 * @return the ToscaServiceTemplate object
\r
129 * @throws PfModelException the PfModel parsing exception
\r
131 public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,
\r
132 String policyVersion) throws PfModelException {
\r
134 return modelsProvider.deletePolicy(policyId, policyVersion);
\r
138 * Retrieves the specified version of the policy.
\r
140 * @param policyTypeName the name of the policy type
\r
141 * @param policyTypeVersion the version of the policy type
\r
142 * @param policyName the name of the policy
\r
143 * @param policyVersion the version of the policy
\r
144 * @param mode the fetch mode for policies
\r
146 * @return the TOSCA service template containing the specified version of the policy
\r
148 * @throws PfModelException the PfModel parsing exception
\r
150 private ToscaServiceTemplate getFilteredPolicies(final String policyTypeName, final String policyTypeVersion,
\r
151 final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
\r
153 ToscaTypedEntityFilter<ToscaPolicy> policyFilter = ToscaTypedEntityFilter.<ToscaPolicy>builder()
\r
154 .name(policyName).version(policyVersion).type(policyTypeName).typeVersion(policyTypeVersion).build();
\r
156 ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);
\r
158 if (mode == null || PolicyFetchMode.BARE.equals(mode)) {
\r
159 serviceTemplate.setPolicyTypes(null);
\r
160 serviceTemplate.setDataTypes(null);
\r
163 return serviceTemplate;
\r