aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services
diff options
context:
space:
mode:
authorayalaben <ayala.benzvi@amdocs.com>2018-04-12 13:20:18 +0300
committerayalaben <ayala.benzvi@amdocs.com>2018-04-12 16:37:18 +0300
commit8da1beb7fb630954a0c7adfc7ae6fbb175877b49 (patch)
treeb6353cb051d382e3774e9a319a88160b9d028521 /openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services
parentc7941c2287c3858e4006f3a15ffd6e5a2f33af2e (diff)
Filter in onboarding - BE
Change-Id: I519ef44889de5314cf6675055cd15beef84dfb6f Issue-ID: SDC-1213 Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services')
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java20
-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.java2
-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.java285
3 files changed, 231 insertions, 76 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 de63c4efdc..942bc59495 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
@@ -17,6 +17,7 @@ package org.openecomp.sdcrests.item.rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
import org.openecomp.sdcrests.item.types.ItemActionRequestDto;
import org.springframework.validation.annotation.Validated;
@@ -35,6 +36,23 @@ import static org.openecomp.sdcrests.common.RestConstants.USER_MISSING_ERROR_MSG
@Validated
public interface Items {
+ @GET
+ @Path("/")
+ @ApiOperation(value = "Get list of items according to desired filters",
+ responseContainer = "List")
+ Response list(@ApiParam(value = "Filter by item status", allowableValues = "ACTIVE,ARCHIVED")
+ @QueryParam("itemStatus") String itemStatusFilter,
+ @ApiParam(value = "Filter by version status" , allowableValues = "Certified,Draft")
+ @QueryParam("versionStatus") String versionStatusFilter,
+ @ApiParam(value = "Filter by item type" , allowableValues = "vsp,vlm")
+ @QueryParam("itemType") String itemTypeFilter,
+ @ApiParam(value = "Filter by user permission" , allowableValues = "Owner,Contributor")
+ @QueryParam("permission") String permissionFilter,
+ @ApiParam(value = "Filter by onboarding method" , allowableValues = "NetworkPackage,Manual")
+ @QueryParam("onboardingMethod") String onboardingMethodFilter,
+ @NotNull(message = USER_MISSING_ERROR_MSG)
+ @HeaderParam(USER_ID_HEADER_PARAM) String user);
+
@GET
@Path("/{itemId}")
@ApiOperation(value = "Get details of a item")
@@ -52,4 +70,6 @@ public interface Items {
+
+
}
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 1886dc462c..55fa1679d7 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
@@ -1,7 +1,6 @@
package org.openecomp.sdcrests.item.rest.mapping;
import org.openecomp.sdc.versioning.types.Item;
-import org.openecomp.sdc.versioning.types.ItemStatus;
import org.openecomp.sdcrests.item.types.ItemDto;
import org.openecomp.sdcrests.mapping.MappingBase;
@@ -14,5 +13,6 @@ public class MapItemToDto extends MappingBase<Item, ItemDto> {
target.setDescription(source.getDescription());
target.setOwner(source.getOwner());
target.setStatus(source.getStatus().name());
+ target.setProperties(source.getProperties());
}
}
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 21c1f5199d..ae6e24bb65 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
@@ -13,13 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.openecomp.sdcrests.item.rest.services;
+import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER;
+import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID;
+import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.EnumMap;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import javax.annotation.PostConstruct;
+import javax.inject.Named;
+import javax.ws.rs.core.Response;
import org.openecomp.sdc.activitylog.ActivityLogManager;
import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
import org.openecomp.sdc.activitylog.dao.type.ActivityType;
-import org.openecomp.sdc.common.errors.Messages;
+import org.openecomp.sdc.datatypes.model.ItemType;
+import org.openecomp.sdc.itempermissions.PermissionsManager;
+import org.openecomp.sdc.itempermissions.PermissionsManagerFactory;
+import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.notification.dtos.Event;
@@ -32,39 +54,31 @@ import org.openecomp.sdc.versioning.VersioningManagerFactory;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.dao.types.VersionStatus;
import org.openecomp.sdc.versioning.types.Item;
+import org.openecomp.sdc.versioning.types.ItemStatus;
import org.openecomp.sdc.versioning.types.NotificationEventTypes;
import org.openecomp.sdcrests.item.rest.Items;
import org.openecomp.sdcrests.item.rest.mapping.MapItemToDto;
import org.openecomp.sdcrests.item.types.ItemAction;
import org.openecomp.sdcrests.item.types.ItemActionRequestDto;
import org.openecomp.sdcrests.item.types.ItemDto;
+import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
-
-import javax.inject.Named;
-import javax.ws.rs.core.Response;
-import java.util.EnumMap;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER;
-import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.*;
-import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION;
+import org.springframework.validation.annotation.Validated;
@Named
@Service("items")
@Scope(value = "prototype")
+@Validated
public class ItemsImpl implements Items {
- private ItemManager itemManager =
- ItemManagerFactory.getInstance().createInterface();
+ private ItemManager itemManager = ItemManagerFactory.getInstance().createInterface();
+
+ private static ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
- private static ActivityLogManager activityLogManager =
- ActivityLogManagerFactory.getInstance().createInterface();
+ private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface();
- private VersioningManager versioningManager =
- VersioningManagerFactory.getInstance().createInterface();
+ private final PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface();
private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class);
@@ -73,65 +87,63 @@ public class ItemsImpl implements Items {
private Map<ItemAction, ActionSideAffects> actionSideAffectsMap = new EnumMap<>(ItemAction.class);
- {
- actionSideAffectsMap.put(ItemAction.ARCHIVE, new ActionSideAffects(ActivityType.Archive,
- NotificationEventTypes.ARCHIVE));
- actionSideAffectsMap.put(ItemAction.RESTORE, new ActionSideAffects(ActivityType.Restore,
- NotificationEventTypes.RESTORE));
+ @PostConstruct
+ public void initActionSideAffectsMap() {
+ actionSideAffectsMap
+ .put(ItemAction.ARCHIVE, new ActionSideAffects(ActivityType.Archive, NotificationEventTypes.ARCHIVE));
+ actionSideAffectsMap
+ .put(ItemAction.RESTORE, new ActionSideAffects(ActivityType.Restore, NotificationEventTypes.RESTORE));
}
- @Override
- public Response actOn(ItemActionRequestDto request, String itemId, String user) {
+ private static final String ONBOARDING_METHOD = "onboardingMethod";
+
+ @Override
+ public Response actOn(ItemActionRequestDto request, String itemId, String user) {
- Item item = itemManager.get(itemId);
- if( item == null){
- return Response.status(Response.Status.NOT_FOUND)
- .entity(new Exception("Item does not exist.")).build();
- }
+ Item item = itemManager.get(itemId);
+ if (item == null) {
+ return Response.status(Response.Status.NOT_FOUND).entity(new Exception("Item does not exist.")).build();
+ }
- switch (request.getAction()) {
- case ARCHIVE:
- itemManager.archive(item);
+ switch (request.getAction()) {
+ case ARCHIVE:
+ itemManager.archive(item);
+ break;
+ case RESTORE:
+ itemManager.restore(item);
break;
- case RESTORE:
- itemManager.restore(item);
- break;
- default:
+ default:
}
- actionSideAffectsMap.get(request.getAction()).execute(item,user);
+ actionSideAffectsMap.get(request.getAction()).execute(item, user);
- return Response.ok().build();
+ return Response.ok().build();
}
@Override
- public Response getItem(String itemId, String user) {
- Item item = itemManager.get(itemId);
- ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class);
+ public Response list(String itemStatusFilter, String versionStatusFilter, String itemTypeFilter,
+ String permissionFilter, String onboardingMethodFilter, String user) {
- return Response.ok(itemDto).build();
- }
+ Predicate<Item> itemPredicate =
+ createItemPredicate(itemStatusFilter, versionStatusFilter, itemTypeFilter, onboardingMethodFilter,
+ permissionFilter, user);
- private Version getLatestVersion(String itemId){
- List<Version> list = versioningManager.list(itemId);
- return list.stream().max(Version::compareTo).get();
- }
+ GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>();
+ MapItemToDto mapper = new MapItemToDto();
+ itemManager.list(itemPredicate).stream()
+ .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime()))
+ .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class)));
- private void notifyUsers(String itemId, String itemName, String message,
- String userName, NotificationEventTypes eventType) {
- Map<String, Object> eventProperties = new HashMap<>();
- eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName);
- eventProperties.put(ITEM_ID, itemId);
+ return Response.ok(results).build();
- eventProperties.put(SUBMIT_DESCRIPTION, message);
- eventProperties.put(PERMISSION_USER, userName);
+ }
- Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId);
- try {
- notifier.notifySubscribers(syncEvent, userName);
- } catch (Exception e) {
- LOGGER.error("Failed to send sync notification to users subscribed to item '" + itemId);
- }
+ @Override
+ public Response getItem(String itemId, String user) {
+ Item item = itemManager.get(itemId);
+ ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class);
+
+ return Response.ok(itemDto).build();
}
private class SyncEvent implements Event {
@@ -141,8 +153,7 @@ public class ItemsImpl implements Items {
private Map<String, Object> attributes;
private String entityId;
- SyncEvent(String eventType, String originatorId,
- Map<String, Object> attributes, String entityId) {
+ SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) {
this.eventType = eventType;
this.originatorId = originatorId;
this.attributes = attributes;
@@ -171,21 +182,145 @@ public class ItemsImpl implements Items {
}
- private class ActionSideAffects{
- private ActivityType activityType;
- private NotificationEventTypes notificationType;
+ private class ActionSideAffects {
+
+ private ActivityType activityType;
+ private NotificationEventTypes notificationType;
+
+ private ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType) {
+ this.activityType = activityType;
+ this.notificationType = notificationType;
- public ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType){
- this.activityType = activityType;
- this.notificationType = notificationType;
+ }
+
+ private Version getLatestVersion(String itemId) {
+ List<Version> list = versioningManager.list(itemId);
+ Optional<Version> max = list.stream().max(Version::compareTo);
- }
- public void execute(Item item, String user){
- notifyUsers(item.getId(), item.getName(), null, user,
- this.notificationType);
- activityLogManager.logActivity(new ActivityLogEntity(item.getId(), getLatestVersion(item.getId()),
- this.activityType, user, true, "", ""));
+ return max.orElse(null);
}
+
+ private void execute(Item item, String user) {
+ notifyUsers(item.getId(), item.getName(), user, this.notificationType);
+ activityLogManager.logActivity(
+ new ActivityLogEntity(item.getId(), getLatestVersion(item.getId()), this.activityType, user, true,
+ "", ""));
+ }
+
+ private void notifyUsers(String itemId, String itemName, String userName, NotificationEventTypes eventType) {
+ Map<String, Object> eventProperties = new HashMap<>();
+ eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName);
+ eventProperties.put(ITEM_ID, itemId);
+
+ eventProperties.put(PERMISSION_USER, userName);
+
+ Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId);
+ try {
+ notifier.notifySubscribers(syncEvent, userName);
+ } catch (Exception e) {
+ LOGGER.error("Failed to send sync notification to users subscribed to item '" + itemId);
+ }
+ }
+ }
+
+ private Predicate<Item> createItemPredicate(String itemStatusFilter, String versionStatusFilter,
+ String itemTypeFilter, String onboardingMethodFilter, String permissionsFilter, String user) {
+ Predicate<Item> itemPredicate = item -> true;
+
+ if (itemStatusFilter != null) {
+ validateItemStatusValue(itemStatusFilter);
+ itemPredicate = itemPredicate.and(createItemStatusPredicate(itemStatusFilter));
+ }
+ if (versionStatusFilter != null) {
+ validateVersionStatusValue(versionStatusFilter);
+ itemPredicate = itemPredicate.and(createVersionStatusPredicate(versionStatusFilter));
+ }
+ if (itemTypeFilter != null) {
+ validateItemTypeValue(itemTypeFilter);
+ itemPredicate = itemPredicate.and(createItemTypePredicate(itemTypeFilter));
+ }
+ if (onboardingMethodFilter != null) {
+ validateOnboardingMethodValue(onboardingMethodFilter);
+ itemPredicate = itemPredicate.and(createOnboardingMethodPredicate(onboardingMethodFilter));
+ }
+ if (permissionsFilter != null) {
+ validatePermissionValue(permissionsFilter);
+ itemPredicate = itemPredicate.and(createPermissionsPredicate(user, permissionsFilter));
+ }
+ return itemPredicate;
+ }
+
+ private String formatFilter(String filterValue) {
+ return filterValue.replace(",", "|");
+ }
+
+ private Predicate<Item> createItemStatusPredicate(String filterValue) {
+ return item -> item.getStatus().name().matches(formatFilter(filterValue));
+ }
+
+ private Predicate<Item> createVersionStatusPredicate(String filterValue) {
+ Set<VersionStatus> versionStatuses =
+ Arrays.stream(filterValue.split(",")).map(VersionStatus::valueOf).collect(Collectors.toSet());
+ return item -> item.getVersionStatusCounters().keySet().stream().anyMatch(versionStatuses::contains);
+ }
+
+ private Predicate<Item> createItemTypePredicate(String filterValue) {
+ return item -> item.getType().matches(formatFilter(filterValue));
+ }
+
+ private Predicate<Item> createOnboardingMethodPredicate(String filterValue) {
+ return item -> !ItemType.vsp.name().equals(item.getType()) || ((String) item.getProperties()
+ .get(ONBOARDING_METHOD))
+ .matches(formatFilter(filterValue));
}
+ private Predicate<Item> createPermissionsPredicate(String user, String filterValue) {
+ String[] permissions = filterValue.split(",");
+ Set<String> itemIds = new HashSet<>();
+ for (String permission : permissions) {
+ itemIds.addAll(permissionsManager.listUserPermittedItems(user, permission));
+ }
+ return item -> itemIds.contains(item.getId());
+ }
+
+ private void validateItemStatusValue(String itemStatusFilter) {
+ String[] values = itemStatusFilter.split(",");
+ for (String value : values) {
+ ItemStatus.valueOf(value);
+ }
+ }
+
+ private void validateVersionStatusValue(String versionStatusFilter) {
+ String[] values = versionStatusFilter.split(",");
+ for (String value : values) {
+ VersionStatus.valueOf(value);
+ }
+
+ }
+
+ private void validateItemTypeValue(String itemTypeFilter) {
+ String[] values = itemTypeFilter.split(",");
+ for (String value : values) {
+ ItemType.valueOf(value);
+ }
+ }
+
+ private void validateOnboardingMethodValue(String onboardingMethodFilter) {
+ String[] values = onboardingMethodFilter.split(",");
+ for (String value : values) {
+ OnboardingMethod.valueOf(value);
+ }
+ }
+
+ private void validatePermissionValue(String permissionsFilter) {
+ String[] values = permissionsFilter.split(",");
+ for (String value : values) {
+ PermissionTypes.valueOf(value);
+ }
+ }
+
+ //Do not delete - is in use, duplicates code to prevent dependency on openecomp-sdc-vendor-software-product-api
+ private enum OnboardingMethod {
+ NetworkPackage, Manual;
+ }
}