1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.provisioning;
\r
27 import java.io.IOException;
\r
28 import java.util.Set;
\r
30 import javax.servlet.http.HttpServletRequest;
\r
31 import javax.servlet.http.HttpServletResponse;
\r
33 import org.json.JSONObject;
\r
35 import com.att.research.datarouter.provisioning.beans.Deleteable;
\r
36 import com.att.research.datarouter.provisioning.beans.EgressRoute;
\r
37 import com.att.research.datarouter.provisioning.beans.EventLogRecord;
\r
38 import com.att.research.datarouter.provisioning.beans.IngressRoute;
\r
39 import com.att.research.datarouter.provisioning.beans.Insertable;
\r
40 import com.att.research.datarouter.provisioning.beans.NetworkRoute;
\r
41 import com.att.research.datarouter.provisioning.beans.NodeClass;
\r
45 * This servlet handles requests to URLs under /internal/route/ on the provisioning server.
\r
46 * This part of the URL tree is used to manipulate the Data Router routing tables.
\r
49 * <div class="contentContainer">
\r
50 * <table class="packageSummary" border="0" cellpadding="3" cellspacing="0">
\r
51 * <caption><span>URL Path Summary</span><span class="tabEnd"> </span></caption>
\r
53 * <th class="colFirst" width="35%">URL Path</th>
\r
54 * <th class="colOne">Method</th>
\r
55 * <th class="colLast">Purpose</th>
\r
57 * <tr class="altColor">
\r
58 * <td class="colFirst">/internal/route/</td>
\r
59 * <td class="colOne">GET</td>
\r
60 * <td class="colLast">used to GET a full JSON copy of all three routing tables.</td>
\r
62 * <tr class="rowColor">
\r
63 * <td class="colFirst" rowspan="2">/internal/route/ingress/</td>
\r
64 * <td class="colOne">GET</td>
\r
65 * <td class="colLast">used to GET a full JSON copy of the ingress routing table (IRT).</td>
\r
67 * <tr class="rowColor">
\r
68 * <td class="colOne">POST</td>
\r
69 * <td class="colLast">used to create a new entry in the ingress routing table (IRT).</td></tr>
\r
70 * <tr class="altColor">
\r
71 * <td class="colFirst" rowspan="2">/internal/route/egress/</td>
\r
72 * <td class="colOne">GET</td>
\r
73 * <td class="colLast">used to GET a full JSON copy of the egress routing table (ERT).</td>
\r
75 * <tr class="altColor">
\r
76 * <td class="colOne">POST</td>
\r
77 * <td class="colLast">used to create a new entry in the egress routing table (ERT).</td></tr>
\r
78 * <tr class="rowColor">
\r
79 * <td class="colFirst" rowspan="2">/internal/route/network/</td>
\r
80 * <td class="colOne">GET</td>
\r
81 * <td class="colLast">used to GET a full JSON copy of the network routing table (NRT).</td>
\r
83 * <tr class="rowColor">
\r
84 * <td class="colOne">POST</td>
\r
85 * <td class="colLast">used to create a new entry in the network routing table (NRT).</td>
\r
87 * <tr class="altColor">
\r
88 * <td class="colFirst">/internal/route/ingress/<feed>/<user>/<subnet></td>
\r
89 * <td class="colOne">DELETE</td>
\r
90 * <td class="colLast">used to DELETE the ingress route corresponding to <i>feed</i>, <i>user</i> and <i>subnet</i>.
\r
91 * The / in the subnet specified should be replaced with a !, since / cannot be used in a URL.</td>
\r
93 * <tr class="rowColor">
\r
94 * <td class="colFirst">/internal/route/ingress/<seq></td>
\r
95 * <td class="colOne">DELETE</td>
\r
96 * <td class="colLast">used to DELETE all ingress routes with the matching <i>seq</i> sequence number.</td>
\r
98 * <tr class="altColor">
\r
99 * <td class="colFirst">/internal/route/egress/<sub></td>
\r
100 * <td class="colOne">DELETE</td>
\r
101 * <td class="colLast">used to DELETE the egress route the matching <i>sub</i> subscriber number.</td>
\r
103 * <tr class="rowColor">
\r
104 * <td class="colFirst">/internal/route/network/<fromnode>/<tonode></td>
\r
105 * <td class="colOne">DELETE</td>
\r
106 * <td class="colLast">used to DELETE the network route corresponding to <i>fromnode</i>
\r
107 * and <i>tonode</i>.</td>
\r
111 * Authorization to use these URLs is a little different than for other URLs on the provisioning server.
\r
112 * For the most part, the IP address that the request comes from should be either:
\r
115 * <li>an IP address of a provisioning server, or</li>
\r
116 * <li>the IP address of a node, or</li>
\r
117 * <li>an IP address from the "<i>special subnet</i>" which is configured with
\r
118 * the PROV_SPECIAL_SUBNET parameter.
\r
121 * All DELETE/GET/POST requests made to this servlet on the standby server are proxied to the
\r
122 * active server (using the {@link ProxyServlet}) if it is up and reachable.
\r
125 * @author Robert Eby
\r
128 @SuppressWarnings("serial")
\r
129 public class RouteServlet extends ProxyServlet {
\r
131 * DELETE route table entries by deleting part of the route table tree.
\r
134 public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
135 EventLogRecord elr = new EventLogRecord(req);
\r
136 if (!isAuthorizedForInternal(req)) {
\r
137 elr.setMessage("Unauthorized.");
\r
138 elr.setResult(HttpServletResponse.SC_FORBIDDEN);
\r
139 eventlogger.info(elr);
\r
140 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
\r
143 if (isProxyOK(req) && isProxyServer()) {
\r
144 super.doDelete(req, resp);
\r
148 String path = req.getPathInfo();
\r
149 String[] parts = path.substring(1).split("/");
\r
150 Deleteable[] d = null;
\r
151 if (parts[0].equals("ingress")) {
\r
152 if (parts.length == 4) {
\r
153 // /internal/route/ingress/<feed>/<user>/<subnet>
\r
155 int feedid = Integer.parseInt(parts[1]);
\r
156 IngressRoute er = IngressRoute.getIngressRoute(feedid, parts[2], parts[3].replaceAll("!", "/"));
\r
158 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "The specified ingress route does not exist.");
\r
161 d = new Deleteable[] { er };
\r
162 } catch (NumberFormatException e) {
\r
163 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid feed ID in 'delete ingress' command.");
\r
166 } else if (parts.length == 2) {
\r
167 // /internal/route/ingress/<seq>
\r
169 int seq = Integer.parseInt(parts[1]);
\r
170 Set<IngressRoute> set = IngressRoute.getIngressRoutesForSeq(seq);
\r
171 d = set.toArray(new Deleteable[0]);
\r
172 } catch (NumberFormatException e) {
\r
173 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid sequence number in 'delete ingress' command.");
\r
177 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete ingress' command.");
\r
180 } else if (parts[0].equals("egress")) {
\r
181 if (parts.length == 2) {
\r
182 // /internal/route/egress/<sub>
\r
184 int subid = Integer.parseInt(parts[1]);
\r
185 EgressRoute er = EgressRoute.getEgressRoute(subid);
\r
187 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "The specified egress route does not exist.");
\r
190 d = new Deleteable[] { er };
\r
191 } catch (NumberFormatException e) {
\r
192 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid sub ID in 'delete egress' command.");
\r
196 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete egress' command.");
\r
199 } else if (parts[0].equals("network")) {
\r
200 if (parts.length == 3) {
\r
201 // /internal/route/network/<from>/<to>
\r
203 NetworkRoute nr = new NetworkRoute(
\r
204 NodeClass.normalizeNodename(parts[1]),
\r
205 NodeClass.normalizeNodename(parts[2])
\r
207 d = new Deleteable[] { nr };
\r
208 } catch (IllegalArgumentException e) {
\r
209 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "The specified network route does not exist.");
\r
213 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid number of arguments in 'delete network' command.");
\r
218 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
\r
222 for (Deleteable dd : d) {
\r
223 rv &= doDelete(dd);
\r
226 elr.setResult(HttpServletResponse.SC_OK);
\r
227 eventlogger.info(elr);
\r
228 resp.setStatus(HttpServletResponse.SC_OK);
\r
229 provisioningDataChanged();
\r
230 provisioningParametersChanged();
\r
232 // Something went wrong with the DELETE
\r
233 elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
\r
234 eventlogger.info(elr);
\r
235 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
\r
239 * GET route table entries from the route table tree specified by the URL path.
\r
242 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
243 EventLogRecord elr = new EventLogRecord(req);
\r
244 if (!isAuthorizedForInternal(req)) {
\r
245 elr.setMessage("Unauthorized.");
\r
246 elr.setResult(HttpServletResponse.SC_FORBIDDEN);
\r
247 eventlogger.info(elr);
\r
248 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
\r
251 if (isProxyOK(req) && isProxyServer()) {
\r
252 super.doGet(req, resp);
\r
256 String path = req.getPathInfo();
\r
257 if (!path.endsWith("/"))
\r
259 if (!path.equals("/") && !path.equals("/ingress/") && !path.equals("/egress/") && !path.equals("/network/")) {
\r
260 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
\r
264 StringBuilder sb = new StringBuilder("{\n");
\r
266 if (path.equals("/") || path.equals("/ingress/")) {
\r
268 sb.append("\"ingress\": [");
\r
269 for (IngressRoute in : IngressRoute.getAllIngressRoutes()) {
\r
271 sb.append(in.asJSONObject().toString());
\r
278 if (path.equals("/") || path.equals("/egress/")) {
\r
281 sb.append("\"egress\": {");
\r
282 for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) {
\r
283 JSONObject jx = eg.asJSONObject();
\r
284 for (String key : jx.keySet()) {
\r
286 sb.append(" \"").append(key).append("\": ");
\r
287 sb.append("\"").append(jx.getString(key)).append("\"");
\r
295 if (path.equals("/") || path.equals("/network/")) {
\r
298 sb.append("\"routing\": [");
\r
299 for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) {
\r
301 sb.append(ne.asJSONObject().toString());
\r
307 resp.setStatus(HttpServletResponse.SC_OK);
\r
308 resp.setContentType("application/json");
\r
309 resp.getOutputStream().print(sb.toString());
\r
312 * PUT on </internal/route/*> -- not supported.
\r
315 public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
316 EventLogRecord elr = new EventLogRecord(req);
\r
317 if (!isAuthorizedForInternal(req)) {
\r
318 elr.setMessage("Unauthorized.");
\r
319 elr.setResult(HttpServletResponse.SC_FORBIDDEN);
\r
320 eventlogger.info(elr);
\r
321 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
\r
324 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
\r
327 * POST - modify existing route table entries in the route table tree specified by the URL path.
\r
330 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
331 EventLogRecord elr = new EventLogRecord(req);
\r
332 if (!isAuthorizedForInternal(req)) {
\r
333 elr.setMessage("Unauthorized.");
\r
334 elr.setResult(HttpServletResponse.SC_FORBIDDEN);
\r
335 eventlogger.info(elr);
\r
336 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
\r
339 if (isProxyOK(req) && isProxyServer()) {
\r
340 super.doPost(req, resp);
\r
343 String path = req.getPathInfo();
\r
344 Insertable[] ins = null;
\r
345 if (path.startsWith("/ingress/")) {
\r
346 // /internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s
\r
348 // Although it probably doesn't make sense, you can install two identical routes in the IRT
\r
349 int feedid = Integer.parseInt(req.getParameter("feed"));
\r
350 String user = req.getParameter("user");
\r
353 String subnet = req.getParameter("subnet");
\r
354 if (subnet == null)
\r
356 String nodepatt = req.getParameter("nodepatt");
\r
357 String t = req.getParameter("seq");
\r
358 int seq = (t != null) ? Integer.parseInt(t) : (IngressRoute.getMaxSequence() + 100);
\r
359 ins = new Insertable[] { new IngressRoute(seq, feedid, user, subnet, NodeClass.lookupNodeNames(nodepatt)) };
\r
360 } catch (Exception e) {
\r
362 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add ingress' command.");
\r
365 } else if (path.startsWith("/egress/")) {
\r
366 // /internal/route/egress/?sub=%s&node=%s
\r
368 int subid = Integer.parseInt(req.getParameter("sub"));
\r
369 EgressRoute er = EgressRoute.getEgressRoute(subid);
\r
371 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "An egress route already exists for that subscriber.");
\r
374 String node = NodeClass.normalizeNodename(req.getParameter("node"));
\r
375 ins = new Insertable[] { new EgressRoute(subid, node) };
\r
376 } catch (Exception e) {
\r
378 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add egress' command.");
\r
381 } else if (path.startsWith("/network/")) {
\r
382 // /internal/route/network/?from=%s&to=%s&via=%s
\r
384 String nfrom = req.getParameter("from");
\r
385 String nto = req.getParameter("to");
\r
386 String nvia = req.getParameter("via");
\r
387 if (nfrom == null || nto == null || nvia == null) {
\r
388 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing arguments in 'add network' command.");
\r
391 nfrom = NodeClass.normalizeNodename(nfrom);
\r
392 nto = NodeClass.normalizeNodename(nto);
\r
393 nvia = NodeClass.normalizeNodename(nvia);
\r
394 NetworkRoute nr = new NetworkRoute(nfrom, nto, nvia);
\r
395 for (NetworkRoute route : NetworkRoute.getAllNetworkRoutes()) {
\r
396 if (route.getFromnode() == nr.getFromnode() && route.getTonode() == nr.getTonode()) {
\r
397 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Network route table already contains a route for "+nfrom+" and "+nto);
\r
401 ins = new Insertable[] { nr };
\r
402 } catch (IllegalArgumentException e) {
\r
404 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments in 'add network' command.");
\r
409 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
\r
413 for (Insertable dd : ins) {
\r
414 rv &= doInsert(dd);
\r
417 elr.setResult(HttpServletResponse.SC_OK);
\r
418 eventlogger.info(elr);
\r
419 resp.setStatus(HttpServletResponse.SC_OK);
\r
420 provisioningDataChanged();
\r
421 provisioningParametersChanged();
\r
423 // Something went wrong with the INSERT
\r
424 elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
\r
425 eventlogger.info(elr);
\r
426 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
\r