Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-core / src / main / java / org / openecomp / sdc / notification / services / impl / PropagationServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.notification.services.impl;
22
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;
31
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35 import java.util.stream.Collectors;
36
37 import static java.util.Objects.requireNonNull;
38
39 public class PropagationServiceImpl implements PropagationService {
40
41     private NotificationsDao notificationsDao;
42
43     public PropagationServiceImpl(NotificationsDao notificationsDao) {
44         this.notificationsDao = notificationsDao;
45     }
46
47
48     @Override
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)) {
54             return;
55         }
56         List<NotificationEntity> notificationEntities = subscribers.stream().map(
57             subscriber -> {
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);
64         }
65     }
66
67     private NotificationEntity createNotificationEntity(String eventType, String subscriber,
68                                                         String originatorId,
69                                                         Map<String, Object> attributes,
70                                                         UUID eventId) {
71         NotificationEntity notificationEntity =
72             new NotificationEntity(subscriber, eventId, eventType, originatorId);
73         if (attributes != null && !attributes.isEmpty()) {
74             notificationEntity.setEventAttributes(JsonUtil.object2Json(attributes));
75         }
76         return notificationEntity;
77     }
78 }