Merge "Reorder modifiers"
[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
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;
175         
176         bpelUrl = SDNCAdapterPortTypeImpl.getProperty (Constants.BPEL_URL_PROP, Constants.DEFAULT_BPEL_URL,msoPropertiesFactory);
177         if (bpelUrl == null) {
178             msoLogger.debug("bpelUrl is NULL:");
179         }
180
181         SDNCResponse sdncResp = new SDNCResponse (reqId);
182         sdncResp.setRespCode (Integer.parseInt (respCode));
183         sdncResp.setRespMsg (respMsg);
184         sdncResp.setSdncRespXml (reqXML);
185         long subStartTime = System.currentTimeMillis ();
186         SDNCRestClient.sendRespToBpel (bpelUrl, sdncResp,msoPropertiesFactory);
187         msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully send request to BPMN", "BPMN", bpelUrl, null);
188
189         msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
190         return Response.ok ().build ();
191     }
192 }