diff options
author | 2019-08-20 15:01:02 +0000 | |
---|---|---|
committer | 2019-08-20 15:01:25 +0000 | |
commit | b7045fd2144322b419c899275b2feeafff3f9f65 (patch) | |
tree | 5db4403d4a8f4a422a0ed8dc81fc50b7db68e57a /plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator | |
parent | 241cd37ac8cf7a12bb0b160e8881d1543726b86b (diff) |
Adding cloud region endpoints
Change-Id: I68563dd035b4e52b29ddce012a84b9124a8a48e3
Issue-ID: SO-2219
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
Diffstat (limited to 'plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator')
7 files changed, 397 insertions, 3 deletions
diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java new file mode 100644 index 00000000..2b45499a --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java @@ -0,0 +1,128 @@ +/*- + * ============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.CLOUD_REGION; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGIONS; +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.Relationship; +import org.onap.so.aaisimulator.models.CloudRegionKey; +import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider; +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 = 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}/relationship-list/relationship", + 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, CLOUD_REGION); + + } + + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java index 173b1099..ac1ad8c5 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java @@ -73,7 +73,7 @@ public class LinesOfBusinessController { } - @GetMapping(value = "/{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @GetMapping(value = "{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public ResponseEntity<?> getLineOfBusiness(@PathVariable("line-of-business-name") final String lineOfBusinessName, final HttpServletRequest request) { LOGGER.info("retrieving Platform for 'platform-name': {} ...", lineOfBusinessName); @@ -83,7 +83,7 @@ public class LinesOfBusinessController { LOGGER.info("found LineOfBusiness {} in cache", platform); return ResponseEntity.ok(platform); } - LOGGER.error("Unable to find LineOfBusiness in cahce using {}", lineOfBusinessName); + LOGGER.error("Unable to find LineOfBusiness in cache using {}", lineOfBusinessName); return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS); } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/models/CloudRegionKey.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/models/CloudRegionKey.java new file mode 100644 index 00000000..d49a1a37 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/models/CloudRegionKey.java @@ -0,0 +1,79 @@ +/*- + * ============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.models; + +import java.io.Serializable; +import org.springframework.util.ObjectUtils; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class CloudRegionKey implements Serializable { + + private static final long serialVersionUID = 6175094050996035737L; + + private final String cloudOwner; + + private final String cloudRegionId; + + public CloudRegionKey(final String cloudOwner, final String cloudRegionId) { + this.cloudOwner = cloudOwner; + this.cloudRegionId = cloudRegionId; + } + + /** + * @return the cloudOwner + */ + public String getCloudOwner() { + return cloudOwner; + } + + /** + * @return the cloudRegionId + */ + public String getCloudRegionId() { + return cloudRegionId; + } + + public boolean isValid() { + return cloudOwner != null && !cloudOwner.isEmpty() && cloudRegionId != null && !cloudRegionId.isEmpty(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (ObjectUtils.nullSafeHashCode(cloudOwner)); + result = prime * result + (ObjectUtils.nullSafeHashCode(cloudRegionId)); + + return result; + } + + @Override + public boolean equals(final Object obj) { + if (obj instanceof CloudRegionKey) { + final CloudRegionKey other = (CloudRegionKey) obj; + return ObjectUtils.nullSafeEquals(cloudOwner, other.cloudOwner) + && ObjectUtils.nullSafeEquals(cloudRegionId, other.cloudRegionId); + } + return false; + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java new file mode 100644 index 00000000..a10a8ac4 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.service.providers; + +import java.util.Optional; +import org.onap.aai.domain.yang.CloudRegion; +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.aaisimulator.models.CloudRegionKey; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public interface CloudRegionCacheServiceProvider extends Clearable { + + void putCloudRegion(final CloudRegionKey cloudRegionKey, final CloudRegion cloudRegion); + + Optional<CloudRegion> getCloudRegion(final CloudRegionKey cloudRegionKey); + + Optional<Relationship> addRelationShip(final CloudRegionKey key, final Relationship relationship, + final String requestUri); +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java new file mode 100644 index 00000000..41422c40 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java @@ -0,0 +1,132 @@ +/*- + * ============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.service.providers; + +import static org.onap.so.aaisimulator.utils.CacheName.CLOUD_REGION_CACHE; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_OWNER; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_REGION_ID; +import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_OWNER_DEFINED_TYPE; +import static org.onap.so.aaisimulator.utils.Constants.LOCATED_IN; +import java.util.List; +import java.util.Optional; +import org.onap.aai.domain.yang.CloudRegion; +import org.onap.aai.domain.yang.RelatedToProperty; +import org.onap.aai.domain.yang.Relationship; +import org.onap.aai.domain.yang.RelationshipData; +import org.onap.aai.domain.yang.RelationshipList; +import org.onap.so.aaisimulator.models.CloudRegionKey; +import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Service +public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider + implements CloudRegionCacheServiceProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class); + + + @Autowired + public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager) { + super(cacheManager); + } + + @Override + public void putCloudRegion(final CloudRegionKey cloudRegionKey, final CloudRegion cloudRegion) { + LOGGER.info("Adding CloudRegion to cache with key: {} ...", cloudRegionKey); + final Cache cache = getCache(CLOUD_REGION_CACHE.getName()); + cache.put(cloudRegionKey, cloudRegion); + + } + + @Override + public Optional<CloudRegion> getCloudRegion(final CloudRegionKey cloudRegionKey) { + LOGGER.info("getting CloudRegion from cache using key: {}", cloudRegionKey); + final Cache cache = getCache(CLOUD_REGION_CACHE.getName()); + final CloudRegion value = cache.get(cloudRegionKey, CloudRegion.class); + if (value != null) { + return Optional.of(value); + } + LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey); + return Optional.empty(); + } + + @Override + public Optional<Relationship> addRelationShip(final CloudRegionKey key, final Relationship relationship, + final String requestUri) { + final Optional<CloudRegion> optional = getCloudRegion(key); + if (optional.isPresent()) { + final CloudRegion cloudRegion = optional.get(); + RelationshipList relationshipList = cloudRegion.getRelationshipList(); + if (relationshipList == null) { + relationshipList = new RelationshipList(); + cloudRegion.setRelationshipList(relationshipList); + } + relationshipList.getRelationship().add(relationship); + + LOGGER.info("Successfully added relation to CloudRegion with key: {}", key); + + + final Relationship resultantRelationship = new Relationship(); + resultantRelationship.setRelatedTo(CLOUD_REGION); + resultantRelationship.setRelationshipLabel(LOCATED_IN); + resultantRelationship.setRelatedLink(requestUri); + + final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData(); + relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegion.getCloudOwner())); + relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegion.getCloudRegionId())); + + final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty(); + + final RelatedToProperty relatedToProperty = new RelatedToProperty(); + relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE); + relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType()); + relatedToPropertyList.add(relatedToProperty); + + return Optional.of(resultantRelationship); + + } + LOGGER.error("Unable to find CloudRegion using key: {} ...", key); + return Optional.empty(); + } + + private RelationshipData getRelationshipData(final String key, final String value) { + final RelationshipData relationshipData = new RelationshipData(); + relationshipData.setRelationshipKey(key); + relationshipData.setRelationshipValue(value); + return relationshipData; + } + + @Override + public void clearAll() { + clearCahce(CLOUD_REGION_CACHE.getName()); + + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java index 7f804c9d..1874b83e 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java @@ -31,7 +31,8 @@ public enum CacheName { GENERIC_VNF_CACHE("generic-vnf-cache"), OWNING_ENTITY_CACHE("owning-entity-cache"), PLATFORM_CACHE("platform-cache"), - LINES_OF_BUSINESS_CACHE("lines-of-business-cache"); + LINES_OF_BUSINESS_CACHE("lines-of-business-cache"), + CLOUD_REGION_CACHE("cloud-region-cache"); private String name; diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java index 29b3af99..979da6b9 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java @@ -31,6 +31,10 @@ public class Constants { public static final String BUSINESS_URL = BASE_URL + "/business"; + public static final String CLOUD_INFRASTRUCTURE_URL = BASE_URL + "/cloud-infrastructure"; + + public static final String CLOUD_REGIONS = CLOUD_INFRASTRUCTURE_URL + "/cloud-regions/cloud-region/"; + public static final String CUSTOMER_URL = BUSINESS_URL + "/customers/customer/"; public static final String PROJECT_URL = BUSINESS_URL + "/projects/project/"; @@ -91,6 +95,17 @@ public class Constants { public static final String SERVICE_INSTANCE_SERVICE_INSTANCE_NAME = "service-instance.service-instance-name"; + public static final String CLOUD_REGION_OWNER_DEFINED_TYPE = "cloud-region.owner-defined-type"; + + public static final String CLOUD_REGION_CLOUD_REGION_ID = "cloud-region.cloud-region-id"; + + public static final String CLOUD_REGION_CLOUD_OWNER = "cloud-region.cloud-owner"; + + public static final String LOCATED_IN = "org.onap.relationships.inventory.LocatedIn"; + + public static final String CLOUD_REGION = "cloud-region"; + + private Constants() {} } |