possibility to use inventoried PNF instance in new service instance 26/107726/7
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Thu, 14 May 2020 15:33:22 +0000 (17:33 +0200)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Tue, 19 May 2020 10:59:46 +0000 (12:59 +0200)
Issue-ID: SO-2937
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
Change-Id: I5b34234d453bbd5ff1fef3c41f5cd50b23f66db1

bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/aai/tasks/AAICreateTasks.java
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/AAIPnfResources.java
bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/orchestration/AAIPnfResourcesTest.java

index 385b96c..e3181c3 100644 (file)
@@ -260,10 +260,11 @@ public class AAICreateTasks {
     public void createPnf(BuildingBlockExecution execution) {
         try {
             Pnf pnf = extractPojosForBB.extractByKey(execution, ResourceKey.PNF);
+            aaiPnfResources.checkIfPnfExistsInAaiAndCanBeUsed(pnf.getPnfName());
             ServiceInstance serviceInstance =
                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
             aaiPnfResources.createPnfAndConnectServiceInstance(pnf, serviceInstance);
-        } catch (BBObjectNotFoundException e) {
+        } catch (Exception e) {
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
         }
     }
index a3df22a..b8562ba 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.so.client.orchestration;
 
+import com.google.common.base.Strings;
 import java.util.Optional;
 import org.onap.so.bpmn.common.InjectionHelper;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
@@ -29,12 +30,16 @@ import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
 import org.onap.so.client.aai.mapper.AAIObjectMapper;
 import org.onap.so.db.catalog.beans.OrchestrationStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 @Component
 public class AAIPnfResources {
 
+    private static final Logger logger = LoggerFactory.getLogger(AAIPnfResources.class);
+
     @Autowired
     private InjectionHelper injectionHelper;
 
@@ -59,4 +64,35 @@ public class AAIPnfResources {
         pnfCopy.setOrchestrationStatus(orchestrationStatus);
         injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
     }
+
+    public void checkIfPnfExistsInAaiAndCanBeUsed(String pnfName) throws Exception {
+        Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai = injectionHelper.getAaiClient()
+                .get(org.onap.aai.domain.yang.Pnf.class, AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfName));
+        if (pnfFromAai.isPresent() && isOrchestrationStatusSet(pnfFromAai.get())) {
+            checkOrchestrationStatusOfExistingPnf(pnfFromAai.get());
+        }
+    }
+
+    private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
+        if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
+            logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
+                    pnfFromAai.getPnfName());
+            return false;
+        }
+        return true;
+    }
+
+    private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
+        if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
+            logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
+                    pnfFromAai.getPnfName());
+        } else {
+            String errorMessage = String.format(
+                    "pnf with name %s already exists with orchestration status %s, only status Inventoried allows to use existing pnf",
+                    pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
+            logger.error(errorMessage);
+            throw new Exception(errorMessage);
+
+        }
+    }
 }
index 215af9a..15c4b48 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.so.client.orchestration;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.argThat;
@@ -27,13 +28,17 @@ import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 import java.util.Optional;
+import joptsimple.internal.Strings;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.aaiclient.client.aai.AAIObjectType;
+import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
 import org.onap.so.bpmn.common.InjectionHelper;
 import org.onap.so.bpmn.common.data.TestDataSetup;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
@@ -46,6 +51,8 @@ import org.onap.so.db.catalog.beans.OrchestrationStatus;
 @RunWith(MockitoJUnitRunner.Silent.class)
 public class AAIPnfResourcesTest extends TestDataSetup {
 
+    private static final String PNF_NAME = "pnfTest";
+
     private Pnf pnf;
     private ServiceInstance serviceInstance;
 
@@ -59,7 +66,7 @@ public class AAIPnfResourcesTest extends TestDataSetup {
     protected AAIResourcesClient aaiResourcesClientMock;
 
     @InjectMocks
-    AAIPnfResources aaiPnfResources = new AAIPnfResources();
+    AAIPnfResources testedObject = new AAIPnfResources();
 
     @Before
     public void setUp() {
@@ -78,7 +85,7 @@ public class AAIPnfResourcesTest extends TestDataSetup {
         doReturn(aaiResourcesClientMock).when(aaiResourcesClientMock).createIfNotExists(any(AAIResourceUri.class),
                 eq(Optional.of(pnfYang)));
 
-        aaiPnfResources.createPnfAndConnectServiceInstance(pnf, serviceInstance);
+        testedObject.createPnfAndConnectServiceInstance(pnf, serviceInstance);
 
         assertEquals(OrchestrationStatus.INVENTORIED, pnf.getOrchestrationStatus());
         verify(aaiResourcesClientMock, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
@@ -89,7 +96,7 @@ public class AAIPnfResourcesTest extends TestDataSetup {
         org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
         doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
 
-        aaiPnfResources.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
+        testedObject.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
 
         assertEquals(OrchestrationStatus.ACTIVE, pnf.getOrchestrationStatus());
         verify(aaiObjectMapperMock, times(1))
@@ -97,4 +104,57 @@ public class AAIPnfResourcesTest extends TestDataSetup {
         verify(aaiResourcesClientMock, times(1)).update(any(AAIResourceUri.class), eq(pnfYang));
     }
 
+    @Test
+    public void existingPnfInAaiWithInventoriedStatusCanBeUsed() throws Exception {
+        // given
+        org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(OrchestrationStatus.INVENTORIED.toString());
+        when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
+                AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
+        // when
+        testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
+    }
+
+    @Test
+    public void existingPnfInAaiWithNullStatusCanBeUsed() throws Exception {
+        // given
+        org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(null);
+        when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
+                AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
+        // when
+        testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
+    }
+
+    @Test
+    public void existingPnfInAaiWithEmptyStatusCanBeUsed() throws Exception {
+        // given
+        org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(Strings.EMPTY);
+        when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
+                AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
+        // when
+        testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
+    }
+
+    @Test
+    public void existingPnfInAaiCanNotBeUsed() {
+        // given
+        org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(OrchestrationStatus.ACTIVE.toString());
+        when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
+                AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
+        // when
+        try {
+            testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
+        } catch (Exception e) {
+            // then
+            assertThat(e.getMessage()).isEqualTo(String.format(
+                    "pnf with name %s already exists with orchestration status Active, only status Inventoried allows to use existing pnf",
+                    PNF_NAME));
+        }
+    }
+
+    private org.onap.aai.domain.yang.Pnf createPnf(String orchestrationStatus) {
+        org.onap.aai.domain.yang.Pnf pnfFromAai = new org.onap.aai.domain.yang.Pnf();
+        pnfFromAai.setPnfName(PNF_NAME);
+        pnfFromAai.setOrchestrationStatus(orchestrationStatus);
+        return pnfFromAai;
+    }
 }