More DR unit tests and code cleanup
[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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.io.Reader;
31 import java.util.ArrayList;
32 import org.jetbrains.annotations.Nullable;
33 import org.json.JSONArray;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.json.JSONTokener;
37 import org.onap.dmaap.datarouter.node.NodeConfig.ProvFeed;
38 import org.onap.dmaap.datarouter.node.NodeConfig.ProvFeedSubnet;
39 import org.onap.dmaap.datarouter.node.NodeConfig.ProvFeedUser;
40 import org.onap.dmaap.datarouter.node.NodeConfig.ProvForceEgress;
41 import org.onap.dmaap.datarouter.node.NodeConfig.ProvForceIngress;
42 import org.onap.dmaap.datarouter.node.NodeConfig.ProvHop;
43 import org.onap.dmaap.datarouter.node.NodeConfig.ProvNode;
44 import org.onap.dmaap.datarouter.node.NodeConfig.ProvParam;
45 import org.onap.dmaap.datarouter.node.NodeConfig.ProvSubscription;
46 import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
47
48 /**
49  * Parser for provisioning data from the provisioning server.
50  *
51  * <p>The ProvData class uses a Reader for the text configuration from the provisioning server to construct arrays of
52  * raw configuration entries.
53  */
54 public class ProvData {
55
56     private static final String FEED_ID = "feedid";
57     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(ProvData.class);
58     private NodeConfig.ProvNode[] pn;
59     private NodeConfig.ProvParam[] pp;
60     private NodeConfig.ProvFeed[] pf;
61     private NodeConfig.ProvFeedUser[] pfu;
62     private NodeConfig.ProvFeedSubnet[] pfsn;
63     private NodeConfig.ProvSubscription[] ps;
64     private NodeConfig.ProvForceIngress[] pfi;
65     private NodeConfig.ProvForceEgress[] pfe;
66     private NodeConfig.ProvHop[] ph;
67
68     /**
69      * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
70      * server.
71      *
72      * @param reader The reader for the JSON text.
73      */
74     public ProvData(Reader reader) throws IOException {
75         ArrayList<ProvNode> pnv = new ArrayList<>();
76         ArrayList<NodeConfig.ProvParam> ppv = new ArrayList<>();
77         ArrayList<NodeConfig.ProvFeed> pfv = new ArrayList<>();
78         ArrayList<NodeConfig.ProvFeedUser> pfuv = new ArrayList<>();
79         ArrayList<NodeConfig.ProvFeedSubnet> pfsnv = new ArrayList<>();
80         ArrayList<NodeConfig.ProvSubscription> psv = new ArrayList<>();
81         ArrayList<NodeConfig.ProvForceIngress> pfiv = new ArrayList<>();
82         ArrayList<NodeConfig.ProvForceEgress> pfev = new ArrayList<>();
83         ArrayList<NodeConfig.ProvHop> phv = new ArrayList<>();
84         try {
85             JSONTokener jtx = new JSONTokener(reader);
86             JSONObject jcfg = new JSONObject(jtx);
87             char c = jtx.nextClean();
88             if (c != '\0') {
89                 throw new JSONException("Spurious characters following configuration");
90             }
91             reader.close();
92             addJSONFeeds(pfv, pfuv, pfsnv, jcfg);
93             addJSONSubs(psv, jcfg);
94             addJSONParams(pnv, ppv, jcfg);
95             addJSONRoutingInformation(pfiv, pfev, phv, jcfg);
96         } catch (JSONException jse) {
97             NodeUtils.setIpAndFqdnForEelf("ProvData");
98             eelfLogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
99             eelfLogger
100                     .error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
101             throw new IOException(jse.toString(), jse);
102         }
103         pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
104         pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
105         pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
106         pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
107         pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
108         ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
109         pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
110         pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
111         ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
112     }
113
114     private static String[] gvasa(JSONObject object, String key) {
115         return (gvasa(object.opt(key)));
116     }
117
118     private static String[] gvasa(Object object) {
119         if (object instanceof JSONArray) {
120             JSONArray jsonArray = (JSONArray) object;
121             ArrayList<String> array = new ArrayList<>();
122             for (int i = 0; i < jsonArray.length(); i++) {
123                 String string = gvas(jsonArray, i);
124                 if (string != null) {
125                     array.add(string);
126                 }
127             }
128             return (array.toArray(new String[array.size()]));
129         } else {
130             String string = gvas(object);
131             if (string == null) {
132                 return (new String[0]);
133             } else {
134                 return (new String[]{string});
135             }
136         }
137     }
138
139     private static String gvas(JSONArray array, int index) {
140         return (gvas(array.get(index)));
141     }
142
143     private static String gvas(JSONObject object, String key) {
144         return (gvas(object.opt(key)));
145     }
146
147     private static String gvas(Object object) {
148         if (object instanceof Boolean || object instanceof Number || object instanceof String) {
149             return (object.toString());
150         }
151         return (null);
152     }
153
154     /**
155      * Get the raw node configuration entries.
156      */
157     public NodeConfig.ProvNode[] getNodes() {
158         return (pn);
159     }
160
161     /**
162      * Get the raw parameter configuration entries.
163      */
164     public NodeConfig.ProvParam[] getParams() {
165         return (pp);
166     }
167
168     /**
169      * Ge the raw feed configuration entries.
170      */
171     public NodeConfig.ProvFeed[] getFeeds() {
172         return (pf);
173     }
174
175     /**
176      * Get the raw feed user configuration entries.
177      */
178     public NodeConfig.ProvFeedUser[] getFeedUsers() {
179         return (pfu);
180     }
181
182     /**
183      * Get the raw feed subnet configuration entries.
184      */
185     public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
186         return (pfsn);
187     }
188
189     /**
190      * Get the raw subscription entries.
191      */
192     public NodeConfig.ProvSubscription[] getSubscriptions() {
193         return (ps);
194     }
195
196     /**
197      * Get the raw forced ingress entries.
198      */
199     public NodeConfig.ProvForceIngress[] getForceIngress() {
200         return (pfi);
201     }
202
203     /**
204      * Get the raw forced egress entries.
205      */
206     public NodeConfig.ProvForceEgress[] getForceEgress() {
207         return (pfe);
208     }
209
210     /**
211      * Get the raw next hop entries.
212      */
213     public NodeConfig.ProvHop[] getHops() {
214         return (ph);
215     }
216
217     @Nullable
218     private String getFeedStatus(JSONObject jfeed) {
219         String stat = null;
220         if (jfeed.optBoolean("suspend", false)) {
221             stat = "Feed is suspended";
222         }
223         if (jfeed.optBoolean("deleted", false)) {
224             stat = "Feed is deleted";
225         }
226         return stat;
227     }
228
229     private void addJSONFeeds(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
230             JSONObject jcfg) {
231         JSONArray jfeeds = jcfg.optJSONArray("feeds");
232         if (jfeeds != null) {
233             for (int fx = 0; fx < jfeeds.length(); fx++) {
234                 addJSONFeed(pfv, pfuv, pfsnv, jfeeds, fx);
235             }
236         }
237     }
238
239     private void addJSONFeed(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
240             JSONArray jfeeds, int fx) {
241         JSONObject jfeed = jfeeds.getJSONObject(fx);
242         String stat = getFeedStatus(jfeed);
243         String fid = gvas(jfeed, FEED_ID);
244         String fname = gvas(jfeed, "name");
245         String fver = gvas(jfeed, "version");
246         String createdDate = gvas(jfeed, "created_date");
247         /*
248          * START - AAF changes
249          * TDP EPIC US# 307413
250          * Passing aafInstance to ProvFeed from feeds json passed by prov to identify legacy/AAF feeds
251          */
252         String aafInstance = gvas(jfeed, "aaf_instance");
253         pfv.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate, aafInstance));
254         /*
255          * END - AAF changes
256          */
257         addJSONFeedAuthArrays(pfuv, pfsnv, jfeed, fid);
258     }
259
260     private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv, JSONObject jfeed,
261             String fid) {
262         JSONObject jauth = jfeed.optJSONObject("authorization");
263         if (jauth == null) {
264             return;
265         }
266         JSONArray jeids = jauth.optJSONArray("endpoint_ids");
267         if (jeids != null) {
268             for (int ux = 0; ux < jeids.length(); ux++) {
269                 JSONObject ju = jeids.getJSONObject(ux);
270                 String login = gvas(ju, "id");
271                 String password = gvas(ju, "password");
272                 pfuv.add(new ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
273             }
274         }
275         JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
276         if (jeips != null) {
277             for (int ix = 0; ix < jeips.length(); ix++) {
278                 String sn = gvas(jeips, ix);
279                 pfsnv.add(new ProvFeedSubnet(fid, sn));
280             }
281         }
282     }
283
284     private void addJSONSubs(ArrayList<ProvSubscription> psv, JSONObject jcfg) {
285         JSONArray jsubs = jcfg.optJSONArray("subscriptions");
286         if (jsubs != null) {
287             for (int sx = 0; sx < jsubs.length(); sx++) {
288                 addJSONSub(psv, jsubs, sx);
289             }
290         }
291     }
292
293     private void addJSONSub(ArrayList<ProvSubscription> psv, JSONArray jsubs, int sx) {
294         JSONObject jsub = jsubs.getJSONObject(sx);
295         if (jsub.optBoolean("suspend", false)) {
296             return;
297         }
298         String sid = gvas(jsub, "subid");
299         String fid = gvas(jsub, FEED_ID);
300         JSONObject jdel = jsub.getJSONObject("delivery");
301         String delurl = gvas(jdel, "url");
302         String id = gvas(jdel, "user");
303         String password = gvas(jdel, "password");
304         boolean monly = jsub.getBoolean("metadataOnly");
305         boolean use100 = jdel.getBoolean("use100");
306         boolean privilegedSubscriber = jsub.getBoolean("privilegedSubscriber");
307         boolean decompress = jsub.getBoolean("decompress");
308         boolean followRedirect = jsub.getBoolean("follow_redirect");
309         psv.add(new ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100,
310                 privilegedSubscriber, followRedirect, decompress));
311     }
312
313     private void addJSONParams(ArrayList<ProvNode> pnv, ArrayList<ProvParam> ppv, JSONObject jcfg) {
314         JSONObject jparams = jcfg.optJSONObject("parameters");
315         if (jparams != null) {
316             for (String pname : JSONObject.getNames(jparams)) {
317                 addJSONParam(ppv, jparams, pname);
318             }
319             addJSONNodesToParams(pnv, jparams);
320         }
321     }
322
323     private void addJSONParam(ArrayList<ProvParam> ppv, JSONObject jparams, String pname) {
324         String pvalue = gvas(jparams, pname);
325         if (pvalue != null) {
326             ppv.add(new ProvParam(pname, pvalue));
327         }
328     }
329
330     private void addJSONNodesToParams(ArrayList<ProvNode> pnv, JSONObject jparams) {
331         String sfx = gvas(jparams, "PROV_DOMAIN");
332         JSONArray jnodes = jparams.optJSONArray("NODES");
333         if (jnodes != null) {
334             for (int nx = 0; nx < jnodes.length(); nx++) {
335                 String nn = gvas(jnodes, nx);
336                 if (nn == null) {
337                     continue;
338                 }
339                 if (nn.indexOf('.') == -1) {
340                     nn = nn + "." + sfx;
341                 }
342                 pnv.add(new ProvNode(nn));
343             }
344         }
345     }
346
347     private void addJSONRoutingInformation(ArrayList<ProvForceIngress> pfiv, ArrayList<ProvForceEgress> pfev,
348             ArrayList<ProvHop> phv, JSONObject jcfg) {
349         JSONArray jingresses = jcfg.optJSONArray("ingress");
350         if (jingresses != null) {
351             for (int fx = 0; fx < jingresses.length(); fx++) {
352                 addJSONIngressRoute(pfiv, jingresses, fx);
353             }
354         }
355         JSONObject jegresses = jcfg.optJSONObject("egress");
356         if (jegresses != null && JSONObject.getNames(jegresses) != null) {
357             for (String esid : JSONObject.getNames(jegresses)) {
358                 addJSONEgressRoute(pfev, jegresses, esid);
359             }
360         }
361         JSONArray jhops = jcfg.optJSONArray("routing");
362         if (jhops != null) {
363             for (int fx = 0; fx < jhops.length(); fx++) {
364                 addJSONRoutes(phv, jhops, fx);
365             }
366         }
367     }
368
369     private void addJSONIngressRoute(ArrayList<ProvForceIngress> pfiv, JSONArray jingresses, int fx) {
370         JSONObject jingress = jingresses.getJSONObject(fx);
371         String fid = gvas(jingress, FEED_ID);
372         String subnet = gvas(jingress, "subnet");
373         String user = gvas(jingress, "user");
374         if (fid == null || "".equals(fid)) {
375             return;
376         }
377         if ("".equals(subnet)) {
378             subnet = null;
379         }
380         if ("".equals(user)) {
381             user = null;
382         }
383         String[] nodes = gvasa(jingress, "node");
384         pfiv.add(new ProvForceIngress(fid, subnet, user, nodes));
385     }
386
387     private void addJSONEgressRoute(ArrayList<ProvForceEgress> pfev, JSONObject jegresses, String esid) {
388         String enode = gvas(jegresses, esid);
389         if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
390             pfev.add(new ProvForceEgress(esid, enode));
391         }
392     }
393
394     private void addJSONRoutes(ArrayList<ProvHop> phv, JSONArray jhops, int fx) {
395         JSONObject jhop = jhops.getJSONObject(fx);
396         String from = gvas(jhop, "from");
397         String to = gvas(jhop, "to");
398         String via = gvas(jhop, "via");
399         if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
400             return;
401         }
402         phv.add(new ProvHop(from, to, via));
403     }
404 }