9b097ccb554c0e4032e2f2e44fd53f19b14913b0
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / service / ToscaNodeTemplateService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2022, Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.service;
22
23 import java.util.Optional;
24 import javax.ws.rs.core.Response;
25 import lombok.RequiredArgsConstructor;
26 import org.onap.policy.models.base.PfConceptKey;
27 import org.onap.policy.models.base.PfModelRuntimeException;
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
29 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
30 import org.onap.policy.pap.main.repository.ToscaNodeTemplateRepository;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Service;
34 import org.springframework.transaction.annotation.Transactional;
35
36 @Service
37 @Transactional(readOnly = true)
38 @RequiredArgsConstructor
39 public class ToscaNodeTemplateService {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaNodeTemplateService.class);
42
43     private final ToscaNodeTemplateRepository nodeTemplateRepository;
44
45     /**
46      * Get node templates.
47      *
48      * @param name the name of the node template to get
49      * @param version the version of the node template to get
50      * @return the node templates found
51      * @throws PfModelRuntimeException on errors getting node templates
52      */
53     public ToscaNodeTemplate getToscaNodeTemplate(final String name, final String version)
54         throws PfModelRuntimeException {
55
56         LOGGER.debug("->getNodeTemplate: name={}, version={}", name, version);
57
58         Optional<JpaToscaNodeTemplate> jpaToscaNodeTemplate = nodeTemplateRepository
59             .findById(new PfConceptKey(name, version));
60         if (jpaToscaNodeTemplate.isPresent()) {
61             var nodeTemplate = jpaToscaNodeTemplate.get().toAuthorative();
62             LOGGER.debug("<-NodeTemplate: name={}, version={}, nodeTemplate={}", name, version, nodeTemplate);
63             return nodeTemplate;
64         } else {
65             throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
66                 "node template for " + name + ":" + version + " do not exist in the database");
67         }
68     }
69
70 }