Merge "Add DecisionException"
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / provider / PdpProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.persistence.provider;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response;
30
31 import lombok.NonNull;
32
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpStatistics;
41 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * This class provides the provision of information on PAP concepts in the database to callers.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 public class PdpProvider {
53     private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
54
55     /**
56      * Get PDP groups.
57      *
58      * @param dao the DAO to use to access the database
59      * @param name the name of the policy to get, null to get all PDP groups
60      * @param version the version of the policy to get, null to get all versions of a PDP group
61      * @return the PDP groups found
62      * @throws PfModelException on errors getting PDP groups
63      */
64     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name, final String version)
65             throws PfModelException {
66
67         PfConceptKey jpaPdpGroupKey = new PfConceptKey(name, version);
68         JpaPdpGroup jpaPdpGroup = dao.get(JpaPdpGroup.class, jpaPdpGroupKey);
69
70         if (jpaPdpGroup != null) {
71             return Collections.singletonList(jpaPdpGroup.toAuthorative());
72         } else {
73             String errorMessage = "PDP group not found: " + jpaPdpGroupKey.getId();
74             LOGGER.warn(errorMessage);
75             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
76         }
77     }
78
79     /**
80      * Get latest PDP Groups.
81      *
82      * @param dao the DAO to use to access the database
83      * @param name the name of the PDP group to get, null to get all PDP groups
84      * @return the PDP groups found
85      * @throws PfModelException on errors getting policies
86      */
87     public List<PdpGroup> getLatestPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
88         return new ArrayList<>();
89     }
90
91     /**
92      * Get a filtered list of PDP groups.
93      *
94      * @param dao the DAO to use to access the database
95      * @param pdpType The PDP type filter for the returned PDP groups, null to get policy types across PDP subgroups
96      * @param supportedPolicyTypes a list of policy type name/version pairs that the PDP groups must support.
97      * @return the PDP groups found
98      */
99     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, final String pdpType,
100             @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
101         return new ArrayList<>();
102     }
103
104     /**
105      * Creates PDP groups.
106      *
107      * @param dao the DAO to use to access the database
108      * @param pdpGroups a specification of the PDP groups to create
109      * @return the PDP groups created
110      * @throws PfModelException on errors creating PDP groups
111      */
112     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
113             throws PfModelException {
114
115         for (PdpGroup pdpGroup : pdpGroups) {
116             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
117             jpaPdpGroup.fromAuthorative(pdpGroup);
118
119             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
120             if (!validationResult.isOk()) {
121                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + "\" is not valid \n" + validationResult;
122                 LOGGER.warn(errorMessage);
123                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
124             }
125
126             dao.create(jpaPdpGroup);
127         }
128
129         // Return the created PDP groups
130         List<PdpGroup> returnPdpGroups = new ArrayList<>();
131
132         for (PdpGroup pdpGroup : pdpGroups) {
133             JpaPdpGroup jpaPdpGroup =
134                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
135             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
136         }
137
138         return returnPdpGroups;
139     }
140
141     /**
142      * Updates PDP groups.
143      *
144      * @param dao the DAO to use to access the database
145      * @param pdpGroups a specification of the PDP groups to update
146      * @return the PDP groups updated
147      * @throws PfModelException on errors updating PDP groups
148      */
149     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
150             throws PfModelException {
151         return new ArrayList<>();
152     }
153
154
155     /**
156      * Update a PDP subgroup.
157      *
158      * @param dao the DAO to use to access the database
159      * @param pdpGroupName the name of the PDP group of the PDP subgroup
160      * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
161      * @param pdpSubGroup the PDP subgroup to be updated
162      * @throws PfModelException on errors updating PDP subgroups
163      */
164     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
165             @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
166         // Not implemented yet
167     }
168
169     /**
170      * Delete a PDP group.
171      *
172      * @param dao the DAO to use to access the database
173      * @param name the name of the policy to get, null to get all PDP groups
174      * @param version the version of the policy to get, null to get all versions of a PDP group
175      * @return the PDP group deleted
176      * @throws PfModelException on errors deleting PDP groups
177      */
178     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version)
179             throws PfModelException {
180         return new PdpGroup();
181
182     }
183
184     /**
185      * Get PDP statistics.
186      *
187      * @param dao the DAO to use to access the database
188      * @param name the name of the PDP group to get statistics for, null to get all PDP groups
189      * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
190      * @return the statistics found
191      * @throws PfModelException on errors getting statistics
192      */
193     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
194             throws PfModelException {
195         return new ArrayList<>();
196     }
197
198     /**
199      * Update PDP statistics for a PDP.
200      *
201      * @param dao the DAO to use to access the database
202      * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
203      * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
204      * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
205      * @param pdpInstanceId the instance ID of the PDP to update statistics for
206      * @throws PfModelException on errors updating statistics
207      */
208     public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
209             @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
210             @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
211         // Not implemented yet
212     }
213
214     /**
215      * Get deployed policies.
216      *
217      * @param dao the DAO to use to access the database
218      * @param name the name of the policy to get deployed policies for, null to get all deployed policies
219      * @return the policies deployed as a map of policy lists keyed by PDP group name and version
220      * @throws PfModelException on errors getting policies
221      */
222     public Map<Pair<String, String>, List<ToscaPolicy>> getDeployedPolicyList(@NonNull final PfDao dao,
223             final String name) throws PfModelException {
224         return new LinkedHashMap<>();
225     }
226 }