2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.sdnc.northbound.asyncrequests;
24 import javax.servlet.Servlet;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import java.util.Properties;
31 import java.io.Writer;
32 import java.io.StringWriter;
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.util.stream.Collectors;
37 import org.apache.velocity.VelocityContext;
38 import org.apache.velocity.app.VelocityEngine;
39 import org.apache.velocity.Template;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.json.JSONArray;
44 import org.json.JSONObject;
45 import org.osgi.service.component.annotations.Component;
47 @Component(immediate = true, property = {"alias=/asyncNotification", "servlet-name=AsyncNotification"})
48 public class AsyncNotificationHandling extends HttpServlet implements Servlet {
50 private static final long serialVersionUID = 1L;
52 private final Logger LOG = LoggerFactory.getLogger(AsyncNotificationHandling.class);
54 private static final String PARAMETER_NAME = "parameter-name";
55 private static final String STRING_VALUE = "string-value";
56 private static final String REQUEST_ID = "request-id";
57 private static final String SVC_REQUEST_ID = "svc-request-id";
58 private static final String RESPONSE_CODE = "response-code";
59 private static final String ACK_FINAL_INDICATOR = "ack-final-indicator";
60 private static final String CONFIGURATION_RESPONSE = "configuration-response-common";
61 private static final String PROPERTIES_PATH = "/opt/onap/ccsdk/data/properties/";
62 private static final String TEMPLATE_NAME = "rpc-message-sliapi-execute-async.vt";
63 private static final String UTF_8 = "UTF-8";
66 * Handles async request for different domain controllers
70 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
72 LOG.info("Reached async servlet");
73 BufferedReader requestInput = null;
75 requestInput = request.getReader();
76 } catch (Exception e) {
77 LOG.error("Unable to read input from request", e);
80 if (requestInput == null) {
83 String json = requestInput.lines().collect(Collectors.joining());
84 LOG.info("Async Request payload {}", json);
89 JSONObject jsonObj = new JSONObject(json);
90 JSONObject input = jsonObj.getJSONObject("input");
92 if (input.has(CONFIGURATION_RESPONSE)) {
93 JSONObject payloadString = null;
96 payloadString = input.getJSONObject(CONFIGURATION_RESPONSE);
97 String rpcMessageBody = buildAsyncNotifRPCMsgRoadm(payloadString);
98 LOG.debug("rpc message body {}", rpcMessageBody);
99 invokeRPC(rpcMessageBody);
100 } catch (Exception e) {
101 LOG.error("Unable to build rpc message body::", e);
104 LOG.info("Async request received for wrong domain");
109 private void invokeRPC(String rpcMsgbody) {
111 String odlUrlBase = "http://sdnc.onap:8282/restconf/operations"; // using cluster SDNC URL
112 String odlUser = "admin";
113 String odlPassword = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
114 String sdncEndpoint = "SLI-API:execute-graph";
116 if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
117 SdncOdlConnection conn =
118 SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
120 conn.send("POST", "application/json", rpcMsgbody);
122 LOG.info("POST message body would be:\n {}", rpcMsgbody);
124 } catch (Exception e) {
125 LOG.error("Unable to process message", e);
129 private String buildAsyncNotifRPCMsgRoadm(JSONObject payloadString) throws IOException {
130 VelocityEngine velocityEngine = new VelocityEngine();
132 Properties props = new Properties();
133 props.put("file.resource.loader.path", PROPERTIES_PATH);
134 velocityEngine.init(props);
136 String sliParameters = "sli_parameters";
138 JSONArray sliParametersArray = new JSONArray();
140 VelocityContext context = new VelocityContext();
142 context.put("rpc_name", "handle-async-notif");
143 String requestId = payloadString.getString(SVC_REQUEST_ID);
144 String responseCode = payloadString.getString(RESPONSE_CODE);
145 String ackIndicator = payloadString.getString(ACK_FINAL_INDICATOR);
147 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, REQUEST_ID).put(STRING_VALUE, requestId));
148 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, RESPONSE_CODE).put(STRING_VALUE, responseCode));
150 .put(new JSONObject().put(PARAMETER_NAME, ACK_FINAL_INDICATOR).put(STRING_VALUE, ackIndicator));
152 context.put(sliParameters, sliParametersArray);
154 Writer writer = new StringWriter();
155 velocityEngine.mergeTemplate(TEMPLATE_NAME, UTF_8, context, writer);
158 return writer.toString();