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