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 * ================================================================================
\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 org.onap.policy.api.main.rest.PolicyFetchMode;
\r
27 import org.onap.policy.models.base.PfModelException;
\r
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
\r
33 * Class to provide all kinds of policy operations.
\r
35 * @author Chenfei Gao (cgao@research.att.com)
\r
37 public class PolicyProvider extends CommonModelProvider {
\r
40 * Default constructor.
\r
42 public PolicyProvider() throws PfModelException {
\r
47 * Retrieves a list of policies matching specified ID and version of both policy type and policy.
\r
49 * @param policyTypeId the ID of policy type
\r
50 * @param policyTypeVersion the version of policy type
\r
51 * @param policyId the ID of policy
\r
52 * @param policyVersion the version of policy
\r
53 * @param mode the fetch mode for policies
\r
55 * @return the ToscaServiceTemplate object
\r
57 * @throws PfModelException the PfModel parsing exception
\r
59 public ToscaServiceTemplate fetchPolicies(final String policyTypeId, final String policyTypeVersion,
\r
60 final String policyId, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
\r
62 return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);
\r
66 * Retrieves a list of policies with the latest versions that match specified policy type id and version.
\r
68 * @param policyTypeId the ID of policy type
\r
69 * @param policyTypeVersion the version of policy type
\r
70 * @param policyId the ID of the policy
\r
71 * @param mode the fetch mode for policies
\r
72 * @return the ToscaServiceTemplate object
\r
74 * @throws PfModelException the PfModel parsing exception
\r
76 public ToscaServiceTemplate fetchLatestPolicies(final String policyTypeId, final String policyTypeVersion,
\r
77 final String policyId, final PolicyFetchMode mode) throws PfModelException {
\r
79 return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaTypedEntityFilter.LATEST_VERSION,
\r
84 * Creates one or more new policies for the same policy type ID and version.
\r
86 * @param policyTypeId the ID of policy type
\r
87 * @param policyTypeVersion the version of policy type
\r
88 * @param body the entity body of polic(ies)
\r
90 * @return the ToscaServiceTemplate object
\r
92 * @throws PfModelException the PfModel parsing exception
\r
94 public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
\r
95 throws PfModelException {
\r
97 return modelsProvider.createPolicies(body);
\r
101 * Creates one or more new policies.
\r
103 * @param body the entity body of policy
\r
105 * @return the ToscaServiceTemplate object
\r
107 * @throws PfModelException the PfModel parsing exception
\r
109 public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {
\r
110 return modelsProvider.createPolicies(body);
\r
114 * Deletes the policy matching specified ID and version of both policy type and policy.
\r
116 * @param policyTypeId the ID of policy type
\r
117 * @param policyTypeVersion the version of policy type
\r
118 * @param policyId the ID of policy
\r
119 * @param policyVersion the version of policy
\r
121 * @return the ToscaServiceTemplate object
\r
123 * @throws PfModelException the PfModel parsing exception
\r
125 public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,
\r
126 String policyVersion) throws PfModelException {
\r
128 return modelsProvider.deletePolicy(policyId, policyVersion);
\r
132 * Retrieves the specified version of the policy.
\r
134 * @param policyTypeName the name of the policy type
\r
135 * @param policyTypeVersion the version of the policy type
\r
136 * @param policyName the name of the policy
\r
137 * @param policyVersion the version of the policy
\r
138 * @param mode the fetch mode for policies
\r
140 * @return the TOSCA service template containing the specified version of the policy
\r
142 * @throws PfModelException the PfModel parsing exception
\r
144 private ToscaServiceTemplate getFilteredPolicies(final String policyTypeName, final String policyTypeVersion,
\r
145 final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
\r
147 ToscaTypedEntityFilter<ToscaPolicy> policyFilter = ToscaTypedEntityFilter.<ToscaPolicy>builder()
\r
148 .name(policyName).version(policyVersion).type(policyTypeName).typeVersion(policyTypeVersion).build();
\r
150 ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);
\r
152 if (mode == null || PolicyFetchMode.BARE.equals(mode)) {
\r
153 serviceTemplate.setPolicyTypes(null);
\r
154 serviceTemplate.setDataTypes(null);
\r
157 return serviceTemplate;
\r