Join ExistingElementsCounterMaps with ServiceInstantiation 49/99349/2
authorIttay Stern <ittay.stern@att.com>
Mon, 9 Dec 2019 18:06:15 +0000 (20:06 +0200)
committerIttay Stern <ittay.stern@att.com>
Mon, 9 Dec 2019 19:18:49 +0000 (21:18 +0200)
This new pretty creature is called "ServiceInstantiationTemplate" :-)

Issue-ID: VID-724
Change-Id: I1437d76e79512920079b574844b19368c07f501f
Signed-off-by: Ittay Stern <ittay.stern@att.com>
vid-app-common/src/main/java/org/onap/vid/controller/AsyncInstantiationController.java
vid-app-common/src/main/java/org/onap/vid/model/aaiTree/ExistingElementsCounterMaps.java [new file with mode: 0644]
vid-app-common/src/main/java/org/onap/vid/model/aaiTree/ServiceInstance.java
vid-app-common/src/main/java/org/onap/vid/model/serviceInstantiation/ServiceInstantiationTemplate.java [new file with mode: 0644]
vid-app-common/src/main/java/org/onap/vid/services/AsyncInstantiationBusinessLogic.java
vid-app-common/src/main/java/org/onap/vid/services/AsyncInstantiationBusinessLogicImpl.java
vid-app-common/src/main/java/org/onap/vid/services/InstantiationTemplatesService.java [new file with mode: 0644]
vid-app-common/src/test/java/org/onap/vid/services/AsyncInstantiationBusinessLogicTest.java
vid-app-common/src/test/java/org/onap/vid/services/InstantiationTemplatesServiceTest.java [new file with mode: 0644]

index c462667..96e777a 100644 (file)
@@ -36,6 +36,7 @@ import org.onap.vid.properties.Features;
 import org.onap.vid.roles.RoleProvider;
 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
 import org.onap.vid.services.AuditService;
