diff options
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src')
4 files changed, 332 insertions, 0 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/mapping/MapNotificationsStatusToDto.java b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/mapping/MapNotificationsStatusToDto.java new file mode 100644 index 0000000000..41376b040f --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/mapping/MapNotificationsStatusToDto.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdcrests.notifications.rest.mapping; + +import com.datastax.driver.core.utils.UUIDs; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; +import org.openecomp.sdcrests.mapping.MappingBase; +import org.openecomp.sdcrests.notifications.types.NotificationEntityDto; +import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto; + +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class MapNotificationsStatusToDto + extends MappingBase<NotificationsStatus, NotificationsStatusDto> { + + private static final DateFormat formatter = + DateFormat.getDateTimeInstance(DateFormat.LONG, + DateFormat.SHORT); + + @Override + public void doMapping(NotificationsStatus source, NotificationsStatusDto target) { + + target.setLastScanned(source.getLastScanned()); + target.setNewEntries(source.getNewEntries()); + target.setEndOfPage(source.getEndOfPage()); + target.setNumOfNotSeenNotifications(source.getNumOfNotSeenNotifications()); + List<NotificationEntityDto> entityDtos = new ArrayList<>(); + source.getNotifications() + .forEach(notification -> entityDtos.add(new NotificationEntityDto(notification.isRead(), + notification.getEventId(), notification.getEventType(), + JsonUtil.json2Object(notification.getEventAttributes(), Map.class), + extractDate(notification)))); + target.setNotifications(entityDtos); + } + + private String extractDate(NotificationEntity notification) { + return formatter.format(UUIDs.unixTimestamp + (notification + .getEventId())); + } +} diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/mapping/MapNotificationsToDto.java b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/mapping/MapNotificationsToDto.java new file mode 100644 index 0000000000..558453b7b6 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/mapping/MapNotificationsToDto.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdcrests.notifications.rest.mapping; + +import com.datastax.driver.core.utils.UUIDs; +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdcrests.mapping.MappingBase; +import org.openecomp.sdcrests.notifications.types.NotificationEntityDto; +import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto; + +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class MapNotificationsToDto extends MappingBase<List<NotificationEntity>, NotificationsStatusDto> { + private static final DateFormat formatter = + DateFormat.getDateTimeInstance(DateFormat.LONG, + DateFormat.SHORT); + @Override + public void doMapping(List<NotificationEntity> source, NotificationsStatusDto target) { + List<NotificationEntityDto> entityDtos = new ArrayList<>(); + if(CollectionUtils.isNotEmpty(source)) { + source.forEach(notification -> entityDtos.add(new NotificationEntityDto(notification.isRead(), + notification.getEventId(), notification.getEventType(), JsonUtil.json2Object(notification.getEventAttributes(), Map.class), + extractDate(notification)))); + target.setNotifications(entityDtos); + target.setLastScanned(source.get(0).getEventId()); + } + } + + private String extractDate(NotificationEntity notification) { + return formatter.format(UUIDs.unixTimestamp + (notification + .getEventId())); + } +} diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/Notifications.java b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/Notifications.java new file mode 100644 index 0000000000..97199f604f --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/Notifications.java @@ -0,0 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdcrests.notifications.rest.services; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto; +import org.openecomp.sdcrests.notifications.types.UpdateNotificationResponseStatus; +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.PUT; +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.MediaType; +import javax.ws.rs.core.Response; +import java.lang.reflect.InvocationTargetException; +import java.util.UUID; + +import static org.openecomp.sdcrests.common.RestConstants.LAST_DELIVERED_QUERY_PARAM; +import static org.openecomp.sdcrests.common.RestConstants.USER_ID_HEADER_PARAM; +import static org.openecomp.sdcrests.common.RestConstants.USER_MISSING_ERROR_MSG; + +@Path("/v1.0/notifications") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Api(value = "Notifications") +@Validated +public interface Notifications { + String LIMIT_QUERY_PARAM = "NOTIFICATION_ROWS_LIMIT"; + String END_OF_PAGE_QUERY_PARAM = "END_OF_PAGE_EVENT_ID"; + + @GET + @ApiOperation(value = "Retrive all user notifications", + response = NotificationsStatusDto.class, + responseContainer = "List") + Response getNotifications( + @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user, + @QueryParam(LAST_DELIVERED_QUERY_PARAM) UUID lastDelvered, + @QueryParam(END_OF_PAGE_QUERY_PARAM) UUID endOfPage); + + @PUT + @Path("/{notificationId}") + @ApiOperation(value = "Mark notification as read", + response = UpdateNotificationResponseStatus.class) + Response markAsRead( + @ApiParam(value = "Notification Id") @PathParam("notificationId") String notificationId, + @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user) + throws InvocationTargetException, IllegalAccessException; + + @PUT + @Path("/last-seen/{notificationId}") + @ApiOperation(value = "Update Last Seen Notification", + response = UpdateNotificationResponseStatus.class) + Response updateLastSeenNotification( + @ApiParam(value = "Notification Id") @PathParam("notificationId") String notificationId, + @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user) + throws InvocationTargetException, IllegalAccessException; + + @GET + @Path("/worker") + @ApiOperation(value = "Retrive user not delivered notifications", + response = NotificationsStatusDto.class, + responseContainer = "List") + Response getNewNotificationsByOwnerId( + @NotNull(message = USER_MISSING_ERROR_MSG) @HeaderParam(USER_ID_HEADER_PARAM) String user, + @QueryParam(LAST_DELIVERED_QUERY_PARAM) String eventId, + @QueryParam(LIMIT_QUERY_PARAM) String limit); + +} diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/impl/NotificationsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/impl/NotificationsImpl.java new file mode 100644 index 0000000000..8ff0ac0eea --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/impl/NotificationsImpl.java @@ -0,0 +1,117 @@ +package org.openecomp.sdcrests.notifications.rest.services.impl; + +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.logging.context.MdcUtil; +import org.openecomp.sdc.logging.types.LoggerServiceName; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; +import org.openecomp.sdc.notification.exceptons.NotificationNotExistException; +import org.openecomp.sdc.notification.factories.NotificationsServiceFactory; +import org.openecomp.sdc.notification.services.NotificationsService; +import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsStatusToDto; +import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsToDto; +import org.openecomp.sdcrests.notifications.rest.services.Notifications; +import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto; +import org.openecomp.sdcrests.notifications.types.UpdateNotificationResponseStatus; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; + +import javax.inject.Named; +import javax.ws.rs.core.Response; +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +/** + * @author Avrahamg + * @since June 22, 2017 + */ +@Named +@Service("notifications") +@Scope(value = "prototype") +public class NotificationsImpl implements Notifications { + + private static int selectionLimit = 10; + + private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class); + private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface(); + + @Override + public Response getNotifications(String user, UUID lastDelivered, UUID endOfPage) { + MdcUtil.initMdc(LoggerServiceName.notifications.toString()); + NotificationsStatus notificationsStatus = notificationsService + .getNotificationsStatus(user, lastDelivered, selectionLimit, endOfPage); + MapNotificationsStatusToDto converter = new MapNotificationsStatusToDto(); + NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto(); + converter.doMapping(notificationsStatus, notificationsStatusDto); + + return Response.ok(notificationsStatusDto).build(); + } + + @Override + public Response updateLastSeenNotification(String notificationId, String user) + throws InvocationTargetException, IllegalAccessException { + UpdateNotificationResponseStatus + updateNotificationResponseStatus = new UpdateNotificationResponseStatus(); + try { + notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId)); + } catch (Exception ex) { + LOGGER.error( + String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(), + user), ex); + updateNotificationResponseStatus.addStructureError(notificationId, + new ErrorMessage(ErrorLevel.ERROR, + Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage())); + } + return Response.ok(updateNotificationResponseStatus).build(); + } + + @Override + public Response markAsRead(String notificationId, String user) + throws InvocationTargetException, IllegalAccessException { + + UpdateNotificationResponseStatus + updateNotificationResponseStatus = new UpdateNotificationResponseStatus(); + try { + notificationsService.markAsRead(user, notificationId); + } catch (NotificationNotExistException ex) { + LOGGER.error(Messages.FAILED_TO_MARK_NOTIFICATION_AS_READ.getErrorMessage(), ex); + updateNotificationResponseStatus.addStructureError( + notificationId, new ErrorMessage(ErrorLevel.ERROR, Messages + .FAILED_TO_MARK_NOTIFICATION_AS_READ + .getErrorMessage())); + } + return Response.ok(updateNotificationResponseStatus).build(); + } + + @Override + public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) { + MdcUtil.initMdc(LoggerServiceName.notifications.toString()); + + int limit = selectionLimit; + + if (Objects.nonNull(limitStr)) { + try { + limit = Integer.parseInt(limitStr); + } + catch (NumberFormatException f) { + LOGGER.error("Non numeric selection list size value specified: " + limitStr); + } + } + + List<NotificationEntity> notifications = Objects.isNull(eventId) + ? notificationsService.getNotificationsByOwnerId(user, limit) + : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit); + + MapNotificationsToDto converter = new MapNotificationsToDto(); + NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto(); + converter.doMapping(notifications, notificationsStatusDto); + + return Response.ok(notificationsStatusDto).build(); + } +} |