From fa9bb10f04035acb0621dd0f0a204b97cdd002fb Mon Sep 17 00:00:00 2001 From: AndrewLamb Date: Thu, 3 Oct 2019 12:02:53 +0100 Subject: Move the VNFM Simulator into CSIT Change-Id: I7335be1a0adb033813fa5405526e95460134d9a6 Issue-ID: SO-1952 Signed-off-by: AndrewLamb --- .../svnfm/simulator/config/ApplicationConfig.java | 53 ++++ .../svnfm/simulator/config/SvnfmApplication.java | 46 +++ .../simulator/config/WebSecurityConfigImpl.java | 48 +++ .../so/svnfm/simulator/constants/Constant.java | 39 +++ .../simulator/controller/SvnfmController.java | 171 ++++++++++ .../exception/GlobalExceptionHandler.java | 47 +++ .../onap/so/svnfm/simulator/model/VnfInstance.java | 182 +++++++++++ .../so/svnfm/simulator/model/VnfOperation.java | 80 +++++ .../org/onap/so/svnfm/simulator/model/Vnfds.java | 97 ++++++ .../VnfInstantiationNotification.java | 47 +++ .../VnfmAdapterCreationNotification.java | 48 +++ .../VnfmAdapterDeletionNotification.java | 48 +++ .../simulator/oauth/AuthorizationServerConfig.java | 28 ++ .../oauth/JsonSerializerConfiguration.java | 49 +++ .../simulator/oauth/OAuth2AccessTokenAdapter.java | 31 ++ .../simulator/oauth/OAuth2ResourceServer.java | 36 +++ .../repository/VnfOperationRepository.java | 33 ++ .../simulator/repository/VnfmCacheRepository.java | 60 ++++ .../svnfm/simulator/repository/VnfmRepository.java | 35 +++ .../services/InstantiateOperationProgressor.java | 123 ++++++++ .../simulator/services/OperationProgressor.java | 348 +++++++++++++++++++++ .../simulator/services/SubscriptionService.java | 21 ++ .../so/svnfm/simulator/services/SvnfmService.java | 169 ++++++++++ .../services/TerminateOperationProgressor.java | 74 +++++ .../so/svnfm/simulator/services/VnfmHelper.java | 106 +++++++ .../src/main/resources/application.yaml | 64 ++++ .../src/main/resources/so-vnfm-adapter.crt.pem | 30 ++ .../src/main/resources/so-vnfm-simulator.p12 | Bin 0 -> 4079 bytes .../simulator/controllers/TestSvnfmController.java | 183 +++++++++++ .../InstantiateOperatorProgressorTest.java | 169 ++++++++++ .../svnfm/simulator/services/SvnfmServiceTest.java | 203 ++++++++++++ .../services/TerminateOperationProgressorTest.java | 157 ++++++++++ .../svnfm/simulator/services/VnfmHelperTest.java | 104 ++++++ 33 files changed, 2929 insertions(+) create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/ApplicationConfig.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/SvnfmApplication.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/WebSecurityConfigImpl.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/constants/Constant.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/controller/SvnfmController.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/exception/GlobalExceptionHandler.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfInstance.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfOperation.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/Vnfds.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfInstantiationNotification.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterCreationNotification.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterDeletionNotification.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/AuthorizationServerConfig.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/JsonSerializerConfiguration.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2AccessTokenAdapter.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2ResourceServer.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfOperationRepository.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmCacheRepository.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmRepository.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/InstantiateOperationProgressor.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/OperationProgressor.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SubscriptionService.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SvnfmService.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressor.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/VnfmHelper.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/application.yaml create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-adapter.crt.pem create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-simulator.p12 create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/controllers/TestSvnfmController.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/InstantiateOperatorProgressorTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/SvnfmServiceTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressorTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/VnfmHelperTest.java (limited to 'plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src') diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/ApplicationConfig.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/ApplicationConfig.java new file mode 100644 index 00000000..79b3fa86 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/ApplicationConfig.java @@ -0,0 +1,53 @@ +package org.onap.so.svnfm.simulator.config; + +import java.util.Arrays; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +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.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class ApplicationConfig implements ApplicationRunner { + + private static final String PORT = "local.server.port"; + + @Value("${server.dns.name:so-vnfm-simulator.onap}") + private String serverDnsName; + + @Value("${server.request.grant.auth:oauth}") + private String grantAuth; + + @Autowired + private Environment environment; + + private String baseUrl; + + @Override + public void run(final ApplicationArguments args) throws Exception { + baseUrl = "https://" + serverDnsName + ":" + environment.getProperty(PORT); + } + + public String getBaseUrl() { + return baseUrl; + } + + public String getGrantAuth() { + return grantAuth; + } + + @Bean + public CacheManager cacheManager() { + final Cache inlineResponse201 = new ConcurrentMapCache(Constant.IN_LINE_RESPONSE_201_CACHE); + final SimpleCacheManager manager = new SimpleCacheManager(); + manager.setCaches(Arrays.asList(inlineResponse201)); + return manager; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/SvnfmApplication.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/SvnfmApplication.java new file mode 100644 index 00000000..612e61d0 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/SvnfmApplication.java @@ -0,0 +1,46 @@ +/*- + * ============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.svnfm.simulator.config; + +import org.onap.so.svnfm.simulator.controller.SvnfmController; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +/** + * The spring boot application for the VNF LCM. + *

+ * The VNFM receives requests through its REST API {@link SvnfmController} + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author ronan.kenny@est.tech + */ +@SpringBootApplication(scanBasePackages = {"org.onap"}) +@EnableJpaRepositories("org.onap.so.svnfm.simulator") +@EntityScan("org.onap.so.svnfm.simulator.model") +@EnableCaching +public class SvnfmApplication { + public static void main(final String[] args) { + SpringApplication.run(SvnfmApplication.class, args); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/WebSecurityConfigImpl.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/WebSecurityConfigImpl.java new file mode 100644 index 00000000..14d5c06b --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/config/WebSecurityConfigImpl.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.svnfm.simulator.config; + +import org.onap.so.security.MSOSpringFirewall; +import org.onap.so.security.WebSecurityConfig; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.firewall.StrictHttpFirewall; + +/** + * Configure the web security for the application. + */ +@EnableWebSecurity +public class WebSecurityConfigImpl extends WebSecurityConfig { + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http.csrf().disable().authorizeRequests().antMatchers("/**").permitAll(); + } + + @Override + public void configure(final WebSecurity web) throws Exception { + super.configure(web); + final StrictHttpFirewall firewall = new MSOSpringFirewall(); + web.httpFirewall(firewall); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/constants/Constant.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/constants/Constant.java new file mode 100644 index 00000000..02a319a2 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/constants/Constant.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.svnfm.simulator.constants; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author ronan.kenny@est.tech + */ +public class Constant { + + public static final String BASE_URL = "/vnflcm/v1"; + public static final String VNF_PROVIDER = "XYZ"; + public static final String VNF_PROVIDER_NAME = "vCPE"; + public static final String VNF_SOFTWARE_VERSION = "1.24"; + public static final String VNFD_VERSION = "onapvcpe01_cxp9025898_4r85d01"; + public static final String VNF_NOT_INSTANTIATED = "NOT_INSTANTIATED"; + public static final String VNF_CONFIG_PROPERTIES = + "{\"isAutoScaleEnabled\": \"true\",\"isAutoHealingEnabled\": \"true\"}"; + public static final String IN_LINE_RESPONSE_201_CACHE = "inlineResponse201"; +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/controller/SvnfmController.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/controller/SvnfmController.java new file mode 100644 index 00000000..6683b696 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/controller/SvnfmController.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.svnfm.simulator.controller; + +import java.util.UUID; +import javax.ws.rs.core.MediaType; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.repository.VnfmCacheRepository; +import org.onap.so.svnfm.simulator.services.SvnfmService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +@RestController +@RequestMapping(path = Constant.BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON) +public class SvnfmController { + + @Autowired + private SvnfmService svnfmService; + + @Autowired + private VnfmCacheRepository vnfmCacheRepository; + + private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmController.class); + + /** + * To create the Vnf and stores the response in cache + * + * @param CreateVnfRequest + * @return InlineResponse201 + */ + @PostMapping(value = "/vnf_instances") + public ResponseEntity createVnf(@RequestBody final CreateVnfRequest createVNFRequest) { + LOGGER.info("Start createVnf {}", createVNFRequest); + final String id = UUID.randomUUID().toString(); + final HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + final ResponseEntity responseEntity = + new ResponseEntity<>(vnfmCacheRepository.createVnf(createVNFRequest, id), headers, HttpStatus.CREATED); + LOGGER.info("Finished create {}", responseEntity); + return responseEntity; + } + + /** + * Get the vnf by id from cache + * + * @param vnfId + * @return InlineResponse201 + */ + @GetMapping(value = "/vnf_instances/{vnfInstanceId}") + @ResponseStatus(code = HttpStatus.OK) + public InlineResponse201 getVnf(@PathVariable("vnfInstanceId") final String vnfId) { + LOGGER.info("Start getVnf------"); + return vnfmCacheRepository.getVnf(vnfId); + } + + /** + * To instantiate the vnf and returns the operation id + * + * @param vnfId + * @throws InterruptedException + */ + @PostMapping(value = "/vnf_instances/{vnfInstanceId}/instantiate") + public ResponseEntity instantiateVnf(@PathVariable("vnfInstanceId") final String vnfId) { + LOGGER.info("Start instantiateVNFRequest for vnf id {} ", vnfId); + + final HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + headers.add(HttpHeaders.LOCATION, svnfmService.instantiateVnf(vnfId)); + return new ResponseEntity<>(headers, HttpStatus.ACCEPTED); + } + + /** + * To delete the vnf by id + * + * @param vnfId + * @return InlineResponse201 + */ + @DeleteMapping(value = "/vnf_instances/{vnfInstanceId}") + @ResponseStatus(code = HttpStatus.OK) + public ResponseEntity deleteVnf(@PathVariable("vnfInstanceId") final String vnfId) { + LOGGER.info("Start deleting Vnf------"); + vnfmCacheRepository.deleteVnf(vnfId); + final HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + return new ResponseEntity<>(headers, HttpStatus.NO_CONTENT); + } + + /** + * To terminate the vnf by id + * + * @param vnfId + * @throws InterruptedException + */ + @PostMapping(value = "/vnf_instances/{vnfInstanceId}/terminate") + public ResponseEntity terminateVnf(@PathVariable("vnfInstanceId") final String vnfId) { + LOGGER.info("Start terminateVNFRequest {}", vnfId); + final HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + headers.add(HttpHeaders.LOCATION, svnfmService.terminateVnf(vnfId)); + return new ResponseEntity<>(headers, HttpStatus.ACCEPTED); + } + + + /** + * To get the status of the operation by id + * + * @param operationId + * @return response entity + * @throws InterruptedException + */ + @GetMapping(value = "/vnf_lcm_op_occs/{vnfLcmOpOccId}") + public ResponseEntity getOperationStatus( + @PathVariable("vnfLcmOpOccId") final String operationId) { + LOGGER.info("Start getOperationStatus"); + final HttpHeaders headers = new HttpHeaders(); + headers.add("Content-Type", MediaType.APPLICATION_JSON); + return new ResponseEntity<>(svnfmService.getOperationStatus(operationId), headers, HttpStatus.OK); + } + + @PostMapping(value = "/subscriptions") + public ResponseEntity subscribeForNotifications( + @RequestBody final LccnSubscriptionRequest lccnSubscriptionRequest) { + LOGGER.info("Subscription request received: {}", lccnSubscriptionRequest); + svnfmService.registerSubscription(lccnSubscriptionRequest); + final InlineResponse2001 response = new InlineResponse2001(); + + final HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + return new ResponseEntity<>(response, headers, HttpStatus.CREATED); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/exception/GlobalExceptionHandler.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/exception/GlobalExceptionHandler.java new file mode 100644 index 00000000..5b1e5ca9 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/exception/GlobalExceptionHandler.java @@ -0,0 +1,47 @@ +/*- + * ============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.svnfm.simulator.exception; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author ronan.kenny@est.tech + */ +@ControllerAdvice +public class GlobalExceptionHandler { + + private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + public void handleConflict() { + final String error = "Media type Not Supported"; + logger.error(error); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfInstance.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfInstance.java new file mode 100644 index 00000000..a633c3ae --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfInstance.java @@ -0,0 +1,182 @@ +/*- + * ============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.svnfm.simulator.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +@Entity +@Table(name = "VNF_INSTANCE") +public class VnfInstance { + @Id + @Column(name = "id", nullable = false) + private String id; + private String vnfInstanceName; + private String vnfInstanceDescription; + private String vnfdId; + private String vnfProvider; + private String vnfProductName; + private String vnfSoftwareVersion; + private String vnfdVersion; + private String vnfPkgId; + private String vnfConfigurableProperties; + private String vimConnectionInfo; + private String vnfInstantiationState; + private String instantiatedVnfInfo; + private String metadata; + private String extensions; + private String links; + + public String getId() { + return id; + } + + public void setId(final String id) { + this.id = id; + } + + public String getVnfInstanceName() { + return vnfInstanceName; + } + + public void setVnfInstanceName(final String vnfInstanceName) { + this.vnfInstanceName = vnfInstanceName; + } + + public String getVnfInstanceDescription() { + return vnfInstanceDescription; + } + + public void setVnfInstanceDescription(final String vnfInstanceDescription) { + this.vnfInstanceDescription = vnfInstanceDescription; + } + + public String getVnfdId() { + return vnfdId; + } + + public void setVnfdId(final String vnfdId) { + this.vnfdId = vnfdId; + } + + public String getVnfProvider() { + return vnfProvider; + } + + public void setVnfProvider(final String vnfProvider) { + this.vnfProvider = vnfProvider; + } + + public String getVnfProductName() { + return vnfProductName; + } + + public void setVnfProductName(final String vnfProductName) { + this.vnfProductName = vnfProductName; + } + + public String getVnfSoftwareVersion() { + return vnfSoftwareVersion; + } + + public void setVnfSoftwareVersion(final String vnfSoftwareVersion) { + this.vnfSoftwareVersion = vnfSoftwareVersion; + } + + public String getVnfdVersion() { + return vnfdVersion; + } + + public void setVnfdVersion(final String vnfdVersion) { + this.vnfdVersion = vnfdVersion; + } + + public String getVnfPkgId() { + return vnfPkgId; + } + + public void setVnfPkgId(final String vnfPkgId) { + this.vnfPkgId = vnfPkgId; + } + + public String getVnfConfigurableProperties() { + return vnfConfigurableProperties; + } + + public void setVnfConfigurableProperties(final String vnfConfigurableProperties) { + this.vnfConfigurableProperties = vnfConfigurableProperties; + } + + public String getVimConnectionInfo() { + return vimConnectionInfo; + } + + public void setVimConnectionInfo(final String vimConnectionInfo) { + this.vimConnectionInfo = vimConnectionInfo; + } + + public String getInstantiationState() { + return vnfInstantiationState; + } + + public void setVnfInstantiationState(final String vnfInstantiationState) { + this.vnfInstantiationState = vnfInstantiationState; + } + + public String getVnfInstantiationState() { + return instantiatedVnfInfo; + } + + public void setInstantiatedVnfInfo(final String instantiatedVnfInfo) { + this.instantiatedVnfInfo = instantiatedVnfInfo; + } + + public String getMetadata() { + return metadata; + } + + public void setMetadata(final String metadata) { + this.metadata = metadata; + } + + public String getExtensions() { + return extensions; + } + + public void setExtensions(final String extensions) { + this.extensions = extensions; + } + + public String getLinks() { + return links; + } + + public void setLinks(final String links) { + this.links = links; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfOperation.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfOperation.java new file mode 100644 index 00000000..a7380dc5 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/VnfOperation.java @@ -0,0 +1,80 @@ +/*- + * ============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.svnfm.simulator.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.Id; +import javax.persistence.Table; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +@Entity +@Table(name = "VNF_OPERATION") +public class VnfOperation { + @Id + @Column(name = "operationId", nullable = false) + private String id; + private String vnfInstanceId; + + @Enumerated(EnumType.STRING) + private InlineResponse200.OperationEnum operation; + + @Enumerated(EnumType.STRING) + private InlineResponse200.OperationStateEnum operationState; + + public String getId() { + return id; + } + + public void setId(final String id) { + this.id = id; + } + + public String getVnfInstanceId() { + return vnfInstanceId; + } + + public void setVnfInstanceId(final String vnfInstanceId) { + this.vnfInstanceId = vnfInstanceId; + } + + public InlineResponse200.OperationEnum getOperation() { + return operation; + } + + public void setOperation(final InlineResponse200.OperationEnum operation) { + this.operation = operation; + } + + public InlineResponse200.OperationStateEnum getOperationState() { + return operationState; + } + + public void setOperationState(final InlineResponse200.OperationStateEnum operationState) { + this.operationState = operationState; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/Vnfds.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/Vnfds.java new file mode 100644 index 00000000..cf550067 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/model/Vnfds.java @@ -0,0 +1,97 @@ +package org.onap.so.svnfm.simulator.model; + +import java.util.List; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@ConfigurationProperties(prefix = "vnfds") +@Component +public class Vnfds { + + private List vnfdList; + + public static class Vnfd { + + private String vnfdId; + private List vnfclist; + + + public String getVnfdId() { + return vnfdId; + } + + public void setVnfdId(final String vnfdId) { + this.vnfdId = vnfdId; + } + + public List getVnfcList() { + return vnfclist; + } + + public void setVnfcList(final List vnfclist) { + this.vnfclist = vnfclist; + } + } + + + public static class Vnfc { + + private String vnfcId; + private String type; + private String vduId; + private String resourceTemplateId; + private String grantResourceId; + + public String getVnfcId() { + return vnfcId; + } + + public void setVnfcId(final String vnfcId) { + this.vnfcId = vnfcId; + } + + public String getVduId() { + return vduId; + } + + public void setVduId(final String vduId) { + this.vduId = vduId; + } + + public String getType() { + return type; + } + + public void setType(final String type) { + this.type = type; + } + + public String getResourceTemplateId() { + return resourceTemplateId; + } + + public void setResourceTemplateId(final String resourceTemplateId) { + this.resourceTemplateId = resourceTemplateId; + } + + public String getGrantResourceId() { + return grantResourceId; + } + + public void setGrantResourceId(final String grantResourceId) { + this.grantResourceId = grantResourceId; + } + + } + + + public List getVnfdList() { + return vnfdList; + } + + + public void setVnfdList(final List vnfdList) { + this.vnfdList = vnfdList; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfInstantiationNotification.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfInstantiationNotification.java new file mode 100644 index 00000000..a8c77db0 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfInstantiationNotification.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.svnfm.simulator.notifications; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +public class VnfInstantiationNotification implements Runnable { + + private static final Logger logger = LoggerFactory.getLogger(VnfInstantiationNotification.class); + + @Override + public void run() { + try { + Thread.sleep(10000); + } catch (final InterruptedException e) { + logger.error("Error occured while simulating instantiation ", e); + Thread.currentThread().interrupt(); + } + logger.info("Instantiation process finished"); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterCreationNotification.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterCreationNotification.java new file mode 100644 index 00000000..667b99e8 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterCreationNotification.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.svnfm.simulator.notifications; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +public class VnfmAdapterCreationNotification implements Runnable { + + private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterCreationNotification.class); + + @Override + public void run() { + try { + Thread.sleep(10000); + } catch (final InterruptedException e) { + logger.error("Error occured while simulating creation ", e); + Thread.currentThread().interrupt(); + } + logger.info("Call to VNFM Adapter-Create"); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterDeletionNotification.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterDeletionNotification.java new file mode 100644 index 00000000..eaab1045 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/notifications/VnfmAdapterDeletionNotification.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * 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.svnfm.simulator.notifications; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +public class VnfmAdapterDeletionNotification implements Runnable { + + private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterDeletionNotification.class); + + @Override + public void run() { + try { + Thread.sleep(10000); + } catch (final InterruptedException e) { + logger.error("Error occured while simulating deletion ", e); + Thread.currentThread().interrupt(); + } + logger.info("Call to VNFM Adapter-Delete"); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/AuthorizationServerConfig.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/AuthorizationServerConfig.java new file mode 100644 index 00000000..500c85be --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/AuthorizationServerConfig.java @@ -0,0 +1,28 @@ +package org.onap.so.svnfm.simulator.oauth; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; + +@Configuration +@EnableAuthorizationServer +@Profile("oauth-authentication") +/** + * Configures the authorization server for oauth token based authentication when the spring profile + * "oauth-authentication" is active + */ +public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { + + private static final int ONE_DAY = 60 * 60 * 24; + + @Override + public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { + clients.inMemory().withClient("vnfmadapter") + .secret("$2a$10$dHzTlqSBcm8hdO52LBvnX./zNTvUzzJy.lZrc4bCBL5gkln0wX6T6") + .authorizedGrantTypes("client_credentials").scopes("write").accessTokenValiditySeconds(ONE_DAY) + .refreshTokenValiditySeconds(ONE_DAY); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/JsonSerializerConfiguration.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/JsonSerializerConfiguration.java new file mode 100644 index 00000000..7d5d6124 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/JsonSerializerConfiguration.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.svnfm.simulator.oauth; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.util.ArrayList; +import java.util.Collection; +import org.springframework.boot.autoconfigure.http.HttpMessageConverters; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.GsonHttpMessageConverter; +import org.springframework.security.oauth2.common.OAuth2AccessToken; + +/** + * Configures message converter + */ +@Configuration +public class JsonSerializerConfiguration { + + @Bean + public HttpMessageConverters customConverters() { + final Collection> messageConverters = new ArrayList<>(); + + final Gson gson = new GsonBuilder() + .registerTypeHierarchyAdapter(OAuth2AccessToken.class, new OAuth2AccessTokenAdapter()).create(); + final GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(gson); + messageConverters.add(gsonHttpMessageConverter); + return new HttpMessageConverters(true, messageConverters); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2AccessTokenAdapter.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2AccessTokenAdapter.java new file mode 100644 index 00000000..ac96a828 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2AccessTokenAdapter.java @@ -0,0 +1,31 @@ +package org.onap.so.svnfm.simulator.oauth; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import java.lang.reflect.Type; +import org.springframework.security.oauth2.common.OAuth2AccessToken; + +public class OAuth2AccessTokenAdapter implements JsonSerializer { + + @Override + public JsonElement serialize(final OAuth2AccessToken src, final Type typeOfSrc, + final JsonSerializationContext context) { + final JsonObject obj = new JsonObject(); + obj.addProperty(OAuth2AccessToken.ACCESS_TOKEN, src.getValue()); + obj.addProperty(OAuth2AccessToken.TOKEN_TYPE, src.getTokenType()); + if (src.getRefreshToken() != null) { + obj.addProperty(OAuth2AccessToken.REFRESH_TOKEN, src.getRefreshToken().getValue()); + } + obj.addProperty(OAuth2AccessToken.EXPIRES_IN, src.getExpiresIn()); + final JsonArray scopeObj = new JsonArray(); + for (final String scope : src.getScope()) { + scopeObj.add(scope); + } + obj.add(OAuth2AccessToken.SCOPE, scopeObj); + + return obj; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2ResourceServer.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2ResourceServer.java new file mode 100644 index 00000000..749abef4 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/oauth/OAuth2ResourceServer.java @@ -0,0 +1,36 @@ +/*- + * ============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.svnfm.simulator.oauth; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; + +@Configuration +@EnableResourceServer +@Profile("oauth-authentication") +/** + * Enforces oauth token based authentication when the spring profile "oauth-authentication" is active + */ +public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfOperationRepository.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfOperationRepository.java new file mode 100644 index 00000000..e179c713 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfOperationRepository.java @@ -0,0 +1,33 @@ +/*- + * ============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.svnfm.simulator.repository; + +import org.onap.so.svnfm.simulator.model.VnfOperation; +import org.springframework.data.repository.CrudRepository; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +public interface VnfOperationRepository extends CrudRepository { + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmCacheRepository.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmCacheRepository.java new file mode 100644 index 00000000..76d73198 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmCacheRepository.java @@ -0,0 +1,60 @@ +/*- + * ============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.svnfm.simulator.repository; + +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.services.SvnfmService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Repository; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +@Repository +public class VnfmCacheRepository { + + @Autowired + private SvnfmService svnfmService; + + @Cacheable(value = Constant.IN_LINE_RESPONSE_201_CACHE, key = "#id") + public InlineResponse201 createVnf(final CreateVnfRequest createVnfRequest, final String id) { + return svnfmService.createVnf(createVnfRequest, id); + } + + + + public InlineResponse201 getVnf(final String id) { + return svnfmService.getVnf(id); + } + + /** + * @param vnfId + * @return + */ + @CacheEvict(value = Constant.IN_LINE_RESPONSE_201_CACHE, key = "#id") + public void deleteVnf(final String id) {} +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmRepository.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmRepository.java new file mode 100644 index 00000000..37c6bb2f --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/repository/VnfmRepository.java @@ -0,0 +1,35 @@ +/*- + * ============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.svnfm.simulator.repository; + +import java.util.Optional; +import org.onap.so.svnfm.simulator.model.VnfInstance; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +public interface VnfmRepository extends CrudRepository { + public Optional findById(@Param("id") String id); +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/InstantiateOperationProgressor.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/InstantiateOperationProgressor.java new file mode 100644 index 00000000..0a5444b9 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/InstantiateOperationProgressor.java @@ -0,0 +1,123 @@ +package org.onap.so.svnfm.simulator.services; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.modelmapper.ModelMapper; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources.TypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201AddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoResourceHandle; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.model.Vnfds.Vnfc; +import org.onap.so.svnfm.simulator.model.Vnfds.Vnfd; +import org.onap.so.svnfm.simulator.repository.VnfOperationRepository; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.model.VnfOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class InstantiateOperationProgressor extends OperationProgressor { + + private static final Logger LOGGER = LoggerFactory.getLogger(InstantiateOperationProgressor.class); + + public InstantiateOperationProgressor(final VnfOperation operation, final SvnfmService svnfmService, + final VnfOperationRepository vnfOperationRepository, final ApplicationConfig applicationConfig, + final Vnfds vnfds, final SubscriptionService subscriptionService) { + super(operation, svnfmService, vnfOperationRepository, applicationConfig, vnfds, subscriptionService); + } + + @Override + protected List getAddResources(final String vnfdId) { + final List resources = new ArrayList<>(); + + for (final Vnfd vnfd : vnfds.getVnfdList()) { + if (vnfd.getVnfdId().equals(vnfdId)) { + for (final Vnfc vnfc : vnfd.getVnfcList()) { + final GrantsAddResources addResource = new GrantsAddResources(); + vnfc.setGrantResourceId(UUID.randomUUID().toString()); + addResource.setId(vnfc.getGrantResourceId()); + addResource.setType(TypeEnum.fromValue(vnfc.getType())); + addResource.setResourceTemplateId(vnfc.getResourceTemplateId()); + addResource.setVduId(vnfc.getVduId()); + resources.add(addResource); + } + } + } + return resources; + } + + @Override + protected List getRemoveResources(final String vnfdId) { + return Collections.emptyList(); + } + + @Override + protected List handleGrantResponse( + final InlineResponse201 grantResponse) { + final InlineResponse201InstantiatedVnfInfo instantiatedVnfInfo = createInstantiatedVnfInfo(grantResponse); + svnfmService.updateVnf(InstantiationStateEnum.INSTANTIATED, instantiatedVnfInfo, operation.getVnfInstanceId(), + getVimConnections(grantResponse)); + return instantiatedVnfInfo.getVnfcResourceInfo(); + } + + private InlineResponse201InstantiatedVnfInfo createInstantiatedVnfInfo(final InlineResponse201 grantResponse) { + final InlineResponse201InstantiatedVnfInfo instantiatedVnfInfo = new InlineResponse201InstantiatedVnfInfo(); + + final Map mapOfGrantResourceIdToVimConnectionId = new HashMap<>(); + for (final InlineResponse201AddResources addResource : grantResponse.getAddResources()) { + mapOfGrantResourceIdToVimConnectionId.put(addResource.getResourceDefinitionId(), + addResource.getVimConnectionId()); + } + LOGGER.info("VIM connections in grant response: {}", mapOfGrantResourceIdToVimConnectionId); + + for (final Vnfd vnfd : vnfds.getVnfdList()) { + if (vnfd.getVnfdId().equals(svnfmService.getVnf(operation.getVnfInstanceId()).getVnfdId())) { + for (final Vnfc vnfc : vnfd.getVnfcList()) { + final InlineResponse201InstantiatedVnfInfoVnfcResourceInfo vnfcResourceInfoItem = + new InlineResponse201InstantiatedVnfInfoVnfcResourceInfo(); + vnfcResourceInfoItem.setId(vnfc.getVnfcId()); + vnfcResourceInfoItem.setVduId(vnfc.getVduId()); + final InlineResponse201InstantiatedVnfInfoResourceHandle computeResource = + new InlineResponse201InstantiatedVnfInfoResourceHandle(); + computeResource.setResourceId(UUID.randomUUID().toString()); + LOGGER.info("Checking for VIM connection id for : {}", vnfc.getGrantResourceId()); + computeResource + .setVimConnectionId(mapOfGrantResourceIdToVimConnectionId.get(vnfc.getGrantResourceId())); + + computeResource.setVimLevelResourceType("OS::Nova::Server"); + vnfcResourceInfoItem.setComputeResource(computeResource); + instantiatedVnfInfo.addVnfcResourceInfoItem(vnfcResourceInfoItem); + } + } + } + + return instantiatedVnfInfo; + } + + + private List getVimConnections(final InlineResponse201 grantResponse) { + final List vimConnectionInfo = new ArrayList<>(); + for (final InlineResponse201VimConnections vimConnection : grantResponse.getVimConnections()) { + final ModelMapper modelMapper = new ModelMapper(); + vimConnectionInfo.add(modelMapper.map(vimConnection, InlineResponse201VimConnectionInfo.class)); + } + return vimConnectionInfo; + } + + @Override + protected ChangeTypeEnum getVnfcChangeType() { + return ChangeTypeEnum.ADDED; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/OperationProgressor.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/OperationProgressor.java new file mode 100644 index 00000000..487a931c --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/OperationProgressor.java @@ -0,0 +1,348 @@ +package org.onap.so.svnfm.simulator.services; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.ws.rs.core.MediaType; +import org.apache.commons.codec.binary.Base64; +import org.modelmapper.ModelMapper; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.ApiResponse; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsLinks; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsLinksVnfLcmOpOcc; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.ApiClient; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.ApiException; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.api.DefaultApi; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.NotificationStatusEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.NotificationTypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthenticationParamsOauth2ClientCredentials; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.repository.VnfOperationRepository; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.model.VnfOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +public abstract class OperationProgressor implements Runnable { + + private static final Logger LOGGER = LoggerFactory.getLogger(OperationProgressor.class); + private static final String CERTIFICATE_TO_TRUST = "so-vnfm-adapter.crt.pem"; + + private Resource keyStoreResource = new ClassPathResource("so-vnfm-simulator.p12"); + private String keyStorePassword = "7Em3&j4.19xYiMelhD5?xbQ."; + + protected final VnfOperation operation; + protected final SvnfmService svnfmService; + private final VnfOperationRepository vnfOperationRepository; + private final ApplicationConfig applicationConfig; + protected final Vnfds vnfds; + private final SubscriptionService subscriptionService; + private final DefaultApi notificationClient; + private final org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.api.DefaultApi grantClient; + + public OperationProgressor(final VnfOperation operation, final SvnfmService svnfmService, + final VnfOperationRepository vnfOperationRepository, final ApplicationConfig applicationConfig, + final Vnfds vnfds, final SubscriptionService subscriptionService) { + this.operation = operation; + this.svnfmService = svnfmService; + this.vnfOperationRepository = vnfOperationRepository; + this.applicationConfig = applicationConfig; + this.vnfds = vnfds; + this.subscriptionService = subscriptionService; + + final ApiClient apiClient = new ApiClient(); + String callBackUrl = subscriptionService.getSubscriptions().iterator().next().getCallbackUri(); + callBackUrl = callBackUrl.substring(0, callBackUrl.indexOf("/lcn/")); + apiClient.setBasePath(callBackUrl); + apiClient.setKeyManagers(getKeyManagers()); + apiClient.setSslCaCert(getCertificateToTrust()); + notificationClient = new DefaultApi(apiClient); + + final org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.ApiClient grantApiClient = + new org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.ApiClient(); + grantApiClient.setBasePath(callBackUrl); + grantApiClient.setKeyManagers(getKeyManagers()); + grantApiClient.setSslCaCert(getCertificateToTrust()); + grantClient = new org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.api.DefaultApi(grantApiClient); + } + + private InputStream getCertificateToTrust() { + try { + return new ClassPathResource(CERTIFICATE_TO_TRUST).getInputStream(); + } catch (final IOException exception) { + LOGGER.error("Error reading certificate to trust, https calls to VNFM adapter will fail", exception); + return null; + } + } + + private KeyManager[] getKeyManagers() { + KeyStore keystore; + try { + keystore = KeyStore.getInstance("pkcs12"); + keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray()); + KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); + keyManagerFactory.init(keystore, keyStorePassword.toCharArray()); + return keyManagerFactory.getKeyManagers(); + } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException + | UnrecoverableKeyException exception) { + LOGGER.error("Error reading certificate, https calls using two way TLS to VNFM adapter will fail", + exception); + return new KeyManager[0]; + } + } + + @Override + public void run() { + try { + final VnfLcmOperationOccurrenceNotification notificationOfStarting = + buildNotification(NotificationStatusEnum.START, OperationStateEnum.STARTING); + sendNotification(notificationOfStarting); + + sleep(2000); + setState(InlineResponse200.OperationStateEnum.PROCESSING); + final VnfLcmOperationOccurrenceNotification notificationOfProcessing = + buildNotification(NotificationStatusEnum.START, OperationStateEnum.PROCESSING); + sendNotification(notificationOfProcessing); + + + final GrantRequest grantRequest = buildGrantRequest(); + final InlineResponse201 grantResponse = sendGrantRequest(grantRequest); + final List vnfcs = handleGrantResponse(grantResponse); + + svnfmService.getVnf(operation.getVnfInstanceId()).getInstantiatedVnfInfo(); + + sleep(10000); + setState(InlineResponse200.OperationStateEnum.COMPLETED); + final VnfLcmOperationOccurrenceNotification notificationOfCompleted = + buildNotification(NotificationStatusEnum.RESULT, OperationStateEnum.COMPLETED); + notificationOfCompleted.setAffectedVnfcs(getVnfcs(vnfcs)); + + sendNotification(notificationOfCompleted); + } catch (final Exception exception) { + LOGGER.error("Error in OperationProgressor ", exception); + } + + } + + private void sleep(final long milliSeconds) { + try { + Thread.sleep(milliSeconds); + } catch (final InterruptedException e) { + operation.setOperationState(InlineResponse200.OperationStateEnum.FAILED); + // Restore interrupted state + Thread.currentThread().interrupt(); + } + } + + private void setState(final InlineResponse200.OperationStateEnum state) { + LOGGER.info("Setting state to {} for operation {}", state, operation.getId()); + operation.setOperationState(state); + vnfOperationRepository.save(operation); + } + + private VnfLcmOperationOccurrenceNotification buildNotification(final NotificationStatusEnum status, + final OperationStateEnum operationState) { + final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification(); + notification.setId(UUID.randomUUID().toString()); + notification.setNotificationType(NotificationTypeEnum.VNFLCMOPERATIONOCCURRENCENOTIFICATION); + notification.setNotificationStatus(status); + notification.setOperationState(operationState); + notification.setOperation(OperationEnum.fromValue(operation.getOperation().toString())); + notification.setVnfInstanceId(operation.getVnfInstanceId()); + notification.setVnfLcmOpOccId(operation.getId()); + + final LcnVnfLcmOperationOccurrenceNotificationLinks links = new LcnVnfLcmOperationOccurrenceNotificationLinks(); + final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance vnfInstanceLink = + new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance(); + vnfInstanceLink.setHref(getVnfLink()); + links.setVnfInstance(vnfInstanceLink); + + + final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance operationLink = + new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance(); + operationLink.setHref(getOperationLink()); + links.setVnfLcmOpOcc(operationLink); + + notification.setLinks(links); + + return notification; + } + + private List getVnfcs( + final List instantiatedVnfcs) { + final List vnfcs = new ArrayList<>(); + if (instantiatedVnfcs != null) { + for (final InlineResponse201InstantiatedVnfInfoVnfcResourceInfo instantiatedVnfc : instantiatedVnfcs) { + LOGGER.info("VNFC TO BE CONVERTED: {}", instantiatedVnfc); + final ModelMapper mapper = new ModelMapper(); + final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc = + mapper.map(instantiatedVnfc, LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.class); + LOGGER.info("VNFC FROM CONVERSION: {}", vnfc); + vnfc.setChangeType(getVnfcChangeType()); + vnfcs.add(vnfc); + } + } + return vnfcs; + } + + private void sendNotification(final VnfLcmOperationOccurrenceNotification notification) { + LOGGER.info("Sending notification: {}", notification); + try { + final SubscriptionsAuthenticationParamsBasic subscriptionAuthentication = + subscriptionService.getSubscriptions().iterator().next().getAuthentication().getParamsBasic(); + final String auth = + subscriptionAuthentication.getUserName() + ":" + subscriptionAuthentication.getPassword(); + final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1)); + String authHeader = "Basic " + new String(encodedAuth); + + notificationClient.lcnVnfLcmOperationOccurrenceNotificationPostWithHttpInfo(notification, + MediaType.APPLICATION_JSON, authHeader); + } catch (final ApiException exception) { + LOGGER.error("Error sending notification: " + notification, exception); + LOGGER.error("Response code: {}, body: {}, basePath: {}", exception.getCode(), exception.getResponseBody(), + notificationClient.getApiClient().getBasePath()); + + } + } + + + public GrantRequest buildGrantRequest() { + final GrantRequest grantRequest = new GrantRequest(); + grantRequest.setVnfInstanceId(operation.getVnfInstanceId()); + final String vnfdId = svnfmService.getVnf(operation.getVnfInstanceId()).getVnfdId(); + grantRequest.setVnfdId(vnfdId); + grantRequest.setAddResources(getAddResources(vnfdId)); + grantRequest.setRemoveResources(getRemoveResources(vnfdId)); + grantRequest.setVnfLcmOpOccId(operation.getId()); + grantRequest + .setOperation(org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest.OperationEnum + .fromValue(operation.getOperation().getValue())); + grantRequest.setIsAutomaticInvocation(false); + + final GrantsLinksVnfLcmOpOcc vnfInstanceLink = new GrantsLinksVnfLcmOpOcc(); + vnfInstanceLink.setHref(getVnfLink()); + final GrantsLinksVnfLcmOpOcc operationInstanceLink = new GrantsLinksVnfLcmOpOcc(); + operationInstanceLink.setHref(getOperationLink()); + final GrantsLinks links = new GrantsLinks(); + links.setVnfInstance(vnfInstanceLink); + links.setVnfLcmOpOcc(operationInstanceLink); + grantRequest.setLinks(links); + return grantRequest; + } + + protected abstract List getAddResources(final String vnfdId); + + protected abstract List getRemoveResources(final String vnfdId); + + protected abstract List handleGrantResponse( + InlineResponse201 grantResponse); + + protected abstract ChangeTypeEnum getVnfcChangeType(); + + private InlineResponse201 sendGrantRequest(final GrantRequest grantRequest) { + LOGGER.info("Sending grant request: {}", grantRequest); + try { + + final SubscriptionsAuthenticationParamsOauth2ClientCredentials subscriptionAuthentication = + subscriptionService.getSubscriptions().iterator().next().getAuthentication() + .getParamsOauth2ClientCredentials(); + + final String authHeader = applicationConfig.getGrantAuth().equals("oauth") + ? "Bearer " + getToken(notificationClient.getApiClient(), subscriptionAuthentication) + : null; + + final ApiResponse response = grantClient.grantsPostWithHttpInfo(grantRequest, + MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, authHeader); + LOGGER.info("Grant Response: {}", response); + return response.getData(); + } catch (final org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.ApiException exception) { + LOGGER.error("Error sending notification: " + grantRequest, exception); + return null; + } + } + + private String getVnfLink() { + return getLinkBaseUrl() + "/vnf_instances/" + operation.getVnfInstanceId(); + } + + private String getOperationLink() { + return getLinkBaseUrl() + "/vnf_lcm_op_occs/" + operation.getId(); + } + + private String getLinkBaseUrl() { + return applicationConfig.getBaseUrl() + "/vnflcm/v1"; + } + + private String getToken(final ApiClient apiClient, + final SubscriptionsAuthenticationParamsOauth2ClientCredentials oauthClientCredentials) { + final String basePath = apiClient.getBasePath().substring(0, apiClient.getBasePath().indexOf("/so/")); + final String tokenUrl = basePath + "/oauth/token?grant_type=client_credentials"; + + try { + URL url = new URL(tokenUrl); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + final String authorizationHeader = getAuthorizationHeader(oauthClientCredentials); + connection.addRequestProperty("Authorization", authorizationHeader); + + connection.connect(); + + return getResponse(connection).get("access_token").getAsString(); + + } catch (IOException exception) { + LOGGER.error("Error getting token", exception); + return null; + } + } + + private String getAuthorizationHeader( + final SubscriptionsAuthenticationParamsOauth2ClientCredentials oauthClientCredentials) { + final String auth = oauthClientCredentials.getClientId() + ":" + oauthClientCredentials.getClientPassword(); + final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.UTF_8)); + return "Basic " + new String(encodedAuth); + } + + private JsonObject getResponse(HttpsURLConnection connection) throws IOException { + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line, data = ""; + while ((line = in.readLine()) != null) { + data += line; + } + in.close(); + connection.getInputStream().close(); + + JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); + return jsonObject; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SubscriptionService.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SubscriptionService.java new file mode 100644 index 00000000..0922180c --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SubscriptionService.java @@ -0,0 +1,21 @@ +package org.onap.so.svnfm.simulator.services; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; +import org.springframework.stereotype.Service; + +@Service +public class SubscriptionService { + + Collection subscriptions = new ArrayList<>(); + + public void registerSubscription(final LccnSubscriptionRequest subscription) { + subscriptions.add(subscription); + } + + public Collection getSubscriptions() { + return Collections.unmodifiableCollection(subscriptions); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SvnfmService.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SvnfmService.java new file mode 100644 index 00000000..4d968ce7 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SvnfmService.java @@ -0,0 +1,169 @@ +/*- + * ============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.svnfm.simulator.services; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.modelmapper.ModelMapper; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.notifications.VnfInstantiationNotification; +import org.onap.so.svnfm.simulator.notifications.VnfmAdapterCreationNotification; +import org.onap.so.svnfm.simulator.repository.VnfOperationRepository; +import org.onap.so.svnfm.simulator.repository.VnfmRepository; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.model.VnfInstance; +import org.onap.so.svnfm.simulator.model.VnfOperation; +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.cache.annotation.CachePut; +import org.springframework.cache.support.SimpleValueWrapper; +import org.springframework.stereotype.Service; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +@Service +public class SvnfmService { + + private VnfmRepository vnfmRepository; + private VnfOperationRepository vnfOperationRepository; + private VnfmHelper vnfmHelper; + private ApplicationConfig applicationConfig; + private CacheManager cacheManager; + private Vnfds vnfds; + private SubscriptionService subscriptionService; + + private final ExecutorService executor = Executors.newCachedThreadPool(); + + private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmService.class); + + @Autowired + public SvnfmService(VnfmRepository vnfmRepository, VnfOperationRepository vnfOperationRepository, + VnfmHelper vnfmHelper, ApplicationConfig applicationConfig, CacheManager cacheManager, Vnfds vnfds, + SubscriptionService subscriptionService) { + this.vnfmRepository = vnfmRepository; + this.vnfOperationRepository = vnfOperationRepository; + this.vnfmHelper = vnfmHelper; + this.applicationConfig = applicationConfig; + this.cacheManager = cacheManager; + this.vnfds = vnfds; + this.subscriptionService = subscriptionService; + } + + public InlineResponse201 createVnf(final CreateVnfRequest createVNFRequest, final String id) { + InlineResponse201 inlineResponse201 = null; + try { + final VnfInstance vnfInstance = vnfmHelper.createVnfInstance(createVNFRequest, id); + vnfmRepository.save(vnfInstance); + final Thread creationNotification = new Thread(new VnfmAdapterCreationNotification()); + creationNotification.start(); + inlineResponse201 = vnfmHelper.getInlineResponse201(vnfInstance); + LOGGER.debug("Response from Create VNF {}", inlineResponse201); + } catch (IllegalAccessException | InvocationTargetException e) { + LOGGER.error("Failed in Create Vnf", e); + } + return inlineResponse201; + } + + @CachePut(value = Constant.IN_LINE_RESPONSE_201_CACHE, key = "#id") + public void updateVnf(final InstantiationStateEnum instantiationState, + final InlineResponse201InstantiatedVnfInfo instantiatedVnfInfo, final String id, + final List vimConnectionInfo) { + final InlineResponse201 vnf = getVnf(id); + vnf.setInstantiatedVnfInfo(instantiatedVnfInfo); + vnf.setInstantiationState(instantiationState); + vnf.setVimConnectionInfo(vimConnectionInfo); + } + + public String instantiateVnf(final String vnfId) { + final VnfOperation vnfOperation = buildVnfOperation(InlineResponse200.OperationEnum.INSTANTIATE, vnfId); + vnfOperationRepository.save(vnfOperation); + executor.submit(new InstantiateOperationProgressor(vnfOperation, this, vnfOperationRepository, + applicationConfig, vnfds, subscriptionService)); + return vnfOperation.getId(); + } + + private VnfOperation buildVnfOperation(final InlineResponse200.OperationEnum operation, final String vnfId) { + final VnfOperation vnfOperation = new VnfOperation(); + vnfOperation.setId(UUID.randomUUID().toString()); + vnfOperation.setOperation(operation); + vnfOperation.setOperationState(InlineResponse200.OperationStateEnum.STARTING); + vnfOperation.setVnfInstanceId(vnfId); + return vnfOperation; + } + + public InlineResponse200 getOperationStatus(final String operationId) { + LOGGER.info("Getting operation status with id: {}", operationId); + final Thread instantiationNotification = new Thread(new VnfInstantiationNotification()); + instantiationNotification.start(); + for (final VnfOperation operation : vnfOperationRepository.findAll()) { + LOGGER.info("Operation found: {}", operation); + if (operation.getId().equals(operationId)) { + final ModelMapper modelMapper = new ModelMapper(); + return modelMapper.map(operation, InlineResponse200.class); + } + } + return null; + } + + public InlineResponse201 getVnf(final String vnfId) { + final Cache ca = cacheManager.getCache(Constant.IN_LINE_RESPONSE_201_CACHE); + if (ca == null) + return null; + final SimpleValueWrapper wrapper = (SimpleValueWrapper) ca.get(vnfId); + if (wrapper == null) + return null; + final InlineResponse201 inlineResponse201 = (InlineResponse201) wrapper.get(); + if (inlineResponse201 != null) { + LOGGER.info("Cache Read Successful"); + return inlineResponse201; + } + return null; + } + + public String terminateVnf(final String vnfId) { + final VnfOperation vnfOperation = buildVnfOperation(InlineResponse200.OperationEnum.TERMINATE, vnfId); + vnfOperationRepository.save(vnfOperation); + executor.submit(new TerminateOperationProgressor(vnfOperation, this, vnfOperationRepository, applicationConfig, + vnfds, subscriptionService)); + return vnfOperation.getId(); + } + + public void registerSubscription(final LccnSubscriptionRequest subscription) { + subscriptionService.registerSubscription(subscription); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressor.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressor.java new file mode 100644 index 00000000..bd729f3a --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressor.java @@ -0,0 +1,74 @@ +package org.onap.so.svnfm.simulator.services; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources.TypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsResource; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoResourceHandle; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.repository.VnfOperationRepository; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.model.VnfOperation; + +public class TerminateOperationProgressor extends OperationProgressor { + + public TerminateOperationProgressor(final VnfOperation operation, final SvnfmService svnfmService, + final VnfOperationRepository vnfOperationRepository, final ApplicationConfig applicationConfig, + final Vnfds vnfds, final SubscriptionService subscriptionService) { + super(operation, svnfmService, vnfOperationRepository, applicationConfig, vnfds, subscriptionService); + } + + @Override + protected List getAddResources(final String vnfdId) { + return Collections.emptyList(); + } + + @Override + protected List getRemoveResources(final String vnfdId) { + final List resources = new ArrayList<>(); + + final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 vnf = + svnfmService.getVnf(operation.getVnfInstanceId()); + for (final InlineResponse201InstantiatedVnfInfoVnfcResourceInfo vnfc : vnf.getInstantiatedVnfInfo() + .getVnfcResourceInfo()) { + final GrantsAddResources addResource = new GrantsAddResources(); + addResource.setId(UUID.randomUUID().toString()); + addResource.setType(TypeEnum.COMPUTE); + addResource.setVduId(vnfc.getVduId()); + final GrantsResource resource = new GrantsResource(); + + final InlineResponse201InstantiatedVnfInfoResourceHandle computeResource = vnfc.getComputeResource(); + resource.setResourceId(computeResource.getResourceId()); + resource.setVimConnectionId(computeResource.getVimConnectionId()); + resource.setVimLevelResourceType(computeResource.getVimLevelResourceType()); + addResource.setResource(resource); + resources.add(addResource); + + } + return resources; + } + + @Override + protected List handleGrantResponse( + final InlineResponse201 grantResponse) { + final List vnfcs = + svnfmService.getVnf(operation.getVnfInstanceId()).getInstantiatedVnfInfo().getVnfcResourceInfo(); + svnfmService.updateVnf(InstantiationStateEnum.NOT_INSTANTIATED, null, operation.getVnfInstanceId(), null); + return vnfcs; + } + + @Override + protected ChangeTypeEnum getVnfcChangeType() { + return ChangeTypeEnum.REMOVED; + } + + + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/VnfmHelper.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/VnfmHelper.java new file mode 100644 index 00000000..03f15af1 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/VnfmHelper.java @@ -0,0 +1,106 @@ +/*- + * ============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.svnfm.simulator.services; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.beanutils.BeanUtils; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.model.VnfInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * + * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech) + * @author Ronan Kenny (ronan.kenny@est.tech) + */ +@Component +public class VnfmHelper { + + private ApplicationConfig applicationConfig; + + @Autowired + public VnfmHelper(ApplicationConfig applicationConfig) { + this.applicationConfig = applicationConfig; + } + + /** + * + * @param createVNFRequest + * @return vnfInstance + */ + public VnfInstance createVnfInstance(final CreateVnfRequest createVNFRequest, final String id) { + final VnfInstance vnfInstance = new VnfInstance(); + vnfInstance.setId(id); + vnfInstance.setVnfInstanceName(createVNFRequest.getVnfInstanceName()); + vnfInstance.setVnfInstanceDescription(createVNFRequest.getVnfInstanceDescription()); + vnfInstance.setVnfdId(createVNFRequest.getVnfdId()); + vnfInstance.setVnfProvider(Constant.VNF_PROVIDER); + vnfInstance.setVnfProductName(Constant.VNF_PROVIDER_NAME); + return vnfInstance; + } + + /** + * + * @param vnfInstance + * @return inlineResponse201 + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + public InlineResponse201 getInlineResponse201(final VnfInstance vnfInstance) + throws IllegalAccessException, InvocationTargetException { + final InlineResponse201 inlineResponse201 = new InlineResponse201(); + BeanUtils.copyProperties(inlineResponse201, vnfInstance); + inlineResponse201.setVnfdVersion(Constant.VNFD_VERSION); + inlineResponse201.setVnfSoftwareVersion(Constant.VNF_SOFTWARE_VERSION); + inlineResponse201.setInstantiationState(InstantiationStateEnum.NOT_INSTANTIATED); + inlineResponse201.setVnfConfigurableProperties(getConfigProperties()); + addAdditionalPRopertyInlineResponse201(inlineResponse201); + return inlineResponse201; + } + + private Map getConfigProperties() { + final Map configProperties = new HashMap<>(); + configProperties.put("ipAddress", "10.11.12.13"); + return configProperties; + } + + private void addAdditionalPRopertyInlineResponse201(final InlineResponse201 inlineResponse201) { + final InlineResponse201LinksSelf VnfInstancesLinksSelf = new InlineResponse201LinksSelf(); + VnfInstancesLinksSelf + .setHref(applicationConfig.getBaseUrl() + "/vnflcm/v1/vnf_instances/" + inlineResponse201.getId()); + final InlineResponse201LinksSelf VnfInstancesLinksSelfInstantiate = new InlineResponse201LinksSelf(); + VnfInstancesLinksSelfInstantiate.setHref(applicationConfig.getBaseUrl() + "/vnflcm/v1/vnf_instances/" + + inlineResponse201.getId() + "/instantiate"); + final InlineResponse201Links inlineResponse201Links = new InlineResponse201Links(); + inlineResponse201Links.setSelf(VnfInstancesLinksSelf); + inlineResponse201Links.setInstantiate(VnfInstancesLinksSelfInstantiate); + inlineResponse201.setLinks(inlineResponse201Links); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/application.yaml b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/application.yaml new file mode 100644 index 00000000..33d865c1 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/application.yaml @@ -0,0 +1,64 @@ +# Copyright © 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. + +spring: + h2: + console: + enabled: true + path: console + datasource: + url: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE + username: admin + password: admin + http: + converters: + preferred-json-mapper: gson + security: + usercredentials: + - username: vnfm + password: '$2a$10$Fh9ffgPw2vnmsghsRD3ZauBL1aKXebigbq3BB1RPWtE62UDILsjke' + role: BPEL-Client + +server: + port: 9093 + tomcat: + max-threads: 50 + ssl: + key-alias: so@so.onap.org + key--store-password: '7Em3&j4.19xYiMelhD5?xbQ.' + key-store: classpath:so-vnfm-simulator.p12 + key-store-type: PKCS12 + +vnfds: + vnfdlist: + - vnfdid: 1 + vnfclist: + - vnfcid: VNFC1 + resourceTemplateId: vnfd1_vnfc1 + vduId: vnfd1_vduForVnfc1 + type: COMPUTE + - vnfcid: VNFC2 + resourceTemplateId: vnfd1_vnfc2 + vduId: vnfd1_vduForVnfc2 + type: COMPUTE + - vnfdid: 2 + vnfclist: + - vnfcid: VNFC3 + resourceTemplateId: vnfd2_vnfc3 + vduId: vnfd2_vduForVnfc3 + type: COMPUTE + - vnfcid: VNFC4 + resourceTemplateId: vnfd2_vnfc4 + vduId: vnfd2_vduForVnfc4 + type: COMPUTE diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-adapter.crt.pem b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-adapter.crt.pem new file mode 100644 index 00000000..3c899e3b --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-adapter.crt.pem @@ -0,0 +1,30 @@ +Bag Attributes + friendlyName: so@so.onap.org + localKeyID: 54 69 6D 65 20 31 35 36 33 34 36 33 36 32 39 35 38 33 +subject=/CN=so-vnfm-adapter/emailAddress=/OU=so@so.onap.org/OU=OSAAF/O=ONAP/C=US +issuer=/C=US/O=ONAP/OU=OSAAF/CN=intermediateCA_9 +-----BEGIN CERTIFICATE----- +MIIEITCCAwmgAwIBAgIILuAnLLineoYwDQYJKoZIhvcNAQELBQAwRzELMAkGA1UE +BhMCVVMxDTALBgNVBAoMBE9OQVAxDjAMBgNVBAsMBU9TQUFGMRkwFwYDVQQDDBBp +bnRlcm1lZGlhdGVDQV85MB4XDTE5MDcxODE1MjcwOVoXDTIwMDcxODE1MjcwOVow +cDEYMBYGA1UEAwwPc28tdm5mbS1hZGFwdGVyMQ8wDQYJKoZIhvcNAQkBFgAxFzAV +BgNVBAsMDnNvQHNvLm9uYXAub3JnMQ4wDAYDVQQLDAVPU0FBRjENMAsGA1UECgwE +T05BUDELMAkGA1UEBhMCVVMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB +AQCu2NbWjFiZ5Tz5P7daCD6mqJqSWV3f+gkM2VC/UYM/43hd/2ILJbbtsv4uzS/P +GXl3UIKBjb7zRiDCvLNMFsHCZ9/gIonG1z737S42LCrdVKq/KQ59yIOPrxYmLyiQ +Xy81ChX77b2KvKPPeF+K/wnh5fLwlcJ18geeCoWGaMK0C/i6J/uUb9z+Ef0Nmtau +NdXAuUnERCKMra+3kFxZwaRC/gSCy+/s6EQdeaGNiijg03AmrUx9XjrJjHbYMDVo +OKSxtv0E4fxbfmTpHaKCuN4eg+0nEXw/eiIEuSHJuh3KKv7wRoP/hG/Tdog7x60M +SD+hdNjCbFP6yAyMPfoxVnjHAgMBAAGjgecwgeQwCQYDVR0TBAIwADAOBgNVHQ8B +Af8EBAMCBeAwIAYDVR0lAQH/BBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMFQGA1Ud +IwRNMEuAFIH3mVsQuciM3vNSXupOaaBDPqzdoTCkLjAsMQ4wDAYDVQQLDAVPU0FB +RjENMAsGA1UECgwET05BUDELMAkGA1UEBhMCVVOCAQcwHQYDVR0OBBYEFFLrO3T4 +QybeDQ28mHgC/xT5f03qMDAGA1UdEQQpMCeCD3NvLXZuZm0tYWRhcHRlcoIUc28t +dm5mbS1hZGFwdGVyLm9uYXAwDQYJKoZIhvcNAQELBQADggEBACe+JaVIjTku/QNp +XoQCNN+sllSZmEHTLmYfpSzY5BY2AeJsgTYqFtAhtp6uQf8Jr993CyEyeJ4if2Z9 +J5NWoJKmY1+a63UphB1mg4sNSCuDxvbxPjtrFkOx/DiB1XEUdoifS9IQSDIIuhaD +YP6sih1TBOh/2ityCe51Mu1J9/wgb24rlYouVtEyQeIai4dqngFHeQHeNXOnGN0z +osEcKSYa0C+ZOAomBMT58C2aDz9vyI8YPuzwVSDKndmXUgvrkkVnxk3qJRtghDQc +RV+4SeZg8s4+5DxKL4AL15IAaAPMJHi+MRtfm7qNzqCEl5sAEzO7S4oVHeWLNFV8 +a9PHErg= +-----END CERTIFICATE----- diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-simulator.p12 b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-simulator.p12 new file mode 100644 index 00000000..7ac02855 Binary files /dev/null and b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/resources/so-vnfm-simulator.p12 differ diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/controllers/TestSvnfmController.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/controllers/TestSvnfmController.java new file mode 100644 index 00000000..84a34623 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/controllers/TestSvnfmController.java @@ -0,0 +1,183 @@ +/*- + * ============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.svnfm.simulator.controllers; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.controller.SvnfmController; +import org.onap.so.svnfm.simulator.repository.VnfmCacheRepository; +import org.onap.so.svnfm.simulator.services.SvnfmService; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class TestSvnfmController { + + private static final String VNF_INSTANCES_URL = "/vnf_instances/"; + private static final String VNFD_ID = "vnfdId"; + private static final String VNF_INSTANCE_NAME = "vnfInstanceName"; + private static final String VNF_INSTANCE_DESCRIPTION = "vnfInstanceDescription"; + private static final String VNF_INSTANCE_ID = "vnfInstanceId"; + private static final String VNF_OPERATION_ID = "vnfOperationId"; + private static final String VNF_INSTANTIATE_URL = "/instantiate"; + private static final String VNF_TERMINATE_URL = "/terminate"; + private static final String VNF_LCM_OP_OCC_ID = "vnfLcmOpOccId"; + private static final String VNF_OPERATION_STATUS_URL = "/vnf_lcm_op_occs/"; + private static final String SUBSCRIPTIONS_URL = "/subscriptions"; + + @InjectMocks + private SvnfmController svnfmController; + + private MockMvc mockMvc; + + @Mock + private SvnfmService svnfmService; + + @Mock + private VnfmCacheRepository vnfmCacheRepository; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + this.mockMvc = MockMvcBuilders.standaloneSetup(svnfmController).build(); + } + + @Test + public void createVnfInstanceTest() throws Exception { + final CreateVnfRequest createVnfRequest = new CreateVnfRequest(); + + createVnfRequest.setVnfdId(VNFD_ID); + createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME); + createVnfRequest.setVnfInstanceDescription(VNF_INSTANCE_DESCRIPTION); + + when(vnfmCacheRepository.createVnf(eq(createVnfRequest), anyString())).thenReturn(new InlineResponse201()); + + final String body = (new ObjectMapper()).valueToTree(createVnfRequest).toString(); + this.mockMvc + .perform(post(Constant.BASE_URL + VNF_INSTANCES_URL).content(body) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()).andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + public void test_getVnf_usingValidVnfInstanceId_vnfInstanceRetrievedSuccessfully() throws Exception { + final InlineResponse201 inlineResponse201 = new InlineResponse201(); + inlineResponse201.setId(VNF_INSTANCE_ID); + inlineResponse201.setVnfInstanceName(VNF_INSTANCE_NAME); + + when(vnfmCacheRepository.getVnf(VNF_INSTANCE_ID)).thenReturn(inlineResponse201); + + final ResultActions resultActions = this.mockMvc + .perform(get(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)); + + final MvcResult result = resultActions.andReturn(); + final String content = result.getResponse().getContentAsString(); + final InlineResponse201 response201 = new ObjectMapper().readValue(content, InlineResponse201.class); + assertThat(response201.getId()).isEqualTo(VNF_INSTANCE_ID); + assertThat(response201.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME); + } + + @Test + public void test_instantiateVnf_usingValidVnfInstanceId_returnsHttpStatusAccepted() throws Exception { + when(svnfmService.instantiateVnf(VNF_INSTANCE_ID)).thenReturn(VNF_OPERATION_ID); + + this.mockMvc + .perform(post(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID + VNF_INSTANTIATE_URL) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isAccepted()).andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + public void test_deleteVnf_usingValidVnfInstanceId_returnsHttpStatusNoContent() throws Exception { + this.mockMvc + .perform(delete(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()).andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + public void test_terminateVnf_usingValidVnfInstanceId_returnsHttpStatusIsAccepted() throws Exception { + when(svnfmService.terminateVnf(VNF_INSTANCE_ID)).thenReturn(VNF_OPERATION_ID); + + this.mockMvc + .perform(post(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID + VNF_TERMINATE_URL) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isAccepted()).andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + public void test_getOperationStatus_usingValidOperationId_operationStatusRetrievedSuccessfully() throws Exception { + final InlineResponse200 inlineResponse200 = new InlineResponse200(); + inlineResponse200.setId(VNF_LCM_OP_OCC_ID); + inlineResponse200.setOperation(OperationEnum.INSTANTIATE); + + when(svnfmService.getOperationStatus(VNF_LCM_OP_OCC_ID)).thenReturn(inlineResponse200); + + final ResultActions resultActions = this.mockMvc + .perform(get(Constant.BASE_URL + VNF_OPERATION_STATUS_URL + VNF_LCM_OP_OCC_ID) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()); + + final MvcResult result = resultActions.andReturn(); + final String content = result.getResponse().getContentAsString(); + final InlineResponse200 response200 = new ObjectMapper().readValue(content, InlineResponse200.class); + assertThat(response200.getId()).isEqualTo(VNF_LCM_OP_OCC_ID); + assertThat(response200.getOperation()).isEqualTo(OperationEnum.INSTANTIATE); + } + + @Test + public void test_subscribeForNotifications_usingValidSubscriptionRequest_returnsHttpStatusCreated() + throws Exception { + final LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest(); + final String body = (new ObjectMapper()).valueToTree(lccnSubscriptionRequest).toString(); + + this.mockMvc + .perform(post(Constant.BASE_URL + SUBSCRIPTIONS_URL).content(body) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()).andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/InstantiateOperatorProgressorTest.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/InstantiateOperatorProgressorTest.java new file mode 100644 index 00000000..71709cb2 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/InstantiateOperatorProgressorTest.java @@ -0,0 +1,169 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Nokia. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.svnfm.simulator.services; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201AddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.model.Vnfds.Vnfc; +import org.onap.so.svnfm.simulator.model.Vnfds.Vnfd; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.model.VnfOperation; + +public class InstantiateOperatorProgressorTest { + + private static final String VNF_ID = "vnfTestId"; + private static final String CALLBACK_URI = "/lcn/uritest"; + private static final String VNFC_TYPE = "COMPUTE"; + private static final String RESOURCE_TEMPLATE_ID = "resTempIdTest"; + private static final String VDU_ID = "vduIdTest"; + private static final String VNF_INSTANCE_ID = "vnfInstanceId"; + private static final String VNFC_ID = "vnfcIdTest"; + private static final String RESOURCE_DEFINITION_ID = "resDefTestId"; + private static final String VIM_CONNECTION_ID = "vimConnTestId"; + + private SvnfmService svnfmServiceMock; + + private InstantiateOperationProgressor testedObject; + + @Before + public void setup() { + svnfmServiceMock = mock(SvnfmService.class); + VnfOperation vnfOperation = new VnfOperation(); + vnfOperation.setVnfInstanceId(VNF_INSTANCE_ID); + vnfOperation.setOperation(OperationEnum.OPERATE); + testedObject = new InstantiateOperationProgressor(vnfOperation, svnfmServiceMock, null, new ApplicationConfig(), + createVnfds(), createSubscriptionService()); + } + + @Test + public void getAddResources_vnfIdFound() { + List result = testedObject.getAddResources(VNF_ID); + assertThat(result).hasSize(1); + GrantsAddResources grantsAddResourceResult = result.get(0); + assertThat(grantsAddResourceResult.getType()).hasToString(VNFC_TYPE); + assertThat(grantsAddResourceResult.getResourceTemplateId()).isEqualTo(RESOURCE_TEMPLATE_ID); + assertThat(grantsAddResourceResult.getVduId()).isEqualTo(VDU_ID); + } + + @Test + public void getAddResources_vnfIdNotFound() { + List result = testedObject.getAddResources("otherVnfId"); + assertThat(result).isEmpty(); + } + + @Test + public void handleGrantResponse_VnfdObjectsAvailable() { + when(svnfmServiceMock.getVnf(VNF_INSTANCE_ID)).thenReturn(createInlineResponse201()); + + InlineResponse201VimConnections inlineResponse201VimConnections = new InlineResponse201VimConnections(); + List listOfVimConnection = new ArrayList<>(); + listOfVimConnection.add(inlineResponse201VimConnections); + + InlineResponse201AddResources inlineResponse201AddResources = new InlineResponse201AddResources(); + inlineResponse201AddResources.setResourceDefinitionId(RESOURCE_DEFINITION_ID); + inlineResponse201AddResources.setVimConnectionId(VIM_CONNECTION_ID); + List listOfResources = new ArrayList<>(); + listOfResources.add(inlineResponse201AddResources); + + InlineResponse201 inlineResponse201 = new InlineResponse201(); + inlineResponse201.setVimConnections(listOfVimConnection); + inlineResponse201.setAddResources(listOfResources); + List resultList = + testedObject.handleGrantResponse(inlineResponse201); + + assertThat(resultList).hasSize(1); + InlineResponse201InstantiatedVnfInfoVnfcResourceInfo resultObject = resultList.get(0); + assertThat(resultObject.getId()).isEqualTo(VNFC_ID); + assertThat(resultObject.getVduId()).isEqualTo(VDU_ID); + assertThat(resultObject.getComputeResource().getVimConnectionId()).isEqualTo(VIM_CONNECTION_ID); + } + + @Test + public void getVnfcChangeType_enumAdded() { + assertThat(testedObject.getVnfcChangeType().getValue()).isEqualTo("ADDED"); + } + + @Test + public void getRemoveResourcesShouldReturnEmptyList() { + assertThat(testedObject.getRemoveResources("anyVnfId")).isEmpty(); + } + + @Test + public void test_buildGrantRequest_usingValidVnfInstanceId_grantRequestRetrievedSuccessfully() { + when(svnfmServiceMock.getVnf(Mockito.eq(VNF_INSTANCE_ID))).thenReturn(createInlineResponse201()); + GrantRequest grantRequest = testedObject.buildGrantRequest(); + assertThat(grantRequest.getVnfdId().equals(VNF_ID)); + } + + private Vnfds createVnfds() { + Vnfd vnfd = new Vnfd(); + vnfd.setVnfdId(VNF_ID); + List vnfcList = new ArrayList<>(); + vnfcList.add(createVnfc()); + vnfd.setVnfcList(vnfcList); + List vnfdList = new ArrayList<>(); + vnfdList.add(vnfd); + + Vnfds vnfds = new Vnfds(); + vnfds.setVnfdList(vnfdList); + return vnfds; + } + + private Vnfc createVnfc() { + Vnfc vnfc = new Vnfc(); + vnfc.setVnfcId(VNFC_ID); + vnfc.setType(VNFC_TYPE); + vnfc.setResourceTemplateId(RESOURCE_TEMPLATE_ID); + vnfc.setVduId(VDU_ID); + vnfc.setGrantResourceId(RESOURCE_DEFINITION_ID); + return vnfc; + } + + private SubscriptionService createSubscriptionService() { + SubscriptionService subscriptionService = new SubscriptionService(); + LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest(); + lccnSubscriptionRequest.setCallbackUri(CALLBACK_URI); + subscriptionService.registerSubscription(lccnSubscriptionRequest); + return subscriptionService; + } + + private org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 createInlineResponse201() { + org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 inlineResponse201 = + new org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201(); + inlineResponse201.setVnfdId(VNF_ID); + return inlineResponse201; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/SvnfmServiceTest.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/SvnfmServiceTest.java new file mode 100644 index 00000000..7e441ab8 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/SvnfmServiceTest.java @@ -0,0 +1,203 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Nokia. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.svnfm.simulator.services; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.model.VnfInstance; +import org.onap.so.svnfm.simulator.model.VnfOperation; +import org.onap.so.svnfm.simulator.repository.VnfOperationRepository; +import org.onap.so.svnfm.simulator.repository.VnfmRepository; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.support.SimpleValueWrapper; + +public class SvnfmServiceTest { + + private static final String VNFD_ID = "vnfdId"; + private static final String VNF_INSTANCE_NAME = "vnfInstanceName"; + private static final String VNF_INSTANCE_ID = "vnfInstanceId"; + private static final String OPERATION_ID = "operationId"; + private SvnfmService testedObject; + private CacheManager cacheManagerMock; + private VnfmHelper vnfmHelperMock; + private VnfmRepository vnfmRepositoryMock; + private VnfOperationRepository vnfOperationRepositoryMock; + + @Before + public void setup() { + vnfmRepositoryMock = mock(VnfmRepository.class); + vnfOperationRepositoryMock = mock(VnfOperationRepository.class); + vnfmHelperMock = mock(VnfmHelper.class); + ApplicationConfig applicationConfigMock = mock(ApplicationConfig.class); + cacheManagerMock = mock(CacheManager.class); + Vnfds vnfdsMock = mock(Vnfds.class); + SubscriptionService subscriptionServiceMock = mock(SubscriptionService.class); + + testedObject = new SvnfmService(vnfmRepositoryMock, vnfOperationRepositoryMock, vnfmHelperMock, + applicationConfigMock, cacheManagerMock, vnfdsMock, subscriptionServiceMock); + } + + @Test + public void getVnfSuccessful() { + // given + Cache cacheMock = mock(Cache.class); + SimpleValueWrapper simpleValueWrapperMock = mock(SimpleValueWrapper.class); + when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock); + when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(simpleValueWrapperMock); + when(simpleValueWrapperMock.get()).thenReturn(getInlineResponse(VNFD_ID, VNF_INSTANCE_NAME)); + // when + InlineResponse201 result = testedObject.getVnf(VNF_INSTANCE_ID); + // then + assertThat(result.getVnfdId()).isEqualTo(VNFD_ID); + assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME); + } + + @Test + public void getVnf_ifCacheNullThenReturnNull() { + // given + when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(null); + // then + assertThat(testedObject.getVnf("any")).isNull(); + } + + @Test + public void getVnf_ifWrapperNullThenReturnNull() { + // given + Cache cacheMock = mock(Cache.class); + when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock); + when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(null); + // then + assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull(); + } + + @Test + public void getVnf_ifResultIsNullThenReturnNull() { + // when + Cache cacheMock = mock(Cache.class); + SimpleValueWrapper simpleValueWrapperMock = mock(SimpleValueWrapper.class); + when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock); + when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(simpleValueWrapperMock); + when(simpleValueWrapperMock.get()).thenReturn(null); + // then + assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull(); + } + + @Test + public void test_createVnf_usingValidCreateVnfRequest_vnfInstanceCreatedSuccessfully() throws Exception { + // given + final CreateVnfRequest createVnfRequest = getCreateVnfRequest(); + final VnfInstance vnfInstance = getVnfInstance(); + final InlineResponse201 inlineResponse201 = getInlineResponse(VNFD_ID, VNF_INSTANCE_NAME); + when(vnfmHelperMock.createVnfInstance(createVnfRequest, VNF_INSTANCE_ID)).thenReturn(vnfInstance); + when(vnfmRepositoryMock.save(vnfInstance)).thenReturn(vnfInstance); + when(vnfmHelperMock.getInlineResponse201(vnfInstance)).thenReturn(inlineResponse201); + // when + final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID); + // then + assertThat(result.getVnfdId()).isEqualTo(VNFD_ID); + assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME); + } + + @Test + public void test_createVnf_illegalAccessExceptionThrown_returnsNull() throws Exception { + // given + final CreateVnfRequest createVnfRequest = getCreateVnfRequest(); + final VnfInstance vnfInstance = getVnfInstance(); + when(vnfmHelperMock.createVnfInstance(createVnfRequest, VNF_INSTANCE_ID)).thenReturn(vnfInstance); + when(vnfmRepositoryMock.save(vnfInstance)).thenReturn(vnfInstance); + when(vnfmHelperMock.getInlineResponse201(vnfInstance)).thenThrow(new IllegalAccessException()); + // when + final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID); + // then + assertNull(result); + } + + @Test + public void test_getOperationStatus_usingValidOperationId_operationStatusRetrievedSuccessfully() { + // given + final OperationEnum operationEnum = OperationEnum.OPERATE; + final VnfOperation vnfOperation = getVnfOperation(OPERATION_ID, operationEnum); + final List vnfOperations = new ArrayList<>(); + vnfOperations.add(vnfOperation); + when(vnfOperationRepositoryMock.findAll()).thenReturn(vnfOperations); + // when + final InlineResponse200 result = testedObject.getOperationStatus(OPERATION_ID); + // then + assertThat(result.getId()).isEqualTo(OPERATION_ID); + assertThat(result.getOperation()).isEqualTo(operationEnum); + } + + @Test + public void test_getOperationStatus_usingInvalidOperationId_returnsNull() { + // given + final OperationEnum operationEnum = OperationEnum.OPERATE; + final VnfOperation vnfOperation = getVnfOperation(OPERATION_ID, operationEnum); + final List vnfOperations = new ArrayList<>(); + vnfOperations.add(vnfOperation); + when(vnfOperationRepositoryMock.findAll()).thenReturn(vnfOperations); + // when + InlineResponse200 result = testedObject.getOperationStatus("invalidOperationId"); + // then + assertNull(result); + } + + private InlineResponse201 getInlineResponse(String vnfdId, String vnfInstanceName) { + InlineResponse201 response201 = new InlineResponse201(); + response201.setVnfdId(vnfdId); + response201.vnfInstanceName(vnfInstanceName); + return response201; + } + + private CreateVnfRequest getCreateVnfRequest() { + final CreateVnfRequest createVnfRequest = new CreateVnfRequest(); + createVnfRequest.setVnfdId(VNFD_ID); + createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME); + return createVnfRequest; + } + + private VnfInstance getVnfInstance() { + final VnfInstance vnfInstance = new VnfInstance(); + vnfInstance.setVnfdId(VNFD_ID); + vnfInstance.setVnfInstanceName(VNF_INSTANCE_NAME); + return vnfInstance; + } + + private VnfOperation getVnfOperation(String operationId, OperationEnum operationEnum) { + final VnfOperation vnfOperation = new VnfOperation(); + vnfOperation.setId(operationId); + vnfOperation.setOperation(operationEnum); + return vnfOperation; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressorTest.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressorTest.java new file mode 100644 index 00000000..0b1ded4d --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/TerminateOperationProgressorTest.java @@ -0,0 +1,157 @@ +/*- + * ============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.svnfm.simulator.services; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoResourceHandle; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; +import org.onap.so.svnfm.simulator.model.Vnfds; +import org.onap.so.svnfm.simulator.model.Vnfds.Vnfc; +import org.onap.so.svnfm.simulator.model.Vnfds.Vnfd; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.model.VnfOperation; + +public class TerminateOperationProgressorTest { + + private static final String VNFD_ID = "vnfdId"; + private static final String VNFC_TYPE = "COMPUTE"; + private static final String VDU_ID = "vduId"; + private static final String VNF_INSTANCE_ID = "vnfInstanceId"; + + @Mock + private SvnfmService svnfmService; + + private TerminateOperationProgressor testedObject; + + @Before + public void setup() { + svnfmService = mock(SvnfmService.class); + VnfOperation vnfOperation = new VnfOperation(); + vnfOperation.setVnfInstanceId(VNF_INSTANCE_ID); + vnfOperation.setOperation(OperationEnum.OPERATE); + testedObject = new TerminateOperationProgressor(vnfOperation, svnfmService, null, new ApplicationConfig(), + createVnfds(), createSubscriptionService()); + } + + @Test + public void test_getAddResources_usingValidVnfdId_returnsEmptyList() { + List results = testedObject.getAddResources(VNFD_ID); + assertThat(results.isEmpty()); + } + + @Test + public void test_getRemoveResources_usingValidVnfdId_retrievesGrantsAddResourcesSuccessfully() { + // given + InlineResponse201 inlineResponse201 = createInlineResponse201(); + InlineResponse201InstantiatedVnfInfo inlineResponse201InstantiatedVnfInfo = + setupInlineResponseInstantiatedVnfInfo(); + inlineResponse201.setInstantiatedVnfInfo(inlineResponse201InstantiatedVnfInfo); + + when(svnfmService.getVnf(VNF_INSTANCE_ID)).thenReturn(inlineResponse201); + + // when + List result = testedObject.getRemoveResources(VNFD_ID); + + // then + assertThat(result).hasSize(1); + GrantsAddResources grantsAddResourcesResult = result.get(0); + assertThat(grantsAddResourcesResult.getType()).hasToString(VNFC_TYPE); + assertThat(grantsAddResourcesResult.getVduId()).isEqualTo(VDU_ID); + } + + @Test + public void test_getVnfcChangeType_isEnumRemoved() { + assertThat(testedObject.getVnfcChangeType().getValue()).isEqualTo("REMOVED"); + } + + private Vnfds createVnfds() { + Vnfd vnfd = new Vnfd(); + vnfd.setVnfdId(VNFD_ID); + List vnfcList = new ArrayList<>(); + vnfcList.add(createVnfc()); + vnfd.setVnfcList(vnfcList); + List vnfdList = new ArrayList<>(); + vnfdList.add(vnfd); + + Vnfds vnfds = new Vnfds(); + vnfds.setVnfdList(vnfdList); + return vnfds; + } + + private Vnfc createVnfc() { + Vnfc vnfc = new Vnfc(); + String vnfcId = "vnfcId"; + vnfc.setVnfcId(vnfcId); + vnfc.setType(VNFC_TYPE); + String resourceTemplateId = "resTempId"; + vnfc.setResourceTemplateId(resourceTemplateId); + vnfc.setVduId(VDU_ID); + String resourceDefinitionId = "resourceDefinitionId"; + vnfc.setGrantResourceId(resourceDefinitionId); + return vnfc; + } + + private SubscriptionService createSubscriptionService() { + SubscriptionService subscriptionService = new SubscriptionService(); + LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest(); + String callbackUri = "/lcn/uriest"; + lccnSubscriptionRequest.setCallbackUri(callbackUri); + subscriptionService.registerSubscription(lccnSubscriptionRequest); + return subscriptionService; + } + + private InlineResponse201 createInlineResponse201() { + InlineResponse201 inlineResponse201 = new InlineResponse201(); + inlineResponse201.setVnfdId(VNFD_ID); + return inlineResponse201; + } + + private InlineResponse201InstantiatedVnfInfo setupInlineResponseInstantiatedVnfInfo() { + InlineResponse201InstantiatedVnfInfo inlineResponse201InstantiatedVnfInfo = + new InlineResponse201InstantiatedVnfInfo(); + List resultList = new ArrayList<>(); + InlineResponse201InstantiatedVnfInfoVnfcResourceInfo resourceInfo = + new InlineResponse201InstantiatedVnfInfoVnfcResourceInfo(); + String resourceInfoId = "resourceInfoId"; + resourceInfo.setId(resourceInfoId); + resourceInfo.setVduId((VDU_ID)); + InlineResponse201InstantiatedVnfInfoResourceHandle resourceHandle = + new InlineResponse201InstantiatedVnfInfoResourceHandle(); + resourceInfo.setComputeResource(resourceHandle); + resultList.add(resourceInfo); + inlineResponse201InstantiatedVnfInfo.setVnfcResourceInfo(resultList); + String flavourId = "flavourId"; + inlineResponse201InstantiatedVnfInfo.setFlavourId(flavourId); + return inlineResponse201InstantiatedVnfInfo; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/VnfmHelperTest.java b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/VnfmHelperTest.java new file mode 100644 index 00000000..da538994 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/test/java/org/onap/so/svnfm/simulator/services/VnfmHelperTest.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Nokia. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.svnfm.simulator.services; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.junit.Before; +import org.junit.Test; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf; +import org.onap.so.svnfm.simulator.config.ApplicationConfig; +import org.onap.so.svnfm.simulator.constants.Constant; +import org.onap.so.svnfm.simulator.model.VnfInstance; + +public class VnfmHelperTest { + + private static final String VNF_INSTANCE_ID = "vnfInstanceTestId"; + private static final String VNFD_ID = "vnfdTestId"; + private static final String VNF_INSTANCE_NAME = "vnfInsNameTest"; + private static final String VNF_INSTANCE_DESCRIPTION = "vnfInstTestDescr"; + private static final String APPLICATION_CONFIG_BASE_URL = "appConfUrl"; + private VnfmHelper testedObject; + private ApplicationConfig applicationConfigMock; + + @Before + public void setup() { + applicationConfigMock = mock(ApplicationConfig.class); + testedObject = new VnfmHelper(applicationConfigMock); + } + + @Test + public void createVnfInstance() { + // when + VnfInstance result = testedObject.createVnfInstance(createVnfRequest(), VNF_INSTANCE_ID); + // then + assertThat(result.getId()).isEqualTo(VNF_INSTANCE_ID); + assertThat(result.getVnfdId()).isEqualTo(VNFD_ID); + assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME); + assertThat(result.getVnfInstanceDescription()).isEqualTo(VNF_INSTANCE_DESCRIPTION); + assertThat(result.getVnfProvider()).isEqualTo(Constant.VNF_PROVIDER); + assertThat(result.getVnfProductName()).isEqualTo(Constant.VNF_PROVIDER_NAME); + } + + @Test + public void getInlineResponse201() throws Exception { + // given + when(applicationConfigMock.getBaseUrl()).thenReturn(APPLICATION_CONFIG_BASE_URL); + // when + InlineResponse201 result = testedObject.getInlineResponse201(prepareVnfInstance()); + // then + assertThat(result.getVnfdVersion()).isEqualTo(Constant.VNFD_VERSION); + assertThat(result.getVnfSoftwareVersion()).isEqualTo(Constant.VNF_SOFTWARE_VERSION); + assertThat(result.getInstantiationState()).isEqualByComparingTo(InstantiationStateEnum.NOT_INSTANTIATED); + verifyAdditionalPropertyInlineResponse201(result); + } + + private CreateVnfRequest createVnfRequest() { + CreateVnfRequest createVnfRequest = new CreateVnfRequest(); + createVnfRequest.setVnfdId(VNFD_ID); + createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME); + createVnfRequest.setVnfInstanceDescription(VNF_INSTANCE_DESCRIPTION); + return createVnfRequest; + } + + private VnfInstance prepareVnfInstance() { + VnfInstance vnfInstance = new VnfInstance(); + vnfInstance.setId(VNF_INSTANCE_ID); + return vnfInstance; + } + + private void verifyAdditionalPropertyInlineResponse201(InlineResponse201 result) { + InlineResponse201LinksSelf expectedVnfInstancesLinksSelf = new InlineResponse201LinksSelf(); + expectedVnfInstancesLinksSelf + .setHref(APPLICATION_CONFIG_BASE_URL + "/vnflcm/v1/vnf_instances/" + VNF_INSTANCE_ID); + assertThat(result.getLinks().getSelf()).isEqualTo(expectedVnfInstancesLinksSelf); + + InlineResponse201LinksSelf expectedVnfInstancesLinksSelfInstantiate = new InlineResponse201LinksSelf(); + expectedVnfInstancesLinksSelfInstantiate.setHref( + (APPLICATION_CONFIG_BASE_URL + "/vnflcm/v1/vnf_instances/" + VNF_INSTANCE_ID + "/instantiate")); + assertThat(result.getLinks().getInstantiate()).isEqualTo(expectedVnfInstancesLinksSelfInstantiate); + } + +} -- cgit 1.2.3-korg