1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
28 import java.util.HashSet;
29 import java.util.Hashtable;
30 import java.util.Vector;
33 * Processed configuration for this node.
35 * The NodeConfig represents a processed configuration from the Data Router provisioning server. Each time
36 * configuration data is received from the provisioning server, a new NodeConfig is created and the previous one
39 public class NodeConfig {
42 * Raw configuration entry for a data router node
44 public static class ProvNode {
49 * Construct a node configuration entry.
51 * @param cname The cname of the node.
53 public ProvNode(String cname) {
58 * Get the cname of the node
60 public String getCName() {
66 * Raw configuration entry for a provisioning parameter
68 public static class ProvParam {
74 * Construct a provisioning parameter configuration entry.
76 * @param name The name of the parameter.
77 * @param value The value of the parameter.
79 public ProvParam(String name, String value) {
85 * Get the name of the parameter.
87 public String getName() {
92 * Get the value of the parameter.
94 public String getValue() {
100 * Raw configuration entry for a data feed.
102 public static class ProvFeed {
105 private String logdata;
106 private String status;
109 * Construct a feed configuration entry.
111 * @param id The feed ID of the entry.
112 * @param logdata String for log entries about the entry.
113 * @param status The reason why this feed cannot be used (Feed has been deleted, Feed has been suspended) or
114 * null if it is valid.
116 public ProvFeed(String id, String logdata, String status) {
118 this.logdata = logdata;
119 this.status = status;
123 * Get the feed id of the data feed.
125 public String getId() {
130 * Get the log data of the data feed.
132 public String getLogData() {
137 * Get the status of the data feed.
139 public String getStatus() {
145 * Raw configuration entry for a feed user.
147 public static class ProvFeedUser {
149 private String feedid;
151 private String credentials;
154 * Construct a feed user configuration entry
156 * @param feedid The feed id.
157 * @param user The user that will publish to the feed.
158 * @param credentials The Authorization header the user will use to publish.
160 public ProvFeedUser(String feedid, String user, String credentials) {
161 this.feedid = feedid;
163 this.credentials = credentials;
167 * Get the feed id of the feed user.
169 public String getFeedId() {
174 * Get the user for the feed user.
176 public String getUser() {
181 * Get the credentials for the feed user.
183 public String getCredentials() {
184 return (credentials);
189 * Raw configuration entry for a feed subnet
191 public static class ProvFeedSubnet {
193 private String feedid;
197 * Construct a feed subnet configuration entry
199 * @param feedid The feed ID
200 * @param cidr The CIDR allowed to publish to the feed.
202 public ProvFeedSubnet(String feedid, String cidr) {
203 this.feedid = feedid;
208 * Get the feed id of the feed subnet.
210 public String getFeedId() {
215 * Get the CIDR of the feed subnet.
217 public String getCidr() {
223 * Raw configuration entry for a subscription
225 public static class ProvSubscription {
227 private String subid;
228 private String feedid;
230 private String authuser;
231 private String credentials;
232 private boolean metaonly;
233 private boolean use100;
236 * Construct a subscription configuration entry
238 * @param subid The subscription ID
239 * @param feedid The feed ID
240 * @param url The base delivery URL (not including the fileid)
241 * @param authuser The user in the credentials used to deliver
242 * @param credentials The credentials used to authenticate to the delivery URL exactly as they go in the
243 * Authorization header.
244 * @param metaonly Is this a meta data only subscription?
245 * @param use100 Should we send Expect: 100-continue?
247 public ProvSubscription(String subid, String feedid, String url, String authuser, String credentials,
248 boolean metaonly, boolean use100) {
250 this.feedid = feedid;
252 this.authuser = authuser;
253 this.credentials = credentials;
254 this.metaonly = metaonly;
255 this.use100 = use100;
259 * Get the subscription ID
261 public String getSubId() {
268 public String getFeedId() {
273 * Get the delivery URL
275 public String getURL() {
282 public String getAuthUser() {
287 * Get the delivery credentials
289 public String getCredentials() {
290 return (credentials);
294 * Is this a meta data only subscription?
296 public boolean isMetaDataOnly() {
301 * Should we send Expect: 100-continue?
303 public boolean isUsing100() {
309 * Raw configuration entry for controlled ingress to the data router node
311 public static class ProvForceIngress {
313 private String feedid;
314 private String subnet;
316 private String[] nodes;
319 * Construct a forced ingress configuration entry
321 * @param feedid The feed ID that this entry applies to
322 * @param subnet The CIDR for which publisher IP addresses this entry applies to or "" if it applies to all
323 * publisher IP addresses
324 * @param user The publishing user this entry applies to or "" if it applies to all publishing users.
325 * @param nodes The array of FQDNs of the data router nodes to redirect publication attempts to.
327 public ProvForceIngress(String feedid, String subnet, String user, String[] nodes) {
328 this.feedid = feedid;
329 this.subnet = subnet;
337 public String getFeedId() {
344 public String getSubnet() {
351 public String getUser() {
358 public String[] getNodes() {
364 * Raw configuration entry for controlled egress from the data router
366 public static class ProvForceEgress {
368 private String subid;
372 * Construct a forced egress configuration entry
374 * @param subid The subscription ID the subscription with forced egress
375 * @param node The node handling deliveries for this subscription
377 public ProvForceEgress(String subid, String node) {
383 * Get the subscription ID
385 public String getSubId() {
392 public String getNode() {
398 * Raw configuration entry for routing within the data router network
400 public static class ProvHop {
407 * A human readable description of this entry
409 public String toString() {
410 return ("Hop " + from + "->" + to + " via " + via);
414 * Construct a hop entry
416 * @param from The FQDN of the node with the data to be delivered
417 * @param to The FQDN of the node that will deliver to the subscriber
418 * @param via The FQDN of the node where the from node should send the data
420 public ProvHop(String from, String to, String via) {
429 public String getFrom() {
436 public String getTo() {
441 * Get the next intermediate node
443 public String getVia() {
448 private static class Redirection {
455 private static class Feed {
459 SubnetMatcher[] subnets;
460 Hashtable<String, String> authusers = new Hashtable<String, String>();
461 Redirection[] redirections;
465 private Hashtable<String, String> params = new Hashtable<String, String>();
466 private Hashtable<String, Feed> feeds = new Hashtable<String, Feed>();
467 private Hashtable<String, DestInfo> nodeinfo = new Hashtable<String, DestInfo>();
468 private Hashtable<String, DestInfo> subinfo = new Hashtable<String, DestInfo>();
469 private Hashtable<String, IsFrom> nodes = new Hashtable<String, IsFrom>();
470 private String myname;
471 private String myauth;
472 private DestInfo[] alldests;
476 * Process the raw provisioning data to configure this node
478 * @param pd The parsed provisioning data
479 * @param myname My name as seen by external systems
480 * @param spooldir The directory where temporary files live
481 * @param port The port number for URLs
482 * @param nodeauthkey The keying string used to generate node authentication credentials
484 public NodeConfig(ProvData pd, String myname, String spooldir, int port, String nodeauthkey) {
485 this.myname = myname;
486 for (ProvParam p : pd.getParams()) {
487 params.put(p.getName(), p.getValue());
489 Vector<DestInfo> div = new Vector<DestInfo>();
490 myauth = NodeUtils.getNodeAuthHdr(myname, nodeauthkey);
491 for (ProvNode pn : pd.getNodes()) {
492 String cn = pn.getCName();
493 if (nodeinfo.get(cn) != null) {
496 String auth = NodeUtils.getNodeAuthHdr(cn, nodeauthkey);
497 DestInfo di = new DestInfo("n:" + cn, spooldir + "/n/" + cn, null, "n2n-" + cn,
498 "https://" + cn + ":" + port + "/internal/publish", cn, myauth, false, true);
499 (new File(di.getSpool())).mkdirs();
501 nodeinfo.put(cn, di);
502 nodes.put(auth, new IsFrom(cn));
504 PathFinder pf = new PathFinder(myname, nodeinfo.keySet().toArray(new String[nodeinfo.size()]), pd.getHops());
505 Hashtable<String, Vector<Redirection>> rdtab = new Hashtable<String, Vector<Redirection>>();
506 for (ProvForceIngress pfi : pd.getForceIngress()) {
507 Vector<Redirection> v = rdtab.get(pfi.getFeedId());
509 v = new Vector<Redirection>();
510 rdtab.put(pfi.getFeedId(), v);
512 Redirection r = new Redirection();
513 if (pfi.getSubnet() != null) {
514 r.snm = new SubnetMatcher(pfi.getSubnet());
516 r.user = pfi.getUser();
517 r.nodes = pfi.getNodes();
520 Hashtable<String, Hashtable<String, String>> pfutab = new Hashtable<String, Hashtable<String, String>>();
521 for (ProvFeedUser pfu : pd.getFeedUsers()) {
522 Hashtable<String, String> t = pfutab.get(pfu.getFeedId());
524 t = new Hashtable<String, String>();
525 pfutab.put(pfu.getFeedId(), t);
527 t.put(pfu.getCredentials(), pfu.getUser());
529 Hashtable<String, String> egrtab = new Hashtable<String, String>();
530 for (ProvForceEgress pfe : pd.getForceEgress()) {
531 if (pfe.getNode().equals(myname) || nodeinfo.get(pfe.getNode()) == null) {
534 egrtab.put(pfe.getSubId(), pfe.getNode());
536 Hashtable<String, Vector<SubnetMatcher>> pfstab = new Hashtable<String, Vector<SubnetMatcher>>();
537 for (ProvFeedSubnet pfs : pd.getFeedSubnets()) {
538 Vector<SubnetMatcher> v = pfstab.get(pfs.getFeedId());
540 v = new Vector<SubnetMatcher>();
541 pfstab.put(pfs.getFeedId(), v);
543 v.add(new SubnetMatcher(pfs.getCidr()));
545 Hashtable<String, StringBuffer> ttab = new Hashtable<String, StringBuffer>();
546 HashSet<String> allfeeds = new HashSet<String>();
547 for (ProvFeed pfx : pd.getFeeds()) {
548 if (pfx.getStatus() == null) {
549 allfeeds.add(pfx.getId());
552 for (ProvSubscription ps : pd.getSubscriptions()) {
553 String sid = ps.getSubId();
554 String fid = ps.getFeedId();
555 if (!allfeeds.contains(fid)) {
558 if (subinfo.get(sid) != null) {
563 sididx = Integer.parseInt(sid);
564 sididx -= sididx % 100;
565 } catch (Exception e) {
567 String siddir = sididx + "/" + sid;
568 DestInfo di = new DestInfo("s:" + sid, spooldir + "/s/" + siddir, sid, fid, ps.getURL(), ps.getAuthUser(),
569 ps.getCredentials(), ps.isMetaDataOnly(), ps.isUsing100());
570 (new File(di.getSpool())).mkdirs();
572 subinfo.put(sid, di);
573 String egr = egrtab.get(sid);
575 sid = pf.getPath(egr) + sid;
577 StringBuffer sb = ttab.get(fid);
579 sb = new StringBuffer();
582 sb.append(' ').append(sid);
584 alldests = div.toArray(new DestInfo[div.size()]);
585 for (ProvFeed pfx : pd.getFeeds()) {
586 String fid = pfx.getId();
587 Feed f = feeds.get(fid);
593 f.loginfo = pfx.getLogData();
594 f.status = pfx.getStatus();
595 Vector<SubnetMatcher> v1 = pfstab.get(fid);
597 f.subnets = new SubnetMatcher[0];
599 f.subnets = v1.toArray(new SubnetMatcher[v1.size()]);
601 Hashtable<String, String> h1 = pfutab.get(fid);
603 h1 = new Hashtable<String, String>();
606 Vector<Redirection> v2 = rdtab.get(fid);
608 f.redirections = new Redirection[0];
610 f.redirections = v2.toArray(new Redirection[v2.size()]);
612 StringBuffer sb = ttab.get(fid);
614 f.targets = new Target[0];
616 f.targets = parseRouting(sb.toString());
622 * Parse a target string into an array of targets
624 * @param routing Target string
625 * @return Array of targets.
627 public Target[] parseRouting(String routing) {
628 routing = routing.trim();
629 if ("".equals(routing)) {
630 return (new Target[0]);
632 String[] xx = routing.split("\\s+");
633 Hashtable<String, Target> tmap = new Hashtable<String, Target>();
634 HashSet<String> subset = new HashSet<String>();
635 Vector<Target> tv = new Vector<Target>();
636 Target[] ret = new Target[xx.length];
637 for (int i = 0; i < xx.length; i++) {
639 int j = t.indexOf('/');
641 DestInfo di = subinfo.get(t);
643 tv.add(new Target(null, t));
645 if (!subset.contains(t)) {
647 tv.add(new Target(di, null));
651 String node = t.substring(0, j);
652 String rtg = t.substring(j + 1);
653 DestInfo di = nodeinfo.get(node);
655 tv.add(new Target(null, t));
657 Target tt = tmap.get(node);
659 tt = new Target(di, rtg);
668 return (tv.toArray(new Target[tv.size()]));
672 * Check whether this is a valid node-to-node transfer
674 * @param credentials Credentials offered by the supposed node
675 * @param ip IP address the request came from
677 public boolean isAnotherNode(String credentials, String ip) {
678 IsFrom n = nodes.get(credentials);
679 return (n != null && n.isFrom(ip));
683 * Check whether publication is allowed.
685 * @param feedid The ID of the feed being requested.
686 * @param credentials The offered credentials
687 * @param ip The requesting IP address
689 public String isPublishPermitted(String feedid, String credentials, String ip) {
690 Feed f = feeds.get(feedid);
691 String nf = "Feed does not exist";
698 String user = f.authusers.get(credentials);
700 return ("Publisher not permitted for this feed");
702 if (f.subnets.length == 0) {
705 byte[] addr = NodeUtils.getInetAddress(ip);
706 for (SubnetMatcher snm : f.subnets) {
707 if (snm.matches(addr)) {
711 return ("Publisher not permitted for this feed");
715 * Get authenticated user
717 public String getAuthUser(String feedid, String credentials) {
718 return (feeds.get(feedid).authusers.get(credentials));
722 * Check if the request should be redirected to a different ingress node
724 public String getIngressNode(String feedid, String user, String ip) {
725 Feed f = feeds.get(feedid);
726 if (f.redirections.length == 0) {
729 byte[] addr = NodeUtils.getInetAddress(ip);
730 for (Redirection r : f.redirections) {
731 if (r.user != null && !user.equals(r.user)) {
734 if (r.snm != null && !r.snm.matches(addr)) {
737 for (String n : r.nodes) {
738 if (myname.equals(n)) {
742 if (r.nodes.length == 0) {
745 return (r.nodes[rrcntr++ % r.nodes.length]);
751 * Get a provisioned configuration parameter
753 public String getProvParam(String name) {
754 return (params.get(name));
758 * Get all the DestInfos
760 public DestInfo[] getAllDests() {
765 * Get the targets for a feed
767 * @param feedid The feed ID
768 * @return The targets this feed should be delivered to
770 public Target[] getTargets(String feedid) {
771 if (feedid == null) {
772 return (new Target[0]);
774 Feed f = feeds.get(feedid);
776 return (new Target[0]);
782 * Get the feed ID for a subscription
784 * @param subid The subscription ID
785 * @return The feed ID
787 public String getFeedId(String subid) {
788 DestInfo di = subinfo.get(subid);
792 return (di.getLogData());
796 * Get the spool directory for a subscription
798 * @param subid The subscription ID
799 * @return The spool directory
801 public String getSpoolDir(String subid) {
802 DestInfo di = subinfo.get(subid);
806 return (di.getSpool());
810 * Get the Authorization value this node uses
812 * @return The Authorization header value for this node
814 public String getMyAuth() {