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";
57 private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(ProvData.class);
58 private NodeConfig.ProvNode[] pn;
59 private NodeConfig.ProvParam[] pp;
60 private NodeConfig.ProvFeed[] pf;
61 private NodeConfig.ProvFeedUser[] pfu;
62 private NodeConfig.ProvFeedSubnet[] pfsn;
63 private NodeConfig.ProvSubscription[] ps;
64 private NodeConfig.ProvForceIngress[] pfi;
65 private NodeConfig.ProvForceEgress[] pfe;
66 private NodeConfig.ProvHop[] ph;
69 * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
72 * @param reader The reader for the JSON text.
74 public ProvData(Reader reader) throws IOException {
75 ArrayList<ProvNode> pnv = new ArrayList<>();
76 ArrayList<NodeConfig.ProvParam> ppv = new ArrayList<>();
77 ArrayList<NodeConfig.ProvFeed> pfv = new ArrayList<>();
78 ArrayList<NodeConfig.ProvFeedUser> pfuv = new ArrayList<>();
79 ArrayList<NodeConfig.ProvFeedSubnet> pfsnv = new ArrayList<>();
80 ArrayList<NodeConfig.ProvSubscription> psv = new ArrayList<>();
81 ArrayList<NodeConfig.ProvForceIngress> pfiv = new ArrayList<>();
82 ArrayList<NodeConfig.ProvForceEgress> pfev = new ArrayList<>();
83 ArrayList<NodeConfig.ProvHop> phv = new ArrayList<>();
85 JSONTokener jtx = new JSONTokener(reader);
86 JSONObject jcfg = new JSONObject(jtx);
87 char c = jtx.nextClean();
89 throw new JSONException("Spurious characters following configuration");
92 addJSONFeeds(pfv, pfuv, pfsnv, jcfg);
93 addJSONSubs(psv, jcfg);
94 addJSONParams(pnv, ppv, jcfg);
95 addJSONRoutingInformation(pfiv, pfev, phv, jcfg);
96 } catch (JSONException jse) {
97 NodeUtils.setIpAndFqdnForEelf("ProvData");
98 eelfLogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
100 .error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
101 throw new IOException(jse.toString(), jse);
103 pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
104 pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
105 pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
106 pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
107 pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
108 ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
109 pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
110 pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
111 ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
114 private static String[] gvasa(JSONObject object, String key) {
115 return (gvasa(object.opt(key)));
118 private static String[] gvasa(Object object) {
119 if (object instanceof JSONArray) {
120 JSONArray jsonArray = (JSONArray) object;
121 ArrayList<String> array = new ArrayList<>();
122 for (int i = 0; i < jsonArray.length(); i++) {
123 String string = gvas(jsonArray, i);
124 if (string != null) {
128 return (array.toArray(new String[array.size()]));
130 String string = gvas(object);
131 if (string == null) {
132 return (new String[0]);
134 return (new String[]{string});
139 private static String gvas(JSONArray array, int index) {
140 return (gvas(array.get(index)));
143 private static String gvas(JSONObject object, String key) {
144 return (gvas(object.opt(key)));
147 private static String gvas(Object object) {
148 if (object instanceof Boolean || object instanceof Number || object instanceof String) {
149 return (object.toString());
155 * Get the raw node configuration entries.
157 public NodeConfig.ProvNode[] getNodes() {
162 * Get the raw parameter configuration entries.
164 public NodeConfig.ProvParam[] getParams() {
169 * Ge the raw feed configuration entries.
171 public NodeConfig.ProvFeed[] getFeeds() {
176 * Get the raw feed user configuration entries.
178 public NodeConfig.ProvFeedUser[] getFeedUsers() {
183 * Get the raw feed subnet configuration entries.
185 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
190 * Get the raw subscription entries.
192 public NodeConfig.ProvSubscription[] getSubscriptions() {
197 * Get the raw forced ingress entries.
199 public NodeConfig.ProvForceIngress[] getForceIngress() {
204 * Get the raw forced egress entries.
206 public NodeConfig.ProvForceEgress[] getForceEgress() {
211 * Get the raw next hop entries.
213 public NodeConfig.ProvHop[] getHops() {
218 private String getFeedStatus(JSONObject jfeed) {
220 if (jfeed.optBoolean("suspend", false)) {
221 stat = "Feed is suspended";
223 if (jfeed.optBoolean("deleted", false)) {
224 stat = "Feed is deleted";
229 private void addJSONFeeds(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
231 JSONArray jfeeds = jcfg.optJSONArray("feeds");
232 if (jfeeds != null) {
233 for (int fx = 0; fx < jfeeds.length(); fx++) {
234 addJSONFeed(pfv, pfuv, pfsnv, jfeeds, fx);
239 private void addJSONFeed(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
240 JSONArray jfeeds, int fx) {
241 JSONObject jfeed = jfeeds.getJSONObject(fx);
242 String stat = getFeedStatus(jfeed);
243 String fid = gvas(jfeed, FEED_ID);
244 String fname = gvas(jfeed, "name");
245 String fver = gvas(jfeed, "version");
246 String createdDate = gvas(jfeed, "created_date");
248 * START - AAF changes
249 * TDP EPIC US# 307413
250 * Passing aafInstance to ProvFeed from feeds json passed by prov to identify legacy/AAF feeds
252 String aafInstance = gvas(jfeed, "aaf_instance");
253 pfv.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate, aafInstance));
257 addJSONFeedAuthArrays(pfuv, pfsnv, jfeed, fid);
260 private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv, JSONObject jfeed,
262 JSONObject jauth = jfeed.optJSONObject("authorization");
266 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
268 for (int ux = 0; ux < jeids.length(); ux++) {
269 JSONObject ju = jeids.getJSONObject(ux);
270 String login = gvas(ju, "id");
271 String password = gvas(ju, "password");
272 pfuv.add(new ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
275 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
277 for (int ix = 0; ix < jeips.length(); ix++) {
278 String sn = gvas(jeips, ix);
279 pfsnv.add(new ProvFeedSubnet(fid, sn));
284 private void addJSONSubs(ArrayList<ProvSubscription> psv, JSONObject jcfg) {
285 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
287 for (int sx = 0; sx < jsubs.length(); sx++) {
288 addJSONSub(psv, jsubs, sx);
293 private void addJSONSub(ArrayList<ProvSubscription> psv, JSONArray jsubs, int sx) {
294 JSONObject jsub = jsubs.getJSONObject(sx);
295 if (jsub.optBoolean("suspend", false)) {
298 String sid = gvas(jsub, "subid");
299 String fid = gvas(jsub, FEED_ID);
300 JSONObject jdel = jsub.getJSONObject("delivery");
301 String delurl = gvas(jdel, "url");
302 String id = gvas(jdel, "user");
303 String password = gvas(jdel, "password");
304 boolean monly = jsub.getBoolean("metadataOnly");
305 boolean use100 = jdel.getBoolean("use100");
306 boolean privilegedSubscriber = jsub.getBoolean("privilegedSubscriber");
307 boolean decompress = jsub.getBoolean("decompress");
308 boolean followRedirect = jsub.getBoolean("follow_redirect");
309 psv.add(new ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100,
310 privilegedSubscriber, followRedirect, decompress));
313 private void addJSONParams(ArrayList<ProvNode> pnv, ArrayList<ProvParam> ppv, JSONObject jcfg) {
314 JSONObject jparams = jcfg.optJSONObject("parameters");
315 if (jparams != null) {
316 for (String pname : JSONObject.getNames(jparams)) {
317 addJSONParam(ppv, jparams, pname);
319 addJSONNodesToParams(pnv, jparams);
323 private void addJSONParam(ArrayList<ProvParam> ppv, JSONObject jparams, String pname) {
324 String pvalue = gvas(jparams, pname);
325 if (pvalue != null) {
326 ppv.add(new ProvParam(pname, pvalue));
330 private void addJSONNodesToParams(ArrayList<ProvNode> pnv, JSONObject jparams) {
331 String sfx = gvas(jparams, "PROV_DOMAIN");
332 JSONArray jnodes = jparams.optJSONArray("NODES");
333 if (jnodes != null) {
334 for (int nx = 0; nx < jnodes.length(); nx++) {
335 String nn = gvas(jnodes, nx);
339 if (nn.indexOf('.') == -1) {
342 pnv.add(new ProvNode(nn));
347 private void addJSONRoutingInformation(ArrayList<ProvForceIngress> pfiv, ArrayList<ProvForceEgress> pfev,
348 ArrayList<ProvHop> phv, JSONObject jcfg) {
349 JSONArray jingresses = jcfg.optJSONArray("ingress");
350 if (jingresses != null) {
351 for (int fx = 0; fx < jingresses.length(); fx++) {
352 addJSONIngressRoute(pfiv, jingresses, fx);
355 JSONObject jegresses = jcfg.optJSONObject("egress");
356 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
357 for (String esid : JSONObject.getNames(jegresses)) {
358 addJSONEgressRoute(pfev, jegresses, esid);
361 JSONArray jhops = jcfg.optJSONArray("routing");
363 for (int fx = 0; fx < jhops.length(); fx++) {
364 addJSONRoutes(phv, jhops, fx);
369 private void addJSONIngressRoute(ArrayList<ProvForceIngress> pfiv, JSONArray jingresses, int fx) {
370 JSONObject jingress = jingresses.getJSONObject(fx);
371 String fid = gvas(jingress, FEED_ID);
372 String subnet = gvas(jingress, "subnet");
373 String user = gvas(jingress, "user");
374 if (fid == null || "".equals(fid)) {
377 if ("".equals(subnet)) {
380 if ("".equals(user)) {
383 String[] nodes = gvasa(jingress, "node");
384 pfiv.add(new ProvForceIngress(fid, subnet, user, nodes));
387 private void addJSONEgressRoute(ArrayList<ProvForceEgress> pfev, JSONObject jegresses, String esid) {
388 String enode = gvas(jegresses, esid);
389 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
390 pfev.add(new ProvForceEgress(esid, enode));
394 private void addJSONRoutes(ArrayList<ProvHop> phv, JSONArray jhops, int fx) {
395 JSONObject jhop = jhops.getJSONObject(fx);
396 String from = gvas(jhop, "from");
397 String to = gvas(jhop, "to");
398 String via = gvas(jhop, "via");
399 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
402 phv.add(new ProvHop(from, to, via));