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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.provider;
23 import javax.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;
33 * This class provides CRUD to and from the database en bloc using Service Template reads and writes.
35 * @author Liam Fallon (liam.fallon@est.tech)
37 public class SimpleToscaServiceTemplateProvider {
38 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaServiceTemplateProvider.class);
40 // There is only one service template in the database becasue TOSCA does not specify names and versions on service
42 private static final PfConceptKey DEFAULT_SERVICE_TEMPLATE_KEY =
43 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION);
46 * Get a service template from the database.
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
52 protected JpaToscaServiceTemplate read(@NonNull final PfDao dao) throws PfModelException {
53 LOGGER.debug("->read");
56 // Get the service template
57 JpaToscaServiceTemplate serviceTemplate =
58 dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY);
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);
69 * Write a service template to the database.
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
76 protected JpaToscaServiceTemplate write(@NonNull final PfDao dao,
77 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
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);
91 * Delete a service template from the database.
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
97 protected JpaToscaServiceTemplate delete(@NonNull final PfDao dao) throws PfModelException {
99 LOGGER.debug("->delete");
101 JpaToscaServiceTemplate serviceTemplateToBeDeleted =
102 dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY);
104 dao.delete(serviceTemplateToBeDeleted);
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);