Fix checkstyle contridictions on datarouter 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  *
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
58     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(ProvData.class);
59     private NodeConfig.ProvNode[] pn;
60     private NodeConfig.ProvParam[] pp;
61     private NodeConfig.ProvFeed[] pf;
62     private NodeConfig.ProvFeedUser[] pfu;
63     private NodeConfig.ProvFeedSubnet[] pfsn;
64     private NodeConfig.ProvSubscription[] ps;
65     private NodeConfig.ProvForceIngress[] pfi;
66     private NodeConfig.ProvForceEgress[] pfe;
67     private NodeConfig.ProvHop[] ph;
68
69     /**
70      * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
71      * server.
72      *
73      * @param reader The reader for the JSON text.
74      */
75     public ProvData(Reader reader) throws IOException {
76         ArrayList<ProvNode> pnv = new ArrayList<>();
77         ArrayList<NodeConfig.ProvParam> ppv = new ArrayList<>();
78         ArrayList<NodeConfig.ProvFeed> pfv = new ArrayList<>();
79         ArrayList<NodeConfig.ProvFeedUser> pfuv = new ArrayList<>();
80         ArrayList<NodeConfig.ProvFeedSubnet> pfsnv = new ArrayList<>();
81         ArrayList<NodeConfig.ProvSubscription> psv = new ArrayList<>();
82         ArrayList<NodeConfig.ProvForceIngress> pfiv = new ArrayList<>();
83         ArrayList<NodeConfig.ProvForceEgress> pfev = new ArrayList<>();
84         ArrayList<NodeConfig.ProvHop> phv = new ArrayList<>();
85         try {
86             JSONTokener jtx = new JSONTokener(reader);
87             JSONObject jcfg = new JSONObject(jtx);
88             char cch = jtx.nextClean();
89             if (cch != '\0') {
90                 throw new JSONException("Spurious characters following configuration");
91             }
92             reader.close();
93             addJSONFeeds(pfv, pfuv, pfsnv, jcfg);
94             addJSONSubs(psv, jcfg);
95             addJSONParams(pnv, ppv, jcfg);
96             addJSONRoutingInformation(pfiv, pfev, phv, jcfg);
97         } catch (JSONException jse) {
98             NodeUtils.setIpAndFqdnForEelf("ProvData");
99             eelfLogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
100             eelfLogger
101                     .error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
102             throw new IOException(jse.toString(), jse);
103         }
104         pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
105         pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
106         pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
107         pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
108         pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
109         ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
110         pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
111         pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
112         ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
113     }
114
115     private static String[] gvasa(JSONObject object, String key) {
116         return (gvasa(object.opt(key)));
117     }
118
119     private static String[] gvasa(Object object) {
120         if (object instanceof JSONArray) {
121             JSONArray jsonArray = (JSONArray) object;
122             ArrayList<String> array = new ArrayList<>();
123             for (int i = 0; i < jsonArray.length(); i++) {
124                 String string = gvas(jsonArray, i);
125                 if (string != null) {
126                     array.add(string);
127                 }
128             }
129             return (array.toArray(new String[array.size()]));
130         } else {
131             String string = gvas(object);
132             if (string == null) {
133                 return (new String[0]);
134             } else {
135                 return (new String[]{string});
136             }
137         }
138     }
139
140     private static String gvas(JSONArray array, int index) {
141         return (gvas(array.get(index)));
142     }
143
144     private static String gvas(JSONObject object, String key) {
145         return (gvas(object.opt(key)));
146     }
147
148     private static String gvas(Object object) {
149         if (object instanceof Boolean || object instanceof Number || object instanceof String) {
150             return (object.toString());
151         }
152         return (null);
153     }
154
155     /**
156      * Get the raw node configuration entries.
157      */
158     public NodeConfig.ProvNode[] getNodes() {
159         return (pn);
160     }
161
162     /**
163      * Get the raw parameter configuration entries.
164      */
165     public NodeConfig.ProvParam[] getParams() {
166         return (pp);
167     }
168
169     /**
170      * Ge the raw feed configuration entries.
171      */
172     public NodeConfig.ProvFeed[] getFeeds() {
173         return (pf);
174     }
175
176     /**
177      * Get the raw feed user configuration entries.
178      */
179     public NodeConfig.ProvFeedUser[] getFeedUsers() {
180         return (pfu);
181     }
182
183     /**
184      * Get the raw feed subnet configuration entries.
185      */
186     public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
187         return (pfsn);
188     }
189
190     /**
191      * Get the raw subscription entries.
192      */
193     public NodeConfig.ProvSubscription[] getSubscriptions() {
194         return (ps);
195     }
196
197     /**
198      * Get the raw forced ingress entries.
199      */
200     public NodeConfig.ProvForceIngress[] getForceIngress() {
201         return (pfi);
202     }
203
204     /**
205      * Get the raw forced egress entries.
206      */
207     public NodeConfig.ProvForceEgress[] getForceEgress() {
208         return (pfe);
209     }
210
211     /**
212      * Get the raw next hop entries.
213      */
214     public NodeConfig.ProvHop[] getHops() {
215         return (ph);
216     }
217
218     @Nullable
219     private String getFeedStatus(JSONObject jfeed) {
220         String stat = null;
221         if (jfeed.optBoolean("suspend", false)) {
222             stat = "Feed is suspended";
223         }
224         if (jfeed.optBoolean("deleted", false)) {
225             stat = "Feed is deleted";
226         }
227         return stat;
228     }
229
230     private void addJSONFeeds(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
231             JSONObject jcfg) {
232         JSONArray jfeeds = jcfg.optJSONArray("feeds");
233         if (jfeeds != null) {
234             for (int fx = 0; fx < jfeeds.length(); fx++) {
235                 addJSONFeed(pfv, pfuv, pfsnv, jfeeds, fx);
236             }
237         }
238     }
239
240     private void addJSONFeed(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
241             JSONArray jfeeds, int fx) {
242         JSONObject jfeed = jfeeds.getJSONObject(fx);
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         pfv.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate, aafInstance));
255         /*
256          * END - AAF changes
257          */
258         addJSONFeedAuthArrays(pfuv, pfsnv, jfeed, fid);
259     }
260
261     private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv, JSONObject jfeed,
262             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                 pfuv.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                 pfsnv.add(new ProvFeedSubnet(fid, sn));
281             }
282         }
283     }
284
285     private void addJSONSubs(ArrayList<ProvSubscription> psv, JSONObject jcfg) {
286         JSONArray jsubs = jcfg.optJSONArray("subscriptions");
287         if (jsubs != null) {
288             for (int sx = 0; sx < jsubs.length(); sx++) {
289                 addJSONSub(psv, jsubs, sx);
290             }
291         }
292     }
293
294     private void addJSONSub(ArrayList<ProvSubscription> psv, 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         psv.add(new ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100,
311                 privilegedSubscriber, followRedirect, decompress));
312     }
313
314     private void addJSONParams(ArrayList<ProvNode> pnv, ArrayList<ProvParam> ppv, JSONObject jcfg) {
315         JSONObject jparams = jcfg.optJSONObject("parameters");
316         if (jparams != null) {
317             for (String pname : JSONObject.getNames(jparams)) {
318                 addJSONParam(ppv, jparams, pname);
319             }
320             addJSONNodesToParams(pnv, jparams);
321         }
322     }
323
324     private void addJSONParam(ArrayList<ProvParam> ppv, JSONObject jparams, String pname) {
325         String pvalue = gvas(jparams, pname);
326         if (pvalue != null) {
327             ppv.add(new ProvParam(pname, pvalue));
328         }
329     }
330
331     private void addJSONNodesToParams(ArrayList<ProvNode> pnv, JSONObject jparams) {
332         String sfx = gvas(jparams, "PROV_DOMAIN");
333         JSONArray jnodes = jparams.optJSONArray("NODES");
334         if (jnodes != null) {
335             for (int nx = 0; nx < jnodes.length(); nx++) {
336                 String nn = gvas(jnodes, nx);
337                 if (nn == null) {
338                     continue;
339                 }
340                 if (nn.indexOf('.') == -1) {
341                     nn = nn + "." + sfx;
342                 }
343                 pnv.add(new ProvNode(nn));
344             }
345         }
346     }
347
348     private void addJSONRoutingInformation(ArrayList<ProvForceIngress> pfiv, ArrayList<ProvForceEgress> pfev,
349             ArrayList<ProvHop> phv, JSONObject jcfg) {
350         JSONArray jingresses = jcfg.optJSONArray("ingress");
351         if (jingresses != null) {
352             for (int fx = 0; fx < jingresses.length(); fx++) {
353                 addJSONIngressRoute(pfiv, jingresses, fx);
354             }
355         }
356         JSONObject jegresses = jcfg.optJSONObject("egress");
357         if (jegresses != null && JSONObject.getNames(jegresses) != null) {
358             for (String esid : JSONObject.getNames(jegresses)) {
359                 addJSONEgressRoute(pfev, jegresses, esid);
360             }
361         }
362         JSONArray jhops = jcfg.optJSONArray("routing");
363         if (jhops != null) {
364             for (int fx = 0; fx < jhops.length(); fx++) {
365                 addJSONRoutes(phv, jhops, fx);
366             }
367         }
368     }
369
370     private void addJSONIngressRoute(ArrayList<ProvForceIngress> pfiv, JSONArray jingresses, int fx) {
371         JSONObject jingress = jingresses.getJSONObject(fx);
372         String fid = gvas(jingress, FEED_ID);
373         String subnet = gvas(jingress, "subnet");
374         String user = gvas(jingress, "user");
375         if (fid == null || "".equals(fid)) {
376             return;
377         }
378         if ("".equals(subnet)) {
379             subnet = null;
380         }
381         if ("".equals(user)) {
382             user = null;
383         }
384         String[] nodes = gvasa(jingress, "node");
385         pfiv.add(new ProvForceIngress(fid, subnet, user, nodes));
386     }
387
388     private void addJSONEgressRoute(ArrayList<ProvForceEgress> pfev, JSONObject jegresses, String esid) {
389         String enode = gvas(jegresses, esid);
390         if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
391             pfev.add(new ProvForceEgress(esid, enode));
392         }
393     }
394
395     private void addJSONRoutes(ArrayList<ProvHop> phv, JSONArray jhops, int fx) {
396         JSONObject jhop = jhops.getJSONObject(fx);
397         String from = gvas(jhop, "from");
398         String to = gvas(jhop, "to");
399         String via = gvas(jhop, "via");
400         if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
401             return;
402         }
403         phv.add(new ProvHop(from, to, via));
404     }
405 }