1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.node;
\r
30 import org.apache.log4j.Logger;
\r
32 import com.att.eelf.configuration.EELFLogger;
\r
33 import com.att.eelf.configuration.EELFManager;
\r
34 import com.att.research.datarouter.node.eelf.EelfMsgs;
\r
37 * Parser for provisioning data from the provisioning server.
\r
39 * The ProvData class uses a Reader for the text configuration from the
\r
40 * provisioning server to construct arrays of raw configuration entries.
\r
42 public class ProvData {
\r
43 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.node.ProvData");
\r
44 private static Logger logger = Logger.getLogger("com.att.research.datarouter.node.ProvData");
\r
45 private NodeConfig.ProvNode[] pn;
\r
46 private NodeConfig.ProvParam[] pp;
\r
47 private NodeConfig.ProvFeed[] pf;
\r
48 private NodeConfig.ProvFeedUser[] pfu;
\r
49 private NodeConfig.ProvFeedSubnet[] pfsn;
\r
50 private NodeConfig.ProvSubscription[] ps;
\r
51 private NodeConfig.ProvForceIngress[] pfi;
\r
52 private NodeConfig.ProvForceEgress[] pfe;
\r
53 private NodeConfig.ProvHop[] ph;
\r
54 private static String[] gvasa(JSONArray a, int index) {
\r
55 return(gvasa(a.get(index)));
\r
57 private static String[] gvasa(JSONObject o, String key) {
\r
58 return(gvasa(o.opt(key)));
\r
60 private static String[] gvasa(Object o) {
\r
61 if (o instanceof JSONArray) {
\r
62 JSONArray a = (JSONArray)o;
\r
63 Vector<String> v = new Vector<String>();
\r
64 for (int i = 0; i < a.length(); i++) {
\r
65 String s = gvas(a, i);
\r
70 return(v.toArray(new String[v.size()]));
\r
74 return(new String[0]);
\r
76 return(new String[] { s });
\r
80 private static String gvas(JSONArray a, int index) {
\r
81 return(gvas(a.get(index)));
\r
83 private static String gvas(JSONObject o, String key) {
\r
84 return(gvas(o.opt(key)));
\r
86 private static String gvas(Object o) {
\r
87 if (o instanceof Boolean || o instanceof Number || o instanceof String) {
\r
88 return(o.toString());
\r
93 * Construct raw provisioing data entries from the text (JSON)
\r
94 * provisioning document received from the provisioning server
\r
95 * @param r The reader for the JSON text.
\r
97 public ProvData(Reader r) throws IOException {
\r
98 Vector<NodeConfig.ProvNode> pnv = new Vector<NodeConfig.ProvNode>();
\r
99 Vector<NodeConfig.ProvParam> ppv = new Vector<NodeConfig.ProvParam>();
\r
100 Vector<NodeConfig.ProvFeed> pfv = new Vector<NodeConfig.ProvFeed>();
\r
101 Vector<NodeConfig.ProvFeedUser> pfuv = new Vector<NodeConfig.ProvFeedUser>();
\r
102 Vector<NodeConfig.ProvFeedSubnet> pfsnv = new Vector<NodeConfig.ProvFeedSubnet>();
\r
103 Vector<NodeConfig.ProvSubscription> psv = new Vector<NodeConfig.ProvSubscription>();
\r
104 Vector<NodeConfig.ProvForceIngress> pfiv = new Vector<NodeConfig.ProvForceIngress>();
\r
105 Vector<NodeConfig.ProvForceEgress> pfev = new Vector<NodeConfig.ProvForceEgress>();
\r
106 Vector<NodeConfig.ProvHop> phv = new Vector<NodeConfig.ProvHop>();
\r
108 JSONTokener jtx = new JSONTokener(r);
\r
109 JSONObject jcfg = new JSONObject(jtx);
\r
110 char c = jtx.nextClean();
\r
112 throw new JSONException("Spurious characters following configuration");
\r
115 JSONArray jfeeds = jcfg.optJSONArray("feeds");
\r
116 if (jfeeds != null) {
\r
117 for (int fx = 0; fx < jfeeds.length(); fx++) {
\r
118 JSONObject jfeed = jfeeds.getJSONObject(fx);
\r
119 String stat = null;
\r
120 if (jfeed.optBoolean("suspend", false)) {
\r
121 stat = "Feed is suspended";
\r
123 if (jfeed.optBoolean("deleted", false)) {
\r
124 stat = "Feed is deleted";
\r
126 String fid = gvas(jfeed, "feedid");
\r
127 String fname = gvas(jfeed, "name");
\r
128 String fver = gvas(jfeed, "version");
\r
129 pfv.add(new NodeConfig.ProvFeed(fid, fname + "//" + fver, stat));
\r
130 JSONObject jauth = jfeed.optJSONObject("authorization");
\r
131 if (jauth == null) {
\r
134 JSONArray jeids = jauth.optJSONArray("endpoint_ids");
\r
135 if (jeids != null) {
\r
136 for (int ux = 0; ux < jeids.length(); ux++) {
\r
137 JSONObject ju = jeids.getJSONObject(ux);
\r
138 String login = gvas(ju, "id");
\r
139 String password = gvas(ju, "password");
\r
140 pfuv.add(new NodeConfig.ProvFeedUser(fid, login, NodeUtils.getAuthHdr(login, password)));
\r
143 JSONArray jeips = jauth.optJSONArray("endpoint_addrs");
\r
144 if (jeips != null) {
\r
145 for (int ix = 0; ix < jeips.length(); ix++) {
\r
146 String sn = gvas(jeips, ix);
\r
147 pfsnv.add(new NodeConfig.ProvFeedSubnet(fid, sn));
\r
152 JSONArray jsubs = jcfg.optJSONArray("subscriptions");
\r
153 if (jsubs != null) {
\r
154 for (int sx = 0; sx < jsubs.length(); sx++) {
\r
155 JSONObject jsub = jsubs.getJSONObject(sx);
\r
156 if (jsub.optBoolean("suspend", false)) {
\r
159 String sid = gvas(jsub, "subid");
\r
160 String fid = gvas(jsub, "feedid");
\r
161 JSONObject jdel = jsub.getJSONObject("delivery");
\r
162 String delurl = gvas(jdel, "url");
\r
163 String id = gvas(jdel, "user");
\r
164 String password = gvas(jdel, "password");
\r
165 boolean monly = jsub.getBoolean("metadataOnly");
\r
166 boolean use100 = jdel.getBoolean("use100");
\r
167 psv.add(new NodeConfig.ProvSubscription(sid, fid, delurl, id, NodeUtils.getAuthHdr(id, password), monly, use100));
\r
170 JSONObject jparams = jcfg.optJSONObject("parameters");
\r
171 if (jparams != null) {
\r
172 for (String pname: JSONObject.getNames(jparams)) {
\r
173 String pvalue = gvas(jparams, pname);
\r
174 if (pvalue != null) {
\r
175 ppv.add(new NodeConfig.ProvParam(pname, pvalue));
\r
178 String sfx = gvas(jparams, "PROV_DOMAIN");
\r
179 JSONArray jnodes = jparams.optJSONArray("NODES");
\r
180 if (jnodes != null) {
\r
181 for (int nx = 0; nx < jnodes.length(); nx++) {
\r
182 String nn = gvas(jnodes, nx);
\r
183 if (nn.indexOf('.') == -1) {
\r
184 nn = nn + "." + sfx;
\r
186 pnv.add(new NodeConfig.ProvNode(nn));
\r
190 JSONArray jingresses = jcfg.optJSONArray("ingress");
\r
191 if (jingresses != null) {
\r
192 for (int fx = 0; fx < jingresses.length(); fx++) {
\r
193 JSONObject jingress = jingresses.getJSONObject(fx);
\r
194 String fid = gvas(jingress, "feedid");
\r
195 String subnet = gvas(jingress, "subnet");
\r
196 String user = gvas(jingress, "user");
\r
197 String[] nodes = gvasa(jingress, "node");
\r
198 if (fid == null || "".equals(fid)) {
\r
201 if ("".equals(subnet)) {
\r
204 if ("".equals(user)) {
\r
207 pfiv.add(new NodeConfig.ProvForceIngress(fid, subnet, user, nodes));
\r
210 JSONObject jegresses = jcfg.optJSONObject("egress");
\r
211 if (jegresses != null && JSONObject.getNames(jegresses) != null) {
\r
212 for (String esid: JSONObject.getNames(jegresses)) {
\r
213 String enode = gvas(jegresses, esid);
\r
214 if (esid != null && enode != null && !"".equals(esid) && !"".equals(enode)) {
\r
215 pfev.add(new NodeConfig.ProvForceEgress(esid, enode));
\r
219 JSONArray jhops = jcfg.optJSONArray("routing");
\r
220 if (jhops != null) {
\r
221 for (int fx = 0; fx < jhops.length(); fx++) {
\r
222 JSONObject jhop = jhops.getJSONObject(fx);
\r
223 String from = gvas(jhop, "from");
\r
224 String to = gvas(jhop, "to");
\r
225 String via = gvas(jhop, "via");
\r
226 if (from == null || to == null || via == null || "".equals(from) || "".equals(to) || "".equals(via)) {
\r
229 phv.add(new NodeConfig.ProvHop(from, to, via));
\r
232 } catch (JSONException jse) {
\r
233 NodeUtils.setIpAndFqdnForEelf("ProvData");
\r
234 eelflogger.error(EelfMsgs.MESSAGE_PARSING_ERROR, jse.toString());
\r
235 logger.error("NODE0201 Error parsing configuration data from provisioning server " + jse.toString(), jse);
\r
236 throw new IOException(jse.toString(), jse);
\r
238 pn = pnv.toArray(new NodeConfig.ProvNode[pnv.size()]);
\r
239 pp = ppv.toArray(new NodeConfig.ProvParam[ppv.size()]);
\r
240 pf = pfv.toArray(new NodeConfig.ProvFeed[pfv.size()]);
\r
241 pfu = pfuv.toArray(new NodeConfig.ProvFeedUser[pfuv.size()]);
\r
242 pfsn = pfsnv.toArray(new NodeConfig.ProvFeedSubnet[pfsnv.size()]);
\r
243 ps = psv.toArray(new NodeConfig.ProvSubscription[psv.size()]);
\r
244 pfi = pfiv.toArray(new NodeConfig.ProvForceIngress[pfiv.size()]);
\r
245 pfe = pfev.toArray(new NodeConfig.ProvForceEgress[pfev.size()]);
\r
246 ph = phv.toArray(new NodeConfig.ProvHop[phv.size()]);
\r
249 * Get the raw node configuration entries
\r
251 public NodeConfig.ProvNode[] getNodes() {
\r
255 * Get the raw parameter configuration entries
\r
257 public NodeConfig.ProvParam[] getParams() {
\r
261 * Ge the raw feed configuration entries
\r
263 public NodeConfig.ProvFeed[] getFeeds() {
\r
267 * Get the raw feed user configuration entries
\r
269 public NodeConfig.ProvFeedUser[] getFeedUsers() {
\r
273 * Get the raw feed subnet configuration entries
\r
275 public NodeConfig.ProvFeedSubnet[] getFeedSubnets() {
\r
279 * Get the raw subscription entries
\r
281 public NodeConfig.ProvSubscription[] getSubscriptions() {
\r
285 * Get the raw forced ingress entries
\r
287 public NodeConfig.ProvForceIngress[] getForceIngress() {
\r
291 * Get the raw forced egress entries
\r
293 public NodeConfig.ProvForceEgress[] getForceEgress() {
\r
297 * Get the raw next hop entries
\r
299 public NodeConfig.ProvHop[] getHops() {
\r