aboutsummaryrefslogtreecommitdiffstats
path: root/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests
diff options
context:
space:
mode:
Diffstat (limited to 'optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests')
-rw-r--r--optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/AsyncNotificationHandling.java162
-rw-r--r--optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/SdncOdlConnection.java150
2 files changed, 312 insertions, 0 deletions
diff --git a/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/AsyncNotificationHandling.java b/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/AsyncNotificationHandling.java
new file mode 100644
index 00000000..955cbb48
--- /dev/null
+++ b/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/AsyncNotificationHandling.java
@@ -0,0 +1,162 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.sdnc.northbound.asyncrequests;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Properties;
+
+import java.io.Writer;
+import java.io.StringWriter;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.stream.Collectors;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.Template;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.osgi.service.component.annotations.Component;
+
+@Component(immediate = true, property = {"alias=/asyncNotification", "servlet-name=AsyncNotification"})
+public class AsyncNotificationHandling extends HttpServlet implements Servlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Logger LOG = LoggerFactory.getLogger(AsyncNotificationHandling.class);
+
+ private static final String PARAMETER_NAME = "parameter-name";
+ private static final String STRING_VALUE = "string-value";
+ private static final String REQUEST_ID = "request-id";
+ private static final String SVC_REQUEST_ID = "svc-request-id";
+ private static final String RESPONSE_CODE = "response-code";
+ private static final String ACK_FINAL_INDICATOR = "ack-final-indicator";
+ private static final String CONFIGURATION_RESPONSE = "configuration-response-common";
+ private static final String PROPERTIES_PATH = "/opt/onap/ccsdk/data/properties/";
+ private static final String TEMPLATE_NAME = "rpc-message-sliapi-execute-async.vt";
+ private static final String UTF_8 = "UTF-8";
+
+ /**
+ * Handles async request for different domain controllers
+ */
+
+ @Override
+ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ LOG.info("Reached async servlet");
+ BufferedReader requestInput = null;
+ try {
+ requestInput = request.getReader();
+ } catch (Exception e) {
+ LOG.error("Unable to read input from request", e);
+ }
+
+ if (requestInput == null) {
+ return;
+ }
+ String json = requestInput.lines().collect(Collectors.joining());
+ LOG.info("Async Request payload {}", json);
+ if (json == null) {
+ return;
+ }
+
+ JSONObject jsonObj = new JSONObject(json);
+ JSONObject input = jsonObj.getJSONObject("input");
+
+ if (input.has(CONFIGURATION_RESPONSE)) {
+ JSONObject payloadString = null;
+
+ try {
+ payloadString = input.getJSONObject(CONFIGURATION_RESPONSE);
+ String rpcMessageBody = buildAsyncNotifRPCMsgRoadm(payloadString);
+ LOG.debug("rpc message body {}", rpcMessageBody);
+ invokeRPC(rpcMessageBody);
+ } catch (Exception e) {
+ LOG.error("Unable to build rpc message body::", e);
+ }
+ } else {
+ LOG.info("Async request received for wrong domain");
+ }
+
+ }
+
+ private void invokeRPC(String rpcMsgbody) {
+ try {
+ String odlUrlBase = "http://sdnc.onap:8282/restconf/operations"; // using cluster SDNC URL
+ String odlUser = "admin";
+ String odlPassword = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
+ String sdncEndpoint = "SLI-API:execute-graph";
+
+ if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
+ SdncOdlConnection conn =
+ SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
+
+ conn.send("POST", "application/json", rpcMsgbody);
+ } else {
+ LOG.info("POST message body would be:\n {}", rpcMsgbody);
+ }
+ } catch (Exception e) {
+ LOG.error("Unable to process message", e);
+ }
+ }
+
+ private String buildAsyncNotifRPCMsgRoadm(JSONObject payloadString) throws IOException {
+ VelocityEngine velocityEngine = new VelocityEngine();
+
+ Properties props = new Properties();
+ props.put("file.resource.loader.path", PROPERTIES_PATH);
+ velocityEngine.init(props);
+
+ String sliParameters = "sli_parameters";
+
+ JSONArray sliParametersArray = new JSONArray();
+
+ VelocityContext context = new VelocityContext();
+
+ context.put("rpc_name", "handle-async-notif");
+ String requestId = payloadString.getString(SVC_REQUEST_ID);
+ String responseCode = payloadString.getString(RESPONSE_CODE);
+ String ackIndicator = payloadString.getString(ACK_FINAL_INDICATOR);
+
+ sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, REQUEST_ID).put(STRING_VALUE, requestId));
+ sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, RESPONSE_CODE).put(STRING_VALUE, responseCode));
+ sliParametersArray
+ .put(new JSONObject().put(PARAMETER_NAME, ACK_FINAL_INDICATOR).put(STRING_VALUE, ackIndicator));
+
+ context.put(sliParameters, sliParametersArray);
+
+ Writer writer = new StringWriter();
+ velocityEngine.mergeTemplate(TEMPLATE_NAME, UTF_8, context, writer);
+ writer.flush();
+
+ return writer.toString();
+
+ }
+
+}
diff --git a/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/SdncOdlConnection.java b/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/SdncOdlConnection.java
new file mode 100644
index 00000000..860e84bb
--- /dev/null
+++ b/optical-service/provider/src/main/java/org/onap/sdnc/northbound/asyncrequests/SdncOdlConnection.java
@@ -0,0 +1,150 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.sdnc.northbound.asyncrequests;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.Authenticator;
+import java.net.HttpURLConnection;
+import java.net.PasswordAuthentication;
+import java.net.URL;
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLSession;
+import org.apache.commons.codec.binary.Base64;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class SdncOdlConnection {
+
+ private static final Logger LOG = LoggerFactory
+ .getLogger(SdncOdlConnection.class);
+
+ private HttpURLConnection httpConn = null;
+
+ private String url = null;
+ private String user = null;
+ private String password = null;
+
+ private class SdncAuthenticator extends Authenticator {
+
+ private String user;
+ private String passwd;
+
+ SdncAuthenticator(String user, String passwd) {
+ this.user = user;
+ this.passwd = passwd;
+ }
+
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(user, passwd.toCharArray());
+ }
+ }
+
+ private SdncOdlConnection() {
+
+ }
+
+ private SdncOdlConnection(String url, String user, String password) {
+ this.url = url;
+ this.user = user;
+ this.password = password;
+
+ try {
+ URL sdncUrl = new URL(url);
+ Authenticator.setDefault(new SdncAuthenticator(user, password));
+
+ this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
+ } catch (Exception e) {
+ LOG.error("Unable to create http connection", e);
+ }
+ }
+
+ public static SdncOdlConnection newInstance(String url, String user, String password) {
+ return new SdncOdlConnection(url, user, password);
+ }
+
+
+ public String send(String method, String contentType, String msg) throws IOException {
+
+ LOG.info(String.format("Sending REST %s to %s", method, url));
+ LOG.info(String.format("Message body:%n%s", msg));
+ String authStr = user + ":" + password;
+ String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
+
+ httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
+
+ httpConn.setRequestMethod(method);
+ httpConn.setRequestProperty("Content-Type", contentType);
+ httpConn.setRequestProperty("Accept", contentType);
+
+ httpConn.setDoInput(true);
+ httpConn.setDoOutput(true);
+ httpConn.setUseCaches(false);
+
+ if (httpConn instanceof HttpsURLConnection) {
+ HostnameVerifier hostnameVerifier = new HostnameVerifier() {
+ @Override
+ public boolean verify(String hostname, SSLSession session) {
+ return true;
+ }
+ };
+ ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
+ }
+
+ // Write message
+ httpConn.setRequestProperty("Content-Length", Integer.toString(msg.length()));
+ DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
+ outStr.write(msg.getBytes());
+ outStr.close();
+
+ // Read response
+ BufferedReader respRdr;
+
+ LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
+
+ if (httpConn.getResponseCode() < 300) {
+
+ respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
+ } else {
+ respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
+ }
+
+ StringBuilder respBuff = new StringBuilder();
+
+ String respLn;
+
+ while ((respLn = respRdr.readLine()) != null) {
+ respBuff.append(respLn).append("\n");
+ }
+ respRdr.close();
+
+ String respString = respBuff.toString();
+
+ LOG.info(String.format("Response body :%n%s", respString));
+
+ return respString;
+ }
+}