2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.notification.services.impl;
23 import com.datastax.driver.core.utils.UUIDs;
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.openecomp.core.utilities.json.JsonUtil;
26 import org.openecomp.sdc.destinationprovider.DestinationProvider;
27 import org.openecomp.sdc.notification.dao.NotificationsDao;
28 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
29 import org.openecomp.sdc.notification.dtos.Event;
30 import org.openecomp.sdc.notification.services.PropagationService;
32 import java.util.List;
34 import java.util.UUID;
35 import java.util.stream.Collectors;
37 import static java.util.Objects.requireNonNull;
39 public class PropagationServiceImpl implements PropagationService {
41 private NotificationsDao notificationsDao;
43 public PropagationServiceImpl(NotificationsDao notificationsDao) {
44 this.notificationsDao = notificationsDao;
49 public void notify(Event event, DestinationProvider destinationProvider) {
50 requireNonNull(event.getEventType());
51 requireNonNull(event.getOriginatorId());
52 List<String> subscribers = destinationProvider.getSubscribers();
53 if (CollectionUtils.isEmpty(subscribers)) {
56 List<NotificationEntity> notificationEntities = subscribers.stream().map(
58 UUID eventId = UUIDs.timeBased();
59 return createNotificationEntity(event.getEventType(), subscriber,
60 event.getOriginatorId(), event.getAttributes(), eventId);
61 }).collect(Collectors.toList());
62 if(CollectionUtils.isNotEmpty(notificationEntities)) {
63 notificationsDao.createBatch(notificationEntities);
67 private NotificationEntity createNotificationEntity(String eventType, String subscriber,
69 Map<String, Object> attributes,
71 NotificationEntity notificationEntity =
72 new NotificationEntity(subscriber, eventId, eventType, originatorId);
73 if (attributes != null && !attributes.isEmpty()) {
74 notificationEntity.setEventAttributes(JsonUtil.object2Json(attributes));
76 return notificationEntity;