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