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 * The ProvData class uses a Reader for the text configuration from the provisioning server to construct arrays of raw
52 * 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[] provNodes;
59 private NodeConfig.ProvParam[] provParams;
60 private NodeConfig.ProvFeed[] provFeeds;
61 private NodeConfig.ProvFeedUser[] provFeedUsers;
62 private NodeConfig.ProvFeedSubnet[] provFeedSubnets;
63 private NodeConfig.ProvSubscription[] provSubscriptions;
64 private NodeConfig.ProvForceIngress[] provForceIngresses;
65 private NodeConfig.ProvForceEgress[] provForceEgresses;
66 private NodeConfig.ProvHop[] provHops;
69 * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
72 * @param r The reader for the JSON text.
74 public ProvData(Reader r) throws IOException {
75 ArrayList<ProvNode> provNodes1 = new ArrayList<>();
76 ArrayList<NodeConfig.ProvParam> provParams1 = new ArrayList<>();
77 ArrayList<NodeConfig.ProvFeed> provFeeds1 = new ArrayList<>();
78 ArrayList<NodeConfig.ProvFeedUser> provFeedUsers1 = new ArrayList<>();
79 ArrayList<NodeConfig.ProvFeedSubnet> provFeedSubnets1 = new ArrayList<>();
80 ArrayList<NodeConfig.ProvSubscription> provSubscriptions1 = new ArrayList<>();
81 ArrayList<NodeConfig.ProvForceIngress> provForceIngresses1 = new ArrayList<>();
82 ArrayList<NodeConfig.ProvForceEgress> provForceEgresses1 = new ArrayList<>();
83 ArrayList<NodeConfig.ProvHop> provHops1 = new ArrayList<>();
85 JSONTokener jtx = new JSONTokener(r);
86 JSONObject jcfg = new JSONObject(jtx);
87 char c = jtx.nextClean();
89 throw new JSONException("Spurious characters following configuration");
92 addJSONFeeds(provFeeds1, provFeedUsers1, provFeedSubnets1, jcfg);
93 addJSONSubs(provSubscriptions1, jcfg);
94 addJSONParams(provNodes1, provParams1, jcfg);
95 addJSONRoutingInformation(provForceIngresses1, provForceEgresses1, provHops1, 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 provNodes = provNodes1.toArray(new NodeConfig.ProvNode[provNodes1.size()]);
104 provParams = provParams1.toArray(new NodeConfig.ProvParam[provParams1.size()]);
105 provFeeds = provFeeds1.toArray(new NodeConfig.ProvFeed[provFeeds1.size()]);
106 provFeedUsers = provFeedUsers1.toArray(new NodeConfig.ProvFeedUser[provFeedUsers1.size()]);
107 provFeedSubnets = provFeedSubnets1.toArray(new NodeConfig.ProvFeedSubnet[provFeedSubnets1.size()]);
108 provSubscriptions = provSubscriptions1.toArray(new NodeConfig.ProvSubscription[provSubscriptions1.size()]);
109 provForceIngresses = provForceIngresses1.toArray(new NodeConfig.ProvForceIngress[provForceIngresses1.size()]);
110 provForceEgresses = provForceEgresses1.toArray(new NodeConfig.ProvForceEgress[provForceEgresses1.size()]);
111 provHops = provHops1.toArray(new NodeConfig.ProvHop[provHops1.size()]);
114 private static String[] gvasa(JSONObject o, String key) {
115 return (gvasa(o.opt(key)));
118 private static String[] gvasa(Object o) {
119 if (o instanceof JSONArray) {
120 JSONArray a = (JSONArray) o;
121 ArrayList<String> v = new ArrayList<>();
122 for (int i = 0; i < a.length(); i++) {
123 String s = gvas(a, i);
128 return (v.toArray(new String[v.size()]));
132 return (new String[0]);
134 return (new String[]{s});
139 private static String gvas(JSONArray a, int index) {
140 return (gvas(a.get(index)));
143 private static String gvas(JSONObject o, String key) {
144 return (gvas(o.opt(key)));
147 private static String gvas(Object o) {
148 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
149 return (o.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() {
179 return (provFeedUsers);
183 * Get the raw feed subnet configuration entries
185 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
186 return (provFeedSubnets);
190 * Get the raw subscription entries
192 public NodeConfig.ProvSubscription[] getSubscriptions() {
193 return (provSubscriptions);
197 * Get the raw forced ingress entries
199 public NodeConfig.ProvForceIngress[] getForceIngress() {
200 return (provForceIngresses);
204 * Get the raw forced egress entries
206 public NodeConfig.ProvForceEgress[] getForceEgress() {
207 return (provForceEgresses);
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> provFeeds1, ArrayList<ProvFeedUser> provFeedUsers1,
230 ArrayList<ProvFeedSubnet> provFeedSubnets1,
231 JSONObject jsonConfig) {
232 JSONArray jfeeds = jsonConfig.optJSONArray("feeds");
233 if (jfeeds != null) {
234 for (int fx = 0; fx < jfeeds.length(); fx++) {
235 addJSONFeed(provFeeds1, provFeedUsers1, provFeedSubnets1, jfeeds, fx);
240 private void addJSONFeed(ArrayList<ProvFeed> provFeeds1, ArrayList<ProvFeedUser> provFeedUsers1,
241 ArrayList<ProvFeedSubnet> provFeedSubnets1, JSONArray jfeeds, int feedIndex) {
242 JSONObject jfeed = jfeeds.getJSONObject(feedIndex);
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 provFeeds1.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate, aafInstance));
258 addJSONFeedAuthArrays(provFeedUsers1, provFeedSubnets1, jfeed, fid);
261 private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> provFeedUsers1,
262 ArrayList<ProvFeedSubnet> provFeedSubnets1, JSONObject jfeed, String fid) {
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 provFeedUsers1.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 provFeedSubnets1.add(new ProvFeedSubnet(fid, sn));
285 private void addJSONSubs(ArrayList<ProvSubscription> provSubscriptions1, JSONObject jsonConfig) {
286 JSONArray jsubs = jsonConfig.optJSONArray("subscriptions");
288 for (int sx = 0; sx < jsubs.length(); sx++) {
289 addJSONSub(provSubscriptions1, jsubs, sx);
294 private void addJSONSub(ArrayList<ProvSubscription> provSubscriptions1, 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");
311 .add(new ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100,
312 privilegedSubscriber, followRedirect, decompress));
315 private void addJSONParams(ArrayList<ProvNode> provNodes1, ArrayList<ProvParam> provParams1,
316 JSONObject jsonconfig) {
317 JSONObject jparams = jsonconfig.optJSONObject("parameters");
318 if (jparams != null) {
319 for (String pname : JSONObject.getNames(jparams)) {
320 addJSONParam(provParams1, jparams, pname);
322 addJSONNodesToParams(provNodes1, jparams);
326 private void addJSONParam(ArrayList<ProvParam> provParams1, JSONObject jparams, String pname) {
327 String pvalue = gvas(jparams, pname);
328 if (pvalue != null) {
329 provParams1.add(new ProvParam(pname, pvalue));
333 private void addJSONNodesToParams(ArrayList<ProvNode> provNodes1, JSONObject jparams) {
334 String sfx = gvas(jparams, "PROV_DOMAIN");
335 JSONArray jnodes = jparams.optJSONArray("NODES");
336 if (jnodes != null) {
337 for (int nx = 0; nx < jnodes.length(); nx++) {
338 String nn = gvas(jnodes, nx);
342 if (nn.indexOf('.') == -1) {
345 provNodes1.add(new ProvNode(nn));
350 private void addJSONRoutingInformation(ArrayList<ProvForceIngress> provForceIngresses1,
351 ArrayList<ProvForceEgress> provForceEgresses1, ArrayList<ProvHop> provHops1, JSONObject jsonConfig) {
352 JSONArray jingresses = jsonConfig.optJSONArray("ingress");
353 if (jingresses != null) {
354 for (int fx = 0; fx < jingresses.length(); fx++) {
355 addJSONIngressRoute(provForceIngresses1, jingresses, fx);
358 JSONObject jegresses = jsonConfig.optJSONObject("egress");
359 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
360 for (String esid : JSONObject.getNames(jegresses)) {
361 addJSONEgressRoute(provForceEgresses1, jegresses, esid);
364 JSONArray jhops = jsonConfig.optJSONArray("routing");
366 for (int fx = 0; fx < jhops.length(); fx++) {
367 addJSONRoutes(provHops1, jhops, fx);
372 private void addJSONIngressRoute(ArrayList<ProvForceIngress> provForceIngresses1, JSONArray jingresses, int fx) {
373 JSONObject jingress = jingresses.getJSONObject(fx);
374 String fid = gvas(jingress, FEED_ID);
375 String subnet = gvas(jingress, "subnet");
376 String user = gvas(jingress, "user");
377 String[] nodes = gvasa(jingress, "node");
378 if (fid == null || "".equals(fid)) {
381 if ("".equals(subnet)) {
384 if ("".equals(user)) {
387 provForceIngresses1.add(new ProvForceIngress(fid, subnet, user, nodes));
390 private void addJSONEgressRoute(ArrayList<ProvForceEgress> provForceEgresses1, JSONObject jegresses, String esid) {
391 String enode = gvas(jegresses, esid);
392 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
393 provForceEgresses1.add(new ProvForceEgress(esid, enode));
397 private void addJSONRoutes(ArrayList<ProvHop> provHops1, JSONArray jhops, int fx) {
398 JSONObject jhop = jhops.getJSONObject(fx);
399 String from = gvas(jhop, "from");
400 String to = gvas(jhop, "to");
401 String via = gvas(jhop, "via");
402 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
405 provHops1.add(new ProvHop(from, to, via));