diff options
Diffstat (limited to 'adapters')
11 files changed, 333 insertions, 58 deletions
diff --git a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java index 038b0e44d3..3995185f23 100644 --- a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java +++ b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/aai/AaiConnection.java @@ -26,12 +26,12 @@ import java.util.List; import java.util.Optional; import org.apache.logging.log4j.util.Strings; import org.onap.aai.domain.yang.*; -import org.onap.so.adapters.vevnfm.exception.VeVnfmException; import org.onap.aaiclient.client.aai.AAIObjectType; import org.onap.aaiclient.client.aai.AAIResourcesClient; import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; import org.onap.aaiclient.client.graphinventory.entities.uri.Depth; +import org.onap.so.adapters.vevnfm.exception.VeVnfmException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -40,7 +40,12 @@ import org.springframework.stereotype.Service; public class AaiConnection { private static final Logger logger = LoggerFactory.getLogger(AaiConnection.class); - + private static final String VSERVER = "vserver"; + private static final String CLOUD_OWNER = "cloud-region.cloud-owner"; + private static final String CLOUD_REGION_ID = "cloud-region.cloud-region-id"; + private static final String TENANT_ID = "tenant.tenant-id"; + private static final String VSERVER_ID = "vserver.vserver-id"; + private static final String VSERVER_NAME = "vserver.vserver-name"; private static final String SELFLINK = "selflink"; private static final int FIRST_INDEX = 0; @@ -52,6 +57,30 @@ public class AaiConnection { } } + static String getRelationshipData(final Relationship relationship, final String relationshipDataKey) { + if (relationship != null && relationship.getRelationshipData() != null) { + for (final RelationshipData relationshipData : relationship.getRelationshipData()) { + if (relationshipDataKey.equals(relationshipData.getRelationshipKey())) { + return relationshipData.getRelationshipValue(); + } + } + } + + return null; + } + + static String getRelatedToProperty(final Relationship relationship, final String propertyKey) { + if (relationship != null && relationship.getRelatedToProperty() != null) { + for (final RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) { + if (propertyKey.equals(relatedToProperty.getPropertyKey())) { + return relatedToProperty.getPropertyValue(); + } + } + } + + return null; + } + private AAIResourcesClient getResourcesClient() { if (resourcesClient == null) { resourcesClient = new AAIResourcesClient(); @@ -125,7 +154,8 @@ public class AaiConnection { final int size = genericVnfList.size(); if (size == 1) { - return genericVnfList.get(FIRST_INDEX).getVnfId(); + final GenericVnf genericVnf = genericVnfList.get(FIRST_INDEX); + return genericVnf.getVnfId(); } else if (size > 1) { logger.warn("more generic vnfs available"); } @@ -133,4 +163,50 @@ public class AaiConnection { return null; } + + public String receiveVserverName(final String genericId) { + final AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, genericId); + final Optional<GenericVnf> response = getResourcesClient().get(GenericVnf.class, resourceUri); + + if (response.isPresent()) { + final GenericVnf genericVnf = response.get(); + final RelationshipList relationshipList = genericVnf.getRelationshipList(); + + if (relationshipList == null || relationshipList.getRelationship() == null) { + return null; + } + + for (final Relationship relationship : relationshipList.getRelationship()) { + if (VSERVER.equals(relationship.getRelatedTo())) { + final String vserverName = getRelatedToProperty(relationship, VSERVER_NAME); + + if (vserverName == null) { + final String cloudOwner = getRelationshipData(relationship, CLOUD_OWNER); + final String cloudId = getRelationshipData(relationship, CLOUD_REGION_ID); + final String tenantId = getRelationshipData(relationship, TENANT_ID); + final String vserverId = getRelationshipData(relationship, VSERVER_ID); + return receiveVserverNameFromParams(cloudOwner, cloudId, tenantId, vserverId); + } + + return vserverName; + } + } + } + + return null; + } + + private String receiveVserverNameFromParams(final String cloudOwner, final String cloudId, final String tenantId, + final String vserverId) { + final AAIResourceUri resourceUri = + AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudId, tenantId, vserverId); + final Optional<Vserver> response = getResourcesClient().get(Vserver.class, resourceUri); + + if (response.isPresent()) { + final Vserver vserver = response.get(); + return vserver.getVserverName(); + } + + return null; + } } diff --git a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/AaiEvent.java b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/AaiEvent.java index ceabb8a020..6c7aaf8f5c 100644 --- a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/AaiEvent.java +++ b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/AaiEvent.java @@ -25,11 +25,13 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class AaiEvent { private final boolean vserverIsClosedLoopDisabled; + private final String vserverName; private final String genericVnfVnfId; - public AaiEvent(final boolean cld, final String id) { - this.vserverIsClosedLoopDisabled = cld; - this.genericVnfVnfId = id; + public AaiEvent(final boolean vserverIsClosedLoopDisabled, final String vserverName, final String genericVnfVnfId) { + this.vserverIsClosedLoopDisabled = vserverIsClosedLoopDisabled; + this.vserverName = vserverName; + this.genericVnfVnfId = genericVnfVnfId; } @JsonProperty("vserver.is-closed-loop-disabled") @@ -37,6 +39,11 @@ public class AaiEvent { return vserverIsClosedLoopDisabled; } + @JsonProperty("vserver.vserver-name") + public String getVserverName() { + return vserverName; + } + @JsonProperty("generic-vnf.vnf-id") public String getGenericVnfVnfId() { return genericVnfVnfId; diff --git a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/DmaapEvent.java b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/DmaapEvent.java index 142c6b0399..55a36a5d85 100644 --- a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/DmaapEvent.java +++ b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/event/DmaapEvent.java @@ -21,10 +21,10 @@ package org.onap.so.adapters.vevnfm.event; import static java.time.temporal.ChronoField.INSTANT_SECONDS; +import com.fasterxml.jackson.annotation.JsonProperty; import java.time.Instant; import java.util.UUID; import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification; -import com.fasterxml.jackson.annotation.JsonProperty; public class DmaapEvent { @@ -47,7 +47,8 @@ public class DmaapEvent { private final VnfLcmOperationOccurrenceNotification etsiLcmEvent; public DmaapEvent(final String closedLoopControlName, final String version, - final VnfLcmOperationOccurrenceNotification etsiLcmEvent, final String genericId) { + final VnfLcmOperationOccurrenceNotification etsiLcmEvent, final String vserverName, + final String genericId) { this.closedLoopControlName = closedLoopControlName; this.closedLoopAlarmStart = Instant.now().getLong(INSTANT_SECONDS); this.closedLoopEventClient = MSERVICE; @@ -55,7 +56,7 @@ public class DmaapEvent { this.requestId = UUID.randomUUID().toString(); this.targetType = VNF; this.target = VNFID; - this.aaiEvent = (genericId == null) ? null : new AaiEvent(false, genericId); + this.aaiEvent = (genericId == null) ? null : new AaiEvent(false, vserverName, genericId); this.from = ETSI; this.version = version; this.etsiLcmEvent = etsiLcmEvent; diff --git a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSender.java b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSender.java index bb1d26f523..13cc5d3a1e 100644 --- a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSender.java +++ b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSender.java @@ -25,6 +25,7 @@ import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccur import org.onap.so.adapters.vevnfm.aai.AaiConnection; import org.onap.so.adapters.vevnfm.configuration.ConfigProperties; import org.onap.so.adapters.vevnfm.constant.NotificationVnfFilterType; +import org.onap.so.adapters.vevnfm.event.DmaapEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -47,19 +48,14 @@ public class DmaapConditionalSender { public void send(final VnfLcmOperationOccurrenceNotification notification) { final String href = notification.getLinks().getVnfInstance().getHref(); - boolean logSent = false; + DmaapEvent dmaapEvent = null; switch (notificationVnfFilterType) { case ALL: - dmaapService.send(notification, aaiConnection.receiveGenericVnfId(href)); - logSent = true; + dmaapEvent = conditionalSend(true, notification, href); break; case AAI_CHECKED: - final String genericId = aaiConnection.receiveGenericVnfId(href); - if (Strings.isNotBlank(genericId)) { - dmaapService.send(notification, genericId); - logSent = true; - } + dmaapEvent = conditionalSend(false, notification, href); break; case NONE: break; @@ -69,7 +65,29 @@ public class DmaapConditionalSender { } final String vnfInstanceId = notification.getVnfInstanceId(); - final String not = logSent ? "" : "not "; - logger.info("The info with the VNF id '{}' is " + not + "sent to DMaaP", vnfInstanceId); + + if (dmaapEvent == null) { + logger.info("The info with the VNF id '{}' is not sent to DMaaP", vnfInstanceId); + } else { + dmaapService.send(dmaapEvent); + logger.info("The info with the VNF id '{}' is sent to DMaaP", vnfInstanceId); + } + } + + private DmaapEvent conditionalSend(final boolean allowAll, final VnfLcmOperationOccurrenceNotification notification, + final String href) { + final String genericId = aaiConnection.receiveGenericVnfId(href); + final boolean idNotBlank = Strings.isNotBlank(genericId); + String vserverName = null; + + if (idNotBlank) { + vserverName = aaiConnection.receiveVserverName(genericId); + } + + if (allowAll || idNotBlank) { + return dmaapService.createDmaapEvent(notification, vserverName, genericId); + } + + return null; } } diff --git a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapService.java b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapService.java index 4319d78d39..435e8e0593 100644 --- a/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapService.java +++ b/adapters/etsi-sol002-adapter/src/main/java/org/onap/so/adapters/vevnfm/service/DmaapService.java @@ -51,9 +51,13 @@ public class DmaapService { this.restProvider = restProvider; } - public void send(final VnfLcmOperationOccurrenceNotification notification, final String genericId) { + public DmaapEvent createDmaapEvent(final VnfLcmOperationOccurrenceNotification notification, + final String vserverName, final String genericId) { + return new DmaapEvent(closedLoopControlName, version, notification, vserverName, genericId); + } + + public void send(final DmaapEvent event) { try { - final DmaapEvent event = new DmaapEvent(closedLoopControlName, version, notification, genericId); final ResponseEntity<String> response = restProvider.postHttpRequest(event, getUrl(), String.class); final HttpStatus statusCode = response.getStatusCode(); final String body = response.getBody(); diff --git a/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/aai/AaiConnectionTest.java b/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/aai/AaiConnectionTest.java new file mode 100644 index 0000000000..12928ae9ed --- /dev/null +++ b/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/aai/AaiConnectionTest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * SO + * ================================================================================ + * Copyright (C) 2020 Samsung. 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.adapters.vevnfm.aai; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.onap.aai.domain.yang.RelatedToProperty; +import org.onap.aai.domain.yang.Relationship; +import org.onap.aai.domain.yang.RelationshipData; + +public class AaiConnectionTest { + + private static final String KEY = "key"; + private static final String VALUE = "value"; + + @Test + public void testRelationshipData() { + // given + final Relationship relationship = new Relationship(); + final RelationshipData data = new RelationshipData(); + data.setRelationshipKey(KEY); + data.setRelationshipValue(VALUE); + relationship.getRelationshipData().add(data); + + // when + final String value = AaiConnection.getRelationshipData(relationship, KEY); + + // then + assertEquals(VALUE, value); + } + + @Test + public void testRelatedToProperty() { + // given + final Relationship relationship = new Relationship(); + final RelatedToProperty property = new RelatedToProperty(); + property.setPropertyKey(KEY); + property.setPropertyValue(VALUE); + relationship.getRelatedToProperty().add(property); + + // when + final String value = AaiConnection.getRelatedToProperty(relationship, KEY); + + // then + assertEquals(VALUE, value); + } +} diff --git a/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSenderTest.java b/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSenderTest.java index b430c2bb7a..428ccad054 100644 --- a/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSenderTest.java +++ b/adapters/etsi-sol002-adapter/src/test/java/org/onap/so/adapters/vevnfm/service/DmaapConditionalSenderTest.java @@ -22,9 +22,7 @@ package org.onap.so.adapters.vevnfm.service; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -35,10 +33,12 @@ import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccur import org.onap.so.adapters.vevnfm.aai.AaiConnection; import org.onap.so.adapters.vevnfm.configuration.ConfigProperties; import org.onap.so.adapters.vevnfm.constant.NotificationVnfFilterType; +import org.onap.so.adapters.vevnfm.event.DmaapEvent; @RunWith(MockitoJUnitRunner.class) public class DmaapConditionalSenderTest { + private static final String VSERVER_NAME = "vsn"; private static final String GENERIC_ID = "gener77"; private static final String INSTANCE_ID = "insta44"; private static final String HREF = "/href"; @@ -66,6 +66,10 @@ public class DmaapConditionalSenderTest { return notification; } + private static DmaapEvent createDmaapEvent() { + return new DmaapEvent(null, null, null, null, null); + } + @Test public void testSendNone() { // given @@ -79,41 +83,52 @@ public class DmaapConditionalSenderTest { // then verify(aaiConnection, never()).receiveGenericVnfId(any()); - verify(dmaapService, never()).send(any(), any()); + verify(dmaapService, never()).createDmaapEvent(any(), any(), any()); + verify(dmaapService, never()).send(any()); } @Test public void testSendAll() { // given + final VnfLcmOperationOccurrenceNotification notification = createNotification(); + final DmaapEvent dmaapEvent = createDmaapEvent(); + when(configProperties.getNotificationVnfFilterType()).thenReturn(NotificationVnfFilterType.ALL); when(aaiConnection.receiveGenericVnfId(eq(HREF))).thenReturn(GENERIC_ID); + when(aaiConnection.receiveVserverName(eq(GENERIC_ID))).thenReturn(VSERVER_NAME); + when(dmaapService.createDmaapEvent(eq(notification), eq(VSERVER_NAME), eq(GENERIC_ID))).thenReturn(dmaapEvent); final DmaapConditionalSender sender = new DmaapConditionalSender(configProperties, aaiConnection, dmaapService); - final VnfLcmOperationOccurrenceNotification notification = createNotification(); // when sender.send(notification); // then verify(aaiConnection).receiveGenericVnfId(eq(HREF)); - verify(dmaapService).send(eq(notification), eq(GENERIC_ID)); + verify(dmaapService).createDmaapEvent(eq(notification), eq(VSERVER_NAME), eq(GENERIC_ID)); + verify(dmaapService).send(eq(dmaapEvent)); } @Test public void testSendAaiCheckedPresent() { // given + final DmaapEvent dmaapEvent = createDmaapEvent(); + final VnfLcmOperationOccurrenceNotification notification = createNotification(); + when(configProperties.getNotificationVnfFilterType()).thenReturn(NotificationVnfFilterType.AAI_CHECKED); when(aaiConnection.receiveGenericVnfId(eq(HREF))).thenReturn(GENERIC_ID); + when(aaiConnection.receiveVserverName(eq(GENERIC_ID))).thenReturn(VSERVER_NAME); + when(dmaapService.createDmaapEvent(eq(notification), eq(VSERVER_NAME), eq(GENERIC_ID))).thenReturn(dmaapEvent); final DmaapConditionalSender sender = new DmaapConditionalSender(configProperties, aaiConnection, dmaapService); - final VnfLcmOperationOccurrenceNotification notification = createNotification(); // when sender.send(notification); // then verify(aaiConnection).receiveGenericVnfId(eq(HREF)); - verify(dmaapService).send(eq(notification), eq(GENERIC_ID)); + verify(dmaapService).createDmaapEvent(eq(notification), eq(VSERVER_NAME), eq(GENERIC_ID)); + verify(dmaapService).send(eq(dmaapEvent)); } @Test @@ -130,6 +145,7 @@ public class DmaapConditionalSenderTest { // then verify(aaiConnection).receiveGenericVnfId(eq(HREF)); - verify(dmaapService, never()).send(any(), any()); + verify(dmaapService, never()).createDmaapEvent(any(), any(), any()); + verify(dmaapService, never()).send(any()); } } 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) { |