diff options
20 files changed, 1173 insertions, 1 deletions
diff --git a/cps-tbdmt-dependencies/pom.xml b/cps-tbdmt-dependencies/pom.xml index 5b3a5b8..34650b9 100644 --- a/cps-tbdmt-dependencies/pom.xml +++ b/cps-tbdmt-dependencies/pom.xml @@ -30,7 +30,12 @@ <properties> <cps.version>0.0.1-SNAPSHOT</cps.version> + <hibernate-jpa-api.version>1.0.1.Final</hibernate-jpa-api.version> + <jinjava.version>2.5.6</jinjava.version> + <lombok.version>1.18.16</lombok.version> <nexusproxy>https://nexus.onap.org</nexusproxy> + <openpojo.version>0.8.13</openpojo.version> + <postgresql.version>42.2.5</postgresql.version> <releaseNexusPath>/content/repositories/releases/</releaseNexusPath> <snapshotNexusPath>/content/repositories/snapshots/</snapshotNexusPath> <sonar.skip>true</sonar.skip> @@ -65,6 +70,50 @@ <type>pom</type> <scope>import</scope> </dependency> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <version>${postgresql.version}</version> + </dependency> + <dependency> + <groupId>org.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.0-api</artifactId> + <version>${hibernate-jpa-api.version}</version> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + </dependency> + <dependency> + <groupId>com.hubspot.jinjava</groupId> + <artifactId>jinjava</artifactId> + <version>${jinjava.version}</version> + <exclusions> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-annotations</artifactId> + </exclusion> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </exclusion> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-core</artifactId> + </exclusion> + <exclusion> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-yaml</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>com.openpojo</groupId> + <artifactId>openpojo</artifactId> + <version>${openpojo.version}</version> + <scope>test</scope> + </dependency> </dependencies> </dependencyManagement> -</project>
\ No newline at end of file +</project> diff --git a/cps-tbdmt-service/pom.xml b/cps-tbdmt-service/pom.xml index 4e21048..3291e8d 100644 --- a/cps-tbdmt-service/pom.xml +++ b/cps-tbdmt-service/pom.xml @@ -36,4 +36,62 @@ <artifactId>cps-tbdmt-service</artifactId> <packaging>jar</packaging> + <properties> + <maven.build.timestamp.format>yyyyMMdd'T'HHmmss</maven.build.timestamp.format> + </properties> + + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.data</groupId> + <artifactId>spring-data-commons</artifactId> + </dependency> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </dependency> + <dependency> + <groupId>com.hubspot.jinjava</groupId> + <artifactId>jinjava</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-json</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-validation</artifactId> + </dependency> + <!--Test dependencies--> + <dependency> + <groupId>com.openpojo</groupId> + <artifactId>openpojo</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> </project>
\ No newline at end of file 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); + } + } +} diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java new file mode 100644 index 0000000..f69d4ad --- /dev/null +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java @@ -0,0 +1,108 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.onap.cps.tbdmt.exception.CpsClientException; +import org.onap.cps.tbdmt.model.AppConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringRunner.class) +@EnableConfigurationProperties(AppConfiguration.class) +@TestPropertySource("classpath:application-test.properties") +public class CpsRestClientTest { + + @TestConfiguration + static class CpsRestClientTestContextConfiguration { + + @Bean + public CpsRestClient cpsRestClient() { + return new CpsRestClient(); + } + } + + @Autowired + private CpsRestClient cpsRestClient; + + @MockBean + private RestTemplate restTemplate; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void testFetchNode() throws Exception { + final HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setContentType(MediaType.APPLICATION_JSON); + final ResponseEntity<String> response = new ResponseEntity<>("sample response", responseHeaders, + HttpStatus.OK); + Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())) + .thenReturn(response); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample")); + + final ResponseEntity<String> errorResponse = new ResponseEntity<>("sample response", + responseHeaders, HttpStatus.NOT_FOUND); + Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())) + .thenReturn(errorResponse); + exception.expect(CpsClientException.class); + exception.expectMessage("Response code from CPS other than 200: 404"); + cpsRestClient.fetchNode("coverage-area-onap", "sample"); + + } + + @Test + public void testFetchNodeException() throws Exception { + Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())) + .thenThrow(new RestClientException("Connection refused")); + exception.expect(CpsClientException.class); + exception.expectMessage("Connection refused"); + cpsRestClient.fetchNode("coverage-area-onap", "sample"); + } +} diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/model/ModelTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/model/ModelTest.java new file mode 100644 index 0000000..1f57891 --- /dev/null +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/model/ModelTest.java @@ -0,0 +1,49 @@ +/*- + * ============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 com.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import org.junit.Test; + +public class ModelTest { + + private static final String POJO_PACKAGE = "org.onap.cps.tbdmt.model"; + + @Test + public void testModels() { + final Validator validator = ValidatorBuilder.create() + .with(new GetterMustExistRule()) + .with(new SetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + + validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + + } + +} diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java new file mode 100644 index 0000000..28a7a49 --- /dev/null +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java @@ -0,0 +1,147 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@EnableConfigurationProperties(AppConfiguration.class) +@TestPropertySource("classpath:application-test.properties") +public class ExecutionBusinessLogicTest { + + @TestConfiguration + static class ExecutionBusinessLogicTestContextConfiguration { + + @Bean + public ExecutionBusinessLogic executionBusinessLogic() { + return new ExecutionBusinessLogic(); + } + } + + @Autowired + private ExecutionBusinessLogic executionBusinessLogic; + + @MockBean + private TemplateRepository templateRepository; + + @MockBean + private CpsRestClient cpsRestClient; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + private ExecutionRequest request; + + private Template template; + + /** + * Setup variables before test. + * + */ + @Before + public void setup() { + final Map<String, String> input = new HashMap<>(); + input.put("coverageArea", "Zone 1"); + request = new ExecutionRequest(input); + final String xpathTemplate = "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + + "/coverage-area[@coverageArea='{{coverageArea}}']"; + template = new Template("getNbr", "ran-network", xpathTemplate); + } + + @Test + public void testExecuteTemplate() throws Exception { + final String resultString = "[{\"key\": \"value\"}]"; + Mockito.when(cpsRestClient + .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + + "/coverage-area[@coverageArea='Zone 1']")) + .thenReturn(resultString); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.of(template)); + assertEquals(resultString, + executionBusinessLogic.executeTemplate("ran-network", "getNbr", request)); + + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.empty()); + exception.expect(TemplateNotFoundException.class); + exception.expectMessage("Template does not exist"); + executionBusinessLogic.executeTemplate("ran-network", "getNbr", request); + + } + + @Test + public void testExecuteTemplateException() throws Exception { + final String exceptionMessage = "Response from CPS other than 200: 404"; + Mockito.when(cpsRestClient + .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + + "/coverage-area[@coverageArea='Zone 1']")) + .thenThrow(new CpsClientException(exceptionMessage)); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.of(template)); + exception.expect(ExecuteException.class); + exception.expectMessage(exceptionMessage); + executionBusinessLogic.executeTemplate("ran-network", "getNbr", request); + + final Template template1 = new Template("getNbr", "ran-net", "sample"); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.of(template1)); + exception.expect(ExecuteException.class); + exception.expectMessage("Anchor not found for the schema"); + executionBusinessLogic.executeTemplate("ran-net", "getNbr", request); + + } + + @Test + public void testExecuteTemplateNoAnchor() { + final Template template = new Template("getNbr", "ran-net", "sample"); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.of(template)); + exception.expect(ExecuteException.class); + exception.expectMessage("Anchor not found for the schema"); + executionBusinessLogic.executeTemplate("ran-net", "getNbr", request); + } + +} diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java new file mode 100644 index 0000000..5cf9fc1 --- /dev/null +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java @@ -0,0 +1,116 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Optional; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +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.boot.test.context.TestConfiguration; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +public class TemplateBusinessLogicTest { + + @TestConfiguration + static class TemplateBusinessLogicTestContextConfiguration { + + @Bean + public TemplateBusinessLogic templateBusinessLogic() { + return new TemplateBusinessLogic(); + } + } + + @Autowired + private TemplateBusinessLogic templateBusinessLogic; + + @MockBean + private TemplateRepository templateRepository; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + private Template template; + private TemplateKey templateKey; + + @Before + public void setup() { + template = new Template("getNbr", "ran-network", "sample"); + final TemplateKey templateKey = new TemplateKey("getNbr", "ran-network"); + } + + @Test + public void testCreateTemplate() throws Exception { + final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample"); + Mockito.when(templateRepository.save(ArgumentMatchers.any())).thenReturn(template); + assertEquals(template, templateBusinessLogic.createTemplate(templateRequest)); + } + + @Test + public void testGetAllTemplates() throws Exception { + final Collection<Template> templates = new HashSet<>(); + templates.add(template); + Mockito.when(templateRepository.findAll()).thenReturn(templates); + assertEquals(templates, templateBusinessLogic.getAllTemplates()); + } + + @Test + public void testGetTemplate() throws Exception { + Mockito.when(templateRepository.findById(templateKey)).thenReturn(Optional.of(template)); + assertEquals(template, templateBusinessLogic.getTemplate(templateKey)); + + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.empty()); + exception.expect(TemplateNotFoundException.class); + exception.expectMessage("Template not found for given id: getNbr"); + templateBusinessLogic.getTemplate(new TemplateKey("getNbr", "empty-schema")); + } + + @Test + public void testDeleteTemplate() throws Exception { + Mockito.when(templateRepository.existsById(templateKey)).thenReturn(true); + templateBusinessLogic.deleteTemplate(templateKey); + verify(templateRepository, times(1)).deleteById(templateKey); + + Mockito.when(templateRepository.existsById(ArgumentMatchers.any())).thenReturn(false); + exception.expect(TemplateNotFoundException.class); + exception.expectMessage("Template not found for given id: getNbr"); + templateBusinessLogic.deleteTemplate(new TemplateKey("getNbr", "empty-schema")); + } +} diff --git a/cps-tbdmt-service/src/test/resources/application-test.properties b/cps-tbdmt-service/src/test/resources/application-test.properties new file mode 100644 index 0000000..c4daedd --- /dev/null +++ b/cps-tbdmt-service/src/test/resources/application-test.properties @@ -0,0 +1,3 @@ +app.xnfProxyUrl=http://localhost:8000/ +app.schemaToAnchor.ran-coverage-area=coverage-area-onap +app.schemaToAnchor.ran-network=ran-network
\ No newline at end of file |