2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022-2023 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.service;
23 import jakarta.ws.rs.core.Response;
24 import java.util.Optional;
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;
37 @Transactional(readOnly = true)
38 @RequiredArgsConstructor
39 public class ToscaNodeTemplateService {
41 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaNodeTemplateService.class);
43 private final ToscaNodeTemplateRepository nodeTemplateRepository;
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
53 public ToscaNodeTemplate getToscaNodeTemplate(final String name, final String version)
54 throws PfModelRuntimeException {
56 LOGGER.debug("->getNodeTemplate: name={}, version={}", name, version);
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);
65 throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
66 "node template for " + name + ":" + version + " do not exist in the database");