1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
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;
49 * Parser for provisioning data from the provisioning server.
51 * <p>The ProvData class uses a Reader for the text configuration from the provisioning server to construct arrays of
52 * raw configuration entries.
54 public class ProvData {
56 private static final String FEED_ID = "feedid";
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;
70 * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
73 * @param reader The reader for the JSON text.
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<>();
86 JSONTokener jtx = new JSONTokener(reader);
87 JSONObject jcfg = new JSONObject(jtx);
88 char cch = jtx.nextClean();
90 throw new JSONException("Spurious characters following configuration");
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());
101 .error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
102 throw new IOException(jse.toString(), jse);
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()]);
115 private static String[] gvasa(JSONObject object, String key) {
116 return (gvasa(object.opt(key)));
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) {
129 return (array.toArray(new String[array.size()]));
131 String string = gvas(object);
132 if (string == null) {
133 return (new String[0]);
135 return (new String[]{string});
140 private static String gvas(JSONArray array, int index) {
141 return (gvas(array.get(index)));
144 private static String gvas(JSONObject object, String key) {
145 return (gvas(object.opt(key)));
148 private static String gvas(Object object) {
149 if (object instanceof Boolean || object instanceof Number || object instanceof String) {
150 return (object.toString());
156 * Get the raw node configuration entries.
158 public NodeConfig.ProvNode[] getNodes() {
163 * Get the raw parameter configuration entries.
165 public NodeConfig.ProvParam[] getParams() {
170 * Ge the raw feed configuration entries.
172 public NodeConfig.ProvFeed[] getFeeds() {
177 * Get the raw feed user configuration entries.
179 public NodeConfig.ProvFeedUser[] getFeedUsers() {
184 * Get the raw feed subnet configuration entries.
186 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
191 * Get the raw subscription entries.
193 public NodeConfig.ProvSubscription[] getSubscriptions() {
198 * Get the raw forced ingress entries.
200 public NodeConfig.ProvForceIngress[] getForceIngress() {
205 * Get the raw forced egress entries.
207 public NodeConfig.ProvForceEgress[] getForceEgress() {
212 * Get the raw next hop entries.
214 public NodeConfig.ProvHop[] getHops() {
219 private String getFeedStatus(JSONObject jfeed) {
221 if (jfeed.optBoolean("suspend", false)) {
222 stat = "Feed is suspended";
224 if (jfeed.optBoolean("deleted", false)) {
225 stat = "Feed is deleted";
230 private void addJSONFeeds(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
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);
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");
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
253 String aafInstance = gvas(jfeed, "aaf_instance");
254 pfv.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate, aafInstance));
258 addJSONFeedAuthArrays(pfuv, pfsnv, jfeed, fid);
261 private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv, JSONObject jfeed,
263 JSONObject jauth = jfeed.optJSONObject("authorization");
267 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
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)));
276 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
278 for (int ix = 0; ix < jeips.length(); ix++) {
279 String sn = gvas(jeips, ix);
280 pfsnv.add(new ProvFeedSubnet(fid, sn));
285 private void addJSONSubs(ArrayList<ProvSubscription> psv, JSONObject jcfg) {
286 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
288 for (int sx = 0; sx < jsubs.length(); sx++) {
289 addJSONSub(psv, jsubs, sx);
294 private void addJSONSub(ArrayList<ProvSubscription> psv, JSONArray jsubs, int sx) {
295 JSONObject jsub = jsubs.getJSONObject(sx);
296 if (jsub.optBoolean("suspend", false)) {
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));
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);
320 addJSONNodesToParams(pnv, jparams);
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));
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);
340 if (nn.indexOf('.') == -1) {
343 pnv.add(new ProvNode(nn));
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);
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);
362 JSONArray jhops = jcfg.optJSONArray("routing");
364 for (int fx = 0; fx < jhops.length(); fx++) {
365 addJSONRoutes(phv, jhops, fx);
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)) {
378 if ("".equals(subnet)) {
381 if ("".equals(user)) {
384 String[] nodes = gvasa(jingress, "node");
385 pfiv.add(new ProvForceIngress(fid, subnet, user, nodes));
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));
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)) {
403 phv.add(new ProvHop(from, to, via));