Remove major and minor code smells in dr-node
[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  * <p>
51  * The ProvData class uses a Reader for the text configuration from the provisioning server to construct arrays of raw
52  * 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[] provNodes;
59     private NodeConfig.ProvParam[] provParams;
60     private NodeConfig.ProvFeed[] provFeeds;
61     private NodeConfig.ProvFeedUser[] provFeedUsers;
62     private NodeConfig.ProvFeedSubnet[] provFeedSubnets;
63     private NodeConfig.ProvSubscription[] provSubscriptions;
64     private NodeConfig.ProvForceIngress[] provForceIngresses;
65     private NodeConfig.ProvForceEgress[] provForceEgresses;
66     private NodeConfig.ProvHop[] provHops;
67
68     /**
69      * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
70      * server
71      *
72      * @param r The reader for the JSON text.
73      */
74     public ProvData(Reader r) throws IOException {
75         ArrayList<ProvNode> provNodes1 = new ArrayList<>();
76         ArrayList<NodeConfig.ProvParam> provParams1 = new ArrayList<>();
77         ArrayList<NodeConfig.ProvFeed> provFeeds1 = new ArrayList<>();
78         ArrayList<NodeConfig.ProvFeedUser> provFeedUsers1 = new ArrayList<>();
79         ArrayList<NodeConfig.ProvFeedSubnet> provFeedSubnets1 = new ArrayList<>();
80         ArrayList<NodeConfig.ProvSubscription> provSubscriptions1 = new ArrayList<>();
81         ArrayList<NodeConfig.ProvForceIngress> provForceIngresses1 = new ArrayList<>();
82         ArrayList<NodeConfig.ProvForceEgress> provForceEgresses1 = new ArrayList<>();
83         ArrayList<NodeConfig.ProvHop> provHops1 = new ArrayList<>();
84         try {
85             JSONTokener jtx = new JSONTokener(r);
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             r.close();
92             addJSONFeeds(provFeeds1, provFeedUsers1, provFeedSubnets1, jcfg);
93             addJSONSubs(provSubscriptions1, jcfg);
94             addJSONParams(provNodes1, provParams1, jcfg);
95             addJSONRoutingInformation(provForceIngresses1, provForceEgresses1, provHops1, 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         provNodes = provNodes1.toArray(new NodeConfig.ProvNode[provNodes1.size()]);
104         provParams = provParams1.toArray(new NodeConfig.ProvParam[provParams1.size()]);
105         provFeeds = provFeeds1.toArray(new NodeConfig.ProvFeed[provFeeds1.size()]);
106         provFeedUsers = provFeedUsers1.toArray(new NodeConfig.ProvFeedUser[provFeedUsers1.size()]);
107         provFeedSubnets = provFeedSubnets1.toArray(new NodeConfig.ProvFeedSubnet[provFeedSubnets1.size()]);
108         provSubscriptions = provSubscriptions1.toArray(new NodeConfig.ProvSubscription[provSubscriptions1.size()]);
109         provForceIngresses = provForceIngresses1.toArray(new NodeConfig.ProvForceIngress[provForceIngresses1.size()]);
110         provForceEgresses = provForceEgresses1.toArray(new NodeConfig.ProvForceEgress[provForceEgresses1.size()]);
111         provHops = provHops1.toArray(new NodeConfig.ProvHop[provHops1.size()]);
112     }
113
114     private static String[] gvasa(JSONObject o, String key) {
115         return (gvasa(o.opt(key)));
116     }
117
118     private static String[] gvasa(Object o) {
119         if (o instanceof JSONArray) {
120             JSONArray a = (JSONArray) o;
121             ArrayList<String> v = new ArrayList<>();
122             for (int i = 0; i < a.length(); i++) {
123                 String s = gvas(a, i);
124                 if (s != null) {
125                     v.add(s);
126                 }
127             }
128             return (v.toArray(new String[v.size()]));
129         } else {
130             String s = gvas(o);
131             if (s == null) {
132                 return (new String[0]);
133             } else {
134                 return (new String[]{s});
135             }
136         }
137     }
138
139     private static String gvas(JSONArray a, int index) {
140         return (gvas(a.get(index)));
141     }
142
143     private static String gvas(JSONObject o, String key) {
144         return (gvas(o.opt(key)));
145     }
146
147     private static String gvas(Object o) {
148         if (o instanceof Boolean || o instanceof Number || o instanceof String) {
149             return (o.toString());
150         }
151         return (null);
152     }
153
154     /**
155      * Get the raw node configuration entries
156      */
157     public NodeConfig.ProvNode[] getNodes() {
158         return (provNodes);
159     }
160
161     /**
162      * Get the raw parameter configuration entries
163      */
164     public NodeConfig.ProvParam[] getParams() {
165         return (provParams);
166     }
167
168     /**
169      * Ge the raw feed configuration entries
170      */
171     public NodeConfig.ProvFeed[] getFeeds() {
172         return (provFeeds);
173     }
174
175     /**
176      * Get the raw feed user configuration entries
177      */
178     public NodeConfig.ProvFeedUser[] getFeedUsers() {
179         return (provFeedUsers);
180     }
181
182     /**
183      * Get the raw feed subnet configuration entries
184      */
185     public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
186         return (provFeedSubnets);
187     }
188
189     /**
190      * Get the raw subscription entries
191      */
192     public NodeConfig.ProvSubscription[] getSubscriptions() {
193         return (provSubscriptions);
194     }
195
196     /**
197      * Get the raw forced ingress entries
198      */
199     public NodeConfig.ProvForceIngress[] getForceIngress() {
200         return (provForceIngresses);
201     }
202
203     /**
204      * Get the raw forced egress entries
205      */
206     public NodeConfig.ProvForceEgress[] getForceEgress() {
207         return (provForceEgresses);
208     }
209
210     /**
211      * Get the raw next hop entries
212      */
213     public NodeConfig.ProvHop[] getHops() {
214         return (provHops);
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> provFeeds1, ArrayList<ProvFeedUser> provFeedUsers1,
230             ArrayList<ProvFeedSubnet> provFeedSubnets1,
231             JSONObject jsonConfig) {
232         JSONArray jfeeds = jsonConfig.optJSONArray("feeds");
233         if (jfeeds != null) {
234             for (int fx = 0; fx < jfeeds.length(); fx++) {
235                 addJSONFeed(provFeeds1, provFeedUsers1, provFeedSubnets1, jfeeds, fx);
236             }
237         }
238     }
239
240     private void addJSONFeed(ArrayList<ProvFeed> provFeeds1, ArrayList<ProvFeedUser> provFeedUsers1,
241             ArrayList<ProvFeedSubnet> provFeedSubnets1, JSONArray jfeeds, int feedIndex) {
242         JSONObject jfeed = jfeeds.getJSONObject(feedIndex);
243         String stat = getFeedStatus(jfeed);
244         String fid = gvas(jfeed, FEED_ID);
245         String fname = gvas(jfeed, "name");
246         String fver = gvas(jfeed, "version");
247         String createdDate = gvas(jfeed, "created_date");
248         /*
249          * START - AAF changes
250          * TDP EPIC US# 307413
251          * Passing aafInstance to ProvFeed from feeds json passed by prov to identify legacy/AAF feeds
252          */
253         String aafInstance = gvas(jfeed, "aaf_instance");
254         provFeeds1.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate, aafInstance));
255         /*
256          * END - AAF changes
257          */
258         addJSONFeedAuthArrays(provFeedUsers1, provFeedSubnets1, jfeed, fid);
259     }
260
261     private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> provFeedUsers1,
262             ArrayList<ProvFeedSubnet> provFeedSubnets1, JSONObject jfeed, String fid) {
263         JSONObject jauth = jfeed.optJSONObject("authorization");
264         if (jauth == null) {
265             return;
266         }
267         JSONArray jeids = jauth.optJSONArray("endpoint_ids");
268         if (jeids != null) {
269             for (int ux = 0; ux < jeids.length(); ux++) {
270                 JSONObject ju = jeids.getJSONObject(ux);
271                 String login = gvas(ju, "id");
272                 String password = gvas(ju, "password");
273                 provFeedUsers1.add(new ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
274             }
275         }
276         JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
277         if (jeips != null) {
278             for (int ix = 0; ix < jeips.length(); ix++) {
279                 String sn = gvas(jeips, ix);
280                 provFeedSubnets1.add(new ProvFeedSubnet(fid, sn));
281             }
282         }
283     }
284
285     private void addJSONSubs(ArrayList<ProvSubscription> provSubscriptions1, JSONObject jsonConfig) {
286         JSONArray jsubs = jsonConfig.optJSONArray("subscriptions");
287         if (jsubs != null) {
288             for (int sx = 0; sx < jsubs.length(); sx++) {
289                 addJSONSub(provSubscriptions1, jsubs, sx);
290             }
291         }
292     }
293
294     private void addJSONSub(ArrayList<ProvSubscription> provSubscriptions1, JSONArray jsubs, int sx) {
295         JSONObject jsub = jsubs.getJSONObject(sx);
296         if (jsub.optBoolean("suspend", false)) {
297             return;
298         }
299         String sid = gvas(jsub, "subid");
300         String fid = gvas(jsub, FEED_ID);
301         JSONObject jdel = jsub.getJSONObject("delivery");
302         String delurl = gvas(jdel, "url");
303         String id = gvas(jdel, "user");
304         String password = gvas(jdel, "password");
305         boolean monly = jsub.getBoolean("metadataOnly");
306         boolean use100 = jdel.getBoolean("use100");
307         boolean privilegedSubscriber = jsub.getBoolean("privilegedSubscriber");
308         boolean decompress = jsub.getBoolean("decompress");
309         boolean followRedirect = jsub.getBoolean("follow_redirect");
310         provSubscriptions1
311                 .add(new ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100,
312                         privilegedSubscriber, followRedirect, decompress));
313     }
314
315     private void addJSONParams(ArrayList<ProvNode> provNodes1, ArrayList<ProvParam> provParams1,
316             JSONObject jsonconfig) {
317         JSONObject jparams = jsonconfig.optJSONObject("parameters");
318         if (jparams != null) {
319             for (String pname : JSONObject.getNames(jparams)) {
320                 addJSONParam(provParams1, jparams, pname);
321             }
322             addJSONNodesToParams(provNodes1, jparams);
323         }
324     }
325
326     private void addJSONParam(ArrayList<ProvParam> provParams1, JSONObject jparams, String pname) {
327         String pvalue = gvas(jparams, pname);
328         if (pvalue != null) {
329             provParams1.add(new ProvParam(pname, pvalue));
330         }
331     }
332
333     private void addJSONNodesToParams(ArrayList<ProvNode> provNodes1, JSONObject jparams) {
334         String sfx = gvas(jparams, "PROV_DOMAIN");
335         JSONArray jnodes = jparams.optJSONArray("NODES");
336         if (jnodes != null) {
337             for (int nx = 0; nx < jnodes.length(); nx++) {
338                 String nn = gvas(jnodes, nx);
339                 if (nn == null) {
340                     continue;
341                 }
342                 if (nn.indexOf('.') == -1) {
343                     nn = nn + "." + sfx;
344                 }
345                 provNodes1.add(new ProvNode(nn));
346             }
347         }
348     }
349
350     private void addJSONRoutingInformation(ArrayList<ProvForceIngress> provForceIngresses1,
351             ArrayList<ProvForceEgress> provForceEgresses1, ArrayList<ProvHop> provHops1, JSONObject jsonConfig) {
352         JSONArray jingresses = jsonConfig.optJSONArray("ingress");
353         if (jingresses != null) {
354             for (int fx = 0; fx < jingresses.length(); fx++) {
355                 addJSONIngressRoute(provForceIngresses1, jingresses, fx);
356             }
357         }
358         JSONObject jegresses = jsonConfig.optJSONObject("egress");
359         if (jegresses != null && JSONObject.getNames(jegresses) != null) {
360             for (String esid : JSONObject.getNames(jegresses)) {
361                 addJSONEgressRoute(provForceEgresses1, jegresses, esid);
362             }
363         }
364         JSONArray jhops = jsonConfig.optJSONArray("routing");
365         if (jhops != null) {
366             for (int fx = 0; fx < jhops.length(); fx++) {
367                 addJSONRoutes(provHops1, jhops, fx);
368             }
369         }
370     }
371
372     private void addJSONIngressRoute(ArrayList<ProvForceIngress> provForceIngresses1, JSONArray jingresses, int fx) {
373         JSONObject jingress = jingresses.getJSONObject(fx);
374         String fid = gvas(jingress, FEED_ID);
375         String subnet = gvas(jingress, "subnet");
376         String user = gvas(jingress, "user");
377         String[] nodes = gvasa(jingress, "node");
378         if (fid == null || "".equals(fid)) {
379             return;
380         }
381         if ("".equals(subnet)) {
382             subnet = null;
383         }
384         if ("".equals(user)) {
385             user = null;
386         }
387         provForceIngresses1.add(new ProvForceIngress(fid, subnet, user, nodes));
388     }
389
390     private void addJSONEgressRoute(ArrayList<ProvForceEgress> provForceEgresses1, JSONObject jegresses, String esid) {
391         String enode = gvas(jegresses, esid);
392         if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
393             provForceEgresses1.add(new ProvForceEgress(esid, enode));
394         }
395     }
396
397     private void addJSONRoutes(ArrayList<ProvHop> provHops1, JSONArray jhops, int fx) {
398         JSONObject jhop = jhops.getJSONObject(fx);
399         String from = gvas(jhop, "from");
400         String to = gvas(jhop, "to");
401         String via = gvas(jhop, "via");
402         if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
403             return;
404         }
405         provHops1.add(new ProvHop(from, to, via));
406     }
407 }