diff options
Diffstat (limited to 'plans/so/integration-etsi-testing/so-simulators/aai-simulator')
35 files changed, 2621 insertions, 26 deletions
diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/pom.xml b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/pom.xml index 1d1f8dba..3dd82228 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/pom.xml +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/pom.xml @@ -8,6 +8,37 @@ </parent> <artifactId>aai-simulator</artifactId> + <properties> + <version.aai.schema>1.0.0</version.aai.schema> + </properties> + + <dependencies> + <dependency> + <groupId>org.onap.aai.schema-service</groupId> + <artifactId>aai-schema</artifactId> + <version>${version.aai.schema}</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.module</groupId> + <artifactId>jackson-module-jaxb-annotations</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-security</artifactId> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-tomcat</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> <plugins> <plugin> diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/AaiSimulatorApplication.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/AaiSimulatorApplication.java index 72c37ad3..26f52e32 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/AaiSimulatorApplication.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/AaiSimulatorApplication.java @@ -22,16 +22,17 @@ package org.onap.so.aai.simulator; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cache.annotation.EnableCaching; /** * @author waqas.ikram@ericsson.com * */ +@EnableCaching @SpringBootApplication(scanBasePackages = {"org.onap"}) public class AaiSimulatorApplication extends SpringBootServletInitializer { public static void main(final String[] args) { SpringApplication.run(AaiSimulatorApplication.class, args); } - } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/configration/ApplicationConfigration.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/configration/ApplicationConfigration.java new file mode 100644 index 00000000..af00ba78 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/configration/ApplicationConfigration.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.configration; + +import static org.onap.so.aai.simulator.utils.Constants.CUSTOMER_CACHE; +import static org.onap.so.aai.simulator.utils.Constants.NODES_CACHE; +import static org.onap.so.aai.simulator.utils.Constants.PROJECT_CACHE; +import java.util.Arrays; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCache; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Configuration +public class ApplicationConfigration { + + @Bean + public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { + return (mapperBuilder) -> mapperBuilder.modulesToInstall(new JaxbAnnotationModule()); + } + + @Bean + public CacheManager cacheManager() { + final SimpleCacheManager manager = new SimpleCacheManager(); + manager.setCaches(Arrays.asList(getCache(CUSTOMER_CACHE), getCache(PROJECT_CACHE), getCache(NODES_CACHE))); + return manager; + } + + private Cache getCache(final String name) { + return new ConcurrentMapCache(name); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/configration/WebSecurityConfigImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/configration/WebSecurityConfigImpl.java new file mode 100644 index 00000000..a4a1fb11 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/configration/WebSecurityConfigImpl.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.configration; + +import org.onap.so.aai.simulator.utils.Constants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Configuration +@EnableWebSecurity +public class WebSecurityConfigImpl extends WebSecurityConfigurerAdapter { + + private final String username; + private final String password; + private final String role; + + public WebSecurityConfigImpl(@Value("${spring.security.username}") final String username, + @Value("${spring.security.password}") final String password, + @Value("${spring.security.role}") final String role) { + this.username = username; + this.password = password; + this.role = role; + } + + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http.csrf().disable().authorizeRequests().antMatchers(Constants.BUSINESS_URL + "/**/**").authenticated().and() + .httpBasic(); + } + + @Bean + public BCryptPasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Autowired + public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).withUser(username).password(password) + .roles(role); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/AaiSimulatorController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/AaiSimulatorController.java index 4acae56d..af0a0150 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/AaiSimulatorController.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/AaiSimulatorController.java @@ -17,32 +17,35 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ -package org.onap.so.aai.simulator; +package org.onap.so.aai.simulator.controller; +import static org.onap.so.aai.simulator.utils.Constants.HEALTHY; import javax.ws.rs.core.MediaType; -import org.onap.so.aai.simulator.utils.Constant; +import org.onap.so.aai.simulator.utils.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; /** * @author waqas.ikram@ericsson.com * */ -@RestController -@RequestMapping(path = Constant.BASE_URL) +@Controller +@RequestMapping(path = Constants.BASE_URL) public class AaiSimulatorController { private static final Logger LOGGER = LoggerFactory.getLogger(AaiSimulatorController.class); + @ResponseBody @GetMapping(value = "/healthcheck", produces = MediaType.TEXT_PLAIN) @ResponseStatus(code = HttpStatus.OK) public String healthCheck() { LOGGER.info("Running health check ..."); - return Constant.HEALTHY; + return HEALTHY; } } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/BusinessController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/BusinessController.java new file mode 100644 index 00000000..e7fd00de --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/BusinessController.java @@ -0,0 +1,210 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller; + +import static org.onap.so.aai.simulator.utils.Constants.CUSTOMER_URL; +import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE; +import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE_ID; +import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE; +import static org.onap.so.aai.simulator.utils.Utils.getResourceVersion; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.Customer; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.aai.domain.yang.ServiceInstances; +import org.onap.aai.domain.yang.ServiceSubscription; +import org.onap.so.aai.simulator.models.NodeServiceInstance; +import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider; +import org.onap.so.aai.simulator.service.providers.NodesCacheServiceProvider; +import org.onap.so.aai.simulator.utils.RequestErrorBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = CUSTOMER_URL) +public class BusinessController { + + private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class); + private final CustomerCacheServiceProvider cacheServiceProvider; + private final NodesCacheServiceProvider nodesCacheServiceProvider; + + @Autowired + public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider, + final NodesCacheServiceProvider nodesCacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + this.nodesCacheServiceProvider = nodesCacheServiceProvider; + } + + @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId, + final HttpServletRequest request) { + LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId); + + final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId); + if (optional.isPresent()) { + final Customer customer = optional.get(); + LOGGER.info("found customer {} in cache", customer); + return ResponseEntity.ok(customer); + } + + LOGGER.error("Couldn't find {} in cache", globalCustomerId); + return getRequestErrorResponseEntity(request); + } + + @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putCustomer(@RequestBody final Customer customer, + @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) { + LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId); + + if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) { + customer.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putCustomer(globalCustomerId, customer); + return ResponseEntity.accepted().build(); + + } + + @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, final HttpServletRequest request) { + LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...", + globalCustomerId, serviceType); + + final Optional<ServiceSubscription> optional = + cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType); + if (optional.isPresent()) { + final ServiceSubscription serviceSubscription = optional.get(); + LOGGER.info("found service subscription {} in cache", serviceSubscription); + return ResponseEntity.ok(serviceSubscription); + } + + LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId, + serviceType); + return getRequestErrorResponseEntity(request); + } + + @GetMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @RequestParam(name = "service-instance-name") final String serviceInstanceName, + @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) { + + LOGGER.info( + "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...", + globalCustomerId, serviceType, serviceInstanceName, depth); + + final Optional<ServiceInstances> optional = + cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName); + if (optional.isPresent()) { + final ServiceInstances serviceInstances = optional.get(); + LOGGER.info("found service instance {} in cache", serviceInstances); + return ResponseEntity.ok(serviceInstances); + } + LOGGER.error( + "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache", + globalCustomerId, serviceType, serviceInstanceName, depth); + return getRequestErrorResponseEntity(request); + } + + @GetMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestParam(name = "depth", required = false) final Integer depth, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + + LOGGER.info( + "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...", + globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format); + + final Optional<ServiceInstance> optional = + cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId); + if (optional.isPresent()) { + final ServiceInstance serviceInstance = optional.get(); + LOGGER.info("found service instance {} in cache", serviceInstance); + return ResponseEntity.ok(serviceInstance); + } + LOGGER.error( + "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache", + globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format); + return getRequestErrorResponseEntity(request); + } + + @PutMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) { + + LOGGER.info( + "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...", + globalCustomerId, serviceType, serviceInstanceId); + + if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) { + serviceInstance.setResourceVersion(getResourceVersion()); + } + + if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId, + serviceInstance)) { + nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance( + globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI())); + return ResponseEntity.accepted().build(); + } + + LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache", + globalCustomerId, serviceType, serviceInstanceId); + return getRequestErrorResponseEntity(request); + } + + private ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) { + return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE) + .variables(request.getMethod(), request.getRequestURI(), + "Node Not Found:No Node of type service-instance found at: " + request.getRequestURI(), + "ERR.5.4.6114") + .build(), HttpStatus.NOT_FOUND); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/NodesController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/NodesController.java new file mode 100644 index 00000000..4fa4d408 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/NodesController.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller; + +import static org.onap.so.aai.simulator.utils.Constants.NODES_URL; +import static org.onap.so.aai.simulator.utils.Constants.RESOURCE_LINK; +import static org.onap.so.aai.simulator.utils.Constants.RESOURCE_TYPE; +import static org.onap.so.aai.simulator.utils.Utils.getRequestErrorResponseEntity; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.so.aai.simulator.models.Format; +import org.onap.so.aai.simulator.models.NodeServiceInstance; +import org.onap.so.aai.simulator.models.Result; +import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider; +import org.onap.so.aai.simulator.service.providers.NodesCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = NODES_URL) +public class NodesController { + + + private static final Logger LOGGER = LoggerFactory.getLogger(NodesController.class); + + private final NodesCacheServiceProvider cacheServiceProvider; + + private final CustomerCacheServiceProvider customerCacheServiceProvider; + + @Autowired + public NodesController(final CustomerCacheServiceProvider customerCacheServiceProvider, + final NodesCacheServiceProvider cacheServiceProvider) { + this.customerCacheServiceProvider = customerCacheServiceProvider; + this.cacheServiceProvider = cacheServiceProvider; + } + + @GetMapping(value = "/service-instances/service-instance/{service-instance-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getProject(@PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + LOGGER.info("retrieving service instance using 'service-instance-id': {} and format: {}...", serviceInstanceId, + format); + + final Optional<NodeServiceInstance> optional = cacheServiceProvider.getNodeServiceInstance(serviceInstanceId); + if (!optional.isPresent()) { + LOGGER.error("Couldn't find {} in cache", serviceInstanceId); + return getRequestErrorResponseEntity(request); + } + + final Format value = Format.forValue(format); + final NodeServiceInstance nodeServiceInstance = optional.get(); + switch (value) { + case PATHED: + LOGGER.info("found project {} in cache", nodeServiceInstance); + final Map<String, Object> map = new HashMap<>(); + map.put(RESOURCE_TYPE, nodeServiceInstance.getResourceType()); + map.put(RESOURCE_LINK, nodeServiceInstance.getResourceLink()); + return ResponseEntity.ok(new Result(map)); + case RAW: + final Optional<ServiceInstance> serviceInstance = + customerCacheServiceProvider.getServiceInstance(nodeServiceInstance.getGlobalCustomerId(), + nodeServiceInstance.getServiceType(), nodeServiceInstance.getServiceInstanceId()); + if (serviceInstance.isPresent()) { + return ResponseEntity.ok(serviceInstance.get()); + } + LOGGER.info("Unable to find Service instance in cahce using {}", nodeServiceInstance); + return getRequestErrorResponseEntity(request); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + return getRequestErrorResponseEntity(request); + + + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/ProjectController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/ProjectController.java new file mode 100644 index 00000000..5f63494d --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/controller/ProjectController.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller; + +import static org.onap.so.aai.simulator.utils.Constants.PROJECT_URL; +import static org.onap.so.aai.simulator.utils.Utils.getRequestErrorResponseEntity; +import static org.onap.so.aai.simulator.utils.Utils.getResourceVersion; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.Project; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aai.simulator.models.Format; +import org.onap.so.aai.simulator.models.Result; +import org.onap.so.aai.simulator.service.providers.ProjectCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = PROJECT_URL) +public class ProjectController { + private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class); + + private final ProjectCacheServiceProvider cacheServiceProvider; + + @Autowired + public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putProject(@RequestBody final Project project, + @PathVariable("project-name") final String projectName, final HttpServletRequest request) { + LOGGER.info("Will put project for 'project-name': {} ...", project.getProjectName()); + + if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) { + project.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putProject(projectName, project); + return ResponseEntity.accepted().build(); + + } + + @GetMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getProject(@PathVariable("project-name") final String projectName, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + LOGGER.info("retrieving project for 'project-name': {} ...", projectName); + + final Optional<Project> optional = cacheServiceProvider.getProject(projectName); + if (!optional.isPresent()) { + LOGGER.error("Couldn't find {} in cache", projectName); + return getRequestErrorResponseEntity(request); + } + + final Format value = Format.forValue(format); + switch (value) { + case RAW: + final Project project = optional.get(); + LOGGER.info("found project {} in cache", project); + return ResponseEntity.ok(project); + case COUNT: + final Map<String, Object> map = new HashMap<>(); + map.put(projectName, 1); + return ResponseEntity.ok(new Result(map)); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + return getRequestErrorResponseEntity(request); + + + } + + @PutMapping(value = "/{project-name}/relationship-list/relationship", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putProjectRelationShip(@RequestBody final Relationship relationship, + @PathVariable("project-name") final String projectName, final HttpServletRequest request) { + + LOGGER.info("adding relationship for project-name: {} ...", projectName); + if (cacheServiceProvider.putProjectRelationShip(projectName, relationship)) { + LOGGER.info("added project relationship {} in cache", relationship); + return ResponseEntity.accepted().build(); + } + LOGGER.error("Couldn't find {} in cache", projectName); + return getRequestErrorResponseEntity(request); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/Format.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/Format.java new file mode 100644 index 00000000..f6ff2403 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/Format.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.models; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public enum Format { + + COUNT("count"), RAW("raw"), PATHED("pathed"); + + private final String value; + + private Format(final String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public static Format forValue(final String value) { + for (final Format format : Format.values()) { + if (format.getValue().equals(value)) { + return format; + } + } + return RAW; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/NodeServiceInstance.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/NodeServiceInstance.java new file mode 100644 index 00000000..f549850d --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/NodeServiceInstance.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.models; + +import java.io.Serializable; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class NodeServiceInstance implements Serializable { + + private static final long serialVersionUID = -3314166327618070948L; + + private String globalCustomerId; + private String serviceType; + private String serviceInstanceId; + private String resourceType; + private String resourceLink; + + public NodeServiceInstance() {} + + + public NodeServiceInstance(final String globalCustomerId, final String serviceType, final String serviceInstanceId, + final String resourceType, final String resourceLink) { + this.globalCustomerId = globalCustomerId; + this.serviceType = serviceType; + this.serviceInstanceId = serviceInstanceId; + this.resourceType = resourceType; + this.resourceLink = resourceLink; + } + + + /** + * @return the globalCustomerId + */ + public String getGlobalCustomerId() { + return globalCustomerId; + } + + + /** + * @param globalCustomerId the globalCustomerId to set + */ + public void setGlobalCustomerId(final String globalCustomerId) { + this.globalCustomerId = globalCustomerId; + } + + + /** + * @return the serviceType + */ + public String getServiceType() { + return serviceType; + } + + + /** + * @param serviceType the serviceType to set + */ + public void setServiceType(final String serviceType) { + this.serviceType = serviceType; + } + + + /** + * @return the serviceInstanceId + */ + public String getServiceInstanceId() { + return serviceInstanceId; + } + + + /** + * @param serviceInstanceId the serviceInstanceId to set + */ + public void setServiceInstanceId(final String serviceInstanceId) { + this.serviceInstanceId = serviceInstanceId; + } + + + /** + * @return the resourceType + */ + public String getResourceType() { + return resourceType; + } + + + /** + * @param resourceType the resourceType to set + */ + public void setResourceType(final String resourceType) { + this.resourceType = resourceType; + } + + + /** + * @return the resourceLink + */ + public String getResourceLink() { + return resourceLink; + } + + + /** + * @param resourceLink the resourceLink to set + */ + public void setResourceLink(final String resourceLink) { + this.resourceLink = resourceLink; + } + + + @Override + public String toString() { + return "NodeServiceInstance [globalCustomerId=" + globalCustomerId + ", serviceType=" + serviceType + + ", serviceInstanceId=" + serviceInstanceId + ", resourceType=" + resourceType + ", resourceLink=" + + resourceLink + "]"; + } + + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/Result.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/Result.java new file mode 100644 index 00000000..6aa92562 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/models/Result.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.models; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonRootName; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@JsonRootName(value = "result") +public class Result { + + private List<Map<String, Object>> values = new ArrayList<>(); + + public Result() {} + + public Result(final Map<String, Object> value) { + this.values.add(value); + } + + /** + * @return the values + */ + public List<Map<String, Object>> getValues() { + return values; + } + + /** + * @param values the values to set + */ + public void setValues(final List<Map<String, Object>> values) { + this.values = values; + } + + + @JsonIgnore + @Override + public String toString() { + return "Result [values=" + values + "]"; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/CustomerCacheServiceProvider.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/CustomerCacheServiceProvider.java new file mode 100644 index 00000000..51fc5546 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/CustomerCacheServiceProvider.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.service.providers; + +import java.util.Optional; +import org.onap.aai.domain.yang.Customer; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.aai.domain.yang.ServiceInstances; +import org.onap.aai.domain.yang.ServiceSubscription; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public interface CustomerCacheServiceProvider { + + Optional<Customer> getCustomer(final String globalCustomerId); + + void putCustomer(final String globalCustomerId, final Customer customer); + + Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId, final String serviceType); + + Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType, + final String serviceInstanceName); + + Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType, + final String serviceInstanceId); + + boolean putServiceInstance(final String globalCustomerId, final String serviceType, final String serviceInstanceId, + final ServiceInstance serviceInstance); + + void clearAll(); + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/CustomerCacheServiceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/CustomerCacheServiceProviderImpl.java new file mode 100644 index 00000000..11a82be8 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/CustomerCacheServiceProviderImpl.java @@ -0,0 +1,193 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.service.providers; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; +import org.onap.aai.domain.yang.Customer; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.aai.domain.yang.ServiceInstances; +import org.onap.aai.domain.yang.ServiceSubscription; +import org.onap.so.aai.simulator.utils.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Service +public class CustomerCacheServiceProviderImpl implements CustomerCacheServiceProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class); + + public final CacheManager cacheManager; + + @Autowired + public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + @Override + public Optional<Customer> getCustomer(final String globalCustomerId) { + LOGGER.info("getting customer from cache using key: {}", globalCustomerId); + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + final Customer value = cache.get(globalCustomerId, Customer.class); + if (value != null) { + return Optional.of(value); + } + return Optional.empty(); + } + + @Override + public void putCustomer(final String globalCustomerId, final Customer customer) { + LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId); + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + + cache.put(globalCustomerId, customer); + } + + @Override + public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId, + final String serviceType) { + LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}", + globalCustomerId, serviceType); + + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + + final Customer value = cache.get(globalCustomerId, Customer.class); + + if (value != null) { + return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream() + .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null)); + } + return Optional.empty(); + + } + + @Override + public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType, + final String serviceInstanceName) { + + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + final Customer value = cache.get(globalCustomerId, Customer.class); + + if (value != null) { + final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions() + .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst(); + + if (serviceSubscription.isPresent()) { + LOGGER.info("Found service subscription ..."); + final List<ServiceInstance> serviceInstancesList = serviceSubscription.get().getServiceInstances() + .getServiceInstance().stream() + .filter(serviceInstance -> serviceInstanceName.equals(serviceInstance.getServiceInstanceName())) + .collect(Collectors.toList()); + if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) { + LOGGER.info("Found {} service instances ", serviceInstancesList.size()); + final ServiceInstances serviceInstances = new ServiceInstances(); + serviceInstances.getServiceInstance().addAll(serviceInstancesList); + return Optional.of(serviceInstances); + + } + } + } + return Optional.empty(); + } + + @Override + public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType, + final String serviceInstanceId) { + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + final Customer value = cache.get(globalCustomerId, Customer.class); + + if (value != null) { + final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions() + .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst(); + + if (serviceSubscription.isPresent()) { + LOGGER.info("Found service subscription ..."); + final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances(); + if (serviceInstances != null) { + return Optional.ofNullable(serviceInstances.getServiceInstance().stream() + .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId())) + .findFirst().orElse(null)); + } + + } + } + return Optional.empty(); + } + + @Override + public boolean putServiceInstance(final String globalCustomerId, final String serviceType, + final String serviceInstanceId, final ServiceInstance serviceInstance) { + LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId); + + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + final Customer value = cache.get(globalCustomerId, Customer.class); + + if (value != null) { + final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions() + .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst(); + + if (serviceSubscription.isPresent()) { + final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription); + + + if (!serviceInstances.getServiceInstance().stream() + .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst() + .isPresent()) { + return serviceInstances.getServiceInstance().add(serviceInstance); + } + LOGGER.error("Service {} already exists ....", serviceInstanceId); + return false; + } + LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType); + return false; + } + LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId); + return false; + } + + private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) { + final ServiceSubscription serviceSubscription = optional.get(); + final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances(); + if (serviceInstances == null) { + final ServiceInstances instances = new ServiceInstances(); + serviceSubscription.setServiceInstances(instances); + return instances; + } + return serviceInstances; + } + + @Override + public void clearAll() { + final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE); + final ConcurrentHashMap<?, ?> nativeCache = (ConcurrentHashMap<?, ?>) cache.getNativeCache(); + LOGGER.info("Clear all entries from cahce: {}", cache.getName()); + nativeCache.clear(); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Constant.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/NodesCacheServiceProvider.java index 9dd503bb..bf444f2f 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Constant.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/NodesCacheServiceProvider.java @@ -5,30 +5,33 @@ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ -package org.onap.so.aai.simulator.utils; +package org.onap.so.aai.simulator.service.providers; + +import java.util.Optional; +import org.onap.so.aai.simulator.models.NodeServiceInstance; /** * @author waqas.ikram@ericsson.com * */ -public class Constant { +public interface NodesCacheServiceProvider { - public static final String BASE_URL = "/simulator/aai"; + void putNodeServiceInstance(final String serviceInstanceId, final NodeServiceInstance nodeServiceInstance); - public static final String HEALTHY = "healthy"; + Optional<NodeServiceInstance> getNodeServiceInstance(final String serviceInstanceId); - private Constant() {} + void clearAll(); } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/NodesCacheServiceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/NodesCacheServiceProviderImpl.java new file mode 100644 index 00000000..61cb35a2 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/NodesCacheServiceProviderImpl.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.service.providers; + +import static org.onap.so.aai.simulator.utils.Constants.NODES_CACHE; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.onap.so.aai.simulator.models.NodeServiceInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Service +public class NodesCacheServiceProviderImpl implements NodesCacheServiceProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(NodesCacheServiceProviderImpl.class); + + public final CacheManager cacheManager; + + @Autowired + public NodesCacheServiceProviderImpl(final CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + @Override + public void putNodeServiceInstance(final String serviceInstanceId, final NodeServiceInstance nodeServiceInstance) { + final Cache cache = cacheManager.getCache(NODES_CACHE); + LOGGER.info("Adding {} to cache with key: {}...", nodeServiceInstance, serviceInstanceId); + cache.put(serviceInstanceId, nodeServiceInstance); + } + + @Override + public Optional<NodeServiceInstance> getNodeServiceInstance(final String serviceInstanceId) { + final Cache cache = cacheManager.getCache(NODES_CACHE); + final NodeServiceInstance value = cache.get(serviceInstanceId, NodeServiceInstance.class); + if (value != null) { + return Optional.of(value); + } + LOGGER.error("Unable to find node service instance in cache using key:{} ", serviceInstanceId); + return Optional.empty(); + } + + @Override + public void clearAll() { + final Cache cache = cacheManager.getCache(NODES_CACHE); + final ConcurrentHashMap<?, ?> nativeCache = (ConcurrentHashMap<?, ?>) cache.getNativeCache(); + LOGGER.info("Clear all entries from cahce: {}", cache.getName()); + nativeCache.clear(); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/ProjectCacheServiceProvider.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/ProjectCacheServiceProvider.java new file mode 100644 index 00000000..59060f27 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/ProjectCacheServiceProvider.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.service.providers; + +import java.util.Optional; +import org.onap.aai.domain.yang.Project; +import org.onap.aai.domain.yang.Relationship; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public interface ProjectCacheServiceProvider { + + void putProject(final String projectName, final Project project); + + Optional<Project> getProject(final String projectName); + + boolean putProjectRelationShip(final String projectName, final Relationship relationship); + + void clearAll(); +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/ProjectCacheServiceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/ProjectCacheServiceProviderImpl.java new file mode 100644 index 00000000..a1047887 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/service/providers/ProjectCacheServiceProviderImpl.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.service.providers; + +import static org.onap.so.aai.simulator.utils.Constants.PROJECT_CACHE; +import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.onap.aai.domain.yang.Project; +import org.onap.aai.domain.yang.Relationship; +import org.onap.aai.domain.yang.RelationshipList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Service +public class ProjectCacheServiceProviderImpl implements ProjectCacheServiceProvider { + + private static final String RELATIONSHIPS_LABEL = "org.onap.relationships.inventory.Uses"; + + private static final Logger LOGGER = LoggerFactory.getLogger(ProjectCacheServiceProviderImpl.class); + + public final CacheManager cacheManager; + + @Autowired + public ProjectCacheServiceProviderImpl(final CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + @Override + public void putProject(final String projectName, final Project project) { + LOGGER.info("Adding project: {} with name to cache", project, projectName); + final Cache cache = cacheManager.getCache(PROJECT_CACHE); + cache.put(projectName, project); + } + + + @Override + public Optional<Project> getProject(final String projectName) { + LOGGER.info("getting project from cache using key: {}", projectName); + final Cache cache = cacheManager.getCache(PROJECT_CACHE); + final Project value = cache.get(projectName, Project.class); + if (value != null) { + return Optional.of(value); + } + return Optional.empty(); + } + + @Override + public boolean putProjectRelationShip(final String projectName, final Relationship relationship) { + final Cache cache = cacheManager.getCache(PROJECT_CACHE); + final Project value = cache.get(projectName, Project.class); + if (value != null) { + RelationshipList relationshipList = value.getRelationshipList(); + if (relationshipList == null) { + relationshipList = new RelationshipList(); + value.setRelationshipList(relationshipList); + } + + if (relationship.getRelatedTo() == null) { + relationship.setRelatedTo(SERVICE_RESOURCE_TYPE); + } + if (relationship.getRelationshipLabel() == null) { + relationship.setRelationshipLabel(RELATIONSHIPS_LABEL); + } + + return relationshipList.getRelationship().add(relationship); + } + return false; + + } + + @Override + public void clearAll() { + final Cache cache = cacheManager.getCache(PROJECT_CACHE); + final ConcurrentHashMap<?, ?> nativeCache = (ConcurrentHashMap<?, ?>) cache.getNativeCache(); + LOGGER.info("Clear all entries from cahce: {}", cache.getName()); + nativeCache.clear(); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Constants.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Constants.java new file mode 100644 index 00000000..1cb63360 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Constants.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class Constants { + + public static final String BASE_URL = "/aai/v15"; + + public static final String NODES_URL = BASE_URL + "/nodes"; + + public static final String BUSINESS_URL = BASE_URL + "/business"; + + public static final String CUSTOMER_URL = BUSINESS_URL + "/customers/customer/"; + + public static final String PROJECT_URL = BUSINESS_URL + "/projects/project/"; + + public static final String HEALTHY = "healthy"; + + public static final String CUSTOMER_CACHE = "customer-cache"; + + public static final String PROJECT_CACHE = "project-cache"; + + public static final String NODES_CACHE = "nodes-cache"; + + public static final String ERROR_MESSAGE_ID = "SVC3001"; + + public static final String ERROR_MESSAGE = "Resource not found for %1 using id %2 (msg=%3) (ec=%4)"; + + public static final String SERVICE_RESOURCE_TYPE = "service-instance"; + + public static final String RESOURCE_LINK = "resource-link"; + + public static final String RESOURCE_TYPE = "resource-type"; + + private Constants() {} + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/RequestError.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/RequestError.java new file mode 100644 index 00000000..471e884d --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/RequestError.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@XmlRootElement(name = "requestError") +@XmlAccessorType(XmlAccessType.FIELD) +public class RequestError { + + @XmlElement(name = "serviceException") + private ServiceException serviceException; + + /** + * @return the serviceException + */ + public ServiceException getServiceException() { + return serviceException; + } + + /** + * @param serviceException the serviceException to set + */ + public void setServiceException(final ServiceException serviceException) { + this.serviceException = serviceException; + } + + @Override + public String toString() { + return "RequestError [serviceException=" + serviceException + "]"; + } + + + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/RequestErrorBuilder.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/RequestErrorBuilder.java new file mode 100644 index 00000000..9dbd4a3d --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/RequestErrorBuilder.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +import java.util.Arrays; +import java.util.List; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class RequestErrorBuilder { + + private final ServiceException serviceException = new ServiceException(); + + public RequestErrorBuilder messageId(final String messageId) { + this.serviceException.setMessageId(messageId); + return this; + } + + public RequestErrorBuilder text(final String text) { + this.serviceException.setText(text); + return this; + } + + public RequestErrorBuilder variables(final List<String> variables) { + this.serviceException.setVariables(variables); + return this; + } + + public RequestErrorBuilder variables(final String... variables) { + this.serviceException.setVariables(Arrays.asList(variables)); + return this; + } + + public RequestError build() { + final RequestError requestError = new RequestError(); + requestError.setServiceException(serviceException); + return requestError; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/ServiceException.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/ServiceException.java new file mode 100644 index 00000000..ba2ee782 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/ServiceException.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@XmlRootElement(name = "serviceException") +@XmlAccessorType(XmlAccessType.FIELD) +public class ServiceException { + + private String messageId; + + private String text; + + private List<String> variables; + + /** + * @return the messageId + */ + public String getMessageId() { + return messageId; + } + + /** + * @param messageId the messageId to set + */ + public void setMessageId(final String messageId) { + this.messageId = messageId; + } + + /** + * @return the text + */ + public String getText() { + return text; + } + + /** + * @param text the text to set + */ + public void setText(final String text) { + this.text = text; + } + + /** + * @return the variables + */ + public List<String> getVariables() { + return variables; + } + + /** + * @param variables the variables to set + */ + public void setVariables(final List<String> variables) { + this.variables = variables; + } + + @Override + public String toString() { + return "ServiceException [messageId=" + messageId + ", text=" + text + ", variables=" + variables + "]"; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Utils.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Utils.java new file mode 100644 index 00000000..db90a235 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aai/simulator/utils/Utils.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE; +import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE_ID; +import javax.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class Utils { + + private static final String EMPTY_STRING = ""; + + public static final String getResourceVersion() { + return System.currentTimeMillis() + EMPTY_STRING; + } + + public static ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) { + return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE) + .variables(request.getMethod(), request.getRequestURI(), + "Node Not Found:No Node of type service-instance found at: " + request.getRequestURI(), + "ERR.5.4.6114") + .build(), HttpStatus.NOT_FOUND); + } + + + private Utils() {} + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/application.yaml b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/application.yaml index 69f46eab..d4f42992 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/application.yaml +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/application.yaml @@ -1,5 +1,17 @@ server: - port: 9993 - tomcat: - max-threads: 4 -ssl-enable: false + port: 9993 + tomcat: + max-threads: 4 + ssl: + key-store: classpath:keystore/org.onap.so.p12 + key-store-password: 'u!@JQoYD])@ykq.NKM,O7iA.' + keyStoreType: PKCS12 +ssl-enable: true + +spring: + security: + username: aai + #password: aai.onap.org:demo123456! + password: $2a$04$crRntT01fAF4kb48mxlvgu68/silcLg.czC1LxQsKTdWuDBPpO3YO + role: VID +
\ No newline at end of file diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/keystore/org.onap.so.p12 b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/keystore/org.onap.so.p12 Binary files differnew file mode 100644 index 00000000..015f2bf6 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/resources/keystore/org.onap.so.p12 diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/AaiSimulatorControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/AaiSimulatorControllerTest.java index 0bd6aaaa..59d3a556 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/AaiSimulatorControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/AaiSimulatorControllerTest.java @@ -17,12 +17,12 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ -package org.onap.so.aai.simulator; +package org.onap.so.aai.simulator.controller; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; -import org.onap.so.aai.simulator.utils.Constant; +import org.onap.so.aai.simulator.utils.Constants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -54,14 +54,11 @@ public class AaiSimulatorControllerTest { final String url = getBaseUrl() + "/healthcheck"; final ResponseEntity<String> object = restTemplate.getForEntity(url, String.class); - assertEquals(Constant.HEALTHY, object.getBody()); - + assertEquals(Constants.HEALTHY, object.getBody()); } private String getBaseUrl() { - return "http://localhost:" + port + Constant.BASE_URL; + return "https://localhost:" + port + Constants.BASE_URL; } - - } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/BusinessControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/BusinessControllerTest.java new file mode 100644 index 00000000..283a2a20 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/BusinessControllerTest.java @@ -0,0 +1,315 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.onap.so.aai.simulator.utils.TestConstants.CUSTOMERS_URL; +import static org.onap.so.aai.simulator.utils.TestConstants.GLOBAL_CUSTOMER_ID; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCES_URL; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_ID; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_URL; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_NAME; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_TYPE; +import static org.onap.so.aai.simulator.utils.TestUtils.getJsonString; +import java.io.IOException; +import java.util.Optional; +import java.util.UUID; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.aai.domain.yang.Customer; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.aai.domain.yang.ServiceInstances; +import org.onap.aai.domain.yang.ServiceSubscription; +import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider; +import org.onap.so.aai.simulator.utils.Constants; +import org.onap.so.aai.simulator.utils.RequestError; +import org.onap.so.aai.simulator.utils.ServiceException; +import org.onap.so.aai.simulator.utils.TestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration +public class BusinessControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Value("${spring.security.username}") + private String username; + + @Autowired + private CustomerCacheServiceProvider cacheServiceProvider; + + @After + public void after() { + cacheServiceProvider.clearAll(); + } + + @Test + public void test_putCustomer_successfullyAddedToCache() throws Exception { + final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); + assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent()); + } + + @Test + public void test_getCustomer_ableToRetrieveCustomer() throws Exception { + final String url = getCustomerEndPointUrl(); + + invokeHttpPut(url, getCustomer()); + + final ResponseEntity<Customer> actual = + restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class); + + assertEquals(HttpStatus.OK, actual.getStatusCode()); + assertTrue(actual.hasBody()); + + final Customer actualCustomer = actual.getBody(); + assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId()); + assertNotNull(actualCustomer.getResourceVersion()); + assertFalse(actualCustomer.getResourceVersion().isEmpty()); + } + + @Test + public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception { + final String url = getCustomerEndPointUrl(); + + final ResponseEntity<RequestError> actual = + restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class); + + assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode()); + + final RequestError actualError = actual.getBody(); + final ServiceException serviceException = actualError.getServiceException(); + + assertNotNull(serviceException); + assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId()); + assertEquals(Constants.ERROR_MESSAGE, serviceException.getText()); + assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString())); + + } + + @Test + public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception { + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL; + + invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET, + new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class); + + assertEquals(HttpStatus.OK, actual.getStatusCode()); + assertTrue(actual.hasBody()); + + final ServiceSubscription actualServiceSubscription = actual.getBody(); + assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType()); + assertNotNull(actualServiceSubscription.getRelationshipList()); + assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty()); + } + + @Test + public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + invokeHttpPut(url, getServiceInstance()); + + final Optional<ServiceInstance> actual = + cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID); + + assertTrue(actual.isPresent()); + final ServiceInstance actualServiceInstance = actual.get(); + + assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId()); + assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName()); + + } + + @Test + public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache() + throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + invokeHttpPut(url, getServiceInstance()); + + final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL + + "?service-instance-name=" + SERVICE_NAME; + + final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET, + new HttpEntity<>(getHttpHeaders()), ServiceInstances.class); + + assertEquals(HttpStatus.OK, actual.getStatusCode()); + assertTrue(actual.hasBody()); + + final ServiceInstances actualServiceInstances = actual.getBody(); + assertFalse(actualServiceInstances.getServiceInstance().isEmpty()); + + assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName()); + + } + + @Test + public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache() + throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + invokeHttpPut(url, getServiceInstance()); + + final ResponseEntity<ServiceInstance> actual = + restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class); + + assertEquals(HttpStatus.OK, actual.getStatusCode()); + assertTrue(actual.hasBody()); + + final ServiceInstance actualServiceInstance = actual.getBody(); + + assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName()); + assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId()); + + } + + @Test + public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + invokeHttpPut(url, getServiceInstance()); + + final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID(); + + final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET, + new HttpEntity<>(getHttpHeaders()), RequestError.class); + + assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode()); + + final RequestError actualError = actual.getBody(); + final ServiceException serviceException = actualError.getServiceException(); + + assertNotNull(serviceException); + assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId()); + assertEquals(Constants.ERROR_MESSAGE, serviceException.getText()); + assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString())); + + } + + @Test + public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance()); + assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode()); + + + final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL + + "?service-instance-name=Dummy&depth=2"; + + final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET, + new HttpEntity<>(getHttpHeaders()), RequestError.class); + + assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode()); + + final RequestError actualError = actual.getBody(); + final ServiceException serviceException = actualError.getServiceException(); + + assertNotNull(serviceException); + assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId()); + assertEquals(Constants.ERROR_MESSAGE, serviceException.getText()); + assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString())); + + } + + private String getCustomer() throws Exception, IOException { + return getJsonString("test-data/business-customer.json"); + } + + private String getCustomerEndPointUrl() { + return TestUtils.getBaseUrl(port) + CUSTOMERS_URL; + } + + private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) { + final HttpEntity<?> httpEntity = getHttpEntity(obj); + return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); + } + + private HttpEntity<?> getHttpEntity(final Object obj) { + return new HttpEntity<>(obj, getHttpHeaders()); + } + + private HttpHeaders getHttpHeaders() { + return TestUtils.getHttpHeaders(username); + } + + private String getServiceInstance() throws Exception, IOException { + return getJsonString("test-data/service-instance.json"); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/NodesControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/NodesControllerTest.java new file mode 100644 index 00000000..5ea6dff0 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/NodesControllerTest.java @@ -0,0 +1,180 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.onap.so.aai.simulator.utils.Constants.RESOURCE_LINK; +import static org.onap.so.aai.simulator.utils.Constants.RESOURCE_TYPE; +import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE; +import static org.onap.so.aai.simulator.utils.TestConstants.CUSTOMERS_URL; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_ID; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_URL; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_NAME; +import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL; +import static org.onap.so.aai.simulator.utils.TestUtils.getJsonString; +import java.io.IOException; +import java.util.Map; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.so.aai.simulator.models.Format; +import org.onap.so.aai.simulator.models.Result; +import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider; +import org.onap.so.aai.simulator.service.providers.NodesCacheServiceProvider; +import org.onap.so.aai.simulator.utils.Constants; +import org.onap.so.aai.simulator.utils.TestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration +public class NodesControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Value("${spring.security.username}") + private String username; + + @Autowired + private NodesCacheServiceProvider nodesCacheServiceProvider; + + @Autowired + private CustomerCacheServiceProvider customerCacheServiceProvider; + + @After + public void after() { + nodesCacheServiceProvider.clearAll(); + customerCacheServiceProvider.clearAll(); + } + + @Test + public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache() + throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + final ResponseEntity<Void> response2 = invokeHttpPut(url, getServiceInstance()); + assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode()); + + final ResponseEntity<ServiceInstance> actual = + restTemplate.exchange(getNodesEndPointUrl() + SERVICE_INSTANCE_URL, HttpMethod.GET, + new HttpEntity<>(getHttpHeaders()), ServiceInstance.class); + + assertEquals(HttpStatus.OK, actual.getStatusCode()); + assertTrue(actual.hasBody()); + + final ServiceInstance actualServiceInstance = actual.getBody(); + + assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName()); + assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId()); + + } + + @Test + public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache() + throws Exception { + + final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL; + + final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer()); + + assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + + final ResponseEntity<Void> response2 = invokeHttpPut(url, getServiceInstance()); + assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode()); + + final ResponseEntity<Result> actual = restTemplate.exchange( + getNodesEndPointUrl() + SERVICE_INSTANCE_URL + "?format=" + Format.PATHED.getValue(), HttpMethod.GET, + new HttpEntity<>(getHttpHeaders()), Result.class); + + assertEquals(HttpStatus.OK, actual.getStatusCode()); + assertTrue(actual.hasBody()); + + final Result result = actual.getBody(); + + assertNotNull(result.getValues()); + assertFalse(result.getValues().isEmpty()); + final Map<String, Object> actualMap = result.getValues().get(0); + + assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK)); + assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE)); + + } + + private String getNodesEndPointUrl() { + return TestUtils.getBaseUrl(port) + Constants.NODES_URL; + } + + + private String getCustomerEndPointUrl() { + return TestUtils.getBaseUrl(port) + CUSTOMERS_URL; + } + + private String getCustomer() throws Exception, IOException { + return getJsonString("test-data/business-customer.json"); + } + + private String getServiceInstance() throws Exception, IOException { + return getJsonString("test-data/service-instance.json"); + } + + private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) { + final HttpEntity<?> httpEntity = getHttpEntity(obj); + return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); + } + + private HttpEntity<?> getHttpEntity(final Object obj) { + return new HttpEntity<>(obj, getHttpHeaders()); + } + + private HttpHeaders getHttpHeaders() { + return TestUtils.getHttpHeaders(username); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/ProjectControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/ProjectControllerTest.java new file mode 100644 index 00000000..b198e356 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/ProjectControllerTest.java @@ -0,0 +1,171 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.onap.so.aai.simulator.utils.TestUtils.getFile; +import static org.onap.so.aai.simulator.utils.TestUtils.getHttpHeaders; +import static org.onap.so.aai.simulator.utils.TestUtils.getJsonString; +import java.io.IOException; +import java.nio.file.Files; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.aai.domain.yang.Project; +import org.onap.so.aai.simulator.models.Result; +import org.onap.so.aai.simulator.service.providers.ProjectCacheServiceProvider; +import org.onap.so.aai.simulator.utils.Constants; +import org.onap.so.aai.simulator.utils.TestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration +public class ProjectControllerTest { + + private static final String RELATIONSHIP_URL = "/relationship-list/relationship"; + + private static final String BUSINESS_PROJECT_JSON_FILE = "test-data/business-project.json"; + + private static final String PROJECT_RELATION_SHIP_JSON_FILE = "test-data/business-project-relation-ship.json"; + + private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE"; + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Value("${spring.security.username}") + private String username; + + @Autowired + private ProjectCacheServiceProvider cacheServiceProvider; + + @After + public void after() { + cacheServiceProvider.clearAll(); + } + + @Test + public void test_putProject_successfullyAddedToCache() throws Exception { + final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE; + final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath())); + final ResponseEntity<Void> actual = invokeHttpPut(url, body); + + assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); + + final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class); + + assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); + assertTrue(actualResponse.hasBody()); + final Project actualProject = actualResponse.getBody(); + assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName()); + assertNotNull(actualProject.getResourceVersion()); + + } + + @Test + public void test_putProjectRelationShip_successfullyAddedToCache() throws Exception { + final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE; + final ResponseEntity<Void> actual = invokeHttpPut(url, getJsonString(BUSINESS_PROJECT_JSON_FILE)); + assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); + + final String projectRelationshipUrl = url + RELATIONSHIP_URL; + + final ResponseEntity<Void> putResponse = invokeHttpPut(projectRelationshipUrl, getRelationship()); + + assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode()); + + final ResponseEntity<Project> actualResponse = invokeHttpGet(url, Project.class); + + assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); + assertTrue(actualResponse.hasBody()); + final Project actualProject = actualResponse.getBody(); + assertEquals(PROJECT_NAME_VALUE, actualProject.getProjectName()); + assertNotNull(actualProject.getRelationshipList()); + assertFalse(actualProject.getRelationshipList().getRelationship().isEmpty()); + assertNotNull(actualProject.getRelationshipList().getRelationship().get(0)); + + } + + @Test + public void test_getProjectCount_correctResult() throws Exception { + final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE; + final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath())); + final ResponseEntity<Void> actual = invokeHttpPut(url, body); + + assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); + + final ResponseEntity<Result> actualResponse = + invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Result.class); + + assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); + assertTrue(actualResponse.hasBody()); + final Result result = actualResponse.getBody(); + assertNotNull(result.getValues()); + assertFalse(result.getValues().isEmpty()); + assertEquals(1, result.getValues().get(0).get(PROJECT_NAME_VALUE)); + + + } + + private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) { + return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(username)), clazz); + } + + private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) { + final HttpEntity<?> httpEntity = getHttpEntity(obj); + return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); + } + + private HttpEntity<?> getHttpEntity(final Object obj) { + return new HttpEntity<>(obj, getHttpHeaders(username)); + } + + private String getProjectEndPointUrl() { + return TestUtils.getBaseUrl(port) + Constants.PROJECT_URL; + } + + private String getRelationship() throws IOException, Exception { + return TestUtils.getJsonString(PROJECT_RELATION_SHIP_JSON_FILE); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/configuration/TestRestTemplateConfigration.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/configuration/TestRestTemplateConfigration.java new file mode 100644 index 00000000..6e8ccac6 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/controller/configuration/TestRestTemplateConfigration.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.controller.configuration; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLSession; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Configuration +public class TestRestTemplateConfigration { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestRestTemplateConfigration.class); + + @Bean + public TestRestTemplate testRestTemplate() throws Exception { + + final TestRestTemplate testRestTemplate = new TestRestTemplate(); + ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRestTemplate().getRequestFactory()) + .setHttpClient(httpClient()); + return testRestTemplate; + + } + + private CloseableHttpClient httpClient() throws Exception { + final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + + final SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory( + SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(), new HostnameVerifier() { + @Override + public boolean verify(final String hostname, final SSLSession session) { + LOGGER.warn("Skiping hostname verification ... "); + return true; + } + + }); + + return HttpClients.custom().setSSLSocketFactory(csf).build(); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/utils/TestConstants.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/utils/TestConstants.java new file mode 100644 index 00000000..0bdf7a79 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/utils/TestConstants.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class TestConstants { + + public static final String SERVICE_INSTANCES_URL = "/service-instances"; + + public static final String SERVICE_NAME = "ServiceTest"; + + public static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b"; + + public static final String SERVICE_INSTANCE_URL = + SERVICE_INSTANCES_URL + "/service-instance/" + SERVICE_INSTANCE_ID; + + public static final String SERVICE_TYPE = "vCPE"; + + public static final String SERVICE_SUBSCRIPTIONS_URL = + "/service-subscriptions/service-subscription/" + SERVICE_TYPE; + + public static final String GLOBAL_CUSTOMER_ID = "DemoCustomer"; + + public static final String CUSTOMERS_URL = Constants.CUSTOMER_URL + GLOBAL_CUSTOMER_ID; + + private TestConstants() {} + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/utils/TestUtils.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/utils/TestUtils.java new file mode 100644 index 00000000..73e43b10 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aai/simulator/utils/TestUtils.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aai.simulator.utils; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Base64; +import org.springframework.core.io.ClassPathResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; + +/** + * @author waqas.ikram@ericsson.com + * + */ + +public class TestUtils { + + private static final String PASSWORD = "aai.onap.org:demo123456!"; + + public static HttpHeaders getHttpHeaders(final String username) { + final HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Authorization", getBasicAuth(username)); + requestHeaders.setContentType(MediaType.APPLICATION_JSON); + return requestHeaders; + } + + public static File getFile(final String file) throws IOException { + return new ClassPathResource(file).getFile(); + } + + public static String getJsonString(final String file) throws IOException { + return new String(Files.readAllBytes(getFile(file).toPath())); + } + + public static <T> T getObjectFromFile(final File file, final Class<T> clazz) throws Exception { + final ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JaxbAnnotationModule()); + + return mapper.readValue(file, clazz); + } + + public static String getBasicAuth(final String username) { + return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes())); + } + + public static String getBaseUrl(final int port) { + return "https://localhost:" + port; + } + + private TestUtils() {} + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-customer.json b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-customer.json new file mode 100644 index 00000000..6c53c056 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-customer.json @@ -0,0 +1,73 @@ +{ + "global-customer-id": "DemoCustomer", + "subscriber-name": "DemoCustomer", + "subscriber-type": "INFRA", + "service-subscriptions": { + "service-subscription": [ + { + "service-type": "vLB", + "relationship-list": { + "relationship": [ + { + "related-to": "tenant", + "relationship-label": "org.onap.relationships.inventory.Uses", + "related-link": "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/xyzcloud/tenants/tenant/693c7729b2364a26a3ca602e6f66187d", + "relationship-data": [ + { + "relationship-key": "cloud-region.cloud-owner", + "relationship-value": "CloudOwner" + }, + { + "relationship-key": "cloud-region.cloud-region-id", + "relationship-value": "xyzcloud" + }, + { + "relationship-key": "tenant.tenant-id", + "relationship-value": "693c7729b2364a26a3ca602e6f66187d" + } + ], + "related-to-property": [ + { + "property-key": "tenant.tenant-name", + "property-value": "admin" + } + ] + } + ] + } + }, + { + "service-type": "vCPE", + "relationship-list": { + "relationship": [ + { + "related-to": "tenant", + "relationship-label": "org.onap.relationships.inventory.Uses", + "related-link": "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/xyzcloud/tenants/tenant/693c7729b2364a26a3ca602e6f66187d", + "relationship-data": [ + { + "relationship-key": "cloud-region.cloud-owner", + "relationship-value": "CloudOwner" + }, + { + "relationship-key": "cloud-region.cloud-region-id", + "relationship-value": "xyzcloud" + }, + { + "relationship-key": "tenant.tenant-id", + "relationship-value": "693c7729b2364a26a3ca602e6f66187d" + } + ], + "related-to-property": [ + { + "property-key": "tenant.tenant-name", + "property-value": "admin" + } + ] + } + ] + } + } + ] + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-project-relation-ship.json b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-project-relation-ship.json new file mode 100644 index 00000000..9f6e5f35 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-project-relation-ship.json @@ -0,0 +1,3 @@ +{ + "related-link": "/aai/v15/business/customers/customer/NordixDemoCustomer/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/ae93f2d8-7d06-4eeb-8712-93a2bdb776f6" +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-project.json b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-project.json new file mode 100644 index 00000000..f78ddfaf --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/business-project.json @@ -0,0 +1,3 @@ +{ + "project-name": "PROJECT_NAME_VALUE" +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/service-instance.json b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/service-instance.json new file mode 100644 index 00000000..18ae8cb6 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/service-instance.json @@ -0,0 +1,9 @@ +{ + "service-instance-id": "ccece8fe-13da-456a-baf6-41b3a4a2bc2b", + "service-instance-name": "ServiceTest", + "environment-context": "General_Revenue-Bearing", + "workload-context": "Production", + "model-invariant-id": "e9acd081-9c89-4b4d-bcb3-e0e2b9715b2a", + "model-version-id": "c112a499-6148-488b-ba82-3f5938cf26d2", + "orchestration-status": "Inventoried" +} |