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