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("org.onap.dmaap.datarouter.node.ProvData");
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 pfv.add(new NodeConfig.ProvFeed(fid, fname + "//" + fver, stat));
139 JSONObject jauth = jfeed.optJSONObject("authorization");
143 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
145 for (int ux = 0; ux < jeids.length(); ux++) {
146 JSONObject ju = jeids.getJSONObject(ux);
147 String login = gvas(ju, "id");
148 String password = gvas(ju, "password");
149 pfuv.add(new NodeConfig.ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
152 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
154 for (int ix = 0; ix < jeips.length(); ix++) {
155 String sn = gvas(jeips, ix);
156 pfsnv.add(new NodeConfig.ProvFeedSubnet(fid, sn));
161 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
163 for (int sx = 0; sx < jsubs.length(); sx++) {
164 JSONObject jsub = jsubs.getJSONObject(sx);
165 if (jsub.optBoolean("suspend", false)) {
168 String sid = gvas(jsub, "subid");
169 String fid = gvas(jsub, "feedid");
170 JSONObject jdel = jsub.getJSONObject("delivery");
171 String delurl = gvas(jdel, "url");
172 String id = gvas(jdel, "user");
173 String password = gvas(jdel, "password");
174 boolean monly = jsub.getBoolean("metadataOnly");
175 boolean use100 = jdel.getBoolean("use100");
176 psv.add(new NodeConfig.ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100));
179 JSONObject jparams = jcfg.optJSONObject("parameters");
180 if (jparams != null) {
181 for (String pname : JSONObject.getNames(jparams)) {
182 String pvalue = gvas(jparams, pname);
183 if (pvalue != null) {
184 ppv.add(new NodeConfig.ProvParam(pname, pvalue));
187 String sfx = gvas(jparams, "PROV_DOMAIN");
188 JSONArray jnodes = jparams.optJSONArray("NODES");
189 if (jnodes != null) {
190 for (int nx = 0; nx < jnodes.length(); nx++) {
191 String nn = gvas(jnodes, nx);
192 if (nn.indexOf('.') == -1) {
195 pnv.add(new NodeConfig.ProvNode(nn));
199 JSONArray jingresses = jcfg.optJSONArray("ingress");
200 if (jingresses != null) {
201 for (int fx = 0; fx < jingresses.length(); fx++) {
202 JSONObject jingress = jingresses.getJSONObject(fx);
203 String fid = gvas(jingress, "feedid");
204 String subnet = gvas(jingress, "subnet");
205 String user = gvas(jingress, "user");
206 String[] nodes = gvasa(jingress, "node");
207 if (fid == null || "".equals(fid)) {
210 if ("".equals(subnet)) {
213 if ("".equals(user)) {
216 pfiv.add(new NodeConfig.ProvForceIngress(fid, subnet, user, nodes));
219 JSONObject jegresses = jcfg.optJSONObject("egress");
220 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
221 for (String esid : JSONObject.getNames(jegresses)) {
222 String enode = gvas(jegresses, esid);
223 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
224 pfev.add(new NodeConfig.ProvForceEgress(esid, enode));
228 JSONArray jhops = jcfg.optJSONArray("routing");
230 for (int fx = 0; fx < jhops.length(); fx++) {
231 JSONObject jhop = jhops.getJSONObject(fx);
232 String from = gvas(jhop, "from");
233 String to = gvas(jhop, "to");
234 String via = gvas(jhop, "via");
235 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
238 phv.add(new NodeConfig.ProvHop(from, to, via));
241 } catch (JSONException jse) {
242 NodeUtils.setIpAndFqdnForEelf("ProvData");
243 eelflogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
244 logger.error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
245 throw new IOException(jse.toString(), jse);
247 pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
248 pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
249 pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
250 pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
251 pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
252 ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
253 pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
254 pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
255 ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
259 * Get the raw node configuration entries
261 public NodeConfig.ProvNode[] getNodes() {
266 * Get the raw parameter configuration entries
268 public NodeConfig.ProvParam[] getParams() {
273 * Ge the raw feed configuration entries
275 public NodeConfig.ProvFeed[] getFeeds() {
280 * Get the raw feed user configuration entries
282 public NodeConfig.ProvFeedUser[] getFeedUsers() {
287 * Get the raw feed subnet configuration entries
289 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
294 * Get the raw subscription entries
296 public NodeConfig.ProvSubscription[] getSubscriptions() {
301 * Get the raw forced ingress entries
303 public NodeConfig.ProvForceIngress[] getForceIngress() {
308 * Get the raw forced egress entries
310 public NodeConfig.ProvForceEgress[] getForceEgress() {
315 * Get the raw next hop entries
317 public NodeConfig.ProvHop[] getHops() {