Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaServiceTemplateProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020, 2023 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.tosca.simple.provider;
22
23 import jakarta.ws.rs.core.Response.Status;
24 import lombok.NonNull;
25 import org.onap.policy.models.base.PfConceptKey;
26 import org.onap.policy.models.base.PfModelException;
27 import org.onap.policy.models.dao.PfDao;
28 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This class provides CRUD to and from the database en bloc using Service Template reads and writes.
34  *
35  * @author Liam Fallon (liam.fallon@est.tech)
36  */
37 public class SimpleToscaServiceTemplateProvider {
38     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaServiceTemplateProvider.class);
39
40     // There is only one service template in the database becasue TOSCA does not specify names and versions on service
41     // templates.
42     private static final PfConceptKey DEFAULT_SERVICE_TEMPLATE_KEY =
43         new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION);
44
45     /**
46      * Get a service template from the database.
47      *
48      * @param dao the DAO to use to access the database
49      * @return the Service Template read from the database
50      * @throws PfModelException on errors getting the service template
51      */
52     protected JpaToscaServiceTemplate read(@NonNull final PfDao dao) throws PfModelException {
53         LOGGER.debug("->read");
54
55         try {
56             // Get the service template
57             JpaToscaServiceTemplate serviceTemplate =
58                 dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY);
59
60             LOGGER.debug("<-read: serviceTemplate={}", serviceTemplate);
61             return serviceTemplate;
62         } catch (Exception dbException) {
63             throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "database read error on service tempalate"
64                 + DEFAULT_SERVICE_TEMPLATE_KEY.getId() + "\n" + dbException.getMessage(), dbException);
65         }
66     }
67
68     /**
69      * Write a service template to the database.
70      *
71      * @param dao the DAO to use to access the database
72      * @param serviceTemplate the service template to be written
73      * @return the TOSCA service template overwritten by this method
74      * @throws PfModelException on errors writing the service template
75      */
76     protected JpaToscaServiceTemplate write(@NonNull final PfDao dao,
77         @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
78
79         try {
80             LOGGER.debug("->write: serviceTempalate={}", serviceTemplate);
81             JpaToscaServiceTemplate overwrittenServiceTemplate = dao.update(serviceTemplate);
82             LOGGER.debug("<-write: overwrittenServiceTemplate={}", overwrittenServiceTemplate);
83             return overwrittenServiceTemplate;
84         } catch (Exception dbException) {
85             throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "database write error on service tempalate"
86                 + serviceTemplate.getKey().getId() + "\n" + dbException.getMessage(), dbException);
87         }
88     }
89
90     /**
91      * Delete a service template from the database.
92      *
93      * @param dao the DAO to use to access the database
94      * @return the Service Template stored in the database
95      * @throws PfModelException on errors getting the service template
96      */
97     protected JpaToscaServiceTemplate delete(@NonNull final PfDao dao) throws PfModelException {
98         try {
99             LOGGER.debug("->delete");
100
101             JpaToscaServiceTemplate serviceTemplateToBeDeleted =
102                 dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY);
103
104             dao.delete(serviceTemplateToBeDeleted);
105
106             LOGGER.debug("<-delete: serviceTemplate={}", serviceTemplateToBeDeleted);
107             return serviceTemplateToBeDeleted;
108         } catch (Exception dbException) {
109             throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "database delete error on service tempalate"
110                 + DEFAULT_SERVICE_TEMPLATE_KEY.getId() + "\n" + dbException.getMessage(), dbException);
111         }
112     }
113 }