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.regex.Matcher;
33 import java.util.regex.Pattern;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServlet;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import org.json.JSONObject;
39 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.DataTreeProviderImpl;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * @author Michael Dürre
49 public class DataTreeHttpServlet extends HttpServlet {
51 public enum FilterMode {
52 Strict, //show only filtered items and their parents
53 Lazy //show root items (and all their children) which have matches inside
59 private static final long serialVersionUID = 1L;
60 private final DataTreeProviderImpl dataTreeProvider;
61 private static final Logger LOG = LoggerFactory.getLogger(DataTreeHttpServlet.class);
63 public DataTreeHttpServlet() {
65 this.dataTreeProvider = new DataTreeProviderImpl();
71 public void setDatabaseClient(HtDatabaseClient client) {
72 this.dataTreeProvider.setDatabaseClient(client);
76 public static String readPayload(HttpServletRequest request) throws IOException {
79 StringBuilder stringBuilder = new StringBuilder();
80 BufferedReader bufferedReader = null;
83 InputStream inputStream = request.getInputStream();
84 if (inputStream != null) {
85 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
86 char[] charBuffer = new char[128];
88 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
89 stringBuilder.append(charBuffer, 0, bytesRead);
92 stringBuilder.append("");
94 } catch (IOException ex) {
97 if (bufferedReader != null) {
99 bufferedReader.close();
100 } catch (IOException ex) {
106 body = stringBuilder.toString();
111 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
112 final String uri = req.getRequestURI();
113 LOG.debug("GET request for {}", uri);
114 final EntityWithTree e = getEntity(uri);
116 LOG.info("GET request for {} to e={} with tree={}", uri, e.entity, e.tree);
118 case Inventoryequipment:
119 DataTreeObject o = this.dataTreeProvider.readInventoryTree(e.tree, null, FilterMode.Lazy);
120 this.doJsonResponse(resp, o);
123 this.notAvailble(resp);
127 LOG.debug("unable to find entity for uri {}", uri);
132 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
133 final String uri = req.getRequestURI();
134 String filter = null;
135 FilterMode mode = FilterMode.Lazy;
137 final String body = readPayload(req);
138 JSONObject data = new JSONObject(body);
139 if (data.has("query")) {
140 filter = data.getString("query");
142 if (data.has("mode")) {
143 mode = data.getString("mode").equals("lazy") ? FilterMode.Lazy : FilterMode.Strict;
147 } catch (Exception e) {
148 LOG.warn("problem reading payload: {}", e);
150 LOG.debug("POST request for {}", uri);
151 final EntityWithTree e = getEntity(uri);
154 case Inventoryequipment:
155 DataTreeObject o = this.dataTreeProvider.readInventoryTree(e.tree, filter, mode);
156 this.doJsonResponse(resp, o);
159 this.notAvailble(resp);
168 private void notAvailble(HttpServletResponse resp) {
170 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
171 } catch (IOException e) {
176 public static EntityWithTree getEntity(String uri) {
177 final String regex = "^\\/tree\\/read-(.*)-tree\\/?(.*)$";
178 final Pattern pattern = Pattern.compile(regex);
179 final Matcher matcher = pattern.matcher(uri);
181 if (matcher.find() && matcher.groupCount() > 0) {
183 e = Entity.forName(matcher.group(1)).get();
184 return new EntityWithTree(e, matcher.groupCount() > 1 ? matcher.group(2) : null);
185 } catch (Exception e2) {
186 LOG.warn("unable to parse {} into entity: {}", matcher.group(2), e2);
193 private void doJsonResponse(HttpServletResponse resp, DataTreeObject data) {
194 resp.setHeader("Content-Type", "application/json");
196 resp.getWriter().write(data.toJSON());
197 } catch (IOException e) {
198 LOG.warn("problem sending response: {}", e);
202 public static class EntityWithTree {
203 public final Entity entity;
204 public final List<String> tree;
207 public String toString() {
208 return "EntityWithTree [entity=" + entity + ", tree=" + tree + "]";
211 public EntityWithTree(Entity e, String tree) {
214 if (tree.startsWith("/")) {
215 tree = tree.substring(1);
217 if (tree.endsWith("/")) {
218 tree = tree.substring(0, tree.length() - 1);
220 String[] tmp = tree.split("\\/");
221 this.tree = new ArrayList<>();
222 for (int i = 0; i < tmp.length; i++) {
224 String s = URLDecoder.decode(tmp[i], "utf-8");
225 if (s != null && s.length() > 0) {
228 } catch (UnsupportedEncodingException e1) {
229 LOG.warn("problem urldecode {}: {}", tmp[i], e);