+import org.onap.vid.services.InstantiationTemplatesService;
 import org.onap.vid.utils.SystemPropertiesWrapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -55,6 +56,7 @@ public class AsyncInstantiationController extends VidRestrictedBaseController {
     public static final String ASYNC_INSTANTIATION = "asyncInstantiation";
 
     protected final AsyncInstantiationBusinessLogic asyncInstantiationBL;
+    protected final InstantiationTemplatesService instantiationTemplates;
     protected final AsyncInstantiationRepository asyncInstantiationRepository;
     private final SystemPropertiesWrapper systemPropertiesWrapper;
 
@@ -62,18 +64,21 @@ public class AsyncInstantiationController extends VidRestrictedBaseController {
 
     private final FeatureManager featureManager;
 
-    @Autowired
-    protected AuditService auditService;
+    protected final AuditService auditService;
 
     @Autowired
     public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL,
+        InstantiationTemplatesService instantiationTemplates,
         AsyncInstantiationRepository asyncInstantiationRepository, RoleProvider roleProvider,
-        FeatureManager featureManager, SystemPropertiesWrapper systemPropertiesWrapper) {
+        FeatureManager featureManager, SystemPropertiesWrapper systemPropertiesWrapper,
+        AuditService auditService) {
         this.asyncInstantiationBL = asyncInstantiationBL;
+        this.instantiationTemplates = instantiationTemplates;
         this.asyncInstantiationRepository = asyncInstantiationRepository;
         this.roleProvider = roleProvider;
         this.featureManager = featureManager;
         this.systemPropertiesWrapper = systemPropertiesWrapper;
+        this.auditService = auditService;
     }
 
     /**
@@ -171,7 +176,7 @@ public class AsyncInstantiationController extends VidRestrictedBaseController {
 
     @GetMapping("templateTopology/{jobId}")
     public ServiceInstantiation getTemplateTopology(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId) {
-        return asyncInstantiationBL.getJobRequestAsTemplate(jobId);
+        return instantiationTemplates.getJobRequestAsTemplate(jobId);
     }
 
     @RequestMapping(value = "/auditStatusForRetry/{trackById}", method = RequestMethod.GET)
diff --git a/vid-app-common/src/main/java/org/onap/vid/model/aaiTree/ExistingElementsCounterMaps.java b/vid-app-common/src/main/java/org/onap/vid/model/aaiTree/ExistingElementsCounterMaps.java
new file mode 100644 (file)
index 0000000..de63c2c
--- /dev/null
@@ -0,0 +1,34 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 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.vid.model.aaiTree;
+
+import java.util.Map;
+
+public interface ExistingElementsCounterMaps {
+
+    Map<String, Long> getExistingVNFCounterMap();
+
+    Map<String, Long> getExistingNetworksCounterMap();
+
+    Map<String, Long> getExistingVnfGroupCounterMap();
+
+    Map<String, Long> getExistingVRFCounterMap();
+}
index 923be13..78afe1d 100644 (file)
@@ -24,7 +24,7 @@ import java.util.HashMap;
 import java.util.Map;
 import org.onap.vid.mso.model.ModelInfo;
 
-public class ServiceInstance extends AbstractNode {
+public class ServiceInstance extends AbstractNode implements ExistingElementsCounterMaps {
 
     private String globalSubscriberId;
     private String subscriptionServiceType;
@@ -198,6 +198,7 @@ public class ServiceInstance extends AbstractNode {
         this.validationCounter = validationCounter;
     }
 
+    @Override
     public Map<String, Long> getExistingVNFCounterMap() {
         return existingVNFCounterMap;
     }
@@ -206,6 +207,7 @@ public class ServiceInstance extends AbstractNode {
         this.existingVNFCounterMap = existingVNFCounterMap;
     }
 
+    @Override
     public Map<String, Long> getExistingNetworksCounterMap() {
         return existingNetworksCounterMap;
     }
@@ -214,6 +216,7 @@ public class ServiceInstance extends AbstractNode {
         this.existingNetworksCounterMap = existingNetworksCounterMap;
     }
 
+    @Override
     public Map<String, Long> getExistingVnfGroupCounterMap() {
         return existingVnfGroupCounterMap;
     }
@@ -230,6 +233,7 @@ public class ServiceInstance extends AbstractNode {
         this.vrfs = vrfs;
     }
 
+    @Override
     public Map<String, Long> getExistingVRFCounterMap() {
         return existingVRFCounterMap;
     }
diff --git a/vid-app-common/src/main/java/org/onap/vid/model/serviceInstantiation/ServiceInstantiationTemplate.java b/vid-app-common/src/main/java/org/onap/vid/model/serviceInstantiation/ServiceInstantiationTemplate.java
new file mode 100644 (file)
index 0000000..17ce1bc
--- /dev/null
@@ -0,0 +1,81 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 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.vid.model.serviceInstantiation;
+
+import java.util.Map;
+import java.util.Objects;
+import org.onap.vid.model.aaiTree.ExistingElementsCounterMaps;
+
+public class ServiceInstantiationTemplate extends ServiceInstantiation implements ExistingElementsCounterMaps {
+
+    private final Map<String, Long> existingVNFCounterMap;
+    private final Map<String, Long> existingNetworksCounterMap;
+    private final Map<String, Long> existingVnfGroupCounterMap;
+    private final Map<String, Long> existingVRFCounterMap;
+
+    public ServiceInstantiationTemplate(
+        ServiceInstantiation baseService,
+        Map<String, Long> vnfCounterMap,
+        Map<String, Long> networksCounterMap,
+        Map<String, Long> vnfGroupCounterMap,
+        Map<String, Long> VRFCounterMap
+    ) {
+        super(
+            baseService.getModelInfo(), baseService.getOwningEntityId(), baseService.getOwningEntityName(),
+            baseService.getProjectName(), baseService.getGlobalSubscriberId(), baseService.getSubscriberName(),
+            baseService.getProductFamilyId(), baseService.getInstanceName(), baseService.getSubscriptionServiceType(),
+            baseService.getLcpCloudRegionId(), baseService.getLcpCloudRegionId(), baseService.getTenantId(),
+            baseService.getTenantName(), baseService.getAicZoneId(), baseService.getAicZoneName(),
+            baseService.getVnfs(), baseService.getNetworks(), baseService.getVnfGroups(), baseService.getVrfs(),
+            baseService.getInstanceParams(), baseService.isPause(), baseService.getBulkSize(),
+            baseService.isRollbackOnFailure(), baseService.isALaCarte(), baseService.getTestApi(),
+            baseService.getInstanceId(), Objects.toString(baseService.getAction(), null),
+            baseService.getTrackById(), baseService.getIsFailed(), baseService.getStatusMessage(),
+            baseService.getVidNotions()
+        );
+
+        this.existingVNFCounterMap = vnfCounterMap;
+        this.existingNetworksCounterMap = networksCounterMap;
+        this.existingVnfGroupCounterMap = vnfGroupCounterMap;
+        this.existingVRFCounterMap = VRFCounterMap;
+    }
+
+    @Override
+    public Map<String, Long> getExistingVNFCounterMap() {
+        return existingVNFCounterMap;
+    }
+
+    @Override
+    public Map<String, Long> getExistingNetworksCounterMap() {
+        return existingNetworksCounterMap;
+    }
+
+    @Override
+    public Map<String, Long> getExistingVnfGroupCounterMap() {
+        return existingVnfGroupCounterMap;
+    }
+
+    @Override
+    public Map<String, Long> getExistingVRFCounterMap() {
+        return existingVRFCounterMap;
+    }
+
+}
index e00758a..bb11213 100644 (file)
@@ -108,7 +108,5 @@ public interface AsyncInstantiationBusinessLogic {
 
     ServiceInstantiation getBulkForRetry(UUID jobId);
 
-    ServiceInstantiation getJobRequestAsTemplate(UUID jobId);
-
     String getResumeRequestPath(String requestId);
 }
index 86d630b..c77eb82 100644 (file)
@@ -533,11 +533,6 @@ public class AsyncInstantiationBusinessLogicImpl implements
          return enrichBulkForRetry( asyncInstantiationRepository.getJobRequest(jobId), jobId);
     }
 
-    @Override
-    public ServiceInstantiation getJobRequestAsTemplate(UUID jobId) {
-         return asyncInstantiationRepository.getJobRequest(jobId);
-    }
-
     @Override
     public void addResourceInfo(JobSharedData sharedData, Job.JobStatus jobStatus, String instanceId) {
         String trackById = ((BaseResource) sharedData.getRequest()).getTrackById();
diff --git a/vid-app-common/src/main/java/org/onap/vid/services/InstantiationTemplatesService.java b/vid-app-common/src/main/java/org/onap/vid/services/InstantiationTemplatesService.java
new file mode 100644 (file)
index 0000000..aa00311
--- /dev/null
@@ -0,0 +1,67 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 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.vid.services;
+
+import static java.util.Collections.emptyMap;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Map;
+import java.util.UUID;
+import javax.inject.Inject;
+import org.onap.vid.dal.AsyncInstantiationRepository;
+import org.onap.vid.model.ModelUtil;
+import org.onap.vid.model.serviceInstantiation.BaseResource;
+import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
+import org.onap.vid.model.serviceInstantiation.ServiceInstantiationTemplate;
+import org.springframework.stereotype.Component;
+
+@Component
+public class InstantiationTemplatesService {
+
+    private final ModelUtil modelUtil;
+    private final AsyncInstantiationRepository asyncInstantiationRepository;
+
+    @Inject
+    public InstantiationTemplatesService(ModelUtil modelUtil,
+        AsyncInstantiationRepository asyncInstantiationRepository) {
+        this.modelUtil = modelUtil;
+        this.asyncInstantiationRepository = asyncInstantiationRepository;
+    }
+
+    public ServiceInstantiationTemplate getJobRequestAsTemplate(UUID jobId) {
+        ServiceInstantiation jobRequest = requireNonNull(asyncInstantiationRepository.getJobRequest(jobId));
+
+        return new ServiceInstantiationTemplate(
+            jobRequest,
+            counterMap(jobRequest.getVnfs()),
+            counterMap(jobRequest.getNetworks()),
+            counterMap(jobRequest.getVnfGroups()),
+            emptyMap() // model info for VRF is not stored
+        );
+    }
+
+    private <T extends BaseResource> Map<String, Long> counterMap(Map<String, T> nodesToCount) {
+        return modelUtil.getExistingCounterMap(
+            nodesToCount, BaseResource::getModelInfo
+        );
+    }
+
+}
index 56aaf1e..d1124f3 100644 (file)
@@ -35,7 +35,6 @@ import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.matchesPattern;
 import static org.hamcrest.Matchers.not;
 import static org.hamcrest.Matchers.nullValue;
-import static org.hamcrest.Matchers.sameInstance;
 import static org.hamcrest.core.Every.everyItem;
 import static org.hamcrest.core.IsEqual.equalTo;
 import static org.mockito.ArgumentMatchers.any;
@@ -1369,15 +1368,4 @@ public class AsyncInstantiationBusinessLogicTest extends AsyncInstantiationBaseT
 
     }
 
-    @Test
-    public void getJobRequestAsTemplate_whenIsCalled_asyncInstantiationRepositoryGetJobRequestIsDelegated() {
-        UUID jobId = UUID.randomUUID();
-        ServiceInstantiation expected = mock(ServiceInstantiation.class);
-        doReturn(expected).when(asyncInstantiationRepository).getJobRequest(jobId);
-
-        ServiceInstantiation jobRequestAsTemplate = asyncInstantiationBL.getJobRequestAsTemplate(jobId);
-
-        assertThat(jobRequestAsTemplate, is(sameInstance(expected)));
-
-    }
 }
diff --git a/vid-app-common/src/test/java/org/onap/vid/services/InstantiationTemplatesServiceTest.java b/vid-app-common/src/test/java/org/onap/vid/services/InstantiationTemplatesServiceTest.java
new file mode 100644 (file)
index 0000000..f09ea31
--- /dev/null
@@ -0,0 +1,102 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 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.vid.services;
+
+import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.anEmptyMap;
+import static org.hamcrest.Matchers.hasProperty;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import java.util.UUID;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.onap.vid.dal.AsyncInstantiationRepository;
+import org.onap.vid.model.ModelUtil;
+import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
+import org.onap.vid.model.serviceInstantiation.ServiceInstantiationTemplate;
+import org.onap.vid.model.serviceInstantiation.Vnf;
+import org.onap.vid.testUtils.TestUtils;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class InstantiationTemplatesServiceTest {
+
+    @Mock
+    private AsyncInstantiationRepository asyncInstantiationRepository;
+
+    @Mock
+    private ModelUtil modelUtil;
+
+    @InjectMocks
+    private InstantiationTemplatesService instantiationTemplatesService;
+
+    @BeforeMethod
+    public void initMocks() {
+        TestUtils.initMockitoMocks(this);
+    }
+
+    @Test
+    public void getJobRequestAsTemplate_whenIsCalled_asyncInstantiationRepositoryGetJobRequestIsInvoked() {
+        UUID jobId = UUID.randomUUID();
+        ServiceInstantiation serviceInstantiationMock = mock(ServiceInstantiation.class, RETURNS_DEEP_STUBS);
+        doReturn(serviceInstantiationMock).when(asyncInstantiationRepository).getJobRequest(jobId);
+
+        // When...
+        instantiationTemplatesService.getJobRequestAsTemplate(jobId);
+
+        verify(asyncInstantiationRepository).getJobRequest(jobId);
+    }
+
+    @Test
+    public void getJobRequestAsTemplate_givenModelUtilReturnsValue_thenVnfCounterMapIsPopulatedWithThatValue() {
+        Map<String, Integer> dummyNonEmptyMap = ImmutableMap.of("dummyKey", 9);
+        ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class, RETURNS_DEEP_STUBS);
+        doReturn(serviceInstantiation).when(asyncInstantiationRepository).getJobRequest(any());
+
+        // Given...
+        when(modelUtil.getExistingCounterMap(any(), any())).thenAnswer(
+            // return empty counterMap if argument is an empty map; otherwise return a mocked response
+            invocation -> ((Map)invocation.getArgument(0)).size() == 0 // isEmpty() does not work on mocks
+                ? ImmutableMap.of()
+                : dummyNonEmptyMap
+        );
+
+        // only vnf will have a non-empty value
+        when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("1", mock(Vnf.class)));
+
+        // When...
+        ServiceInstantiationTemplate result = instantiationTemplatesService.getJobRequestAsTemplate(UUID.randomUUID());
+
+        assertThat(result, hasProperty("existingVNFCounterMap", jsonEquals(dummyNonEmptyMap)));
+        assertThat(result, hasProperty("existingNetworksCounterMap", anEmptyMap()));
+        assertThat(result, hasProperty("existingVnfGroupCounterMap", anEmptyMap()));
+        assertThat(result, hasProperty("existingVRFCounterMap", anEmptyMap()));
+    }
+
+}