2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.adapters.sdnc.impl;
24 import java.io.StringReader;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
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;
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;
49 import org.openecomp.mso.logger.MsoLogger;
50 import org.openecomp.mso.properties.MsoPropertiesFactory;
51 import org.openecomp.mso.logger.MessageEnum;
53 public class SDNCAdapterRestImpl {
55 private MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
56 public final static String MSO_PROP_ADAPTER="MSO_PROP_SDNC_ADAPTER";
58 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
61 private HttpHeaders headers;
62 @Context HttpServletRequest request;
66 @Consumes("application/xml")
67 @Produces("application/xml")
68 public Response MSORequest(String reqXML) {
69 msoLogger.debug("***Received MSO Rest Request. XML:" + reqXML);
71 Document reqDoc = null;
72 SDNCResponse sdncResp = null;
73 RequestTunables rt = null;
75 long startTime = System.currentTimeMillis();
76 MsoLogger.setServiceName("UNKNOWN");
78 String operation = "";
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);
85 MsoLogger.setLogContext(reqId, "");
87 msoLogger.debug ("Received MSO Rest Request XML: " + reqXML);
88 rt = new RequestTunables(reqId, "", operation, action, msoPropertiesFactory);
91 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
92 dbf.setFeature (XMLConstants.FEATURE_SECURE_PROCESSING, true);
93 DocumentBuilder db = dbf.newDocumentBuilder();
95 InputSource source = new InputSource(new StringReader(reqXML));
97 reqDoc = db.parse(source);
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");
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);
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");
116 if (sdncResp.getSdncRespXml() == null) {
117 sdncResp.setSdncRespXml(Utils.genMsoFailResp(sdncResp));
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();
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;
138 if (!healthCheck.configFileCheck(msoLogger, startTime, MSO_PROP_ADAPTER)) {
139 return HealthCheckUtils.NOT_STARTED_RESPONSE;
141 msoLogger.debug("healthcheck - Successful");
142 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
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;
159 if (healthCheck.verifyGlobalHealthCheck(enableBpmn, requestId)) {
160 msoLogger.debug("globalHealthcheck - Successful");
161 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
163 msoLogger.debug("globalHealthcheck - At leaset one of the sub-modules is not available.");
164 return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
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;
182 if (healthCheck.verifyNodeHealthCheck(HealthCheckUtils.NodeType.RA, requestId)) {
183 msoLogger.debug("nodeHealthcheck - Successful");
184 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
186 msoLogger.debug("nodeHealthcheck - At leaset one of the sub-modules is not available.");
187 return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;