diff options
Diffstat (limited to 'plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller')
11 files changed, 2013 insertions, 0 deletions
diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/AaiSimulatorController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/AaiSimulatorController.java new file mode 100755 index 00000000..f5912908 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/AaiSimulatorController.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.HEALTHY; +import javax.ws.rs.core.MediaType; +import org.onap.so.aaisimulator.utils.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = Constants.BASE_URL) +public class AaiSimulatorController { + private static final Logger LOGGER = LoggerFactory.getLogger(AaiSimulatorController.class); + + @ResponseBody + @GetMapping(value = "/healthcheck", produces = MediaType.TEXT_PLAIN) + @ResponseStatus(code = HttpStatus.OK) + public String healthCheck() { + LOGGER.info("Running health check ..."); + return HEALTHY; + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/BusinessController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/BusinessController.java new file mode 100755 index 00000000..4a0ed1b9 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/BusinessController.java @@ -0,0 +1,356 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_TYPE; +import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_URL; +import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF; +import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID; +import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE; +import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION; +import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.Customer; +import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aai.domain.yang.GenericVnfs; +import org.onap.aai.domain.yang.Relationship; +import org.onap.aai.domain.yang.RelationshipData; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.aai.domain.yang.ServiceInstances; +import org.onap.aai.domain.yang.ServiceSubscription; +import org.onap.so.aaisimulator.models.NodeServiceInstance; +import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider; +import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider; +import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +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.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = CUSTOMER_URL) +public class BusinessController { + + private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class); + private final CustomerCacheServiceProvider cacheServiceProvider; + private final NodesCacheServiceProvider nodesCacheServiceProvider; + private final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider; + + @Autowired + public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider, + final NodesCacheServiceProvider nodesCacheServiceProvider, + final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + this.nodesCacheServiceProvider = nodesCacheServiceProvider; + this.genericVnfCacheServiceProvider = genericVnfCacheServiceProvider; + } + + @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId, + final HttpServletRequest request) { + LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId); + + final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId); + if (optional.isPresent()) { + final Customer customer = optional.get(); + LOGGER.info("found customer {} in cache", customer); + return ResponseEntity.ok(customer); + } + + LOGGER.error("Couldn't find {} in cache", globalCustomerId); + return getRequestErrorResponseEntity(request, CUSTOMER_TYPE); + } + + @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putCustomer(@RequestBody final Customer customer, + @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) { + LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId); + + if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) { + customer.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putCustomer(globalCustomerId, customer); + return ResponseEntity.accepted().build(); + + } + + @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, final HttpServletRequest request) { + LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...", + globalCustomerId, serviceType); + + final Optional<ServiceSubscription> optional = + cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType); + if (optional.isPresent()) { + final ServiceSubscription serviceSubscription = optional.get(); + LOGGER.info("found service subscription {} in cache", serviceSubscription); + return ResponseEntity.ok(serviceSubscription); + } + + LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId, + serviceType); + return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION); + } + + @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putServiceSubscription(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @RequestBody final ServiceSubscription serviceSubscription, final HttpServletRequest request) { + LOGGER.info("Will add service subscription for 'global customer id': {} and 'service type': {} ...", + globalCustomerId, serviceType); + + if (cacheServiceProvider.putServiceSubscription(globalCustomerId, serviceType, serviceSubscription)) { + LOGGER.info("Successfully add service subscription in cache ..."); + return ResponseEntity.accepted().build(); + } + + LOGGER.error("Couldn't add service subscription using 'global customer id': {} and 'service type': {}", + globalCustomerId, serviceType); + return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION); + } + + @GetMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @RequestParam(name = "service-instance-name") final String serviceInstanceName, + @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) { + + LOGGER.info( + "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...", + globalCustomerId, serviceType, serviceInstanceName, depth); + + final Optional<ServiceInstances> optional = + cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName); + if (optional.isPresent()) { + final ServiceInstances serviceInstances = optional.get(); + LOGGER.info("found service instance {} in cache", serviceInstances); + return ResponseEntity.ok(serviceInstances); + } + LOGGER.error( + "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache", + globalCustomerId, serviceType, serviceInstanceName, depth); + return getRequestErrorResponseEntity(request); + } + + @GetMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestParam(name = "depth", required = false) final Integer depth, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + + LOGGER.info( + "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...", + globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format); + + final Optional<ServiceInstance> optional = + cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId); + if (optional.isPresent()) { + final ServiceInstance serviceInstance = optional.get(); + LOGGER.info("found service instance {} in cache", serviceInstance); + return ResponseEntity.ok(serviceInstance); + } + LOGGER.error( + "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache", + globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format); + return getRequestErrorResponseEntity(request); + } + + @PutMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String invocationId, + @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) { + + LOGGER.info( + "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...", + globalCustomerId, serviceType, serviceInstanceId); + + if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) { + serviceInstance.setResourceVersion(getResourceVersion()); + } + + if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId, + serviceInstance)) { + nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance( + globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI())); + return ResponseEntity.accepted().build(); + } + + LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache", + globalCustomerId, serviceType, serviceInstanceId); + return getRequestErrorResponseEntity(request); + } + + @PostMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> patchSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride, + @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) { + + LOGGER.info( + "Will post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...", + globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride); + + if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) { + cacheServiceProvider.patchServiceInstance(globalCustomerId, serviceType, serviceInstanceId, + serviceInstance); + return ResponseEntity.accepted().build(); + } + LOGGER.error("{} not supported ... ", xHttpHeaderOverride); + + return getRequestErrorResponseEntity(request); + } + + + @GetMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/related-to/generic-vnfs", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getRelatedToGenericVnf(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestParam(name = "vnf-name", required = true) final String vnfName, final HttpServletRequest request) { + + LOGGER.info( + "Will retrieve generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...", + globalCustomerId, serviceType, serviceInstanceId, vnfName); + + final Optional<Relationship> optional = + cacheServiceProvider.getRelationship(globalCustomerId, serviceType, serviceInstanceId, vnfName); + + if (optional.isPresent()) { + + final Relationship relationship = optional.get(); + final Optional<RelationshipData> relationshipDataOptional = relationship.getRelationshipData().stream() + .filter(existing -> GENERIC_VNF_VNF_ID.equals(existing.getRelationshipKey())).findFirst(); + + if (relationshipDataOptional.isPresent()) { + final RelationshipData relationshipData = relationshipDataOptional.get(); + final String vnfId = relationshipData.getRelationshipValue(); + final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(vnfId); + if (genericVnfOptional.isPresent()) { + final GenericVnfs genericVnfs = new GenericVnfs(); + genericVnfs.getGenericVnf().add(genericVnfOptional.get()); + LOGGER.info("found service instance {} in cache", relationship); + return ResponseEntity.ok(genericVnfs); + } + } + } + LOGGER.error( + "Couldn't find generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...", + globalCustomerId, serviceType, serviceInstanceId, vnfName); + return getRequestErrorResponseEntity(request, GENERIC_VNF); + } + + @PutMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}" + + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putSericeInstanceRelationShip( + @PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + + LOGGER.info( + "Will add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...", + relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId); + final Optional<Relationship> optional = cacheServiceProvider.addRelationShip(globalCustomerId, serviceType, + serviceInstanceId, relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + + LOGGER.error( + "Couldn't add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...", + relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId); + + return getRequestErrorResponseEntity(request); + } + + @DeleteMapping( + value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> deleteSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId, + @PathVariable("service-type") final String serviceType, + @PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) { + + LOGGER.info( + "Will delete SericeInstance for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}", + globalCustomerId, serviceType, serviceInstanceId, resourceVersion); + + if (cacheServiceProvider.deleteSericeInstance(globalCustomerId, serviceType, serviceInstanceId, + resourceVersion)) { + LOGGER.info( + "Successfully deleted SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}", + globalCustomerId, serviceType, serviceInstanceId, resourceVersion); + return ResponseEntity.noContent().build(); + } + + LOGGER.error( + "Unable to delete SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}", + globalCustomerId, serviceType, serviceInstanceId, resourceVersion); + + return getRequestErrorResponseEntity(request); + + } +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java new file mode 100755 index 00000000..8e007de3 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java @@ -0,0 +1,396 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGIONS; +import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST; +import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.onap.so.aaisimulator.utils.Constants.VSERVER; +import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.CloudRegion; +import org.onap.aai.domain.yang.EsrSystemInfo; +import org.onap.aai.domain.yang.EsrSystemInfoList; +import org.onap.aai.domain.yang.Relationship; +import org.onap.aai.domain.yang.Tenant; +import org.onap.aai.domain.yang.Vserver; +import org.onap.so.aaisimulator.models.CloudRegionKey; +import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider; +import org.onap.so.aaisimulator.utils.HttpServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +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.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(path = CLOUD_REGIONS) +public class CloudRegionsController { + private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class); + + private final CloudRegionCacheServiceProvider cacheServiceProvider; + + @Autowired + public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "{cloud-owner}/{cloud-region-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putCloudRegion(@RequestBody final CloudRegion cloudRegion, + @PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + + if (key.isValid()) { + LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key); + if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) { + cloudRegion.setResourceVersion(getResourceVersion()); + } + cacheServiceProvider.putCloudRegion(key, cloudRegion); + return ResponseEntity.accepted().build(); + } + + LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @GetMapping(value = "{cloud-owner}/{cloud-region-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getCloudRegion(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Retrieving CloudRegion using key : {} with depth: {}...", key, depth); + if (key.isValid()) { + final Optional<CloudRegion> optional = cacheServiceProvider.getCloudRegion(key); + if (optional.isPresent()) { + final CloudRegion cloudRegion = optional.get(); + LOGGER.info("found CloudRegion {} in cache", cloudRegion); + return ResponseEntity.ok(cloudRegion); + } + } + LOGGER.error("Unable to find CloudRegion in cache using {}", key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @PutMapping(value = "{cloud-owner}/{cloud-region-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putRelationShip(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, @RequestBody final Relationship relationship, + final HttpServletRequest request) { + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + + final Optional<Relationship> optional = + cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + + LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key); + return getRequestErrorResponseEntity(request, VSERVER); + + } + + @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putTenant(@RequestBody final Tenant tenant, + @PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + + if (key.isValid()) { + LOGGER.info("Will add Tenant to cache with key 'key': {} ....", key); + if (tenant.getResourceVersion() == null || tenant.getResourceVersion().isEmpty()) { + tenant.setResourceVersion(getResourceVersion()); + } + if (cacheServiceProvider.putTenant(key, tenantId, tenant)) { + return ResponseEntity.accepted().build(); + } + } + + LOGGER.error("Unable to add Tenant in cache using key {}", key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getTenant(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Retrieving Tenant using key : {} and tenant-id:{} ...", key, tenantId); + if (key.isValid()) { + final Optional<Tenant> optional = cacheServiceProvider.getTenant(key, tenantId); + if (optional.isPresent()) { + final Tenant tenant = optional.get(); + LOGGER.info("found Tenant {} in cache", tenant); + return ResponseEntity.ok(tenant); + } + } + LOGGER.error("Unable to find Tenant in cache key : {} and tenant-id:{} ...", key, tenantId); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @PutMapping( + value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putRelationShip(@RequestBody final Relationship relationship, + @PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Will put RelationShip for key : {} and tenant-id:{} ...", key, tenantId); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), key, tenantId, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + + } + LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink()); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @PutMapping(value = "{cloud-owner}/{cloud-region-id}/esr-system-info-list/esr-system-info/{esr-system-info-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putEsrSystemInfo(@RequestBody final EsrSystemInfo esrSystemInfo, + @PathVariable("esr-system-info-id") final String esrSystemInfoId, + @PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + + LOGGER.info("Will put esrSystemInfo for 'key': {} ...", key); + + if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) { + esrSystemInfo.setResourceVersion(getResourceVersion()); + + } + + if (cacheServiceProvider.putEsrSystemInfo(key, esrSystemInfoId, esrSystemInfo)) { + LOGGER.info("Successfully added EsrSystemInfo key : {} ...", key, esrSystemInfo); + return ResponseEntity.accepted().build(); + } + LOGGER.error("Unable to add EsrSystemInfo in cache for key : {} ...", key); + + return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST); + } + + @GetMapping(value = "{cloud-owner}/{cloud-region-id}/esr-system-info-list", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getEsrSystemInfoList(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Retrieving EsrSystemInfoList using key : {} ...", key); + if (key.isValid()) { + final Optional<EsrSystemInfoList> optional = cacheServiceProvider.getEsrSystemInfoList(key); + if (optional.isPresent()) { + final EsrSystemInfoList esrSystemInfoList = optional.get(); + LOGGER.info("found EsrSystemInfoList {} in cache", esrSystemInfoList); + return ResponseEntity.ok(esrSystemInfoList); + } + } + LOGGER.error("Unable to find EsrSystemInfoList in cache using key : {} ...", key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putVserver(@RequestBody final Vserver vServer, + @PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + if (vServer.getResourceVersion() == null || vServer.getResourceVersion().isEmpty()) { + vServer.setResourceVersion(getResourceVersion()); + } + LOGGER.info("Will put Vserver in cache using using key: {}, tenantId: {}, vServerId: {} ...", key, tenantId, + vServerId); + + if (cacheServiceProvider.putVserver(key, tenantId, vServerId, vServer)) { + + if (vServer.getRelationshipList() != null) { + for (final Relationship relationship : vServer.getRelationshipList().getRelationship()) { + if (relationship.getRelatedLink() != null) { + final String requestUri = request.getRequestURI(); + final String targetBaseUrl = + HttpServiceUtils.getBaseUrl(request.getRequestURL(), requestUri).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addVServerRelationShip(incomingHeader, + targetBaseUrl, requestUri, key, tenantId, vServerId, relationship); + if (!result) { + LOGGER.error( + "Unable to add Vserver relationship in cache using key: {}, tenantId: {}, vServerId: {}", + key, tenantId, vServerId); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + LOGGER.info("Successfully added relationship with {}", relationship.getRelatedLink()); + } + } + } + + LOGGER.info("Successfully added Vserver for key: {}, tenantId: {}, vServerId: {} ...", key, tenantId, + vServerId); + return ResponseEntity.accepted().build(); + } + LOGGER.error("Unable to add Vserver in cache using key: {}, tenantId: {}, vServerId: {}", key, tenantId, + vServerId); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getVserver(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Retrieving Vserver using key: {}, tenant-id: {} and vserver-id: {}...", key, tenantId, vServerId); + final Optional<Vserver> optional = cacheServiceProvider.getVserver(key, tenantId, vServerId); + if (optional.isPresent()) { + final Vserver vServer = optional.get(); + LOGGER.info("found Vserver {} in cache", vServer); + return ResponseEntity.ok(vServer); + } + LOGGER.error("Unable to find Vserver in cache using key: {}, tenant-id: {} and vserver-id: {}...", key, + tenantId, vServerId); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + + @DeleteMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> deleteVserver(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) { + + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Will delete Vserver using key: {}, tenant-id: {}, vserver-id: {} and resource-version: {}...", key, + tenantId, vServerId, resourceVersion); + + + if (cacheServiceProvider.deleteVserver(key, tenantId, vServerId, resourceVersion)) { + LOGGER.info( + "Successfully delete Vserver from cache for key: {}, tenant-id: {}, vserver-id: {} and resource-version: {}", + key, tenantId, vServerId, resourceVersion); + return ResponseEntity.noContent().build(); + } + + LOGGER.error( + "Unable to delete Vserver from cache using key: {}, tenant-id: {}, vserver-id: {} and resource-version: {} ...", + key, tenantId, vServerId, resourceVersion); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + + @PutMapping( + value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}" + + RELATIONSHIP_LIST_RELATIONSHIP_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putVserverRelationShip(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addVServerRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), key, tenantId, vServerId, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + } + LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + + } + + @PutMapping( + value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}" + + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putBiDirectionalVServerRelationShip(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + final Optional<Relationship> optional = cacheServiceProvider.addvServerRelationShip(key, tenantId, vServerId, + relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + + } +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/ExternalSystemEsrController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/ExternalSystemEsrController.java new file mode 100755 index 00000000..297c2eb0 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/ExternalSystemEsrController.java @@ -0,0 +1,175 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO; +import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST; +import static org.onap.so.aaisimulator.utils.Constants.ESR_VNFM; +import static org.onap.so.aaisimulator.utils.Constants.EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.List; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.EsrSystemInfo; +import org.onap.aai.domain.yang.EsrSystemInfoList; +import org.onap.aai.domain.yang.EsrVnfm; +import org.onap.aai.domain.yang.EsrVnfmList; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.service.providers.ExternalSystemCacheServiceProvider; +import org.onap.so.aaisimulator.utils.HttpServiceUtils; +import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(path = EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL) +public class ExternalSystemEsrController { + private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemEsrController.class); + + private final ExternalSystemCacheServiceProvider cacheServiceProvider; + + @Autowired + public ExternalSystemEsrController(final ExternalSystemCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "/esr-vnfm/{vnfm-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putEsrVnfm(@RequestBody final EsrVnfm esrVnfm, + @PathVariable("vnfm-id") final String vnfmId, final HttpServletRequest request) { + LOGGER.info("Will put esr-vnfm to cache for 'vnfm id': {} ...", esrVnfm.getVnfmId()); + + if (esrVnfm.getResourceVersion() == null || esrVnfm.getResourceVersion().isEmpty()) { + esrVnfm.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putEsrVnfm(vnfmId, esrVnfm); + return ResponseEntity.accepted().build(); + } + + @GetMapping(value = "/esr-vnfm/{vnfm-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getEsrVnfm(@PathVariable("vnfm-id") final String vnfmId, + @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) { + LOGGER.info("Will retrieve ESR VNFM for 'vnfm id': {} with depth: {}...", vnfmId, depth); + + final Optional<EsrVnfm> optional = cacheServiceProvider.getEsrVnfm(vnfmId); + if (optional.isPresent()) { + final EsrVnfm esrVnfm = optional.get(); + LOGGER.info("found esrVnfm {} in cache", esrVnfm); + return ResponseEntity.ok(esrVnfm); + } + + LOGGER.error("Couldn't Esr Vnfm for 'vnfm id': {} with depth: {}...", vnfmId, depth); + return getRequestErrorResponseEntity(request, ESR_VNFM); + } + + @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getEsrVnfmList(final HttpServletRequest request) { + LOGGER.info("Will retrieve a list of all ESR VNFMs"); + + final List<EsrVnfm> esrVnfms = cacheServiceProvider.getAllEsrVnfm(); + LOGGER.info("found {} Esr Vnfms in cache", esrVnfms.size()); + + final EsrVnfmList esrVnfmList = new EsrVnfmList(); + esrVnfmList.getEsrVnfm().addAll(esrVnfms); + + return ResponseEntity.ok(esrVnfmList); + } + + @PutMapping(value = "/esr-vnfm/{vnfm-id}/esr-system-info-list/esr-system-info/{esr-system-info-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putEsrSystemInfo(@RequestBody final EsrSystemInfo esrSystemInfo, + @PathVariable("vnfm-id") final String vnfmId, + @PathVariable("esr-system-info-id") final String esrSystemInfoId, final HttpServletRequest request) { + LOGGER.info("Will put esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId, esrSystemInfo); + + if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) { + esrSystemInfo.setResourceVersion(getResourceVersion()); + + } + + if (cacheServiceProvider.putEsrSystemInfo(vnfmId, esrSystemInfoId, esrSystemInfo)) { + LOGGER.info("Successfully added EsrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId, + esrSystemInfo); + return ResponseEntity.accepted().build(); + } + LOGGER.error("Unable to add esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId, + esrSystemInfo); + return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST); + } + + @GetMapping(value = "/esr-vnfm/{vnfm-id}/esr-system-info-list", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getEsrSystemInfoList(@PathVariable("vnfm-id") final String vnfmId, + final HttpServletRequest request) { + LOGGER.info("Will retrieve esrSystemInfoList for 'vnfm id': {} ...", vnfmId); + + final Optional<EsrSystemInfoList> optional = cacheServiceProvider.getEsrSystemInfoList(vnfmId); + if (optional.isPresent()) { + final EsrSystemInfoList esrSystemInfoList = optional.get(); + LOGGER.info("found esrSystemInfoList {} in cache", esrSystemInfoList); + return ResponseEntity.ok(esrSystemInfoList); + } + + LOGGER.error("Couldn't find esrSystemInfoList for 'vnfm id': {} ...", vnfmId); + return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO); + } + + @PutMapping(value = "/esr-vnfm/{vnfm-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putEsrVnfmRelationShip(@RequestBody final Relationship relationship, + @PathVariable("vnfm-id") final String vnfmId, final HttpServletRequest request) { + LOGGER.info("Will put RelationShip for 'vnfm-id': {} ...", vnfmId); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), vnfmId, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + } + LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink()); + return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, ESR_VNFM); + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/GenericVnfsController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/GenericVnfsController.java new file mode 100755 index 00000000..43fe47da --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/GenericVnfsController.java @@ -0,0 +1,215 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.APPLICATION_MERGE_PATCH_JSON; +import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF; +import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNFS_URL; +import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE; +import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.List; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aai.domain.yang.GenericVnfs; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider; +import org.onap.so.aaisimulator.utils.HttpServiceUtils; +import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +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.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(path = GENERIC_VNFS_URL) +public class GenericVnfsController { + + private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class); + + private final GenericVnfCacheServiceProvider cacheServiceProvider; + + + @Autowired + public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf, + @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) { + LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId); + + if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) { + genericVnf.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putGenericVnf(vnfId, genericVnf); + return ResponseEntity.accepted().build(); + + } + + @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId, + @RequestParam(name = "depth", required = false) final Integer depth, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + LOGGER.info( + "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...", + vnfId, depth, resultIndex, resultSize, format); + + final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId); + + if (optional.isPresent()) { + final GenericVnf genericVnf = optional.get(); + LOGGER.info("found GenericVnf {} in cache", genericVnf); + return ResponseEntity.ok(genericVnf); + } + + LOGGER.error( + "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...", + vnfId, depth, resultIndex, resultSize, format); + return getRequestErrorResponseEntity(request, GENERIC_VNF); + + } + + @PutMapping(value = "/generic-vnf/{vnf-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship, + @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) { + LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), vnfId, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + } + LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink()); + return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF); + } + + @PutMapping(value = "/generic-vnf/{vnf-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putBiDirectionalRelationShip(@RequestBody final Relationship relationship, + @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) { + LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId); + + final Optional<Relationship> optional = + cacheServiceProvider.addRelationShip(vnfId, relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + + LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink()); + return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF); + } + + @PostMapping(value = "/generic-vnf/{vnf-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> patchGenericVnf(@RequestBody final GenericVnf genericVnf, + @PathVariable("vnf-id") final String vnfId, + @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride, + final HttpServletRequest request) { + + LOGGER.info("Will post GenericVnf to cache with 'vnf-id': {} and '{}': {} ...", vnfId, X_HTTP_METHOD_OVERRIDE, + xHttpHeaderOverride); + + if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) { + if (cacheServiceProvider.patchGenericVnf(vnfId, genericVnf)) { + return ResponseEntity.accepted().build(); + } + LOGGER.error("Unable to apply patch to GenericVnf using 'vnf-id': {} ... ", vnfId); + return getRequestErrorResponseEntity(request, GENERIC_VNF); + } + LOGGER.error("{} not supported ... ", xHttpHeaderOverride); + + return getRequestErrorResponseEntity(request, GENERIC_VNF); + } + + @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getGenericVnfs(@RequestParam(name = "selflink") final String selflink, + final HttpServletRequest request) { + LOGGER.info("will retrieve GenericVnfs using selflink: {}", selflink); + + final List<GenericVnf> genericVnfList = cacheServiceProvider.getGenericVnfs(selflink); + + if (genericVnfList.isEmpty()) { + LOGGER.error("No matching generic vnfs found using selflink: {}", selflink); + return getRequestErrorResponseEntity(request, GENERIC_VNF); + } + + LOGGER.info("found {} GenericVnfs in cache", genericVnfList.size()); + final GenericVnfs genericVnfs = new GenericVnfs(); + genericVnfs.getGenericVnf().addAll(genericVnfList); + return ResponseEntity.ok(genericVnfs); + } + + @DeleteMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> deleteGenericVnf(@PathVariable("vnf-id") final String vnfId, + @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) { + LOGGER.info("Will delete GenericVnf for 'vnf-id': {} and 'resource-version': {}", vnfId, resourceVersion); + + if (cacheServiceProvider.deleteGenericVnf(vnfId, resourceVersion)) { + LOGGER.info("Successfully delete GenericVnf from cache for 'vnf-id': {} and 'resource-version': {}", vnfId, + resourceVersion); + return ResponseEntity.noContent().build(); + } + + LOGGER.error("Unable to delete GenericVnf for 'vnf-id': {} and 'resource-version': {} ...", vnfId, + resourceVersion); + return getRequestErrorResponseEntity(request, GENERIC_VNF); + + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java new file mode 100755 index 00000000..b3438d27 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java @@ -0,0 +1,138 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.LINES_OF_BUSINESS_URL; +import static org.onap.so.aaisimulator.utils.Constants.LINE_OF_BUSINESS; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.LineOfBusiness; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.models.Format; +import org.onap.so.aaisimulator.models.Results; +import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(path = LINES_OF_BUSINESS_URL) +public class LinesOfBusinessController { + private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class); + + private final LinesOfBusinessCacheServiceProvider cacheServiceProvider; + + @Autowired + public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "{line-of-business-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putLineOfBusiness(@RequestBody final LineOfBusiness lineOfBusiness, + @PathVariable("line-of-business-name") final String lineOfBusinessName, final HttpServletRequest request) { + + LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...", + lineOfBusiness.getLineOfBusinessName()); + + if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) { + lineOfBusiness.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness); + return ResponseEntity.accepted().build(); + } + + + @GetMapping(value = "{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getLineOfBusiness(@PathVariable("line-of-business-name") final String lineOfBusinessName, + @RequestParam(name = "depth", required = false) final Integer depth, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + + LOGGER.info( + "retrieving Platform for 'platform-name': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...", + lineOfBusinessName, depth, resultIndex, resultSize, format); + + final Optional<LineOfBusiness> optional = cacheServiceProvider.getLineOfBusiness(lineOfBusinessName); + if (optional.isPresent()) { + + final Format value = Format.forValue(format); + switch (value) { + case RAW: + final LineOfBusiness platform = optional.get(); + LOGGER.info("found LineOfBusiness {} in cache", platform); + return ResponseEntity.ok(platform); + case COUNT: + final Map<String, Object> map = new HashMap<>(); + map.put(LINE_OF_BUSINESS, 1); + return ResponseEntity.ok(new Results(map)); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + } + LOGGER.error("Unable to find LineOfBusiness in cache using {}", lineOfBusinessName); + return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS); + } + + @PutMapping(value = "/{line-of-business-name}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putRelationShip(@PathVariable("line-of-business-name") final String lineOfBusinessName, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + final Optional<Relationship> optional = + cacheServiceProvider.addRelationShip(lineOfBusinessName, relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + + LOGGER.error("Couldn't add {} relationship for 'line-of-business-name': {} ...", relationship.getRelatedTo(), + lineOfBusinessName); + + return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS); + + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/NodesController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/NodesController.java new file mode 100755 index 00000000..21bcae93 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/NodesController.java @@ -0,0 +1,115 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF; +import static org.onap.so.aaisimulator.utils.Constants.NODES_URL; +import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_LINK; +import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_TYPE; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.GenericVnfs; +import org.onap.aai.domain.yang.ServiceInstance; +import org.onap.so.aaisimulator.models.Format; +import org.onap.so.aaisimulator.models.NodeServiceInstance; +import org.onap.so.aaisimulator.models.Results; +import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = NODES_URL) +public class NodesController { + + + private static final Logger LOGGER = LoggerFactory.getLogger(NodesController.class); + + private final NodesCacheServiceProvider cacheServiceProvider; + + @Autowired + public NodesController(final NodesCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @GetMapping(value = "/service-instances/service-instance/{service-instance-id}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getProject(@PathVariable(name = "service-instance-id") final String serviceInstanceId, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + LOGGER.info("retrieving service instance using 'service-instance-id': {} and format: {}...", serviceInstanceId, + format); + + final Optional<NodeServiceInstance> optional = cacheServiceProvider.getNodeServiceInstance(serviceInstanceId); + if (!optional.isPresent()) { + LOGGER.error("Couldn't find {} in cache", serviceInstanceId); + return getRequestErrorResponseEntity(request); + } + + final Format value = Format.forValue(format); + final NodeServiceInstance nodeServiceInstance = optional.get(); + switch (value) { + case PATHED: + LOGGER.info("found project {} in cache", nodeServiceInstance); + final Map<String, Object> map = new LinkedHashMap<>(); + map.put(RESOURCE_TYPE, nodeServiceInstance.getResourceType()); + map.put(RESOURCE_LINK, nodeServiceInstance.getResourceLink()); + return ResponseEntity.ok(new Results(map)); + case RAW: + final Optional<ServiceInstance> serviceInstance = + cacheServiceProvider.getServiceInstance(nodeServiceInstance); + if (serviceInstance.isPresent()) { + return ResponseEntity.ok(serviceInstance.get()); + } + LOGGER.error("Unable to find Service instance in cahce using {}", nodeServiceInstance); + return getRequestErrorResponseEntity(request); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + return getRequestErrorResponseEntity(request); + } + + @GetMapping(value = "/generic-vnfs", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getGenericVnfs(@RequestParam(name = "vnf-name") final String vnfName, + final HttpServletRequest request) { + LOGGER.info("will find GenericVnfs for name: {}", vnfName); + final Optional<GenericVnfs> optional = cacheServiceProvider.getGenericVnfs(vnfName); + if (optional.isPresent()) { + LOGGER.info("found matching GenericVnfs for name: {}", vnfName); + return ResponseEntity.ok(optional.get()); + } + LOGGER.error("Unable to find GenericVnfs in cahce using {}", vnfName); + return getRequestErrorResponseEntity(request, GENERIC_VNF); + } +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/OwningEntityController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/OwningEntityController.java new file mode 100755 index 00000000..c5ade0cb --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/OwningEntityController.java @@ -0,0 +1,137 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.OWNING_ENTITY; +import static org.onap.so.aaisimulator.utils.Constants.OWNING_ENTITY_URL; +import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.OwningEntity; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.models.Format; +import org.onap.so.aaisimulator.models.Results; +import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider; +import org.onap.so.aaisimulator.utils.HttpServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = OWNING_ENTITY_URL) +public class OwningEntityController { + + private static final Logger LOGGER = LoggerFactory.getLogger(OwningEntityController.class); + + private final OwnEntityCacheServiceProvider cacheServiceProvider; + + @Autowired + public OwningEntityController(final OwnEntityCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "{owning-entity-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putOwningEntity(@RequestBody final OwningEntity owningEntity, + @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) { + LOGGER.info("Will add OwningEntity to cache with key 'owning-entity-id': {} ...", + owningEntity.getOwningEntityId()); + + if (owningEntity.getResourceVersion() == null || owningEntity.getResourceVersion().isEmpty()) { + owningEntity.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putOwningEntity(owningEntityId, owningEntity); + return ResponseEntity.accepted().build(); + } + + @GetMapping(value = "{owning-entity-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getOwningEntity(@PathVariable("owning-entity-id") final String owningEntityId, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + LOGGER.info("retrieving owning entity for 'owning-entity-id': {} ...", owningEntityId); + + final Optional<OwningEntity> optional = cacheServiceProvider.getOwningEntity(owningEntityId); + if (!optional.isPresent()) { + LOGGER.error("Couldn't find {} in cache", owningEntityId); + return getRequestErrorResponseEntity(request); + } + + final Format value = Format.forValue(format); + switch (value) { + case RAW: + final OwningEntity owningEntity = optional.get(); + LOGGER.info("found OwningEntity {} in cache", owningEntity); + return ResponseEntity.ok(owningEntity); + case COUNT: + final Map<String, Object> map = new HashMap<>(); + map.put(OWNING_ENTITY, 1); + return ResponseEntity.ok(new Results(map)); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + return getRequestErrorResponseEntity(request); + } + + @PutMapping(value = "/{owning-entity-id}/relationship-list/relationship", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putOwningEntityRelationShip(@RequestBody final Relationship relationship, + @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) { + + LOGGER.info("adding relationship for owning-entity-id: {} ...", owningEntityId); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + + final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), owningEntityId, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + } + + LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink()); + return getRequestErrorResponseEntity(request); + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java new file mode 100755 index 00000000..39e35947 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java @@ -0,0 +1,134 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.PLATFORM; +import static org.onap.so.aaisimulator.utils.Constants.PLATFORMS_URL; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.Platform; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.models.Format; +import org.onap.so.aaisimulator.models.Results; +import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(path = PLATFORMS_URL) +public class PlatformController { + private static final Logger LOGGER = LoggerFactory.getLogger(PlatformController.class); + + private final PlatformCacheServiceProvider cacheServiceProvider; + + @Autowired + public PlatformController(final PlatformCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "{platform-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putPlatform(@RequestBody final Platform platform, + @PathVariable("platform-name") final String platformName, final HttpServletRequest request) { + LOGGER.info("Will add Platform to cache with key 'platform-name': {} ...", platform.getPlatformName()); + + if (platform.getResourceVersion() == null || platform.getResourceVersion().isEmpty()) { + platform.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putPlatform(platformName, platform); + return ResponseEntity.accepted().build(); + } + + @GetMapping(value = "/{platform-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getPlatform(@PathVariable("platform-name") final String platformName, + @RequestParam(name = "depth", required = false) final Integer depth, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + + LOGGER.info( + "retrieving Platform for 'platform-name': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...", + platformName, depth, resultIndex, resultSize, format); + final Optional<Platform> optional = cacheServiceProvider.getPlatform(platformName); + if (optional.isPresent()) { + + final Format value = Format.forValue(format); + switch (value) { + case RAW: + final Platform platform = optional.get(); + LOGGER.info("found Platform {} in cache", platform); + return ResponseEntity.ok(platform); + case COUNT: + final Map<String, Object> map = new HashMap<>(); + map.put(PLATFORM, 1); + return ResponseEntity.ok(new Results(map)); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + + } + LOGGER.error("Unable to find Platform in cahce using {}", platformName); + return getRequestErrorResponseEntity(request, PLATFORM); + } + + @PutMapping(value = "/{platform-name}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putRelationShip(@PathVariable("platform-name") final String platformName, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + final Optional<Relationship> optional = + cacheServiceProvider.addRelationShip(platformName, relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + + LOGGER.error("Couldn't add {} relationship for 'platform-name': {} ...", relationship.getRelatedTo(), + platformName); + + return getRequestErrorResponseEntity(request, PLATFORM); + + } +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PnfsController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PnfsController.java new file mode 100755 index 00000000..ff0e3dcf --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PnfsController.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.aaisimulator.controller; + + +import org.onap.aai.domain.yang.v15.Pnf; +import org.onap.aai.domain.yang.v15.Pnfs; +import org.onap.so.aaisimulator.service.providers.PnfCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +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.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import java.util.List; +import java.util.Optional; + +import static org.onap.so.aaisimulator.utils.Constants.APPLICATION_MERGE_PATCH_JSON; +import static org.onap.so.aaisimulator.utils.Constants.PNF; +import static org.onap.so.aaisimulator.utils.Constants.PNFS_URL; +import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; + +/** + * @author Raj Gumma (raj.gumma@est.tech) + */ +@Controller +@RequestMapping(path = PNFS_URL) +public class PnfsController { + + private static final Logger LOGGER = LoggerFactory.getLogger(PnfsController.class); + + private final PnfCacheServiceProvider cacheServiceProvider; + + + @Autowired + public PnfsController(final PnfCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "/pnf/{pnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putPnf(@RequestBody final Pnf pnf, + @PathVariable("pnf-id") final String pnfId, final HttpServletRequest request) { + LOGGER.info("Will add Pnf to cache with 'pnf-id': {} ...", pnfId); + + if (pnf.getResourceVersion() == null || pnf.getResourceVersion().isEmpty()) { + pnf.setResourceVersion(getResourceVersion()); + } + cacheServiceProvider.putPnf(pnfId, pnf); + return ResponseEntity.accepted().build(); + } + + @GetMapping(value = "/pnf/{pnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getPnf(@PathVariable("pnf-id") final String pnfId, final HttpServletRequest request) { + LOGGER.info("Will get Pnf for 'pnf-id': {} ", pnfId); + + final Optional<Pnf> optional = cacheServiceProvider.getPnf(pnfId); + + if (optional.isPresent()) { + final Pnf pnf = optional.get(); + LOGGER.info("found Pnf {} in cache", pnf); + return ResponseEntity.ok(pnf); + } + + LOGGER.error("Unable to find Pnf in cache for 'pnf-id': {}", pnfId); + return getRequestErrorResponseEntity(request, "pnf"); + + } + + @PostMapping(value = "/pnf/{pnf-id}", + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> patchPnf(@RequestBody final Pnf pnf, + @PathVariable("pnf-id") final String pnfId, + @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride, + final HttpServletRequest request) { + + LOGGER.info("Will post Pnf to cache with 'pnf-id': {} and '{}': {} ...", pnfId, X_HTTP_METHOD_OVERRIDE, + xHttpHeaderOverride); + + if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) { + if (cacheServiceProvider.patchPnf(pnfId, pnf)) { + return ResponseEntity.accepted().build(); + } + LOGGER.error("Unable to apply patch to Pnf using 'pnf-id': {} ... ", pnfId); + return getRequestErrorResponseEntity(request, PNF); + } + LOGGER.error("{} not supported ... ", xHttpHeaderOverride); + + return getRequestErrorResponseEntity(request, PNF); + } + + @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getPnfs(@RequestParam(name = "selflink") final String selflink, + final HttpServletRequest request) { + LOGGER.info("will retrieve Pnfs using selflink: {}", selflink); + + final List<Pnf> pnfList = cacheServiceProvider.getPnfs(selflink); + + if (pnfList.isEmpty()) { + LOGGER.error("No matching pnfs found using selflink: {}", selflink); + return getRequestErrorResponseEntity(request, PNF); + } + + LOGGER.info("found {} Pnfs in cache", pnfList.size()); + final Pnfs pnfs = new Pnfs(); + pnfs.getPnf().addAll(pnfList); + return ResponseEntity.ok(pnfs); + } + + @DeleteMapping(value = "/pnf/{pnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> deletePnf(@PathVariable("pnf-id") final String pnfId, + @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) { + LOGGER.info("Will delete Pnf for 'pnf-id': {} and 'resource-version': {}", pnfId, resourceVersion); + + if (cacheServiceProvider.deletePnf(pnfId, resourceVersion)) { + LOGGER.info("Successfully delete Pnf from cache for 'pnf-id': {} and 'resource-version': {}", pnfId, + resourceVersion); + return ResponseEntity.noContent().build(); + } + + LOGGER.error("Unable to delete Pnf for 'pnf-id': {} and 'resource-version': {} ...", pnfId, + resourceVersion); + return getRequestErrorResponseEntity(request, PNF); + + } + +} diff --git a/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/ProjectController.java b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/ProjectController.java new file mode 100755 index 00000000..490982a5 --- /dev/null +++ b/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/ProjectController.java @@ -0,0 +1,137 @@ +/*- + * ============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.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.PROJECT; +import static org.onap.so.aaisimulator.utils.Constants.PROJECT_URL; +import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.Project; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.models.Format; +import org.onap.so.aaisimulator.models.Results; +import org.onap.so.aaisimulator.service.providers.ProjectCacheServiceProvider; +import org.onap.so.aaisimulator.utils.HttpServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Controller +@RequestMapping(path = PROJECT_URL) +public class ProjectController { + private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class); + + private final ProjectCacheServiceProvider cacheServiceProvider; + + @Autowired + public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putProject(@RequestBody final Project project, + @PathVariable("project-name") final String projectName, final HttpServletRequest request) { + LOGGER.info("Will put project for 'project-name': {} ...", project.getProjectName()); + + if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) { + project.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putProject(projectName, project); + return ResponseEntity.accepted().build(); + + } + + @GetMapping(value = "/{project-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getProject(@PathVariable("project-name") final String projectName, + @RequestParam(name = "resultIndex", required = false) final Integer resultIndex, + @RequestParam(name = "resultSize", required = false) final Integer resultSize, + @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) { + LOGGER.info("retrieving project for 'project-name': {} ...", projectName); + + final Optional<Project> optional = cacheServiceProvider.getProject(projectName); + if (!optional.isPresent()) { + LOGGER.error("Couldn't find {} in cache", projectName); + return getRequestErrorResponseEntity(request); + } + + final Format value = Format.forValue(format); + switch (value) { + case RAW: + final Project project = optional.get(); + LOGGER.info("found project {} in cache", project); + return ResponseEntity.ok(project); + case COUNT: + final Map<String, Object> map = new HashMap<>(); + map.put(PROJECT, 1); + return ResponseEntity.ok(new Results(map)); + default: + break; + } + LOGGER.error("invalid format type :{}", format); + return getRequestErrorResponseEntity(request); + } + + @PutMapping(value = "/{project-name}" + RELATIONSHIP_LIST_RELATIONSHIP_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> putProjectRelationShip(@RequestBody final Relationship relationship, + @PathVariable("project-name") final String projectName, final HttpServletRequest request) { + + LOGGER.info("adding relationship for project-name: {} ...", projectName); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + + final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), projectName, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + } + + LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink()); + return getRequestErrorResponseEntity(request); + } + +} |