optical-service package added for service create
[sdnc/northbound.git] / optical-service / provider / src / main / java / org / onap / sdnc / northbound / asyncrequests / AsyncNotificationHandling.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
6  *                             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.onap.sdnc.northbound.asyncrequests;
23
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;
30
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;
36
37 import org.apache.velocity.VelocityContext;
38 import org.apache.velocity.app.VelocityEngine;
39 import org.apache.velocity.Template;
40
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;
46
47 @Component(immediate = true, property = {"alias=/asyncNotification", "servlet-name=AsyncNotification"})
48 public class AsyncNotificationHandling extends HttpServlet implements Servlet {
49
50     private static final long serialVersionUID = 1L;
51
52     private final Logger LOG = LoggerFactory.getLogger(AsyncNotificationHandling.class);
53
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";
64
65     /**
66      * Handles async request for different domain controllers
67      */
68
69     @Override
70     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
71
72         LOG.info("Reached async servlet");
73         BufferedReader requestInput = null;
74         try {
75             requestInput = request.getReader();
76         } catch (Exception e) {
77             LOG.error("Unable to read input from request", e);
78         }
79
80         if (requestInput == null) {
81             return;
82         }
83         String json = requestInput.lines().collect(Collectors.joining());
84         LOG.info("Async Request payload {}", json);
85         if (json == null) {
86             return;
87         }
88
89         JSONObject jsonObj = new JSONObject(json);
90         JSONObject input = jsonObj.getJSONObject("input");
91
92         if (input.has(CONFIGURATION_RESPONSE)) {
93             JSONObject payloadString = null;
94
95             try {
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);
102             }
103         } else {
104             LOG.info("Async request received for wrong domain");
105         }
106
107     }
108
109     private void invokeRPC(String rpcMsgbody) {
110         try {
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";
115
116             if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
117                 SdncOdlConnection conn =
118                         SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
119
120                 conn.send("POST", "application/json", rpcMsgbody);
121             } else {
122                 LOG.info("POST message body would be:\n {}", rpcMsgbody);
123             }
124         } catch (Exception e) {
125             LOG.error("Unable to process message", e);
126         }
127     }
128
129     private String buildAsyncNotifRPCMsgRoadm(JSONObject payloadString) throws IOException {
130         VelocityEngine velocityEngine = new VelocityEngine();
131
132         Properties props = new Properties();
133         props.put("file.resource.loader.path", PROPERTIES_PATH);
134         velocityEngine.init(props);
135
136         String sliParameters = "sli_parameters";
137
138         JSONArray sliParametersArray = new JSONArray();
139
140         VelocityContext context = new VelocityContext();
141
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);
146
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));
149         sliParametersArray
150                 .put(new JSONObject().put(PARAMETER_NAME, ACK_FINAL_INDICATOR).put(STRING_VALUE, ackIndicator));
151
152         context.put(sliParameters, sliParametersArray);
153
154         Writer writer = new StringWriter();
155         velocityEngine.mergeTemplate(TEMPLATE_NAME, UTF_8, context, writer);
156         writer.flush();
157
158         return writer.toString();
159
160     }
161
162 }