[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-node / src / main / java / com / att / research / datarouter / node / ProvData.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package com.att.research.datarouter.node;\r
26 \r
27 import java.io.*;\r
28 import java.util.*;\r
29 import org.json.*;\r
30 import org.apache.log4j.Logger;\r
31 \r
32 import com.att.eelf.configuration.EELFLogger;\r
33 import com.att.eelf.configuration.EELFManager;\r
34 import com.att.research.datarouter.node.eelf.EelfMsgs;\r
35 \r
36 /**\r
37  *      Parser for provisioning data from the provisioning server.\r
38  *      <p>\r
39  *      The ProvData class uses a Reader for the text configuration from the\r
40  *      provisioning server to construct arrays of raw configuration entries.\r
41  */\r
42 public class ProvData   {\r
43     private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.node.ProvData");\r
44         private static Logger logger = Logger.getLogger("com.att.research.datarouter.node.ProvData");\r
45         private NodeConfig.ProvNode[]   pn;\r
46         private NodeConfig.ProvParam[]  pp;\r
47         private NodeConfig.ProvFeed[]   pf;\r
48         private NodeConfig.ProvFeedUser[]       pfu;\r
49         private NodeConfig.ProvFeedSubnet[]     pfsn;\r
50         private NodeConfig.ProvSubscription[]   ps;\r
51         private NodeConfig.ProvForceIngress[]   pfi;\r
52         private NodeConfig.ProvForceEgress[]    pfe;\r
53         private NodeConfig.ProvHop[]    ph;\r
54         private static String[] gvasa(JSONArray a, int index) {\r
55                 return(gvasa(a.get(index)));\r
56         }\r
57         private static String[] gvasa(JSONObject o, String key) {\r
58                 return(gvasa(o.opt(key)));\r
59         }\r
60         private static String[] gvasa(Object o) {\r
61                 if (o instanceof JSONArray) {\r
62                         JSONArray a = (JSONArray)o;\r
63                         Vector<String> v = new Vector<String>();\r
64                         for (int i = 0; i < a.length(); i++) {\r
65                                 String s = gvas(a, i);\r
66                                 if (s != null) {\r
67                                         v.add(s);\r
68                                 }\r
69                         }\r
70                         return(v.toArray(new String[v.size()]));\r
71                 } else {\r
72                         String s = gvas(o);\r
73                         if (s == null) {\r
74                                 return(new String[0]);\r
75                         } else {\r
76                                 return(new String[] { s });\r
77                         }\r
78                 }\r
79         }\r
80         private static String gvas(JSONArray a, int index) {\r
81                 return(gvas(a.get(index)));\r
82         }\r
83         private static String gvas(JSONObject o, String key) {\r
84                 return(gvas(o.opt(key)));\r
85         }\r
86         private static String gvas(Object o) {\r
87                 if (o instanceof Boolean || o instanceof Number || o instanceof String) {\r
88                         return(o.toString());\r
89                 }\r
90                 return(null);\r
91         }\r
92         /**\r
93          *      Construct raw provisioing data entries from the text (JSON)\r
94          *      provisioning document received from the provisioning server\r
95          *      @param r        The reader for the JSON text.\r
96          */\r
97         public ProvData(Reader r) throws IOException {\r
98                 Vector<NodeConfig.ProvNode> pnv = new Vector<NodeConfig.ProvNode>();\r
99                 Vector<NodeConfig.ProvParam> ppv = new Vector<NodeConfig.ProvParam>();\r
100                 Vector<NodeConfig.ProvFeed> pfv = new Vector<NodeConfig.ProvFeed>();\r
101                 Vector<NodeConfig.ProvFeedUser> pfuv = new Vector<NodeConfig.ProvFeedUser>();\r
102                 Vector<NodeConfig.ProvFeedSubnet> pfsnv = new Vector<NodeConfig.ProvFeedSubnet>();\r
103                 Vector<NodeConfig.ProvSubscription> psv = new Vector<NodeConfig.ProvSubscription>();\r
104                 Vector<NodeConfig.ProvForceIngress> pfiv = new Vector<NodeConfig.ProvForceIngress>();\r
105                 Vector<NodeConfig.ProvForceEgress> pfev = new Vector<NodeConfig.ProvForceEgress>();\r
106                 Vector<NodeConfig.ProvHop> phv = new Vector<NodeConfig.ProvHop>();\r
107                 try {\r
108                         JSONTokener jtx = new JSONTokener(r);\r
109                         JSONObject jcfg = new JSONObject(jtx);\r
110                         char c = jtx.nextClean();\r
111                         if (c != '\0') {\r
112                                 throw new JSONException("Spurious characters following configuration");\r
113                         }\r
114                         r.close();\r
115                         JSONArray jfeeds = jcfg.optJSONArray("feeds");\r
116                         if (jfeeds != null) {\r
117                                 for (int fx = 0; fx < jfeeds.length(); fx++) {\r
118                                         JSONObject jfeed = jfeeds.getJSONObject(fx);\r
119                                         String stat = null;\r
120                                         if (jfeed.optBoolean("suspend", false)) {\r
121                                                 stat = "Feed is suspended";\r
122                                         }\r
123                                         if (jfeed.optBoolean("deleted", false)) {\r
124                                                 stat = "Feed is deleted";\r
125                                         }\r
126                                         String fid = gvas(jfeed, "feedid");\r
127                                         String fname = gvas(jfeed, "name");\r
128                                         String fver = gvas(jfeed, "version");\r
129                                         pfv.add(new NodeConfig.ProvFeed(fid, fname + "//" + fver, stat));\r
130                                         JSONObject jauth = jfeed.optJSONObject("authorization");\r
131                                         if (jauth == null) {\r
132                                                 continue;\r
133                                         }\r
134                                         JSONArray jeids = jauth.optJSONArray("endpoint_ids");\r
135                                         if (jeids != null) {\r
136                                                 for (int ux = 0; ux < jeids.length(); ux++) {\r
137                                                         JSONObject ju = jeids.getJSONObject(ux);\r
138                                                         String login = gvas(ju, "id");\r
139                                                         String password = gvas(ju, "password");\r
140                                                         pfuv.add(new NodeConfig.ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));\r
141                                                 }\r
142                                         }\r
143                                         JSONArray jeips = jauth.optJSONArray("endpoint_addrs");\r
144                                         if (jeips != null) {\r
145                                                 for (int ix = 0; ix < jeips.length(); ix++) {\r
146                                                         String sn = gvas(jeips, ix);\r
147                                                         pfsnv.add(new NodeConfig.ProvFeedSubnet(fid, sn));\r
148                                                 }\r
149                                         }\r
150                                 }\r
151                         }\r
152                         JSONArray jsubs = jcfg.optJSONArray("subscriptions");\r
153                         if (jsubs != null) {\r
154                                 for (int sx = 0; sx < jsubs.length(); sx++) {\r
155                                         JSONObject jsub = jsubs.getJSONObject(sx);\r
156                                         if (jsub.optBoolean("suspend", false)) {\r
157                                                 continue;\r
158                                         }\r
159                                         String sid = gvas(jsub, "subid");\r
160                                         String fid = gvas(jsub, "feedid");\r
161                                         JSONObject jdel = jsub.getJSONObject("delivery");\r
162                                         String delurl = gvas(jdel, "url");\r
163                                         String id = gvas(jdel, "user");\r
164                                         String password = gvas(jdel, "password");\r
165                                         boolean monly = jsub.getBoolean("metadataOnly");\r
166                                         boolean use100 = jdel.getBoolean("use100");\r
167                                         psv.add(new NodeConfig.ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100));\r
168                                 }\r
169                         }\r
170                         JSONObject jparams = jcfg.optJSONObject("parameters");\r
171                         if (jparams != null) {\r
172                                 for (String pname: JSONObject.getNames(jparams)) {\r
173                                         String pvalue = gvas(jparams, pname);\r
174                                         if (pvalue != null) {\r
175                                                 ppv.add(new NodeConfig.ProvParam(pname, pvalue));\r
176                                         }\r
177                                 }\r
178                                 String sfx = gvas(jparams, "PROV_DOMAIN");\r
179                                 JSONArray jnodes = jparams.optJSONArray("NODES");\r
180                                 if (jnodes != null) {\r
181                                         for (int nx = 0; nx < jnodes.length(); nx++) {\r
182                                                 String nn = gvas(jnodes, nx);\r
183                                                 if (nn.indexOf('.') == -1) {\r
184                                                         nn = nn + "." + sfx;\r
185                                                 }\r
186                                                 pnv.add(new NodeConfig.ProvNode(nn));\r
187                                         }\r
188                                 }\r
189                         }\r
190                         JSONArray jingresses = jcfg.optJSONArray("ingress");\r
191                         if (jingresses != null) {\r
192                                 for (int fx = 0; fx < jingresses.length(); fx++) {\r
193                                         JSONObject jingress = jingresses.getJSONObject(fx);\r
194                                         String fid = gvas(jingress, "feedid");\r
195                                         String subnet = gvas(jingress, "subnet");\r
196                                         String user = gvas(jingress, "user");\r
197                                         String[] nodes = gvasa(jingress, "node");\r
198                                         if (fid == null || "".equals(fid)) {\r
199                                                 continue;\r
200                                         }\r
201                                         if ("".equals(subnet)) {\r
202                                                 subnet = null;\r
203                                         }\r
204                                         if ("".equals(user)) {\r
205                                                 user = null;\r
206                                         }\r
207                                         pfiv.add(new NodeConfig.ProvForceIngress(fid, subnet, user, nodes));\r
208                                 }\r
209                         }\r
210                         JSONObject jegresses = jcfg.optJSONObject("egress");\r
211                         if (jegresses != null && JSONObject.getNames(jegresses) != null) {\r
212                                 for (String esid: JSONObject.getNames(jegresses)) {\r
213                                         String enode = gvas(jegresses, esid);\r
214                                         if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {\r
215                                                 pfev.add(new NodeConfig.ProvForceEgress(esid, enode));\r
216                                         }\r
217                                 }\r
218                         }\r
219                         JSONArray jhops = jcfg.optJSONArray("routing");\r
220                         if (jhops != null) {\r
221                                 for (int fx = 0; fx < jhops.length(); fx++) {\r
222                                         JSONObject jhop = jhops.getJSONObject(fx);\r
223                                         String from = gvas(jhop, "from");\r
224                                         String to = gvas(jhop, "to");\r
225                                         String via = gvas(jhop, "via");\r
226                                         if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {\r
227                                                 continue;\r
228                                         }\r
229                                         phv.add(new NodeConfig.ProvHop(from, to, via));\r
230                                 }\r
231                         }\r
232                 } catch (JSONException jse) {\r
233                         NodeUtils.setIpAndFqdnForEelf("ProvData");\r
234                         eelflogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());\r
235                         logger.error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);\r
236                         throw new IOException(jse.toString(), jse);\r
237                 }\r
238                 pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);\r
239                 pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);\r
240                 pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);\r
241                 pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);\r
242                 pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);\r
243                 ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);\r
244                 pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);\r
245                 pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);\r
246                 ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);\r
247         }\r
248         /**\r
249          *      Get the raw node configuration entries\r
250          */\r
251         public NodeConfig.ProvNode[] getNodes() {\r
252                 return(pn);\r
253         }\r
254         /**\r
255          *      Get the raw parameter configuration entries\r
256          */\r
257         public NodeConfig.ProvParam[] getParams() {\r
258                 return(pp);\r
259         }\r
260         /**\r
261          *      Ge the raw feed configuration entries\r
262          */\r
263         public NodeConfig.ProvFeed[] getFeeds() {\r
264                 return(pf);\r
265         }\r
266         /**\r
267          *      Get the raw feed user configuration entries\r
268          */\r
269         public NodeConfig.ProvFeedUser[] getFeedUsers() {\r
270                 return(pfu);\r
271         }\r
272         /**\r
273          *      Get the raw feed subnet configuration entries\r
274          */\r
275         public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {\r
276                 return(pfsn);\r
277         }\r
278         /**\r
279          *      Get the raw subscription entries\r
280          */\r
281         public NodeConfig.ProvSubscription[] getSubscriptions() {\r
282                 return(ps);\r
283         }\r
284         /**\r
285          *      Get the raw forced ingress entries\r
286          */\r
287         public NodeConfig.ProvForceIngress[] getForceIngress() {\r
288                 return(pfi);\r
289         }\r
290         /**\r
291          *      Get the raw forced egress entries\r
292          */\r
293         public NodeConfig.ProvForceEgress[] getForceEgress() {\r
294                 return(pfe);\r
295         }\r
296         /**\r
297          *      Get the raw next hop entries\r
298          */\r
299         public NodeConfig.ProvHop[] getHops() {\r
300                 return(ph);\r
301         }\r
302 }\r