2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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 * ================================================================================
20 package org.openecomp.portalapp.portal.controller;
22 import java.util.Arrays;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.openecomp.portalapp.portal.domain.EPUser;
33 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
34 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
35 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
36 import org.openecomp.portalapp.portal.service.UserNotificationService;
37 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
38 import org.openecomp.portalapp.portal.transport.EpRoleNotificationItem;
39 import org.openecomp.portalapp.portal.utils.PortalConstants;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.EnableAspectJAutoProxy;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.bind.annotation.RestController;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
52 import io.swagger.annotations.ApiOperation;
55 * Receives messages from the Collaboration Bus (C-BUS) notification and event
56 * brokering tool. Creates notifications for ECOMP Portal users.
59 @RequestMapping(PortalConstants.REST_AUX_API)
61 @EnableAspectJAutoProxy
63 public class TicketEventController implements BasicAuthenticationController {
66 private UserNotificationService userNotificationService;
68 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TicketEventController.class);
70 public boolean isAuxRESTfulCall() {
74 private final ObjectMapper mapper = new ObjectMapper();
76 @ApiOperation(value = "Accepts messages from external ticketing systems and creates notifications for Portal users.", response = PortalRestResponse.class)
77 @RequestMapping(value = { "/ticketevent" }, method = RequestMethod.POST)
78 public PortalRestResponse<String> handleRequest(HttpServletRequest request, HttpServletResponse response,
79 @RequestBody String ticketEventJson) throws Exception {
81 logger.debug(EELFLoggerDelegate.debugLogger, "Ticket Event notification" + ticketEventJson);
82 PortalRestResponse<String> portalResponse = new PortalRestResponse<>();
84 JsonNode ticketEventNotif = mapper.readTree(ticketEventJson);
86 // Reject request if required fields are missing.
87 String error = validateTicketEventMessage(ticketEventNotif);
89 portalResponse.setStatus(PortalRestStatusEnum.ERROR);
90 portalResponse.setMessage(error);
91 response.setStatus(400);
92 return portalResponse;
95 EpNotificationItem epItem = new EpNotificationItem();
96 epItem.setCreatedDate(new Date());
97 epItem.setIsForOnlineUsers("Y");
98 epItem.setIsForAllRoles("N");
99 epItem.setActiveYn("Y");
101 JsonNode event = ticketEventNotif.get("event");
102 JsonNode header = event.get("header");
103 JsonNode body = event.get("body");
104 epItem.setMsgDescription(body.toString());
105 Long eventDate = System.currentTimeMillis();
106 if (body.get("eventDate") != null) {
107 eventDate = body.get("eventDate").asLong();
109 String eventSource = header.get("eventSource").asText();
110 epItem.setMsgSource(eventSource);
111 epItem.setStartTime(new Date(eventDate));
112 Calendar calendar = Calendar.getInstance();
113 calendar.setTime(epItem.getStartTime());
114 int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
115 calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth + 30);
116 epItem.setEndTime(calendar.getTime());
117 String severityString = "1";
118 if (body.get("severity") != null) {
119 severityString = (body.get("severity").toString()).substring(1, 2);
121 Long severity = Long.parseLong(severityString);
122 epItem.setPriority(severity);
123 epItem.setCreatorId(null);
124 Set<EpRoleNotificationItem> roles = new HashSet<>();
125 JsonNode SubscriberInfo = ticketEventNotif.get("SubscriberInfo");
126 JsonNode userList = SubscriberInfo.get("UserList");
127 String UserIds[] = userList.toString().replace("[", "").replace("]", "").trim().replace("\"", "")
129 String assetID = eventSource + ' '
130 + userList.toString().replace("[", "").replace("]", "").trim().replace("\"", "") + ' '
131 + new Date(eventDate);
132 if (body.get("assetID") != null) {
133 assetID = body.get("assetID").asText();
135 epItem.setMsgHeader(assetID);
136 List<EPUser> users = userNotificationService.getUsersByOrgIds(Arrays.asList(UserIds));
137 for (String userId : UserIds) {
138 EpRoleNotificationItem roleNotifItem = new EpRoleNotificationItem();
139 for (EPUser user : users) {
140 if (user.getOrgUserId().equals(userId)) {
141 roleNotifItem.setRecvUserId(user.getId().intValue());
142 roles.add(roleNotifItem);
148 epItem.setRoles(roles);
149 userNotificationService.saveNotification(epItem);
151 portalResponse.setStatus(PortalRestStatusEnum.OK);
152 portalResponse.setMessage("processEventNotification: notification created");
153 portalResponse.setResponse("NotificationId is :" + epItem.notificationId);
154 } catch (Exception ex) {
155 portalResponse.setStatus(PortalRestStatusEnum.ERROR);
156 response.setStatus(400);
157 portalResponse.setMessage(ex.toString());
159 return portalResponse;
163 * Validates that mandatory fields are present.
165 * @param ticketEventNotif
166 * @return Error message if a problem is found; null if all is well.
168 private String validateTicketEventMessage(JsonNode ticketEventNotif) {
169 JsonNode application = ticketEventNotif.get("application");
170 JsonNode event = ticketEventNotif.get("event");
171 JsonNode header = event.get("header");
172 JsonNode body = event.get("body");
173 JsonNode SubscriberInfo = ticketEventNotif.get("SubscriberInfo");
174 if (application == null)
175 return "application is mandatory";
177 return "body is mandatory";
178 if (header.get("eventSource") == null)
179 return "Message Source is mandatory";
180 if (SubscriberInfo.get("UserList") == null)
181 return "At least one user Id is mandatory";