summaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest
diff options
context:
space:
mode:
authorKartik Hegde <kh00735564@techmahindra.com>2022-11-12 14:29:11 +0530
committerKartik Hegde <kh00735564@techmahindra.com>2022-12-21 12:04:30 +0000
commitcf04a1a714ef4a1df973929dc750232b4d67d7b4 (patch)
treeedd2e6e5074ba58efb98663a1ea1851208895fff /openecomp-be/api/openecomp-sdc-rest-webapp/item-rest
parentf995db01ee95606b6cded82822a73435ebc190c8 (diff)
Multitenancy in SDC
Issue-ID: SDC-4215 Change-Id: Ie24ba38acc9f1998d4a7e722e8f98456dab9201d Signed-off-by: Kartik Hegde <kh00735564@techmahindra.com>
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest')
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java4
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java1
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java25
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java84
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java8
5 files changed, 115 insertions, 7 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java
index 066acb9370..a2c0e39e8f 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java
@@ -26,6 +26,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
+import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -35,6 +36,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.openecomp.sdc.versioning.types.Item;
@@ -61,7 +63,7 @@ public interface Items {
@QueryParam("permission") String permissionFilter,
@Parameter(description = "Filter by onboarding method", schema = @Schema(type = "string", allowableValues = {"NetworkPackage", "manual"}))
@QueryParam("onboardingMethod") String onboardingMethodFilter,
- @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user);
+ @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user, @Context HttpServletRequest hreq);
@GET
@Path("/{itemId}")
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java
index 3bd8a7fe9f..0eb74a03b6 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java
@@ -34,5 +34,6 @@ public class MapItemToDto extends MappingBase<Item, ItemDto> {
target.setOwner(source.getOwner());
target.setStatus(source.getStatus().name());
target.setProperties(source.getProperties());
+ target.setTenant(source.getTenant());
}
}
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java
index 9fb6ebd8b8..af3568cd2a 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java
@@ -35,12 +35,15 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.inject.Named;
+import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
+import org.keycloak.representations.AccessToken;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
import org.openecomp.sdc.activitylog.dao.type.ActivityType;
import org.openecomp.sdc.be.csar.storage.StorageFactory;
import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder;
import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
+import org.openecomp.sdc.common.util.Multitenancy;
import org.openecomp.sdc.datatypes.model.ItemType;
import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes;
import org.openecomp.sdc.logging.api.Logger;
@@ -116,15 +119,27 @@ public class ItemsImpl implements Items {
@Override
public Response list(String itemStatusFilter, String versionStatusFilter, String itemTypeFilter, String permissionFilter,
- String onboardingMethodFilter, String user) {
+ String onboardingMethodFilter, String user, HttpServletRequest hreq) {
Predicate<Item> itemPredicate = createItemPredicate(itemStatusFilter, versionStatusFilter, itemTypeFilter, onboardingMethodFilter,
permissionFilter, user);
GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>();
MapItemToDto mapper = new MapItemToDto();
- getManagersProvider().getItemManager().list(itemPredicate).stream()
- .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime()))
- .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class)));
- return Response.ok(results).build();
+ Multitenancy keyaccess= new Multitenancy();
+ if (keyaccess.multiTenancyCheck()) {
+ AccessToken.Access realmAccess = keyaccess.getAccessToken(hreq).getRealmAccess();
+ Set<String> realmroles = realmAccess.getRoles();
+ realmroles.stream().forEach(role -> getManagersProvider().getItemManager().list(itemPredicate).stream()
+ .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime()))
+ .filter(item -> item.getTenant().contains(role))
+ .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class))));
+ return Response.ok(results).build();
+ }
+ else{
+ getManagersProvider().getItemManager().list(itemPredicate).stream()
+ .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime()))
+ .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class)));
+ return Response.ok(results).build();
+ }
}
@Override
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java
index 25de08304b..da3e6f3297 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java
@@ -22,6 +22,7 @@ package org.openecomp.sdcrests.item.rest.services;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -39,7 +40,11 @@ import io.minio.MinioClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
import javax.ws.rs.core.Response;
+
+import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -58,6 +63,7 @@ import org.openecomp.sdc.versioning.ItemManager;
import org.openecomp.sdc.versioning.VersioningManager;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.types.Item;
+import org.openecomp.sdc.versioning.types.ItemStatus;
import org.openecomp.sdcrests.item.types.ItemActionRequestDto;
@ExtendWith(MockitoExtension.class)
@@ -71,6 +77,8 @@ class ItemsImplTest {
private static final String CREDENTIALS = "credentials";
private static final String TEMP_PATH = "tempPath";
private static final String UPLOAD_PARTSIZE = "uploadPartSize";
+ private static final boolean MULTITENANCY_ENABLED = true;
+ private static final String TEST_TENANT = "test_tenant";
@Mock
private ManagersProvider managersProvider;
@@ -190,7 +198,7 @@ class ItemsImplTest {
items.initActionSideAffectsMap();
items.setManagersProvider(managersProvider);
when(managersProvider.getItemManager()).thenReturn(itemManager);
- Response response = items.list(null, null, null, null, null, USER);
+ Response response = items.list(null, null, null, null, null, USER, null);
assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
}
@@ -201,4 +209,78 @@ class ItemsImplTest {
versions.add(new Version("3"));
return versions;
}
+
+ @Test
+ void getItemList_withMultitenancyValidTenant_ReturnSuccessList() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ Assert.assertNotNull(getTestRoles());
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Response response = items.list(null, null, null, null, null, USER, null);
+ assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
+ List<Item> expectedItems=new ArrayList<>();
+ List<Item> actualItems=getAllItems();
+ getTestRoles().stream().forEach(role -> getAllItems().stream()
+ .filter(item -> item.getTenant()!=null)
+ .filter(item -> item.getTenant().contains(role))
+ .forEach(item -> expectedItems.add(item)));
+ assertNotSame(expectedItems.size(), actualItems.size());
+ }
+
+
+ @Test
+ void getItemList_withMultitenancyInvalidTenant_ReturnsEmptylList() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+
+ Assert.assertNotNull(getTestRoles());
+ String tenant= "invalid tenant";
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Response response = items.list(null, null, null, null, null, USER, null);
+ assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
+ List<Item> expectedItems=new ArrayList<>();
+ List<Item> actualItems=getAllItems();
+ assertNotNull(tenant);
+ getTestRoles().stream().forEach(role -> getAllItems().stream()
+ .filter(item -> item.getTenant()!=null)
+ .filter(item -> item.getTenant().contains(tenant))
+ .forEach(item -> expectedItems.add(item)));
+ Assert.assertEquals(expectedItems.size(), 0);
+ Assert.assertNotEquals(expectedItems.containsAll(actualItems), actualItems.containsAll(expectedItems));
+ }
+
+
+ private List<Item> getAllItems(){
+ List<Item> items=new ArrayList<>();
+
+ Item itemOne = new Item();
+ itemOne.setType(ItemType.vlm.name());
+ itemOne.setOwner(USER);
+ itemOne.setStatus(ItemStatus.ACTIVE);
+ itemOne.setName("TEST_VENDOR_ONE");
+ itemOne.setDescription("TEST_DESCRIPTION");
+ itemOne.setTenant(TEST_TENANT);
+
+ Item itemTwo = new Item();
+ itemTwo.setType(ItemType.vsp.name());
+ itemTwo.setOwner(USER);
+ itemTwo.setStatus(ItemStatus.ACTIVE);
+ itemTwo.setName("TEST_VSP_ONE");
+ itemTwo.setDescription("TEST_DESCRIPTION");
+ itemTwo.setTenant("admin_tenant");
+
+ items.add(itemOne);
+ items.add(itemTwo);
+ return items;
+ }
+
+
+ private Set<String> getTestRoles(){
+ Set<String> roles = new HashSet<>();
+ roles.add("test_admin");
+ roles.add("test_tenant");
+ return roles;
+ }
}
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java
index 34ad19fa7c..5e2810cc9e 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java
@@ -32,6 +32,7 @@ public class ItemDto {
private String description;
private String owner;
private String status;
+ private String tenant;
private Map<String, Object> properties;
public void setId(final String id) {
@@ -58,4 +59,11 @@ public class ItemDto {
this.status = ValidationUtils.sanitizeInputString(status);
}
+ public void setTenant(final String tenant) {
+ if(tenant != null) {
+ this.tenant = ValidationUtils.sanitizeInputString(tenant);
+ }
+ else this.tenant=tenant;
+ }
+
}