diff options
author | Plummer, Brittany <brittany.plummer@att.com> | 2020-05-20 11:24:38 -0400 |
---|---|---|
committer | Benjamin, Max (mb388a) <mb388a@att.com> | 2020-05-20 11:24:40 -0400 |
commit | 49e0a823189e91d31a157481a7b1721ead26a1e6 (patch) | |
tree | b03e064ddad98ded49a031113311228d4f078043 | |
parent | 5dd30849655e5a218c63d6c3855e8916399e49e7 (diff) |
Setup cache for getNovaClient
Setup cache for getNovaClient
Issue-ID: SO-2946
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: I2031c684fee65d73c1528ec6a60c95c1c60de1d5
4 files changed, 116 insertions, 28 deletions
diff --git a/adapters/mso-adapter-utils/pom.xml b/adapters/mso-adapter-utils/pom.xml index b8dec73f80..a121d2d89b 100644 --- a/adapters/mso-adapter-utils/pom.xml +++ b/adapters/mso-adapter-utils/pom.xml @@ -152,5 +152,13 @@ <artifactId>mso-requests-db</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-cache</artifactId> + </dependency> + <dependency> + <groupId>com.github.ben-manes.caffeine</groupId> + <artifactId>caffeine</artifactId> + </dependency> </dependencies> </project> diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaCacheConfig.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaCacheConfig.java new file mode 100644 index 0000000000..2f7d19ff1d --- /dev/null +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaCacheConfig.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.openstack.utils; + +import java.util.concurrent.TimeUnit; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import com.github.benmanes.caffeine.cache.Caffeine; + +@Configuration +public class NovaCacheConfig { + + @Bean + public CacheManager cacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager("novaClient"); + cacheManager.setCaffeine(caffeineCacheBuilder()); + return cacheManager; + } + + private Caffeine<Object, Object> caffeineCacheBuilder() { + return Caffeine.newBuilder().initialCapacity(100).maximumSize(500).expireAfterWrite(90, TimeUnit.MINUTES); + } +} diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClient.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClient.java new file mode 100644 index 0000000000..c5eeb34157 --- /dev/null +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClient.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.openstack.utils; + +import org.onap.so.cloud.authentication.KeystoneAuthHolder; +import org.onap.so.openstack.exceptions.MsoException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; +import com.woorea.openstack.nova.Nova; + + +@Component +public class NovaClient extends MsoCommonUtils { + + private static final Logger logger = LoggerFactory.getLogger(NovaClient.class); + + /** + * Gets the Nova client + * + * @param cloudSiteId id of the cloud site + * @param tenantId the tenant id + * @return the Nova client + * @throws MsoException the mso exception + */ + @Cacheable(value = "novaClient") + public Nova getNovaClient(String cloudSiteId, String tenantId) throws MsoException { + KeystoneAuthHolder keystone = getKeystoneAuthHolder(cloudSiteId, tenantId, "compute"); + Nova novaClient = new Nova(keystone.getServiceUrl()); + novaClient.token(keystone.getId()); + return novaClient; + } +} diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClientImpl.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClientImpl.java index 5d28eaaf71..dc276d9b8f 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClientImpl.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/NovaClientImpl.java @@ -21,11 +21,11 @@ package org.onap.so.openstack.utils; import java.io.IOException; -import org.onap.so.cloud.authentication.KeystoneAuthHolder; import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound; import org.onap.so.openstack.exceptions.MsoException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -50,21 +50,8 @@ public class NovaClientImpl extends MsoCommonUtils { /** The logger. */ private static final Logger logger = LoggerFactory.getLogger(NovaClientImpl.class); - /** - * Gets the Nova client - * - * @param cloudSiteId id of the cloud site - * @param tenantId the tenant id - * @return the Nova client - * @throws MsoException the mso exception - */ - private Nova getNovaClient(String cloudSiteId, String tenantId) throws MsoException { - KeystoneAuthHolder keystone = getKeystoneAuthHolder(cloudSiteId, tenantId, "compute"); - Nova novaClient = new Nova(keystone.getServiceUrl()); - novaClient.token(keystone.getId()); - return novaClient; - } - + @Autowired + private NovaClient client; /** * Query Networks @@ -83,7 +70,7 @@ public class NovaClientImpl extends MsoCommonUtils { public Flavors queryFlavors(String cloudSiteId, String tenantId, int limit, String marker) throws MsoCloudSiteNotFound, NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Flavors> request = novaClient.flavors().list(false).queryParam("limit", limit).queryParam("marker", marker); return executeAndRecordOpenstackRequest(request, false); @@ -108,8 +95,7 @@ public class NovaClientImpl extends MsoCommonUtils { public Flavor queryFlavorById(String cloudSiteId, String tenantId, String id) throws MsoCloudSiteNotFound, NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); - novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Flavor> request = novaClient.flavors().show(id); return executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { @@ -133,7 +119,7 @@ public class NovaClientImpl extends MsoCommonUtils { public HostAggregates queryHostAggregates(String cloudSiteId, String tenantId, int limit, String marker) throws MsoCloudSiteNotFound, NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<HostAggregates> request = novaClient.aggregates().list().queryParam("limit", limit).queryParam("marker", marker); return executeAndRecordOpenstackRequest(request, false); @@ -158,7 +144,7 @@ public class NovaClientImpl extends MsoCommonUtils { public HostAggregate queryHostAggregateById(String cloudSiteId, String tenantId, String id) throws MsoCloudSiteNotFound, NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<HostAggregate> request = novaClient.aggregates().showAggregate(id); return executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { @@ -182,7 +168,7 @@ public class NovaClientImpl extends MsoCommonUtils { public QuotaSet queryOSQuotaSet(String cloudSiteId, String tenantId) throws MsoCloudSiteNotFound, NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<QuotaSet> request = novaClient.quotaSets().showQuota(tenantId); return executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { @@ -204,7 +190,7 @@ public class NovaClientImpl extends MsoCommonUtils { public void deleteKeyPair(String cloudSiteId, String tenantId, String keyPairName) throws MsoCloudSiteNotFound, NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Void> request = novaClient.keyPairs().delete(keyPairName); executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { @@ -215,7 +201,7 @@ public class NovaClientImpl extends MsoCommonUtils { public Server queryServerById(String cloudSiteId, String tenantId, String id) throws NovaClientException { try { - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Server> request = novaClient.servers().show(id); return executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { @@ -230,7 +216,7 @@ public class NovaClientImpl extends MsoCommonUtils { JsonNode actualObj = mapper.readTree(request); Entity<JsonNode> openstackEntity = new Entity<>(actualObj, "application/json"); CharSequence actionPath = "/servers/" + id + "/action"; - Nova novaClient = getNovaClient(cloudSiteId, tenantId); + Nova novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Void> OSRequest = new OpenStackRequest<>(novaClient, HttpMethod.POST, actionPath, openstackEntity, Void.class); executeAndRecordOpenstackRequest(OSRequest, false); @@ -240,7 +226,7 @@ public class NovaClientImpl extends MsoCommonUtils { throws NovaClientException { Nova novaClient; try { - novaClient = getNovaClient(cloudSiteId, tenantId); + novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Void> request = novaClient.servers().attachVolume(serverId, volumeAttachment.getVolumeId(), volumeAttachment.getDevice()); executeAndRecordOpenstackRequest(request, false); @@ -254,7 +240,7 @@ public class NovaClientImpl extends MsoCommonUtils { throws NovaClientException { Nova novaClient; try { - novaClient = getNovaClient(cloudSiteId, tenantId); + novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Void> request = novaClient.servers().detachVolume(serverId, volumeId); executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { @@ -266,7 +252,7 @@ public class NovaClientImpl extends MsoCommonUtils { public Hypervisors getHypervisorDetails(String cloudSiteId, String tenantId) throws NovaClientException { Nova novaClient; try { - novaClient = getNovaClient(cloudSiteId, tenantId); + novaClient = client.getNovaClient(cloudSiteId, tenantId); OpenStackRequest<Hypervisors> request = novaClient.hypervisors().listDetail(); return executeAndRecordOpenstackRequest(request, false); } catch (MsoException e) { |