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;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
37 * Parser for provisioning data from the provisioning server.
39 * The ProvData class uses a Reader for the text configuration from the
40 * provisioning server to construct arrays of raw configuration entries.
42 public class ProvData {
43 private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(ProvData.class);
44 private NodeConfig.ProvNode[] pn;
45 private NodeConfig.ProvParam[] pp;
46 private NodeConfig.ProvFeed[] pf;
47 private NodeConfig.ProvFeedUser[] pfu;
48 private NodeConfig.ProvFeedSubnet[] pfsn;
49 private NodeConfig.ProvSubscription[] ps;
50 private NodeConfig.ProvForceIngress[] pfi;
51 private NodeConfig.ProvForceEgress[] pfe;
52 private NodeConfig.ProvHop[] ph;
54 private static String[] gvasa(JSONArray a, int index) {
55 return (gvasa(a.get(index)));
58 private static String[] gvasa(JSONObject o, String key) {
59 return (gvasa(o.opt(key)));
62 private static String[] gvasa(Object o) {
63 if (o instanceof JSONArray) {
64 JSONArray a = (JSONArray) o;
65 Vector<String> v = new Vector<String>();
66 for (int i = 0; i < a.length(); i++) {
67 String s = gvas(a, i);
72 return (v.toArray(new String[v.size()]));
76 return (new String[0]);
78 return (new String[]{s});
83 private static String gvas(JSONArray a, int index) {
84 return (gvas(a.get(index)));
87 private static String gvas(JSONObject o, String key) {
88 return (gvas(o.opt(key)));
91 private static String gvas(Object o) {
92 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
93 return (o.toString());
99 * Construct raw provisioing data entries from the text (JSON)
100 * provisioning document received from the provisioning server
102 * @param r The reader for the JSON text.
104 public ProvData(Reader r) throws IOException {
105 Vector<NodeConfig.ProvNode> pnv = new Vector<NodeConfig.ProvNode>();
106 Vector<NodeConfig.ProvParam> ppv = new Vector<NodeConfig.ProvParam>();
107 Vector<NodeConfig.ProvFeed> pfv = new Vector<NodeConfig.ProvFeed>();
108 Vector<NodeConfig.ProvFeedUser> pfuv = new Vector<NodeConfig.ProvFeedUser>();
109 Vector<NodeConfig.ProvFeedSubnet> pfsnv = new Vector<NodeConfig.ProvFeedSubnet>();
110 Vector<NodeConfig.ProvSubscription> psv = new Vector<NodeConfig.ProvSubscription>();
111 Vector<NodeConfig.ProvForceIngress> pfiv = new Vector<NodeConfig.ProvForceIngress>();
112 Vector<NodeConfig.ProvForceEgress> pfev = new Vector<NodeConfig.ProvForceEgress>();
113 Vector<NodeConfig.ProvHop> phv = new Vector<NodeConfig.ProvHop>();
115 JSONTokener jtx = new JSONTokener(r);
116 JSONObject jcfg = new JSONObject(jtx);
117 char c = jtx.nextClean();
119 throw new JSONException("Spurious characters following configuration");
122 JSONArray jfeeds = jcfg.optJSONArray("feeds");
123 if (jfeeds != null) {
124 for (int fx = 0; fx < jfeeds.length(); fx++) {
125 JSONObject jfeed = jfeeds.getJSONObject(fx);
127 if (jfeed.optBoolean("suspend", false)) {
128 stat = "Feed is suspended";
130 if (jfeed.optBoolean("deleted", false)) {
131 stat = "Feed is deleted";
133 String fid = gvas(jfeed, "feedid");
134 String fname = gvas(jfeed, "name");
135 String fver = gvas(jfeed, "version");
136 String createdDate = gvas(jfeed, "created_date");
138 * START - AAF changes
139 * TDP EPIC US# 307413
140 * Passing aafInstance to ProvFeed from feeds json passed by prov to identify legacy/AAF feeds
142 String aafInstance = gvas(jfeed, "aaf_instance");
143 pfv.add(new NodeConfig.ProvFeed(fid, fname + "//" + fver, stat,createdDate, aafInstance));
147 JSONObject jauth = jfeed.optJSONObject("authorization");
151 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
153 for (int ux = 0; ux < jeids.length(); ux++) {
154 JSONObject ju = jeids.getJSONObject(ux);
155 String login = gvas(ju, "id");
156 String password = gvas(ju, "password");
157 pfuv.add(new NodeConfig.ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
160 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
162 for (int ix = 0; ix < jeips.length(); ix++) {
163 String sn = gvas(jeips, ix);
164 pfsnv.add(new NodeConfig.ProvFeedSubnet(fid, sn));
169 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
171 for (int sx = 0; sx < jsubs.length(); sx++) {
172 JSONObject jsub = jsubs.getJSONObject(sx);
173 if (jsub.optBoolean("suspend", false)) {
176 String sid = gvas(jsub, "subid");
177 String fid = gvas(jsub, "feedid");
178 JSONObject jdel = jsub.getJSONObject("delivery");
179 String delurl = gvas(jdel, "url");
180 String id = gvas(jdel, "user");
181 String password = gvas(jdel, "password");
182 boolean monly = jsub.getBoolean("metadataOnly");
183 boolean use100 = jdel.getBoolean("use100");
184 boolean privilegedSubscriber = jsub.getBoolean("privilegedSubscriber");
185 boolean decompress = jsub.getBoolean("decompress");
186 boolean followRedirect = jsub.getBoolean("follow_redirect");
187 psv.add(new NodeConfig.ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100, privilegedSubscriber, followRedirect, decompress));
190 JSONObject jparams = jcfg.optJSONObject("parameters");
191 if (jparams != null) {
192 for (String pname : JSONObject.getNames(jparams)) {
193 String pvalue = gvas(jparams, pname);
194 if (pvalue != null) {
195 ppv.add(new NodeConfig.ProvParam(pname, pvalue));
198 String sfx = gvas(jparams, "PROV_DOMAIN");
199 JSONArray jnodes = jparams.optJSONArray("NODES");
200 if (jnodes != null) {
201 for (int nx = 0; nx < jnodes.length(); nx++) {
202 String nn = gvas(jnodes, nx);
203 if (nn.indexOf('.') == -1) {
206 pnv.add(new NodeConfig.ProvNode(nn));
210 JSONArray jingresses = jcfg.optJSONArray("ingress");
211 if (jingresses != null) {
212 for (int fx = 0; fx < jingresses.length(); fx++) {
213 JSONObject jingress = jingresses.getJSONObject(fx);
214 String fid = gvas(jingress, "feedid");
215 String subnet = gvas(jingress, "subnet");
216 String user = gvas(jingress, "user");
217 String[] nodes = gvasa(jingress, "node");
218 if (fid == null || "".equals(fid)) {
221 if ("".equals(subnet)) {
224 if ("".equals(user)) {
227 pfiv.add(new NodeConfig.ProvForceIngress(fid, subnet, user, nodes));
230 JSONObject jegresses = jcfg.optJSONObject("egress");
231 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
232 for (String esid : JSONObject.getNames(jegresses)) {
233 String enode = gvas(jegresses, esid);
234 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
235 pfev.add(new NodeConfig.ProvForceEgress(esid, enode));
239 JSONArray jhops = jcfg.optJSONArray("routing");
241 for (int fx = 0; fx < jhops.length(); fx++) {
242 JSONObject jhop = jhops.getJSONObject(fx);
243 String from = gvas(jhop, "from");
244 String to = gvas(jhop, "to");
245 String via = gvas(jhop, "via");
246 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
249 phv.add(new NodeConfig.ProvHop(from, to, via));
252 } catch (JSONException jse) {
253 NodeUtils.setIpAndFqdnForEelf("ProvData");
254 eelfLogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
255 eelfLogger.error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
256 throw new IOException(jse.toString(), jse);
258 pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
259 pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
260 pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
261 pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
262 pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
263 ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
264 pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
265 pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
266 ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
270 * Get the raw node configuration entries
272 public NodeConfig.ProvNode[] getNodes() {
277 * Get the raw parameter configuration entries
279 public NodeConfig.ProvParam[] getParams() {
284 * Ge the raw feed configuration entries
286 public NodeConfig.ProvFeed[] getFeeds() {
291 * Get the raw feed user configuration entries
293 public NodeConfig.ProvFeedUser[] getFeedUsers() {
298 * Get the raw feed subnet configuration entries
300 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
305 * Get the raw subscription entries
307 public NodeConfig.ProvSubscription[] getSubscriptions() {
312 * Get the raw forced ingress entries
314 public NodeConfig.ProvForceIngress[] getForceIngress() {
319 * Get the raw forced egress entries
321 public NodeConfig.ProvForceEgress[] getForceEgress() {
326 * Get the raw next hop entries
328 public NodeConfig.ProvHop[] getHops() {