Added the "@Override" annotation
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / notify / SDNCNotifyResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.adapters.sdnc.notify;
23
24
25 import java.io.StringReader;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.xml.XMLConstants;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40 import javax.xml.parsers.ParserConfigurationException;
41 import javax.xml.xpath.XPath;
42 import javax.xml.xpath.XPathConstants;
43 import javax.xml.xpath.XPathFactory;
44 import javax.ejb.EJB;
45
46 import org.openecomp.mso.utils.UUIDChecker;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.NodeList;
49 import org.xml.sax.InputSource;
50
51 import org.openecomp.mso.adapters.sdnc.impl.Constants;
52 import org.openecomp.mso.adapters.sdnc.impl.SDNCAdapterPortTypeImpl;
53 import org.openecomp.mso.adapters.sdnc.impl.SDNCResponse;
54 import org.openecomp.mso.adapters.sdnc.impl.SDNCRestClient;
55 import org.openecomp.mso.adapters.sdnc.util.SDNCRequestIdUtil;
56 import org.openecomp.mso.logger.MsoLogger;
57 import org.openecomp.mso.properties.MsoPropertiesFactory;
58 import org.openecomp.mso.logger.MessageEnum;
59
60 //SDNC to SDNC Async Notifcations
61 @Path("/")
62 public class SDNCNotifyResource {
63
64         private MsoPropertiesFactory msoPropertiesFactory=new MsoPropertiesFactory();
65         
66     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
67
68     @GET()
69     public Response printMessage () {
70         long startTime = System.currentTimeMillis ();
71         UUIDChecker.generateUUID (msoLogger);
72         String result = "SDNCAdapter Rest services";
73         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
74         return Response.status (HttpServletResponse.SC_OK).entity (result).build ();
75
76     }
77
78     @GET()
79     @Path("/{param}")
80     public Response printMessageParam (@PathParam("param") String msg) {
81         long startTime = System.currentTimeMillis ();
82         UUIDChecker.generateUUID (msoLogger);
83         String result = "SDNCAdapter Rest services : " + msg;
84         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
85         return Response.status (HttpServletResponse.SC_OK).entity (result).build ();
86
87     }
88
89     @POST
90     @Path("/SDNCNotify")
91     @Consumes("application/xml")
92     @Produces("application/xml")
93     public Response SDNCNotify (String reqXML, @Context HttpServletRequest request) {
94
95         XPathFactory xpathFactory = XPathFactory.newInstance ();
96         XPath xpath = xpathFactory.newXPath ();
97         long startTime = System.currentTimeMillis ();
98
99         msoLogger.info (MessageEnum.RA_RECEIVE_SDNC_NOTIF, reqXML, "SDNC", "SDNCNotify");
100
101         InputSource source = new InputSource (new StringReader (reqXML));
102
103         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
104
105         DocumentBuilder db;
106
107         try {
108             dbf.setFeature (XMLConstants.FEATURE_SECURE_PROCESSING, true);
109             db = dbf.newDocumentBuilder ();
110         } catch (ParserConfigurationException e) {
111             msoLogger.error (MessageEnum.RA_PARSING_REQUEST_ERROR, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.SchemaError, "Exception - Invalid XML request format", e);
112             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, "Invalid XML request format");
113             return Response.status (HttpServletResponse.SC_BAD_REQUEST).entity ("Invalid XML request format").build ();
114         }
115
116         Document doc = null;
117         try {
118             doc = db.parse (source);
119         } catch (Exception e) {
120             msoLogger.error (MessageEnum.RA_PARSING_REQUEST_ERROR, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.SchemaError, "Exception - Invalid XML request format", e);
121             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, "Invalid XML request format");
122             return Response.status (HttpServletResponse.SC_BAD_REQUEST).entity ("Invalid XML request format").build ();
123         }
124
125         try {
126             NodeList nl = (NodeList) xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT, doc, XPathConstants.NODESET);
127             if (nl.getLength () <= 0) {
128                 msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Missing param");
129                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT);
130                 return Response.status (HttpServletResponse.SC_BAD_REQUEST)
131                                .entity ("Missing " + Constants.SDNC_SVCCFGRESP_ROOT)
132                                .build ();
133             }
134         } catch (Exception e) {
135             msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param", e);
136             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT);
137             return Response.status (HttpServletResponse.SC_BAD_REQUEST)
138                            .entity ("Missing " + Constants.SDNC_SVCCFGRESP_ROOT)
139                            .build ();
140         }
141
142         String reqId;
143         try {
144             reqId = xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID, doc);
145         } catch (Exception e) {
146             msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param", e);
147             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID);
148             return Response.status (HttpServletResponse.SC_BAD_REQUEST)
149                            .entity ("Missing " + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID)
150                            .build ();
151         }
152
153         MsoLogger.setLogContext (SDNCRequestIdUtil.getSDNCOriginalRequestId (reqId), "");
154
155         String respCode;
156         try {
157             respCode = xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE, doc);
158         } catch (Exception e) {
159             msoLogger.error (MessageEnum.RA_MISSING_PARAM,
160                              Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param",
161                              e);
162             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE);
163             return Response.status (HttpServletResponse.SC_BAD_REQUEST)
164                            .entity ("Missing" + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE)
165                            .build ();
166         }
167
168         String respMsg = "";
169         try {
170             respMsg = xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_MSG, doc);
171         } catch (Exception e) {
172             msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_MSG, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param", e);
173         }
174
175         String bpelUrl;
176         /*
177          * TODO Hibernate
178          * try {
179          * bpelUrl = RequestsDatabase.getBpelUrl(reqId);
180          * }
181          * catch (Exception e)
182          * {
183          * logger.error("Unable to get SDNC_CALLBACK_URL from ActiveRequests, using default for reqid:" + reqId, e);
184          * }
185          */
186         
187         bpelUrl = SDNCAdapterPortTypeImpl.getProperty (Constants.BPEL_URL_PROP, Constants.DEFAULT_BPEL_URL,msoPropertiesFactory);
188         if (bpelUrl == null) {
189             msoLogger.debug("bpelUrl is NULL:");
190         }
191
192         SDNCResponse sdncResp = new SDNCResponse (reqId);
193         sdncResp.setRespCode (Integer.parseInt (respCode));
194         sdncResp.setRespMsg (respMsg);
195         sdncResp.setSdncRespXml (reqXML);
196         long subStartTime = System.currentTimeMillis ();
197         SDNCRestClient.sendRespToBpel (bpelUrl, sdncResp,msoPropertiesFactory);
198         msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully send request to BPMN", "BPMN", bpelUrl, null);
199
200         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
201         return Response.ok ().build ();
202     }
203 }