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