2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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.ccsdk.features.sdnr.wt.dataprovider.http;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.UnsupportedEncodingException;
29 import java.net.URLDecoder;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 import javax.servlet.ServletException;
36 import javax.servlet.http.HttpServlet;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import org.json.JSONObject;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.InventoryTreeProvider;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.DataTreeObject;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * @author Michael Dürre
51 //@HttpWhiteboardServletPattern("/tree/*")
52 //@HttpWhiteboardServletName("DataTreeHttpServlet")
53 //@Component(service = Servlet.class)
54 public class DataTreeHttpServlet extends HttpServlet {
56 private static final long serialVersionUID = 1L;
57 public static final String URI_PRE = "/tree";
58 private InventoryTreeProvider dataTreeProvider;
59 private static final Logger LOG = LoggerFactory.getLogger(DataTreeHttpServlet.class);
61 public DataTreeHttpServlet() {
65 public void setInventoryTreeProvider(InventoryTreeProvider provider) {
66 this.dataTreeProvider = provider;
70 public static String readPayload(HttpServletRequest request) throws IOException {
73 StringBuilder stringBuilder = new StringBuilder();
74 BufferedReader bufferedReader = null;
75 IOException toThrow = null;
76 try (InputStream inputStream = request.getInputStream()) {
78 if (inputStream != null) {
79 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
80 char[] charBuffer = new char[128];
82 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
83 stringBuilder.append(charBuffer, 0, bytesRead);
86 stringBuilder.append("");
88 } catch (IOException ex) {
91 if (bufferedReader != null) {
93 bufferedReader.close();
94 } catch (IOException ex) {
95 LOG.debug("problem closing reader:", ex);
100 if (toThrow != null) {
103 body = stringBuilder.toString();
108 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
109 final String uri = req.getRequestURI();
110 LOG.debug("GET request for {}", uri);
111 final EntityWithTree e = getEntity(uri);
113 LOG.debug("GET request for {} to e={} with tree={}", uri, e.entity, e.tree);
115 case Inventoryequipment:
116 DataTreeObject o = this.dataTreeProvider.readInventoryTree(e.tree, null);
117 this.doJsonResponse(resp, o);
120 this.notAvailble(resp);
124 LOG.debug("unable to find entity for uri {}", uri);
129 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
130 final String uri = req.getRequestURI();
131 String filter = null;
133 final String body = readPayload(req);
134 JSONObject data = new JSONObject(body);
135 if (data.has("query")) {
136 filter = data.getString("query");
139 } catch (Exception e) {
140 LOG.warn("problem reading payload: {}", e);
142 LOG.debug("POST request for {}", uri);
143 final EntityWithTree e = getEntity(uri);
146 case Inventoryequipment:
147 DataTreeObject o = this.dataTreeProvider.readInventoryTree(e.tree, filter);
148 this.doJsonResponse(resp, o);
151 this.notAvailble(resp);
160 private void notAvailble(HttpServletResponse resp) {
162 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
163 } catch (IOException e) {
168 public static EntityWithTree getEntity(String uri) {
169 final String regex = "^\\/tree\\/read-(.*)-tree\\/?(.*)$";
170 final Pattern pattern = Pattern.compile(regex);
171 final Matcher matcher = pattern.matcher(uri);
172 if (matcher.find() && matcher.groupCount() > 0) {
174 Optional<Entity> oe = Optional.ofNullable(Entity.forName(matcher.group(1)));
175 if (oe.isPresent()) {
176 return new EntityWithTree(oe.get(), matcher.groupCount() > 1 ? matcher.group(2) : null);
178 LOG.warn("unable to find entity for name {}", matcher.group(1));
180 } catch (Exception e2) {
181 LOG.warn("unable to parse {} into entity: ", matcher.group(2), e2);
188 private void doJsonResponse(HttpServletResponse resp, DataTreeObject data) {
189 resp.setHeader("Content-Type", "application/json");
191 resp.getWriter().write(data.toJSON());
192 } catch (IOException e) {
193 LOG.warn("problem sending response: ", e);
197 public static class EntityWithTree {
198 public final Entity entity;
199 public final List<String> tree;
202 public String toString() {
203 return "EntityWithTree [entity=" + entity + ", tree=" + tree + "]";
208 * @param e database enttity to access
209 * @param tree tree description
210 * e.g. nodeA => tree entry for node-id=nodeA
211 * nodeA/key0 => tree entry for node-id=nodeA and uuid=key0 and tree-level=0
212 * nodeA/key0/key1 => tree entry for node-id=nodeA and uuid=key1 and tree-level=1
215 public EntityWithTree(Entity e, String tree) {
218 if (tree.startsWith("/")) {
219 tree = tree.substring(1);
221 if (tree.endsWith("/")) {
222 tree = tree.substring(0, tree.length() - 1);
224 String[] tmp = tree.split("\\/");
225 this.tree = new ArrayList<>();
226 for (int i = 0; i < tmp.length; i++) {
228 String s = URLDecoder.decode(tmp[i], "utf-8");
229 if (s != null && s.length() > 0) {
232 } catch (UnsupportedEncodingException e1) {
233 LOG.warn("problem urldecode {}: {}", tmp[i], e);