diff options
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java')
4 files changed, 171 insertions, 131 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java new file mode 100644 index 0000000000..c057ecf7e0 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdcrests.item.rest.models; + +import java.util.Map; +import org.openecomp.sdc.notification.dtos.Event; + +public class SyncEvent implements Event { + + private String eventType; + private String originatorId; + private Map<String, Object> attributes; + private String entityId; + + public SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) { + this.eventType = eventType; + this.originatorId = originatorId; + this.attributes = attributes; + this.entityId = entityId; + } + + @Override + public String getEventType() { + return eventType; + } + + @Override + public String getOriginatorId() { + return originatorId; + } + + @Override + public Map<String, Object> getAttributes() { + return attributes; + } + + @Override + public String getEntityId() { + return entityId; + } + +}
\ No newline at end of file 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 cdc8615786..4c1a0fb659 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 @@ -16,23 +16,14 @@ package org.openecomp.sdcrests.item.rest.services; -import org.openecomp.sdc.activitylog.ActivityLogManager; -import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; +import com.google.common.annotations.VisibleForTesting; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; import org.openecomp.sdc.activitylog.dao.type.ActivityType; 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; -import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory; -import org.openecomp.sdc.notification.services.NotificationPropagationManager; -import org.openecomp.sdc.versioning.ItemManager; -import org.openecomp.sdc.versioning.ItemManagerFactory; -import org.openecomp.sdc.versioning.VersioningManager; -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; @@ -40,6 +31,7 @@ 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.rest.models.SyncEvent; import org.openecomp.sdcrests.item.rest.services.catalog.notification.Notifier; import org.openecomp.sdcrests.item.rest.services.catalog.notification.NotifierFactory; import org.openecomp.sdcrests.item.types.ItemAction; @@ -68,23 +60,10 @@ import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM public class ItemsImpl implements Items { private static final String ONBOARDING_METHOD = "onboardingMethod"; - - private ItemManager itemManager = ItemManagerFactory.getInstance().createInterface(); - - private static ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); - - private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); - - private final PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); - private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class); - private NotificationPropagationManager notifier = - NotificationPropagationManagerFactory.getInstance().createInterface(); - - - private Map<ItemAction, ActionSideAffects> actionSideAffectsMap = new EnumMap<>(ItemAction.class); + private ManagersProvider managersProvider; @PostConstruct public void initActionSideAffectsMap() { @@ -94,21 +73,20 @@ public class ItemsImpl implements Items { .put(ItemAction.RESTORE, new ActionSideAffects(ActivityType.Restore, NotificationEventTypes.RESTORE)); } - @Override public Response actOn(ItemActionRequestDto request, String itemId, String user) { - Item item = itemManager.get(itemId); + Item item = getManagersProvider().getItemManager().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); + getManagersProvider().getItemManager().archive(item); break; case RESTORE: - itemManager.restore(item); + getManagersProvider().getItemManager().restore(item); break; default: } @@ -134,7 +112,7 @@ public class ItemsImpl implements Items { GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>(); MapItemToDto mapper = new MapItemToDto(); - itemManager.list(itemPredicate).stream() + getManagersProvider().getItemManager().list(itemPredicate).stream() .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())) .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class))); @@ -144,48 +122,12 @@ public class ItemsImpl implements Items { @Override public Response getItem(String itemId, String user) { - Item item = itemManager.get(itemId); + Item item = getManagersProvider().getItemManager().get(itemId); ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class); return Response.ok(itemDto).build(); } - private class SyncEvent implements Event { - - private String eventType; - private String originatorId; - private Map<String, Object> attributes; - private String entityId; - - SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) { - this.eventType = eventType; - this.originatorId = originatorId; - this.attributes = attributes; - this.entityId = entityId; - } - - @Override - public String getEventType() { - return eventType; - } - - @Override - public String getOriginatorId() { - return originatorId; - } - - @Override - public Map<String, Object> getAttributes() { - return attributes; - } - - @Override - public String getEntityId() { - return entityId; - } - - } - private class ActionSideAffects { private ActivityType activityType; @@ -198,7 +140,7 @@ public class ItemsImpl implements Items { } private Version getLatestVersion(String itemId) { - List<Version> list = versioningManager.list(itemId); + List<Version> list = getManagersProvider().getVersioningManager().list(itemId); Optional<Version> max = list.stream().max(Version::compareTo); return max.orElse(null); @@ -206,21 +148,21 @@ public class ItemsImpl implements Items { private void execute(Item item, String user) { notifyUsers(item.getId(), item.getName(), user, this.notificationType); - activityLogManager.logActivity( + getManagersProvider().getActivityLogManager().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_NAME, itemName == null ? getManagersProvider().getItemManager().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); + getManagersProvider().getNotificationPropagationManager().notifySubscribers(syncEvent, userName); } catch (Exception e) { LOGGER.error("Failed to send sync notification to users subscribed to item '{}'", itemId, e); } @@ -282,7 +224,7 @@ public class ItemsImpl implements Items { String[] permissions = filterValue.split(","); Set<String> itemIds = new HashSet<>(); for (String permission : permissions) { - itemIds.addAll(permissionsManager.listUserPermittedItems(user, permission)); + itemIds.addAll(getManagersProvider().getPermissionsManager().listUserPermittedItems(user, permission)); } return item -> itemIds.contains(item.getId()); } @@ -327,4 +269,22 @@ public class ItemsImpl implements Items { private enum OnboardingMethod { NetworkPackage, Manual } + + @VisibleForTesting + void setManagersProvider(ManagersProvider managersProvider) { + this.managersProvider = managersProvider; + } + + @VisibleForTesting + Map<ItemAction, ActionSideAffects> getActionSideAffectsMap() { + return actionSideAffectsMap; + } + + private ManagersProvider getManagersProvider() { + if (managersProvider == null){ + managersProvider = new ManagersProvider(); + } + return managersProvider; + } + } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java new file mode 100644 index 0000000000..9d2ca6ef57 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdcrests.item.rest.services; + +import lombok.Getter; +import org.openecomp.sdc.activitylog.ActivityLogManager; +import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; +import org.openecomp.sdc.conflicts.ConflictsManager; +import org.openecomp.sdc.conflicts.ConflictsManagerFactory; +import org.openecomp.sdc.itempermissions.PermissionsManager; +import org.openecomp.sdc.itempermissions.PermissionsManagerFactory; +import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.versioning.AsdcItemManager; +import org.openecomp.sdc.versioning.AsdcItemManagerFactory; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.ItemManagerFactory; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.VersioningManagerFactory; + +@Getter +class ManagersProvider { + private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); + private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface(); + private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); + private ConflictsManager conflictsManager = ConflictsManagerFactory.getInstance().createInterface(); + private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); + private NotificationPropagationManager notificationPropagationManager = NotificationPropagationManagerFactory + .getInstance().createInterface(); + private ItemManager itemManager = ItemManagerFactory.getInstance().createInterface(); +} diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java index cd6364f145..114a97a039 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java @@ -16,6 +16,7 @@ package org.openecomp.sdcrests.item.rest.services; +import com.google.common.annotations.VisibleForTesting; import org.openecomp.sdc.activitylog.ActivityLogManager; import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; @@ -45,6 +46,7 @@ import org.openecomp.sdcrests.item.rest.Versions; import org.openecomp.sdcrests.item.rest.mapping.MapActivityLogEntityToDto; import org.openecomp.sdcrests.item.rest.mapping.MapRevisionToDto; import org.openecomp.sdcrests.item.rest.mapping.MapVersionToDto; +import org.openecomp.sdcrests.item.rest.models.SyncEvent; import org.openecomp.sdcrests.item.types.*; import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper; import org.springframework.context.annotation.Scope; @@ -67,20 +69,15 @@ public class VersionsImpl implements Versions { private static final String COMMIT_ITEM_ACTION = "Commit_Item"; private static final Logger LOGGER = LoggerFactory.getLogger(VersionsImpl.class); - private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); - private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface(); - private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); - private ConflictsManager conflictsManager = ConflictsManagerFactory.getInstance().createInterface(); - private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); - private NotificationPropagationManager notifier = - NotificationPropagationManagerFactory.getInstance().createInterface(); + private ManagersProvider managersProvider; @Override public Response list(String itemId, String user) { GenericCollectionWrapper<VersionDto> results = new GenericCollectionWrapper<>(); MapVersionToDto mapper = new MapVersionToDto(); - versioningManager.list(itemId).forEach(version -> results.add(mapper.applyMapping(version, VersionDto.class))); + getManagersProvider().getVersioningManager().list(itemId) + .forEach(version -> results.add(mapper.applyMapping(version, VersionDto.class))); return Response.ok(results).build(); } @@ -90,13 +87,13 @@ public class VersionsImpl implements Versions { version.setBaseId(baseVersionId); version.setDescription(request.getDescription()); - version = versioningManager - .create(itemId, version, VersionCreationMethod.valueOf(request.getCreationMethod().name())); + version = getManagersProvider().getVersioningManager() + .create(itemId, version, VersionCreationMethod.valueOf(request.getCreationMethod().name())); VersionDto versionDto = new MapVersionToDto().applyMapping(version, VersionDto.class); - activityLogManager - .logActivity(new ActivityLogEntity(itemId, version, ActivityType.Create_Version, user, true, "", "")); + getManagersProvider().getActivityLogManager().logActivity( + new ActivityLogEntity(itemId, version, ActivityType.Create_Version, user, true, "", "")); return Response.ok(versionDto).build(); } @@ -113,7 +110,7 @@ public class VersionsImpl implements Versions { GenericCollectionWrapper<ActivityLogDto> results = new GenericCollectionWrapper<>(); MapActivityLogEntityToDto mapper = new MapActivityLogEntityToDto(); - activityLogManager.listLoggedActivities(itemId, new Version(versionId)) + getManagersProvider().getActivityLogManager().listLoggedActivities(itemId, new Version(versionId)) .forEach(loggedActivity -> results.add(mapper.applyMapping(loggedActivity, ActivityLogDto.class))); return Response.ok(results).build(); @@ -121,7 +118,7 @@ public class VersionsImpl implements Versions { @Override public Response listRevisions(String itemId, String versionId, String user) { - List<Revision> revisions = versioningManager.listRevisions(itemId, new Version(versionId)); + List<Revision> revisions = getManagersProvider().getVersioningManager().listRevisions(itemId, new Version(versionId)); filterRevisions(revisions); GenericCollectionWrapper<RevisionDto> results = new GenericCollectionWrapper<>(); @@ -138,7 +135,7 @@ public class VersionsImpl implements Versions { sync(itemId, version); break; case Commit: - if (!permissionsManager.isAllowed(itemId, user, COMMIT_ITEM_ACTION)) { + if (!getManagersProvider().getPermissionsManager().isAllowed(itemId, user, COMMIT_ITEM_ACTION)) { return Response.status(Response.Status.FORBIDDEN) .entity(new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build(); } @@ -148,7 +145,7 @@ public class VersionsImpl implements Versions { revert(request.getRevisionRequest(), itemId, versionId); break; case Clean: - versioningManager.clean(itemId, version); + getManagersProvider().getVersioningManager().clean(itemId, version); break; default: throw new UnsupportedOperationException(String.format("Action %s not supported.", request.getAction())); @@ -157,29 +154,29 @@ public class VersionsImpl implements Versions { } private Version getVersion(String itemId, Version version) { - Version retrievedVersion = versioningManager.get(itemId, version); + Version retrievedVersion = getManagersProvider().getVersioningManager().get(itemId, version); if (retrievedVersion.getState().getSynchronizationState() != SynchronizationState.Merging && // looks for sdc applicative conflicts - conflictsManager.isConflicted(itemId, retrievedVersion)) { + getManagersProvider().getConflictsManager().isConflicted(itemId, retrievedVersion)) { retrievedVersion.getState().setSynchronizationState(SynchronizationState.Merging); } return retrievedVersion; } private void sync(String itemId, Version version) { - versioningManager.sync(itemId, version); - conflictsManager.finalizeMerge(itemId, version); + getManagersProvider().getVersioningManager().sync(itemId, version); + getManagersProvider().getConflictsManager().finalizeMerge(itemId, version); } private void commit(CommitRequestDto request, String itemId, Version version, String user) { String message = request == null ? "" : request.getMessage(); - versioningManager.publish(itemId, version, message); + getManagersProvider().getVersioningManager().publish(itemId, version, message); notifyUsers(itemId, version, message, user, NotificationEventTypes.COMMIT); - activityLogManager - .logActivity(new ActivityLogEntity(itemId, version, ActivityType.Commit, user, true, "", message)); + getManagersProvider().getActivityLogManager().logActivity( + new ActivityLogEntity(itemId, version, ActivityType.Commit, user, true, "", message)); } private void revert(RevisionRequestDto request, String itemId, String versionId) { @@ -187,7 +184,7 @@ public class VersionsImpl implements Versions { throw new CoreException(new RevisionIdNotFoundErrorBuilder().build()); } - versioningManager.revert(itemId, new Version(versionId), request.getRevisionId()); + getManagersProvider().getVersioningManager().revert(itemId, new Version(versionId), request.getRevisionId()); } private void filterRevisions(List<Revision> revisions) { @@ -208,10 +205,10 @@ public class VersionsImpl implements Versions { private void notifyUsers(String itemId, Version version, String message, String userName, NotificationEventTypes eventType) { Map<String, Object> eventProperties = new HashMap<>(); - eventProperties.put(ITEM_NAME, asdcItemManager.get(itemId).getName()); + eventProperties.put(ITEM_NAME, getManagersProvider().getAsdcItemManager().get(itemId).getName()); eventProperties.put(ITEM_ID, itemId); - Version ver = versioningManager.get(itemId, version); + Version ver = getManagersProvider().getVersioningManager().get(itemId, version); eventProperties.put(VERSION_NAME, ver.getName()); eventProperties.put(VERSION_ID, ver.getId()); @@ -220,46 +217,22 @@ public class VersionsImpl implements Versions { Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId); try { - notifier.notifySubscribers(syncEvent, userName); + getManagersProvider().getNotificationPropagationManager().notifySubscribers(syncEvent, userName); } catch (Exception e) { LOGGER.error("Failed to send sync notification to users subscribed o item '" + itemId); } } - private class SyncEvent implements Event { - - private String eventType; - private String originatorId; - private Map<String, Object> attributes; - - private String entityId; - - public SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) { - this.eventType = eventType; - this.originatorId = originatorId; - this.attributes = attributes; - this.entityId = entityId; - } - - @Override - public String getEventType() { - return eventType; - } - - @Override - public String getOriginatorId() { - return originatorId; - } - - @Override - public Map<String, Object> getAttributes() { - return attributes; - } + @VisibleForTesting + void setManagersProvider(ManagersProvider managersProvider) { + this.managersProvider = managersProvider; + } - @Override - public String getEntityId() { - return entityId; + private ManagersProvider getManagersProvider() { + if (managersProvider == null){ + managersProvider = new ManagersProvider(); } - + return managersProvider; } + } |