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.config;
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.config.NodeConfig.ProvFeed;
38 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvFeedSubnet;
39 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvFeedUser;
40 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvForceEgress;
41 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvForceIngress;
42 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvHop;
43 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvNode;
44 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvParam;
45 import org.onap.dmaap.datarouter.node.config.NodeConfig.ProvSubscription;
46 import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
47 import org.onap.dmaap.datarouter.node.utils.NodeUtils;
50 * Parser for provisioning data from the provisioning server.
52 * <p>The ProvData class uses a Reader for the text configuration from the provisioning server to construct arrays of
53 * raw configuration entries.
55 public class ProvData {
57 private static final String FEED_ID = "feedid";
59 private static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(ProvData.class);
60 private final NodeConfig.ProvNode[] provNodes;
61 private final NodeConfig.ProvParam[] provParams;
62 private final NodeConfig.ProvFeed[] provFeeds;
63 private final NodeConfig.ProvFeedUser[] provFeedUsers;
64 private final NodeConfig.ProvFeedSubnet[] provFeedSubnets;
65 private final NodeConfig.ProvSubscription[] provSubscriptions;
66 private final NodeConfig.ProvForceIngress[] provForceIngresses;
67 private final NodeConfig.ProvForceEgress[] provForceEgresses;
68 private final NodeConfig.ProvHop[] provHops;
71 * Construct raw provisioing data entries from the text (JSON) provisioning document received from the provisioning
74 * @param reader The reader for the JSON text.
76 public ProvData(Reader reader) throws IOException {
77 ArrayList<ProvNode> provNodeArrayList = new ArrayList<>();
78 ArrayList<NodeConfig.ProvParam> provParamArrayList = new ArrayList<>();
79 ArrayList<NodeConfig.ProvFeed> provFeedArrayList = new ArrayList<>();
80 ArrayList<NodeConfig.ProvFeedUser> provFeedUserArrayList = new ArrayList<>();
81 ArrayList<NodeConfig.ProvFeedSubnet> provFeedSubnetArrayList = new ArrayList<>();
82 ArrayList<NodeConfig.ProvSubscription> provSubscriptionArrayList = new ArrayList<>();
83 ArrayList<NodeConfig.ProvForceIngress> provForceIngressArrayList = new ArrayList<>();
84 ArrayList<NodeConfig.ProvForceEgress> provForceEgressArrayList = new ArrayList<>();
85 ArrayList<NodeConfig.ProvHop> provHopArrayList = new ArrayList<>();
87 JSONTokener jsonTokener = new JSONTokener(reader);
88 JSONObject jsonObject = new JSONObject(jsonTokener);
89 char nextCleanChar = jsonTokener.nextClean();
90 if (nextCleanChar != '\0') {
91 throw new JSONException("Spurious characters following configuration");
94 addJSONFeeds(provFeedArrayList, provFeedUserArrayList, provFeedSubnetArrayList, jsonObject);
95 addJSONSubs(provSubscriptionArrayList, jsonObject);
96 addJSONParams(provNodeArrayList, provParamArrayList, jsonObject);
97 addJSONRoutingInformation(provForceIngressArrayList, provForceEgressArrayList, provHopArrayList, jsonObject);
98 } catch (JSONException jse) {
99 NodeUtils.setIpAndFqdnForEelf("ProvData");
100 eelfLogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
102 .error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
103 throw new IOException(jse.toString(), jse);
105 provNodes = provNodeArrayList.toArray(new ProvNode[0]);
106 provParams = provParamArrayList.toArray(new ProvParam[0]);
107 provFeeds = provFeedArrayList.toArray(new ProvFeed[0]);
108 provFeedUsers = provFeedUserArrayList.toArray(new ProvFeedUser[0]);
109 provFeedSubnets = provFeedSubnetArrayList.toArray(new ProvFeedSubnet[0]);
110 provSubscriptions = provSubscriptionArrayList.toArray(new ProvSubscription[0]);
111 provForceIngresses = provForceIngressArrayList.toArray(new ProvForceIngress[0]);
112 provForceEgresses = provForceEgressArrayList.toArray(new ProvForceEgress[0]);
113 provHops = provHopArrayList.toArray(new ProvHop[0]);
116 private static String[] gvasa(JSONObject object, String key) {
117 return (gvasa(object.opt(key)));
120 private static String[] gvasa(Object object) {
121 if (object instanceof JSONArray) {
122 JSONArray jsonArray = (JSONArray) object;
123 ArrayList<String> array = new ArrayList<>();
124 for (int i = 0; i < jsonArray.length(); i++) {
125 String string = gvas(jsonArray, i);
126 if (string != null) {
130 return (array.toArray(new String[array.size()]));
132 String string = gvas(object);
133 if (string == null) {
134 return (new String[0]);
136 return (new String[]{string});
141 private static String gvas(JSONArray array, int index) {
142 return (gvas(array.get(index)));
145 private static String gvas(JSONObject object, String key) {
146 return (gvas(object.opt(key)));
149 private static String gvas(Object object) {
150 if (object instanceof Boolean || object instanceof Number || object instanceof String) {
151 return (object.toString());
157 * Get the raw node configuration entries.
159 public NodeConfig.ProvNode[] getNodes() {
164 * Get the raw parameter configuration entries.
166 public NodeConfig.ProvParam[] getParams() {
171 * Ge the raw feed configuration entries.
173 public NodeConfig.ProvFeed[] getFeeds() {
178 * Get the raw feed user configuration entries.
180 public NodeConfig.ProvFeedUser[] getFeedUsers() {
181 return (provFeedUsers);
185 * Get the raw feed subnet configuration entries.
187 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
188 return (provFeedSubnets);
192 * Get the raw subscription entries.
194 public NodeConfig.ProvSubscription[] getSubscriptions() {
195 return (provSubscriptions);
199 * Get the raw forced ingress entries.
201 public NodeConfig.ProvForceIngress[] getForceIngress() {
202 return (provForceIngresses);
206 * Get the raw forced egress entries.
208 public NodeConfig.ProvForceEgress[] getForceEgress() {
209 return (provForceEgresses);
213 * Get the raw next hop entries.
215 public NodeConfig.ProvHop[] getHops() {
220 private String getFeedStatus(JSONObject jfeed) {
222 if (jfeed.optBoolean("suspend", false)) {
223 stat = "Feed is suspended";
225 if (jfeed.optBoolean("deleted", false)) {
226 stat = "Feed is deleted";
231 private void addJSONFeeds(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
233 JSONArray jfeeds = jcfg.optJSONArray("feeds");
234 if (jfeeds != null) {
235 for (int fx = 0; fx < jfeeds.length(); fx++) {
236 addJSONFeed(pfv, pfuv, pfsnv, jfeeds, fx);
241 private void addJSONFeed(ArrayList<ProvFeed> pfv, ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv,
242 JSONArray jfeeds, int fx) {
243 JSONObject jfeed = jfeeds.getJSONObject(fx);
244 String stat = getFeedStatus(jfeed);
245 String fid = gvas(jfeed, FEED_ID);
246 String fname = gvas(jfeed, "name");
247 String fver = gvas(jfeed, "version");
248 String createdDate = gvas(jfeed, "created_date");
249 pfv.add(new ProvFeed(fid, fname + "//" + fver, stat, createdDate));
250 addJSONFeedAuthArrays(pfuv, pfsnv, jfeed, fid);
253 private void addJSONFeedAuthArrays(ArrayList<ProvFeedUser> pfuv, ArrayList<ProvFeedSubnet> pfsnv, JSONObject jfeed,
255 JSONObject jauth = jfeed.optJSONObject("authorization");
259 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
261 for (int ux = 0; ux < jeids.length(); ux++) {
262 JSONObject ju = jeids.getJSONObject(ux);
263 String login = gvas(ju, "id");
264 String password = gvas(ju, "password");
265 pfuv.add(new ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
268 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
270 for (int ix = 0; ix < jeips.length(); ix++) {
271 String sn = gvas(jeips, ix);
272 pfsnv.add(new ProvFeedSubnet(fid, sn));
277 private void addJSONSubs(ArrayList<ProvSubscription> psv, JSONObject jcfg) {
278 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
280 for (int sx = 0; sx < jsubs.length(); sx++) {
281 addJSONSub(psv, jsubs, sx);
286 private void addJSONSub(ArrayList<ProvSubscription> psv, JSONArray jsubs, int sx) {
287 JSONObject jsub = jsubs.getJSONObject(sx);
288 if (jsub.optBoolean("suspend", false)) {
291 String sid = gvas(jsub, "subid");
292 String fid = gvas(jsub, FEED_ID);
293 JSONObject jdel = jsub.getJSONObject("delivery");
294 String delurl = gvas(jdel, "url");
295 String id = gvas(jdel, "user");
296 String password = gvas(jdel, "password");
297 boolean monly = jsub.getBoolean("metadataOnly");
298 boolean use100 = jdel.getBoolean("use100");
299 boolean privilegedSubscriber = jsub.getBoolean("privilegedSubscriber");
300 boolean decompress = jsub.getBoolean("decompress");
301 boolean followRedirect = jsub.getBoolean("follow_redirect");
302 psv.add(new ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100,
303 privilegedSubscriber, followRedirect, decompress));
306 private void addJSONParams(ArrayList<ProvNode> pnv, ArrayList<ProvParam> ppv, JSONObject jcfg) {
307 JSONObject jparams = jcfg.optJSONObject("parameters");
308 if (jparams != null) {
309 for (String pname : JSONObject.getNames(jparams)) {
310 addJSONParam(ppv, jparams, pname);
312 addJSONNodesToParams(pnv, jparams);
316 private void addJSONParam(ArrayList<ProvParam> ppv, JSONObject jparams, String pname) {
317 String pvalue = gvas(jparams, pname);
318 if (pvalue != null) {
319 ppv.add(new ProvParam(pname, pvalue));
323 private void addJSONNodesToParams(ArrayList<ProvNode> pnv, JSONObject jparams) {
324 String sfx = gvas(jparams, "PROV_DOMAIN");
325 JSONArray jnodes = jparams.optJSONArray("NODES");
326 if (jnodes != null) {
327 for (int nx = 0; nx < jnodes.length(); nx++) {
328 String nn = gvas(jnodes, nx);
332 if (nn.indexOf('.') == -1) {
335 pnv.add(new ProvNode(nn));
340 private void addJSONRoutingInformation(ArrayList<ProvForceIngress> pfiv, ArrayList<ProvForceEgress> pfev,
341 ArrayList<ProvHop> phv, JSONObject jcfg) {
342 JSONArray jingresses = jcfg.optJSONArray("ingress");
343 if (jingresses != null) {
344 for (int fx = 0; fx < jingresses.length(); fx++) {
345 addJSONIngressRoute(pfiv, jingresses, fx);
348 JSONObject jegresses = jcfg.optJSONObject("egress");
349 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
350 for (String esid : JSONObject.getNames(jegresses)) {
351 addJSONEgressRoute(pfev, jegresses, esid);
354 JSONArray jhops = jcfg.optJSONArray("routing");
356 for (int fx = 0; fx < jhops.length(); fx++) {
357 addJSONRoutes(phv, jhops, fx);
362 private void addJSONIngressRoute(ArrayList<ProvForceIngress> pfiv, JSONArray jingresses, int fx) {
363 JSONObject jingress = jingresses.getJSONObject(fx);
364 String fid = gvas(jingress, FEED_ID);
365 String subnet = gvas(jingress, "subnet");
366 String user = gvas(jingress, "user");
367 if (fid == null || "".equals(fid)) {
370 if ("".equals(subnet)) {
373 if ("".equals(user)) {
376 String[] nodes = gvasa(jingress, "node");
377 pfiv.add(new ProvForceIngress(fid, subnet, user, nodes));
380 private void addJSONEgressRoute(ArrayList<ProvForceEgress> pfev, JSONObject jegresses, String esid) {
381 String enode = gvas(jegresses, esid);
382 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
383 pfev.add(new ProvForceEgress(esid, enode));
387 private void addJSONRoutes(ArrayList<ProvHop> phv, JSONArray jhops, int fx) {
388 JSONObject jhop = jhops.getJSONObject(fx);
389 String from = gvas(jhop, "from");
390 String to = gvas(jhop, "to");
391 String via = gvas(jhop, "via");
392 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
395 phv.add(new ProvHop(from, to, via));