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