-/*******************************************************************************\r
- * ============LICENSE_START==================================================\r
- * * org.onap.dmaap\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-\r
-\r
-package com.att.research.datarouter.node;\r
-\r
-import java.io.*;\r
-import java.util.*;\r
-import org.json.*;\r
-import org.apache.log4j.Logger;\r
-\r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
-import com.att.research.datarouter.node.eelf.EelfMsgs;\r
-\r
-/**\r
- * Parser for provisioning data from the provisioning server.\r
- * <p>\r
- * The ProvData class uses a Reader for the text configuration from the\r
- * provisioning server to construct arrays of raw configuration entries.\r
- */\r
-public class ProvData {\r
- private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.node.ProvData");\r
- private static Logger logger = Logger.getLogger("com.att.research.datarouter.node.ProvData");\r
- private NodeConfig.ProvNode[] pn;\r
- private NodeConfig.ProvParam[] pp;\r
- private NodeConfig.ProvFeed[] pf;\r
- private NodeConfig.ProvFeedUser[] pfu;\r
- private NodeConfig.ProvFeedSubnet[] pfsn;\r
- private NodeConfig.ProvSubscription[] ps;\r
- private NodeConfig.ProvForceIngress[] pfi;\r
- private NodeConfig.ProvForceEgress[] pfe;\r
- private NodeConfig.ProvHop[] ph;\r
- private static String[] gvasa(JSONArray a, int index) {\r
- return(gvasa(a.get(index)));\r
- }\r
- private static String[] gvasa(JSONObject o, String key) {\r
- return(gvasa(o.opt(key)));\r
- }\r
- private static String[] gvasa(Object o) {\r
- if (o instanceof JSONArray) {\r
- JSONArray a = (JSONArray)o;\r
- Vector<String> v = new Vector<String>();\r
- for (int i = 0; i < a.length(); i++) {\r
- String s = gvas(a, i);\r
- if (s != null) {\r
- v.add(s);\r
- }\r
- }\r
- return(v.toArray(new String[v.size()]));\r
- } else {\r
- String s = gvas(o);\r
- if (s == null) {\r
- return(new String[0]);\r
- } else {\r
- return(new String[] { s });\r
- }\r
- }\r
- }\r
- private static String gvas(JSONArray a, int index) {\r
- return(gvas(a.get(index)));\r
- }\r
- private static String gvas(JSONObject o, String key) {\r
- return(gvas(o.opt(key)));\r
- }\r
- private static String gvas(Object o) {\r
- if (o instanceof Boolean || o instanceof Number || o instanceof String) {\r
- return(o.toString());\r
- }\r
- return(null);\r
- }\r
- /**\r
- * Construct raw provisioing data entries from the text (JSON)\r
- * provisioning document received from the provisioning server\r
- * @param r The reader for the JSON text.\r
- */\r
- public ProvData(Reader r) throws IOException {\r
- Vector<NodeConfig.ProvNode> pnv = new Vector<NodeConfig.ProvNode>();\r
- Vector<NodeConfig.ProvParam> ppv = new Vector<NodeConfig.ProvParam>();\r
- Vector<NodeConfig.ProvFeed> pfv = new Vector<NodeConfig.ProvFeed>();\r
- Vector<NodeConfig.ProvFeedUser> pfuv = new Vector<NodeConfig.ProvFeedUser>();\r
- Vector<NodeConfig.ProvFeedSubnet> pfsnv = new Vector<NodeConfig.ProvFeedSubnet>();\r
- Vector<NodeConfig.ProvSubscription> psv = new Vector<NodeConfig.ProvSubscription>();\r
- Vector<NodeConfig.ProvForceIngress> pfiv = new Vector<NodeConfig.ProvForceIngress>();\r
- Vector<NodeConfig.ProvForceEgress> pfev = new Vector<NodeConfig.ProvForceEgress>();\r
- Vector<NodeConfig.ProvHop> phv = new Vector<NodeConfig.ProvHop>();\r
- try {\r
- JSONTokener jtx = new JSONTokener(r);\r
- JSONObject jcfg = new JSONObject(jtx);\r
- char c = jtx.nextClean();\r
- if (c != '\0') {\r
- throw new JSONException("Spurious characters following configuration");\r
- }\r
- r.close();\r
- JSONArray jfeeds = jcfg.optJSONArray("feeds");\r
- if (jfeeds != null) {\r
- for (int fx = 0; fx < jfeeds.length(); fx++) {\r
- JSONObject jfeed = jfeeds.getJSONObject(fx);\r
- String stat = null;\r
- if (jfeed.optBoolean("suspend", false)) {\r
- stat = "Feed is suspended";\r
- }\r
- if (jfeed.optBoolean("deleted", false)) {\r
- stat = "Feed is deleted";\r
- }\r
- String fid = gvas(jfeed, "feedid");\r
- String fname = gvas(jfeed, "name");\r
- String fver = gvas(jfeed, "version");\r
- pfv.add(new NodeConfig.ProvFeed(fid, fname + "//" + fver, stat));\r
- JSONObject jauth = jfeed.optJSONObject("authorization");\r
- if (jauth == null) {\r
- continue;\r
- }\r
- JSONArray jeids = jauth.optJSONArray("endpoint_ids");\r
- if (jeids != null) {\r
- for (int ux = 0; ux < jeids.length(); ux++) {\r
- JSONObject ju = jeids.getJSONObject(ux);\r
- String login = gvas(ju, "id");\r
- String password = gvas(ju, "password");\r
- pfuv.add(new NodeConfig.ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));\r
- }\r
- }\r
- JSONArray jeips = jauth.optJSONArray("endpoint_addrs");\r
- if (jeips != null) {\r
- for (int ix = 0; ix < jeips.length(); ix++) {\r
- String sn = gvas(jeips, ix);\r
- pfsnv.add(new NodeConfig.ProvFeedSubnet(fid, sn));\r
- }\r
- }\r
- }\r
- }\r
- JSONArray jsubs = jcfg.optJSONArray("subscriptions");\r
- if (jsubs != null) {\r
- for (int sx = 0; sx < jsubs.length(); sx++) {\r
- JSONObject jsub = jsubs.getJSONObject(sx);\r
- if (jsub.optBoolean("suspend", false)) {\r
- continue;\r
- }\r
- String sid = gvas(jsub, "subid");\r
- String fid = gvas(jsub, "feedid");\r
- JSONObject jdel = jsub.getJSONObject("delivery");\r
- String delurl = gvas(jdel, "url");\r
- String id = gvas(jdel, "user");\r
- String password = gvas(jdel, "password");\r
- boolean monly = jsub.getBoolean("metadataOnly");\r
- boolean use100 = jdel.getBoolean("use100");\r
- psv.add(new NodeConfig.ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100));\r
- }\r
- }\r
- JSONObject jparams = jcfg.optJSONObject("parameters");\r
- if (jparams != null) {\r
- for (String pname: JSONObject.getNames(jparams)) {\r
- String pvalue = gvas(jparams, pname);\r
- if (pvalue != null) {\r
- ppv.add(new NodeConfig.ProvParam(pname, pvalue));\r
- }\r
- }\r
- String sfx = gvas(jparams, "PROV_DOMAIN");\r
- JSONArray jnodes = jparams.optJSONArray("NODES");\r
- if (jnodes != null) {\r
- for (int nx = 0; nx < jnodes.length(); nx++) {\r
- String nn = gvas(jnodes, nx);\r
- if (nn.indexOf('.') == -1) {\r
- nn = nn + "." + sfx;\r
- }\r
- pnv.add(new NodeConfig.ProvNode(nn));\r
- }\r
- }\r
- }\r
- JSONArray jingresses = jcfg.optJSONArray("ingress");\r
- if (jingresses != null) {\r
- for (int fx = 0; fx < jingresses.length(); fx++) {\r
- JSONObject jingress = jingresses.getJSONObject(fx);\r
- String fid = gvas(jingress, "feedid");\r
- String subnet = gvas(jingress, "subnet");\r
- String user = gvas(jingress, "user");\r
- String[] nodes = gvasa(jingress, "node");\r
- if (fid == null || "".equals(fid)) {\r
- continue;\r
- }\r
- if ("".equals(subnet)) {\r
- subnet = null;\r
- }\r
- if ("".equals(user)) {\r
- user = null;\r
- }\r
- pfiv.add(new NodeConfig.ProvForceIngress(fid, subnet, user, nodes));\r
- }\r
- }\r
- JSONObject jegresses = jcfg.optJSONObject("egress");\r
- if (jegresses != null && JSONObject.getNames(jegresses) != null) {\r
- for (String esid: JSONObject.getNames(jegresses)) {\r
- String enode = gvas(jegresses, esid);\r
- if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {\r
- pfev.add(new NodeConfig.ProvForceEgress(esid, enode));\r
- }\r
- }\r
- }\r
- JSONArray jhops = jcfg.optJSONArray("routing");\r
- if (jhops != null) {\r
- for (int fx = 0; fx < jhops.length(); fx++) {\r
- JSONObject jhop = jhops.getJSONObject(fx);\r
- String from = gvas(jhop, "from");\r
- String to = gvas(jhop, "to");\r
- String via = gvas(jhop, "via");\r
- if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {\r
- continue;\r
- }\r
- phv.add(new NodeConfig.ProvHop(from, to, via));\r
- }\r
- }\r
- } catch (JSONException jse) {\r
- NodeUtils.setIpAndFqdnForEelf("ProvData");\r
- eelflogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());\r
- logger.error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);\r
- throw new IOException(jse.toString(), jse);\r
- }\r
- pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);\r
- pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);\r
- pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);\r
- pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);\r
- pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);\r
- ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);\r
- pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);\r
- pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);\r
- ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);\r
- }\r
- /**\r
- * Get the raw node configuration entries\r
- */\r
- public NodeConfig.ProvNode[] getNodes() {\r
- return(pn);\r
- }\r
- /**\r
- * Get the raw parameter configuration entries\r
- */\r
- public NodeConfig.ProvParam[] getParams() {\r
- return(pp);\r
- }\r
- /**\r
- * Ge the raw feed configuration entries\r
- */\r
- public NodeConfig.ProvFeed[] getFeeds() {\r
- return(pf);\r
- }\r
- /**\r
- * Get the raw feed user configuration entries\r
- */\r
- public NodeConfig.ProvFeedUser[] getFeedUsers() {\r
- return(pfu);\r
- }\r
- /**\r
- * Get the raw feed subnet configuration entries\r
- */\r
- public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {\r
- return(pfsn);\r
- }\r
- /**\r
- * Get the raw subscription entries\r
- */\r
- public NodeConfig.ProvSubscription[] getSubscriptions() {\r
- return(ps);\r
- }\r
- /**\r
- * Get the raw forced ingress entries\r
- */\r
- public NodeConfig.ProvForceIngress[] getForceIngress() {\r
- return(pfi);\r
- }\r
- /**\r
- * Get the raw forced egress entries\r
- */\r
- public NodeConfig.ProvForceEgress[] getForceEgress() {\r
- return(pfe);\r
- }\r
- /**\r
- * Get the raw next hop entries\r
- */\r
- public NodeConfig.ProvHop[] getHops() {\r
- return(ph);\r
- }\r
-}\r