Added checks on the cloud owner to determine if 12/109912/1
authorBoslet, Cory <cory.boslet@att.com>
Tue, 7 Jul 2020 15:45:27 +0000 (11:45 -0400)
committerBenjamin, Max (mb388a) <mb388a@att.com>
Tue, 7 Jul 2020 15:45:29 +0000 (11:45 -0400)
Added checks on the cloud owner to determine if vlans should be done,
added null pointer checks, fixed and add unit test.
Removed autowiring of client and removed logging
Changed the default value of the cloud owner.
Updated unit test to mock the new env property call
Removed to make HeatBridgeImpl a spring class.

Issue-ID: SO-3033
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: I31cfa83bc74fe9786f220d952a78e5bf4f04bb3e

adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateAAIInventory.java
adapters/mso-openstack-adapters/src/main/java/org/onap/so/heatbridge/HeatBridgeApi.java
adapters/mso-openstack-adapters/src/main/java/org/onap/so/heatbridge/HeatBridgeImpl.java
adapters/mso-openstack-adapters/src/test/java/org/onap/so/heatbridge/HeatBridgeImplTest.java

index eab0451..df4229c 100644 (file)
@@ -108,7 +108,8 @@ public class CreateAAIInventory {
             if (!CollectionUtils.isEmpty(oobMgtNetNames)) {
                 oobMgtNetIds = heatBridgeClient.extractNetworkIds(oobMgtNetNames);
             }
-            heatBridgeClient.buildAddVserverLInterfacesToAaiAction(stackResources, oobMgtNetIds);
+            heatBridgeClient.buildAddVserverLInterfacesToAaiAction(stackResources, oobMgtNetIds,
+                    cloudInformation.getOwner());
             logger.debug(
                     "Successfully queried neutron resources and built AAI actions to add l-interfaces to vservers.");
 
index d0ca87d..9c09886 100644 (file)
@@ -134,8 +134,10 @@ public interface HeatBridgeApi {
      *
      * @param stackResources Openstack Heat stack resource list
      * @param oobMgtNetIds List of OOB network IDs list
+     * @param cloudOwner
      */
-    void buildAddVserverLInterfacesToAaiAction(List<Resource> stackResources, List<String> oobMgtNetIds);
+    void buildAddVserverLInterfacesToAaiAction(List<Resource> stackResources, List<String> oobMgtNetIds,
+            String cloudOwner);
 
     /**
      * Query and build AAI actions for Openstack Compute resources to AAI's pserver and pinterface objects
index 53736e9..f375553 100644 (file)
@@ -79,6 +79,7 @@ import org.onap.so.heatbridge.openstack.factory.OpenstackClientFactoryImpl;
 import org.onap.so.heatbridge.utils.HeatBridgeUtils;
 import org.onap.so.logger.LoggingAnchor;
 import org.onap.so.logger.MessageEnum;
+import org.onap.so.spring.SpringContextHelper;
 import org.openstack4j.model.compute.Server;
 import org.openstack4j.model.heat.Resource;
 import org.openstack4j.model.network.IP;
@@ -87,6 +88,7 @@ import org.openstack4j.model.network.NetworkType;
 import org.openstack4j.model.network.Port;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableMap;
@@ -109,6 +111,8 @@ public class HeatBridgeImpl implements HeatBridgeApi {
     private String tenantId;
     private AaiHelper aaiHelper = new AaiHelper();
     private CloudIdentity cloudIdentity;
+    private Environment env;
+
 
     public HeatBridgeImpl(AAIResourcesClient resourcesClient, final CloudIdentity cloudIdentity,
             @Nonnull final String cloudOwner, @Nonnull final String cloudRegionId, @Nonnull final String regionId,
@@ -124,7 +128,10 @@ public class HeatBridgeImpl implements HeatBridgeApi {
         this.regionId = regionId;
         this.tenantId = tenantId;
         this.resourcesClient = resourcesClient;
-        this.transaction = resourcesClient.beginSingleTransaction();
+        if (resourcesClient != null)
+            this.transaction = resourcesClient.beginSingleTransaction();
+        if (SpringContextHelper.getAppContext() != null)
+            this.env = SpringContextHelper.getAppContext().getEnvironment();
     }
 
     public HeatBridgeImpl() {
@@ -253,7 +260,7 @@ public class HeatBridgeImpl implements HeatBridgeApi {
 
     @Override
     public void buildAddVserverLInterfacesToAaiAction(final List<Resource> stackResources,
-            final List<String> oobMgtNetIds) {
+            final List<String> oobMgtNetIds, String cloudOwner) {
         Objects.requireNonNull(osClient, ERR_MSG_NULL_OS_CLIENT);
         List<String> portIds =
                 extractStackResourceIdsByResourceType(stackResources, HeatBridgeConstants.OS_PORT_RESOURCE_TYPE);
@@ -283,7 +290,9 @@ public class HeatBridgeImpl implements HeatBridgeApi {
             }
             lIf.setL2Multicasting(isL2Multicast);
             updateLInterfaceIps(port, lIf);
-            updateLInterfaceVlan(port, lIf);
+            if (cloudOwner.equals(env.getProperty("mso.cloudOwner.included", ""))) {
+                updateLInterfaceVlan(port, lIf);
+            }
 
             // Update l-interface to the vserver
             transaction.create(AAIUriFactory.createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner, cloudRegionId,
index 464a17d..de40f1e 100644 (file)
@@ -42,6 +42,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.doReturn;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.Charset;
@@ -58,7 +59,9 @@ import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.InjectMocks;
 import org.mockito.Mock;
+import org.mockito.Spy;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.aai.domain.yang.LInterface;
 import org.onap.aai.domain.yang.PInterface;
@@ -85,6 +88,7 @@ import org.openstack4j.model.network.NetworkType;
 import org.openstack4j.model.network.Port;
 import org.openstack4j.openstack.heat.domain.HeatResource;
 import org.openstack4j.openstack.heat.domain.HeatResource.Resources;
+import org.springframework.core.env.Environment;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.collect.ImmutableMap;
 
@@ -104,16 +108,20 @@ public class HeatBridgeImplTest {
 
     @Mock
     private AAIResourcesClient resourcesClient;
+
     @Mock
     private AAISingleTransactionClient transaction;
 
-    private HeatBridgeImpl heatbridge;
+    @Mock
+    private Environment env;
+
+    @InjectMocks
+    private HeatBridgeImpl heatbridge =
+            new HeatBridgeImpl(resourcesClient, cloudIdentity, CLOUD_OWNER, REGION_ID, REGION_ID, TENANT_ID);
 
     @Before
     public void setUp() throws HeatBridgeException, OpenstackClientException, BulkProcessFailed {
-
         when(resourcesClient.beginSingleTransaction()).thenReturn(transaction);
-        heatbridge = new HeatBridgeImpl(resourcesClient, cloudIdentity, CLOUD_OWNER, REGION_ID, REGION_ID, TENANT_ID);
     }
 
     @Ignore
@@ -396,7 +404,6 @@ public class HeatBridgeImplTest {
         verify(transaction, times(2)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
     }
 
-    @Ignore
     @Test
     public void testUpdateVserverLInterfacesToAai() throws HeatBridgeException {
         // Arrange
@@ -430,9 +437,10 @@ public class HeatBridgeImplTest {
         PInterface pIf = mock(PInterface.class);
         when(pIf.getInterfaceName()).thenReturn("test-port-id");
         when(resourcesClient.get(eq(PInterface.class), any(AAIResourceUri.class))).thenReturn(Optional.of(pIf));
+        when(env.getProperty("mso.cloudOwner.included", "")).thenReturn("CloudOwner");
 
         // Act
-        heatbridge.buildAddVserverLInterfacesToAaiAction(stackResources, Arrays.asList("1", "2"));
+        heatbridge.buildAddVserverLInterfacesToAaiAction(stackResources, Arrays.asList("1", "2"), "CloudOwner");
 
         // Assert
         verify(transaction, times(5)).create(any(AAIResourceUri.class), any(LInterface.class));
@@ -440,6 +448,48 @@ public class HeatBridgeImplTest {
         verify(osClient, times(5)).getNetworkById(anyString());
     }
 
+    @Test
+    public void testUpdateVserverLInterfacesToAai_skipVlans() throws HeatBridgeException {
+        // Arrange
+        List<Resource> stackResources = (List<Resource>) extractTestStackResources();
+        Port port = mock(Port.class);
+        when(port.getId()).thenReturn("test-port-id");
+        when(port.getName()).thenReturn("test-port-name");
+        when(port.getvNicType()).thenReturn(HeatBridgeConstants.OS_SRIOV_PORT_TYPE);
+        when(port.getMacAddress()).thenReturn("78:4f:43:68:e2:78");
+        when(port.getNetworkId()).thenReturn("890a203a-23gg-56jh-df67-731656a8f13a");
+        when(port.getDeviceId()).thenReturn("test-device-id");
+        when(port.getVifDetails()).thenReturn(ImmutableMap.of(HeatBridgeConstants.OS_VLAN_NETWORK_KEY, "2345"));
+        String pfPciId = "0000:08:00.0";
+        when(port.getProfile()).thenReturn(ImmutableMap.of(HeatBridgeConstants.OS_PCI_SLOT_KEY, pfPciId,
+                HeatBridgeConstants.OS_PHYSICAL_NETWORK_KEY, "physical_network_id"));
+
+        Network network = mock(Network.class);
+        when(network.getId()).thenReturn("test-network-id");
+        when(network.getNetworkType()).thenReturn(NetworkType.VLAN);
+        when(network.getProviderSegID()).thenReturn("2345");
+
+        when(osClient.getPortById("212a203a-9764-4f42-84ea-731536a8f13a")).thenReturn(port);
+        when(osClient.getPortById("387e3904-8948-43d1-8635-b6c2042b54da")).thenReturn(port);
+        when(osClient.getPortById("70a09dfd-f1c5-4bc8-bd8f-dc539b8d662a")).thenReturn(port);
+        when(osClient.getPortById("12f88b4d-c8a4-4fbd-bcb4-7e36af02430b")).thenReturn(port);
+        when(osClient.getPortById("c54b9f45-b413-4937-bbe4-3c8a5689cfc9")).thenReturn(port);
+
+        SriovPf sriovPf = new SriovPf();
+        sriovPf.setPfPciId(pfPciId);
+        PInterface pIf = mock(PInterface.class);
+        when(pIf.getInterfaceName()).thenReturn("test-port-id");
+        when(resourcesClient.get(eq(PInterface.class), any(AAIResourceUri.class))).thenReturn(Optional.of(pIf));
+
+        // Act
+        heatbridge.buildAddVserverLInterfacesToAaiAction(stackResources, Arrays.asList("1", "2"), "CloudOwner");
+
+        // Assert
+        verify(transaction, times(5)).create(any(AAIResourceUri.class), any(LInterface.class));
+        verify(osClient, times(5)).getPortById(anyString());
+        verify(osClient, times(0)).getNetworkById(anyString());
+    }
+
     private List<? extends Resource> extractTestStackResources() {
         List<HeatResource> stackResources = null;
         try {
@@ -463,4 +513,6 @@ public class HeatBridgeImplTest {
         }
         return content;
     }
+
+
 }