diff options
author | krishnaa96 <krishna.moorthy6@wipro.com> | 2021-02-26 13:02:24 +0530 |
---|---|---|
committer | krishnaa96 <krishna.moorthy6@wipro.com> | 2021-03-01 11:08:54 +0530 |
commit | eb514d63eb47f335a1eb3119124a56225ac730cc (patch) | |
tree | f2b5cc8ffd63738ffee36b1b3ae38b0f1eb4ec99 /cps-tbdmt-service/src/main/java/org/onap | |
parent | 6668f15295fc6fe7a89a77cd98bcd3f4dfb39788 (diff) |
Add cps-tdmt-service code
Issue-ID: CPS-243
Signed-off-by: krishnaa96 <krishna.moorthy6@wipro.com>
Change-Id: I466bf00586dc8c11fcc50b673cf58d46ec461089
Diffstat (limited to 'cps-tbdmt-service/src/main/java/org/onap')
13 files changed, 642 insertions, 0 deletions
diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java new file mode 100644 index 0000000..c58ebd0 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.client; + +import java.util.Arrays; +import org.onap.cps.tbdmt.exception.CpsClientException; +import org.onap.cps.tbdmt.model.AppConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class CpsRestClient { + + private static final String NODES_API_PATH = "%s/anchors/%s/nodes?cps-path=%s"; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private AppConfiguration appConfiguration; + + /** + * Fetch node from the CPS using xpath. + * + * @param anchor anchor + * @param xpath xpath query + * @return result Response string from CPS + */ + public String fetchNode(final String anchor, final String xpath) throws CpsClientException { + final String url = appConfiguration.getXnfProxyUrl(); + + final String uri = String.format(NODES_API_PATH, url, anchor, xpath); + + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + final HttpEntity<String> entity = new HttpEntity<>(headers); + + ResponseEntity<String> responseEntity = null; + try { + responseEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); + } catch (final Exception e) { + throw new CpsClientException(e.getLocalizedMessage()); + } + + final int statusCode = responseEntity.getStatusCodeValue(); + + if (statusCode == 200) { + return responseEntity.getBody(); + } else { + throw new CpsClientException( + String.format("Response code from CPS other than 200: %d", statusCode)); + } + } + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/db/TemplateRepository.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/db/TemplateRepository.java new file mode 100644 index 0000000..bf9f41d --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/db/TemplateRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.db; + +import org.onap.cps.tbdmt.model.Template; +import org.onap.cps.tbdmt.model.TemplateKey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface TemplateRepository extends CrudRepository<Template, TemplateKey> { + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/CpsClientException.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/CpsClientException.java new file mode 100644 index 0000000..293a5f8 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/CpsClientException.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.exception; + +public class CpsClientException extends Exception { + + public CpsClientException(final String exception) { + super(exception); + } +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/ExecuteException.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/ExecuteException.java new file mode 100644 index 0000000..c89093c --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/ExecuteException.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.OK) +public class ExecuteException extends RuntimeException { + + public ExecuteException(final String exception) { + super(exception); + } +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/TemplateNotFoundException.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/TemplateNotFoundException.java new file mode 100644 index 0000000..dd52ca2 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/exception/TemplateNotFoundException.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +public class TemplateNotFoundException extends RuntimeException { + + public TemplateNotFoundException(final String exception) { + super(exception); + } +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java new file mode 100644 index 0000000..90666cd --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/AppConfiguration.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import java.util.Map; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Getter +@Setter +@Configuration +@EnableConfigurationProperties +@ConfigurationProperties(prefix = "app") +public class AppConfiguration { + + private String xnfProxyUrl; + private Map<String, String> schemaToAnchor; + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ErrorResponse.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ErrorResponse.java new file mode 100644 index 0000000..fa4fdef --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ErrorResponse.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class ErrorResponse { + + private String message; + private List<String> details; +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ExecutionRequest.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ExecutionRequest.java new file mode 100644 index 0000000..322c6d4 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ExecutionRequest.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class ExecutionRequest { + + private Map<String, String> inputParameters; +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java new file mode 100644 index 0000000..14f159f --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Entity +@Table(name = "Template") +@IdClass(TemplateKey.class) +public class Template implements Serializable { + + private static final long serialVersionUID = 345L; + + @Id + private String templateId; + + @Id + private String model; + + private String xpathTemplate; + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateKey.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateKey.java new file mode 100644 index 0000000..ab465b9 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateKey.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import java.io.Serializable; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@EqualsAndHashCode +@AllArgsConstructor +@NoArgsConstructor +public class TemplateKey implements Serializable { + + private static final long serialVersionUID = 400L; + + private String templateId; + + private String model; + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java new file mode 100644 index 0000000..c679a56 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.model; + +import java.io.Serializable; +import javax.validation.constraints.NotEmpty; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class TemplateRequest implements Serializable { + + private static final long serialVersionUID = 543L; + + @NotEmpty(message = "template id missing") + private String templateId; + + @NotEmpty(message = "model missing") + private String model; + + @NotEmpty(message = "template missing") + private String xpathTemplate; + +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java new file mode 100644 index 0000000..ff72cf7 --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.service; + +import com.hubspot.jinjava.Jinjava; +import java.util.Map; +import java.util.Optional; +import org.onap.cps.tbdmt.client.CpsRestClient; +import org.onap.cps.tbdmt.db.TemplateRepository; +import org.onap.cps.tbdmt.exception.CpsClientException; +import org.onap.cps.tbdmt.exception.ExecuteException; +import org.onap.cps.tbdmt.exception.TemplateNotFoundException; +import org.onap.cps.tbdmt.model.AppConfiguration; +import org.onap.cps.tbdmt.model.ExecutionRequest; +import org.onap.cps.tbdmt.model.Template; +import org.onap.cps.tbdmt.model.TemplateKey; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ExecutionBusinessLogic { + + @Autowired + private TemplateRepository templateRepository; + + @Autowired + private AppConfiguration appConfiguration; + + @Autowired + private CpsRestClient cpsRestClient; + + /** + * Execute a template stored in the database. + * + * @param schemaSet schema set + * @param id id + * @param executionRequest inputs to be applied to the templates + * @return result response from the execution of template + */ + public String executeTemplate(final String schemaSet, final String id, final ExecutionRequest executionRequest) { + + final Optional<Template> templateOptional = templateRepository.findById(new TemplateKey(id, schemaSet)); + if (templateOptional.isPresent()) { + return execute(templateOptional.get(), executionRequest.getInputParameters()); + } + throw new TemplateNotFoundException("Template does not exist"); + } + + private String execute(final Template template, final Map<String, String> inputParameters) { + final String anchor = appConfiguration.getSchemaToAnchor().get(template.getModel()); + if (anchor == null) { + throw new ExecuteException("Anchor not found for the schema"); + } + final String xpath = generateXpath(template.getXpathTemplate(), inputParameters); + try { + return cpsRestClient.fetchNode(anchor, xpath); + } catch (final CpsClientException e) { + throw new ExecuteException(e.getLocalizedMessage()); + } + } + + private String generateXpath(final String xpathTemplate, final Map<String, String> templateParameters) { + return new Jinjava().render(xpathTemplate, templateParameters); + } +} diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java new file mode 100644 index 0000000..f75352f --- /dev/null +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.tbdmt.service; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Optional; +import org.onap.cps.tbdmt.db.TemplateRepository; +import org.onap.cps.tbdmt.exception.TemplateNotFoundException; +import org.onap.cps.tbdmt.model.Template; +import org.onap.cps.tbdmt.model.TemplateKey; +import org.onap.cps.tbdmt.model.TemplateRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class TemplateBusinessLogic { + + private static final String TEMPLATE_NOT_FOUND_ERROR = "Template not found for given id: %s"; + + @Autowired + private TemplateRepository templateRepository; + + /** + * Create Template. + * + * @param templateRequest request object + * @return template + */ + public Template createTemplate(final TemplateRequest templateRequest) { + final Template template = new Template(templateRequest.getTemplateId(), + templateRequest.getModel(), + templateRequest.getXpathTemplate()); + return templateRepository.save(template); + } + + /** + * Get All Templates. + * + * @return templates + */ + public Collection<Template> getAllTemplates() { + final Collection<Template> templates = new HashSet<>(); + templateRepository.findAll().forEach(templates::add); + return templates; + } + + /** + * Get Template by Id. + * + * @param templateKey template id to find the template + * @return template + */ + public Template getTemplate(final TemplateKey templateKey) { + final Optional<Template> template = templateRepository.findById(templateKey); + if (template.isPresent()) { + return template.get(); + } else { + final String errorMessage = String.format(TEMPLATE_NOT_FOUND_ERROR, + templateKey.getTemplateId()); + throw new TemplateNotFoundException(errorMessage); + } + } + + /** + * Delete Template. + * + * @param templateKey template id to find the template + */ + public void deleteTemplate(final TemplateKey templateKey) { + if (templateRepository.existsById(templateKey)) { + templateRepository.deleteById(templateKey); + } else { + final String errorMessage = String.format(TEMPLATE_NOT_FOUND_ERROR, + templateKey.getTemplateId()); + throw new TemplateNotFoundException(errorMessage); + } + } +} |