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;
25 import lombok.NonNull;
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;
35 * This class provides CRUD to and from the database en bloc using Service Template reads and writes.
37 * @author Liam Fallon (liam.fallon@est.tech)
39 public class SimpleToscaServiceTemplateProvider {
40 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaServiceTemplateProvider.class);
42 // There is only one service template in the database becasue TOSCA does not specify names and versions on service
44 private static final PfConceptKey DEFAULT_SERVICE_TEMPLATE_KEY =
45 new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION);
48 * Get a service template from the database.
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
54 protected JpaToscaServiceTemplate read(@NonNull final PfDao dao) throws PfModelException {
55 LOGGER.debug("->read");
58 // Get the service template
59 JpaToscaServiceTemplate serviceTemplate =
60 dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY);
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);
71 * Write a service template to the database.
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
78 protected JpaToscaServiceTemplate write(@NonNull final PfDao dao,
79 @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
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);
93 * Delete a service template from the database.
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
99 protected JpaToscaServiceTemplate delete(@NonNull final PfDao dao) throws PfModelException {
101 LOGGER.debug("->delete");
103 JpaToscaServiceTemplate serviceTemplateToBeDeleted =
104 dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY);
106 dao.delete(serviceTemplateToBeDeleted);
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);