Containerization feature of SO
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / onap / so / adapters / sdnc / sdncrest / SNIROResponse.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.adapters.sdnc.sdncrest;
22
23 import javax.servlet.http.HttpServletResponse;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.POST;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.onap.so.adapters.sdnc.impl.Constants;
33 import org.onap.so.logger.MessageEnum;
34 import org.onap.so.logger.MsoAlarmLogger;
35 import org.onap.so.logger.MsoLogger;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.core.env.Environment;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * A temporary interface to support notifications from SNIRO to BPMN.
42  * We added this to the SDNC adapter because we didn't have time to
43  * develop a SNIRO adapter in 1702.
44  */
45 @Path("/")
46 @Component
47 public class SNIROResponse {
48         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA,SNIROResponse.class);
49         private static final MsoAlarmLogger ALARMLOGGER = new MsoAlarmLogger();
50         
51         @Autowired
52         private Environment env;
53         
54         @Autowired
55         private BPRestCallback callback;
56
57         @POST
58         @Path("/SDNCNotify/SNIROResponse/{correlator}")
59         @Consumes("*/*")
60         @Produces({MediaType.TEXT_PLAIN})
61         public Response serviceNotification(@PathParam("correlator") String correlator, String content) {
62                 LOGGER.info(MessageEnum.RA_RECEIVE_SDNC_NOTIF, content, "SDNC", "SDNCNotify/SNIROResponse");
63
64                 long startTime = System.currentTimeMillis();
65
66                 String bpUrl = env.getProperty(Constants.BPEL_REST_URL_PROP, ""); 
67
68                 if (bpUrl == null || bpUrl.equals("")) {
69                         String error = "Missing configuration for: " + Constants.BPEL_REST_URL_PROP;
70                         LOGGER.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, Constants.BPEL_REST_URL_PROP, "SDNC", "",
71                                 MsoLogger.ErrorCode.DataError, "Missing config param");
72                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
73                         return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity(error).build();
74                 }
75
76                 long bpStartTime = System.currentTimeMillis();
77                 boolean callbackSuccess = callback.send(bpUrl, "SNIROResponse", correlator, content);
78
79                 if (callbackSuccess) {
80                         LOGGER.recordMetricEvent(bpStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
81                                 "Sent notification", "BPMN", bpUrl, null);
82                         LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
83                 } else {
84                         LOGGER.recordMetricEvent(bpStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError,
85                                 "Failed to send notification", "BPMN", bpUrl, null);
86                         LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError,
87                                 "Failed to send notification");
88                 }
89
90                 return Response.status(204).build();
91         }
92 }