55e6192ead04797d512127c9fd636816928dbab2
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / impl / SDNCAdapterRestImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.impl;
22
23
24 import java.io.StringReader;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import javax.ws.rs.GET;
30 import javax.ws.rs.HEAD;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.DefaultValue;
38 import javax.ws.rs.core.HttpHeaders;
39 import javax.ws.rs.core.Response;
40 import javax.xml.XMLConstants;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43
44 import org.openecomp.mso.HealthCheckUtils;
45 import org.openecomp.mso.utils.UUIDChecker;
46 import org.w3c.dom.Document;
47 import org.xml.sax.InputSource;
48
49 import org.openecomp.mso.logger.MsoLogger;
50 import org.openecomp.mso.properties.MsoPropertiesFactory;
51 import org.openecomp.mso.logger.MessageEnum;
52 @Path("/")
53 public class SDNCAdapterRestImpl {
54
55         private MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
56         public final static String MSO_PROP_ADAPTER="MSO_PROP_SDNC_ADAPTER";
57
58         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
59
60         @Context
61         private HttpHeaders headers;
62         @Context HttpServletRequest request;
63
64         @POST
65         @Path("/MSORequest")
66         @Consumes("application/xml")
67         @Produces("application/xml")
68         public Response MSORequest(String reqXML) {
69             msoLogger.debug("***Received MSO Rest Request. XML:" + reqXML);
70
71             Document reqDoc = null;
72         SDNCResponse sdncResp = null;
73         RequestTunables rt = null;
74         String reqId = "";
75         long startTime = System.currentTimeMillis();
76         MsoLogger.setServiceName("UNKNOWN");
77                 String action = "";
78                 String operation = "";
79             try {
80
81                 reqId = headers.getRequestHeader("mso-request-id").get(0);
82                 action = headers.getRequestHeader("mso-request-action").get(0);
83                 operation = headers.getRequestHeader("mso-request-operation").get(0);
84
85                 MsoLogger.setLogContext(reqId, "");
86
87                 msoLogger.debug ("Received MSO Rest Request XML: " + reqXML);
88                         rt = new RequestTunables(reqId, "", operation, action, msoPropertiesFactory);
89                 rt.setTunables();
90
91                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
92                 dbf.setFeature (XMLConstants.FEATURE_SECURE_PROCESSING, true);
93                 DocumentBuilder db = dbf.newDocumentBuilder();
94
95                 InputSource source = new InputSource(new StringReader(reqXML));
96
97                 reqDoc = db.parse(source);
98
99             } catch (Exception e) {
100                 msoLogger.error(MessageEnum.RA_PARSING_REQUEST_ERROR, reqXML, "SDNC", "", MsoLogger.ErrorCode.DataError, "Exception - Invalid XML request format", e);
101                 sdncResp = new SDNCResponse(reqId, HttpServletResponse.SC_BAD_REQUEST, "Invalid XML request format");
102             }
103
104                 if (reqDoc != null) {
105                         msoLogger.debug("***Getting response from sdnc***");
106                         long subStartTime = System.currentTimeMillis ();
107                         sdncResp = SDNCRestClient.getSdncResp(Utils.genSdncReq(reqDoc, rt), rt,msoPropertiesFactory);
108                         msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from SDNC", "SDNC", action + "." + operation, null);
109                 }
110
111                 if (sdncResp == null) {
112                         msoLogger.debug("An Internal Server error has occurred in SDNC Adapter");
113                         sdncResp = new SDNCResponse(reqId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "MSO - SDNCA Internal Error");
114                 }
115
116                 if (sdncResp.getSdncRespXml() == null) {
117             sdncResp.setSdncRespXml(Utils.genMsoFailResp(sdncResp));
118         }
119
120                 msoLogger.debug("***Completed MSO Rest Request." + sdncResp.toString());
121                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
122                 return Response.status(sdncResp.getRespCode()).entity(sdncResp.getSdncRespXml()).build();
123         }
124         
125     @HEAD
126     @GET
127     @Path("/healthcheck")
128     @Produces("text/html")
129     public Response healthcheck (@QueryParam("requestId") String requestId) {
130                 long startTime = System.currentTimeMillis ();
131                 MsoLogger.setServiceName ("Healthcheck");
132                 UUIDChecker.verifyOldUUID(requestId, msoLogger);
133                 HealthCheckUtils healthCheck = new HealthCheckUtils ();
134                 if (!healthCheck.siteStatusCheck(msoLogger, startTime)) {
135                         return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
136                 }
137
138                 if (!healthCheck.configFileCheck(msoLogger, startTime, MSO_PROP_ADAPTER)) {
139                         return HealthCheckUtils.NOT_STARTED_RESPONSE;
140                 }
141                 msoLogger.debug("healthcheck - Successful");
142                 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
143     }
144
145         @HEAD
146         @GET
147         @Path("/globalhealthcheck")
148         @Produces("text/html")
149         public Response globalHealthcheck (@DefaultValue("true") @QueryParam("enableBpmn") boolean enableBpmn) {
150                 long startTime = System.currentTimeMillis ();
151                 MsoLogger.setServiceName ("GlobalHealthcheck");
152                 // Generate a Request Id
153                 String requestId = UUIDChecker.generateUUID(msoLogger);
154                 HealthCheckUtils healthCheck = new HealthCheckUtils ();
155                 if (!healthCheck.siteStatusCheck (msoLogger, startTime)) {
156                         return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
157                 }
158
159                 if (healthCheck.verifyGlobalHealthCheck(enableBpmn, requestId)) {
160                         msoLogger.debug("globalHealthcheck - Successful");
161                         return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
162                 } else {
163                         msoLogger.debug("globalHealthcheck - At leaset one of the sub-modules is not available.");
164                         return  HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
165                 }
166         }
167
168         @HEAD
169         @GET
170         @Path("/nodehealthcheck")
171         @Produces("text/html")
172         public Response nodeHealthcheck () {
173                 long startTime = System.currentTimeMillis ();
174                 MsoLogger.setServiceName ("NodeHealthcheck");
175                 // Generate a Request Id
176                 String requestId = UUIDChecker.generateUUID(msoLogger);
177                 HealthCheckUtils healthCheck = new HealthCheckUtils ();
178                 if (!healthCheck.siteStatusCheck (msoLogger, startTime)) {
179                         return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
180                 }
181
182                 if (healthCheck.verifyNodeHealthCheck(HealthCheckUtils.NodeType.RA, requestId)) {
183                         msoLogger.debug("nodeHealthcheck - Successful");
184                         return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
185                 } else {
186                         msoLogger.debug("nodeHealthcheck - At leaset one of the sub-modules is not available.");
187                         return  HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
188                 }
189         }
190
191 }