Change the header to SO
[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  * ================================================================================
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.mso.adapters.sdnc.notify;
22
23
24 import java.io.StringReader;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.Response;
36 import javax.xml.XMLConstants;
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 import javax.xml.parsers.ParserConfigurationException;
40 import javax.xml.xpath.XPath;
41 import javax.xml.xpath.XPathConstants;
42 import javax.xml.xpath.XPathFactory;
43 import javax.ejb.EJB;
44
45 import org.openecomp.mso.utils.UUIDChecker;
46 import org.w3c.dom.Document;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.InputSource;
49
50 import org.openecomp.mso.adapters.sdnc.impl.Constants;
51 import org.openecomp.mso.adapters.sdnc.impl.SDNCAdapterPortTypeImpl;
52 import org.openecomp.mso.adapters.sdnc.impl.SDNCResponse;
53 import org.openecomp.mso.adapters.sdnc.impl.SDNCRestClient;
54 import org.openecomp.mso.adapters.sdnc.util.SDNCRequestIdUtil;
55 import org.openecomp.mso.logger.MsoLogger;
56 import org.openecomp.mso.properties.MsoPropertiesFactory;
57 import org.openecomp.mso.logger.MessageEnum;
58
59 //SDNC to SDNC Async Notifcations
60 @Path("/")
61 public class SDNCNotifyResource {
62
63         private MsoPropertiesFactory msoPropertiesFactory=new MsoPropertiesFactory();
64         
65     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
66
67     @GET()
68     public Response printMessage () {
69         long startTime = System.currentTimeMillis ();
70         UUIDChecker.generateUUID (msoLogger);
71         String result = "SDNCAdapter Rest services";
72         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
73         return Response.status (HttpServletResponse.SC_OK).entity (result).build ();
74
75     }
76
77     @GET()
78     @Path("/{param}")
79     public Response printMessageParam (@PathParam("param") String msg) {
80         long startTime = System.currentTimeMillis ();
81         UUIDChecker.generateUUID (msoLogger);
82         String result = "SDNCAdapter Rest services : " + msg;
83         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
84         return Response.status (HttpServletResponse.SC_OK).entity (result).build ();
85
86     }
87
88     @POST
89     @Path("/SDNCNotify")
90     @Consumes("application/xml")
91     @Produces("application/xml")
92     public Response SDNCNotify (String reqXML, @Context HttpServletRequest request) {
93
94         XPathFactory xpathFactory = XPathFactory.newInstance ();
95         XPath xpath = xpathFactory.newXPath ();
96         long startTime = System.currentTimeMillis ();
97
98         msoLogger.info (MessageEnum.RA_RECEIVE_SDNC_NOTIF, reqXML, "SDNC", "SDNCNotify");
99
100         InputSource source = new InputSource (new StringReader (reqXML));
101
102         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
103
104         DocumentBuilder db;
105
106         try {
107             dbf.setFeature (XMLConstants.FEATURE_SECURE_PROCESSING, true);
108             db = dbf.newDocumentBuilder ();
109         } catch (ParserConfigurationException e) {
110             msoLogger.error (MessageEnum.RA_PARSING_REQUEST_ERROR, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.SchemaError, "Exception - Invalid XML request format", e);
111             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, "Invalid XML request format");
112             return Response.status (HttpServletResponse.SC_BAD_REQUEST).entity ("Invalid XML request format").build ();
113         }
114
115         Document doc = null;
116         try {
117             doc = db.parse (source);
118         } catch (Exception e) {
119             msoLogger.error (MessageEnum.RA_PARSING_REQUEST_ERROR, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.SchemaError, "Exception - Invalid XML request format", e);
120             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, "Invalid XML request format");
121             return Response.status (HttpServletResponse.SC_BAD_REQUEST).entity ("Invalid XML request format").build ();
122         }
123
124         try {
125             NodeList nl = (NodeList) xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT, doc, XPathConstants.NODESET);
126             if (nl.getLength () <= 0) {
127                 msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Missing param");
128                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT);
129                 return Response.status (HttpServletResponse.SC_BAD_REQUEST)
130                                .entity ("Missing " + Constants.SDNC_SVCCFGRESP_ROOT)
131                                .build ();
132             }
133         } catch (Exception e) {
134             msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param", e);
135             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT);
136             return Response.status (HttpServletResponse.SC_BAD_REQUEST)
137                            .entity ("Missing " + Constants.SDNC_SVCCFGRESP_ROOT)
138                            .build ();
139         }
140
141         String reqId;
142         try {
143             reqId = xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID, doc);
144         } catch (Exception e) {
145             msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param", e);
146             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID);
147             return Response.status (HttpServletResponse.SC_BAD_REQUEST)
148                            .entity ("Missing " + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_REQ_ID)
149                            .build ();
150         }
151
152         MsoLogger.setLogContext (SDNCRequestIdUtil.getSDNCOriginalRequestId (reqId), "");
153
154         String respCode;
155         try {
156             respCode = xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE, doc);
157         } catch (Exception e) {
158             msoLogger.error (MessageEnum.RA_MISSING_PARAM,
159                              Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param",
160                              e);
161             msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.BadRequest, "Missing" + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE);
162             return Response.status (HttpServletResponse.SC_BAD_REQUEST)
163                            .entity ("Missing" + Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_CODE)
164                            .build ();
165         }
166
167         String respMsg = "";
168         try {
169             respMsg = xpath.evaluate (Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_MSG, doc);
170         } catch (Exception e) {
171             msoLogger.error (MessageEnum.RA_MISSING_PARAM, Constants.SDNC_SVCCFGRESP_ROOT + Constants.SDNC_RESP_MSG, "SDNC", "SDNCNotify", MsoLogger.ErrorCode.DataError, "Exception - Missing param", e);
172         }
173
174         String bpelUrl = null;
175         /*
176          * TODO Hibernate
177          * try {
178          * bpelUrl = RequestsDatabase.getBpelUrl(reqId);
179          * }
180          * catch (Exception e)
181          * {
182          * logger.error("Unable to get SDNC_CALLBACK_URL from ActiveRequests, using default for reqid:" + reqId, e);
183          * }
184          */
185         if (bpelUrl == null) {
186             bpelUrl = SDNCAdapterPortTypeImpl.getProperty (Constants.BPEL_URL_PROP, Constants.DEFAULT_BPEL_URL,msoPropertiesFactory);
187         }
188
189         SDNCResponse sdncResp = new SDNCResponse (reqId);
190         sdncResp.setRespCode (Integer.parseInt (respCode));
191         sdncResp.setRespMsg (respMsg);
192         sdncResp.setSdncRespXml (reqXML);
193         long subStartTime = System.currentTimeMillis ();
194         SDNCRestClient.sendRespToBpel (bpelUrl, sdncResp,msoPropertiesFactory);
195         msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully send request to BPMN", "BPMN", bpelUrl, null);
196
197         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
198         return Response.ok ().build ();
199     }
200 }