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;
31 import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
32 import org.apache.log4j.Logger;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
38 * Parser for provisioning data from the provisioning server.
40 * The ProvData class uses a Reader for the text configuration from the
41 * provisioning server to construct arrays of raw configuration entries.
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;
56 private static String[] gvasa(JSONArray a, int index) {
57 return (gvasa(a.get(index)));
60 private static String[] gvasa(JSONObject o, String key) {
61 return (gvasa(o.opt(key)));
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);
74 return (v.toArray(new String[v.size()]));
78 return (new String[0]);
80 return (new String[]{s});
85 private static String gvas(JSONArray a, int index) {
86 return (gvas(a.get(index)));
89 private static String gvas(JSONObject o, String key) {
90 return (gvas(o.opt(key)));
93 private static String gvas(Object o) {
94 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
95 return (o.toString());
101 * Construct raw provisioing data entries from the text (JSON)
102 * provisioning document received from the provisioning server
104 * @param r The reader for the JSON text.
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>();
117 JSONTokener jtx = new JSONTokener(r);
118 JSONObject jcfg = new JSONObject(jtx);
119 char c = jtx.nextClean();
121 throw new JSONException("Spurious characters following configuration");
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);
129 if (jfeed.optBoolean("suspend", false)) {
130 stat = "Feed is suspended";
132 if (jfeed.optBoolean("deleted", false)) {
133 stat = "Feed is deleted";
135 String fid = gvas(jfeed, "feedid");
136 String fname = gvas(jfeed, "name");
137 String fver = gvas(jfeed, "version");
138 String createdDate = gvas(jfeed, "created_date");
140 * START - AAF changes
141 * TDP EPIC US# 307413
142 * Passing aafInstance to ProvFeed from feeds json passed by prov to identify legacy/AAF feeds
144 String aafInstance = gvas(jfeed, "aaf_instance");
145 pfv.add(new NodeConfig.ProvFeed(fid, fname + "//" + fver, stat,createdDate, aafInstance));
149 JSONObject jauth = jfeed.optJSONObject("authorization");
153 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
155 for (int ux = 0; ux < jeids.length(); ux++) {
156 JSONObject ju = jeids.getJSONObject(ux);
157 String login = gvas(ju, "id");
158 String password = gvas(ju, "password");
159 pfuv.add(new NodeConfig.ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
162 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
164 for (int ix = 0; ix < jeips.length(); ix++) {
165 String sn = gvas(jeips, ix);
166 pfsnv.add(new NodeConfig.ProvFeedSubnet(fid, sn));
171 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
173 for (int sx = 0; sx < jsubs.length(); sx++) {
174 JSONObject jsub = jsubs.getJSONObject(sx);
175 if (jsub.optBoolean("suspend", false)) {
178 String sid = gvas(jsub, "subid");
179 String fid = gvas(jsub, "feedid");
180 JSONObject jdel = jsub.getJSONObject("delivery");
181 String delurl = gvas(jdel, "url");
182 String id = gvas(jdel, "user");
183 String password = gvas(jdel, "password");
184 boolean monly = jsub.getBoolean("metadataOnly");
185 boolean use100 = jdel.getBoolean("use100");
186 boolean privilegedSubscriber = jsub.getBoolean("privilegedSubscriber");
187 boolean decompress = jsub.getBoolean("decompress");
188 boolean followRedirect = jsub.getBoolean("follow_redirect");
189 psv.add(new NodeConfig.ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100, privilegedSubscriber, followRedirect, decompress));
192 JSONObject jparams = jcfg.optJSONObject("parameters");
193 if (jparams != null) {
194 for (String pname : JSONObject.getNames(jparams)) {
195 String pvalue = gvas(jparams, pname);
196 if (pvalue != null) {
197 ppv.add(new NodeConfig.ProvParam(pname, pvalue));
200 String sfx = gvas(jparams, "PROV_DOMAIN");
201 JSONArray jnodes = jparams.optJSONArray("NODES");
202 if (jnodes != null) {
203 for (int nx = 0; nx < jnodes.length(); nx++) {
204 String nn = gvas(jnodes, nx);
205 if (nn.indexOf('.') == -1) {
208 pnv.add(new NodeConfig.ProvNode(nn));
212 JSONArray jingresses = jcfg.optJSONArray("ingress");
213 if (jingresses != null) {
214 for (int fx = 0; fx < jingresses.length(); fx++) {
215 JSONObject jingress = jingresses.getJSONObject(fx);
216 String fid = gvas(jingress, "feedid");
217 String subnet = gvas(jingress, "subnet");
218 String user = gvas(jingress, "user");
219 String[] nodes = gvasa(jingress, "node");
220 if (fid == null || "".equals(fid)) {
223 if ("".equals(subnet)) {
226 if ("".equals(user)) {
229 pfiv.add(new NodeConfig.ProvForceIngress(fid, subnet, user, nodes));
232 JSONObject jegresses = jcfg.optJSONObject("egress");
233 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
234 for (String esid : JSONObject.getNames(jegresses)) {
235 String enode = gvas(jegresses, esid);
236 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
237 pfev.add(new NodeConfig.ProvForceEgress(esid, enode));
241 JSONArray jhops = jcfg.optJSONArray("routing");
243 for (int fx = 0; fx < jhops.length(); fx++) {
244 JSONObject jhop = jhops.getJSONObject(fx);
245 String from = gvas(jhop, "from");
246 String to = gvas(jhop, "to");
247 String via = gvas(jhop, "via");
248 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
251 phv.add(new NodeConfig.ProvHop(from, to, via));
254 } catch (JSONException jse) {
255 NodeUtils.setIpAndFqdnForEelf("ProvData");
256 eelflogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
257 logger.error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
258 throw new IOException(jse.toString(), jse);
260 pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
261 pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
262 pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
263 pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
264 pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
265 ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
266 pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
267 pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
268 ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
272 * Get the raw node configuration entries
274 public NodeConfig.ProvNode[] getNodes() {
279 * Get the raw parameter configuration entries
281 public NodeConfig.ProvParam[] getParams() {
286 * Ge the raw feed configuration entries
288 public NodeConfig.ProvFeed[] getFeeds() {
293 * Get the raw feed user configuration entries
295 public NodeConfig.ProvFeedUser[] getFeedUsers() {
300 * Get the raw feed subnet configuration entries
302 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
307 * Get the raw subscription entries
309 public NodeConfig.ProvSubscription[] getSubscriptions() {
314 * Get the raw forced ingress entries
316 public NodeConfig.ProvForceIngress[] getForceIngress() {
321 * Get the raw forced egress entries
323 public NodeConfig.ProvForceEgress[] getForceEgress() {
328 * Get the raw next hop entries
330 public NodeConfig.ProvHop[] getHops() {