2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (C) 2018 IBM.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.sli.core.slipluginutils;
26 import java.io.UnsupportedEncodingException;
27 import java.net.URLEncoder;
29 import org.apache.commons.lang3.StringEscapeUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * A SvcLogicJavaPlugin that exposes java.lang.String functions to DirectedGraph
40 public class SliStringUtils implements SvcLogicJavaPlugin {
41 private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);
42 public static final String INPUT_PARAM_KEY = "key";
43 public static final String INPUT_PARAM_SOURCE = "source";
44 public static final String INPUT_PARAM_TARGET = "target";
45 public static final String TRUE_CONSTANT = "true";
46 public static final String FALSE_CONSTANT = "false";
48 public SliStringUtils() {}
51 * Provides split functionality to Directed Graphs.
52 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
54 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
56 * <tr><td>original_string</td><td>Mandatory</td><td>String to perform split on</td></tr>
57 * <tr><td>regex</td><td>Mandatory</td><td>the delimiting regular expression</td></tr>
58 * <tr><td>limit</td><td>Optional</td><td>result threshold. See String.split method for further description. Defaults to 0</td></tr>
59 * <tr><td>ctx_memory_result_key</td><td>Mandatory</td><td>Key in context memory to populate the resulting array of strings under</td></tr>
62 * @param ctx Reference to context memory
63 * @throws SvcLogicException
65 * @see String#split(String, int)
67 public void split( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
68 final String original_string = parameters.get("original_string");
69 LOG.trace("original_string = " + original_string);
70 final String regex = parameters.get("regex");
71 LOG.trace("regex = " + regex);
72 final String limit_str = parameters.get("limit");
73 LOG.trace("limit_str = " + limit_str);
74 final String ctx_memory_result_key = parameters.get("ctx_memory_result_key");
75 LOG.trace("ctx_memory_result_key = " + ctx_memory_result_key);
78 // Validation that parameters are not null
79 SliPluginUtils.checkParameters( parameters, new String[]{"original_string","regex","ctx_memory_result_key"}, LOG );
81 // Read limit from context memory. Default to 0 if null/empty
83 if( StringUtils.isNotEmpty(limit_str) ) {
85 limit = Integer.parseInt(limit_str);
87 catch( NumberFormatException e ) {
88 throw new IllegalArgumentException( "The limit parameter of the SliStringUtils.split() function must be a number, empty string, or null", e );
92 // Call String.split(regex,limit) on string passed in
93 String[] split_string = original_string.split(regex, limit);
95 // Populate context memory with results
96 for( int i = 0; i < split_string.length; i++ ) {
97 SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + '[' + i + ']', split_string[i], LOG, SliPluginUtils.LogLevel.DEBUG);
99 SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + "_length", new Integer(split_string.length), LOG, SliPluginUtils.LogLevel.DEBUG);
101 catch( Exception e ) {
102 // Have error message print parameters
103 throw new SvcLogicException( "An error occurred during SliStringUtils.split() where original_string = " + quotedOrNULL(regex) +
104 " regex = " + quotedOrNULL(regex) +
105 " limit = " + quotedOrNULL(regex) +
106 " ctx_memory_result_key = " + quotedOrNULL(regex), e );
110 public static String quotedOrNULL( String str ) {
111 return (str == null) ? "NULL" : '"' + str + '"';
115 * exposes equalsIgnoreCase to directed graph
116 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
117 * emits a true or false outcome
119 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
121 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
122 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
125 * @param ctx Reference to context memory
126 * @throws SvcLogicException
129 public static String equalsIgnoreCase(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
130 SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
131 if (parameters.get(INPUT_PARAM_SOURCE).equalsIgnoreCase(parameters.get(INPUT_PARAM_TARGET))) {
132 return TRUE_CONSTANT;
134 return FALSE_CONSTANT;
138 * exposes toUpperCase to directed graph
139 * writes an upperCase version of source to outputPath
140 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
142 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
144 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
145 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
148 * @param ctx Reference to context memory
149 * @throws SvcLogicException
152 public static void toUpper(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
153 SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"outputPath"}, LOG);
154 ctx.setAttribute(parameters.get("outputPath"), parameters.get(INPUT_PARAM_SOURCE).toUpperCase());
158 * exposes toLowerCase to directed graph
159 * writes a lowerCase version of source to outputPath
160 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
162 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
164 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
165 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
168 * @param ctx Reference to context memory
169 * @throws SvcLogicException
172 public static void toLower(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
173 SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"outputPath"}, LOG);
174 ctx.setAttribute(parameters.get("outputPath"), parameters.get(INPUT_PARAM_SOURCE).toLowerCase());
178 * exposes contains to directed graph to test if one string contains another
179 * tests if the source contains the target
180 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
181 * emits a true or false outcome
183 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
185 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
186 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
189 * @param ctx Reference to context memory
190 * @throws SvcLogicException
193 public static String contains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
194 SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
195 if (parameters.get(INPUT_PARAM_SOURCE).contains(parameters.get(INPUT_PARAM_TARGET))) {
196 return TRUE_CONSTANT;
198 return FALSE_CONSTANT;
202 * exposes endsWith to directed graph to test if one string endsWith another string
203 * tests if the source ends with the target
204 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
205 * emits a true or false outcome
207 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
209 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
210 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
213 * @param ctx Reference to context memory
214 * @throws SvcLogicException
217 public static String endsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
218 SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
219 if (parameters.get(INPUT_PARAM_SOURCE).endsWith(parameters.get(INPUT_PARAM_TARGET))) {
220 return TRUE_CONSTANT;
222 return FALSE_CONSTANT;
226 * exposes startsWith to directed graph to test if one string endsWith another string
227 * tests if the source ends with the target
228 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
229 * emits a true or false outcome
231 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
233 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
234 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
237 * @param ctx Reference to context memory
238 * @throws SvcLogicException
241 public static String startsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
242 SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
243 if (parameters.get(INPUT_PARAM_SOURCE).startsWith(parameters.get(INPUT_PARAM_TARGET))) {
244 return TRUE_CONSTANT;
246 return FALSE_CONSTANT;
250 * exposes trim to directed graph
251 * writes a trimmed version of the string to the outputPath
252 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
254 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
256 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
257 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
260 * @param ctx Reference to context memory
261 * @throws SvcLogicException
264 public static void trim(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
265 SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"outputPath"}, LOG);
266 ctx.setAttribute(parameters.get("outputPath"), parameters.get(INPUT_PARAM_SOURCE).trim());
270 * exposes String.length() to directed graph
271 * writes the length of source to outputPath
272 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
274 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
276 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
277 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
280 * @param ctx Reference to context memory
281 * @throws SvcLogicException
284 public static void getLength(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
285 SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"outputPath"}, LOG);
286 ctx.setAttribute(parameters.get("outputPath"), String.valueOf(parameters.get(INPUT_PARAM_SOURCE).length()));
290 * exposes replace to directed graph
291 * writes the length of source to outputPath
292 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
294 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
296 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
297 * <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
298 * <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
299 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
302 * @param ctx Reference to context memory
303 * @throws SvcLogicException
306 public static void replace(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
307 SliPluginUtils.checkParameters(parameters,
308 new String[] {INPUT_PARAM_SOURCE, "outputPath", INPUT_PARAM_TARGET, "replacement"}, LOG);
309 ctx.setAttribute(parameters.get("outputPath"), (parameters.get(INPUT_PARAM_SOURCE)
310 .replace(parameters.get(INPUT_PARAM_TARGET), parameters.get("replacement"))));
314 * exposes replaceAll to directed graph
315 * writes the length of source to outputPath
316 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
318 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
320 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
321 * <tr><td>target</td><td>Mandatory</td><td>This should be a valid regular expression</td></tr>
322 * <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
323 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
326 * @param ctx Reference to context memory
327 * @throws SvcLogicException
330 public static void replaceAll(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
331 SliPluginUtils.checkParameters(parameters,
332 new String[] {INPUT_PARAM_SOURCE, "outputPath", INPUT_PARAM_TARGET, "replacement"}, LOG);
333 ctx.setAttribute(parameters.get("outputPath"), parameters.get(INPUT_PARAM_SOURCE)
334 .replaceAll(parameters.get(INPUT_PARAM_TARGET), parameters.get("replacement")));
338 * Provides substring functionality to Directed Graphs.
340 * Calls either String.substring(String beginIndex) or
341 * String.substring(String beginInded, String endIndex) if the end-index
343 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
345 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
347 * <tr><td>string</td><td>Mandatory</td><td>String to perform substring on</td></tr>
348 * <tr><td>result</td><td>Mandatory</td><td>Key in context memory to populate the resulting string in</td></tr>
349 * <tr><td>begin-index</td><td>Mandatory</td><td>Beginning index to pass to Java substring function</td></tr>
350 * <tr><td>end-index</td><td>Optional</td><td>Ending index to pass to Java substring function. If not included, String.substring(begin) will be called.</td></tr>
353 * @param ctx Reference to context memory
354 * @throws SvcLogicException
357 public void substring( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
359 SliPluginUtils.checkParameters( parameters, new String[]{"string","begin-index","result"}, LOG );
360 final String string = parameters.get("string");
361 final String result = parameters.get("result");
362 final String begin = parameters.get("begin-index");
363 final String end = parameters.get("end-index");
364 if( StringUtils.isEmpty(end) ) {
365 ctx.setAttribute( result, string.substring(Integer.parseInt(begin)) );
368 ctx.setAttribute( result, string.substring(Integer.parseInt(begin), Integer.parseInt(end)) );
371 catch( Exception e ) {
372 throw new SvcLogicException( "An error occurred while the Directed Graph was performing a substring", e );
377 * Provides concat functionality to Directed Graphs.
379 * Will concat target to source and write the result to outputPath
380 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
382 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
384 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
385 * <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
386 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
389 * @param ctx Reference to context memory
390 * @throws SvcLogicException
393 public static void concat( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
394 SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET, "outputPath"},
396 String result = parameters.get(INPUT_PARAM_SOURCE).concat(parameters.get(INPUT_PARAM_TARGET));
397 ctx.setAttribute(parameters.get("outputPath"), result);
401 * Provides url encoding functionality to Directed Graphs.
403 * Will url encode the source and write the result to outputPath
404 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
406 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
408 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
409 * <tr><td>encoding</td><td>Optional</td><td>the name of a supported character encoding, defaulted to UTF-8 if not supplied</td></tr>
410 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
413 * @param ctx Reference to context memory
414 * @throws SvcLogicException
416 public static void urlEncode(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
417 SliPluginUtils.checkParameters(parameters, new String[] { INPUT_PARAM_SOURCE, "outputPath" }, LOG);
418 String encoding = parameters.get("encoding");
419 if (encoding == null) {
423 String result = URLEncoder.encode(parameters.get(INPUT_PARAM_SOURCE), encoding);
424 ctx.setAttribute(parameters.get("outputPath"), result);
425 } catch (UnsupportedEncodingException e) {
426 throw new SvcLogicException("Url encode failed.", e);
431 * xmlEscapeText() will be used to format input xml with text.
434 * accepts the instance of {@link Map} holds the input xml in string
437 * accepts the instance of {@link SvcLogicContext} holds the service
441 public static void xmlEscapeText(Map<String, String> inParams, SvcLogicContext ctx) {
442 String source = inParams.get(INPUT_PARAM_SOURCE);
443 String target = inParams.get(INPUT_PARAM_TARGET);
444 source = StringEscapeUtils.escapeXml(source);
445 ctx.setAttribute(target, source);
449 * unescapeJsonString takes an escaped json string stored as a single property in context memory and unescapes it storing it as a single property
450 * @param parameters - requires source and outputPath to not be null.
451 * @param ctx Reference to context memory
452 * @throws SvcLogicException if a required parameter is missing an exception is thrown
454 public static void unescapeJsonString(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
455 SliPluginUtils.checkParameters(parameters, new String[] { INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET }, LOG);
457 String source = parameters.get(INPUT_PARAM_SOURCE);
458 String target = parameters.get(INPUT_PARAM_TARGET);
459 String unescapedJson = StringEscapeUtils.unescapeJson(source);
460 ctx.setAttribute(target, unescapedJson);
461 } catch (Exception ex) {
462 ex.printStackTrace();
463 throw new SvcLogicException("problem with unescapeJsonString", ex);
468 * escapeJsonString takes json stored as a single string in context memory and escapes it storing it as a single property
469 * @param parameters - requires source and outputPath to not be null.
470 * @param ctx Reference to context memory
471 * @throws SvcLogicException if a required parameter is missing an exception is thrown
473 public static void escapeJsonString(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
474 SliPluginUtils.checkParameters(parameters, new String[] { INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET }, LOG);
476 String source = parameters.get(INPUT_PARAM_SOURCE);
477 String target = parameters.get(INPUT_PARAM_TARGET);
478 String unescapedJson = StringEscapeUtils.escapeJson(source);
479 ctx.setAttribute(target, unescapedJson);
480 } catch (Exception ex) {
481 ex.printStackTrace();
482 throw new SvcLogicException("problem with escapeJsonString", ex);
486 public static String isBlank(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
487 String ctxLocation = parameters.get(INPUT_PARAM_KEY);
488 String str = ctx.getAttribute(ctxLocation);
489 if (str == null || str.isEmpty() || " ".equals(str)) {
490 return TRUE_CONSTANT;
492 return FALSE_CONSTANT;
495 public static String isEmpty(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
496 String ctxLocation = parameters.get(INPUT_PARAM_KEY);
497 String str = ctx.getAttribute(ctxLocation);
498 if (str == null || str.isEmpty()) {
499 return TRUE_CONSTANT;
501 return FALSE_CONSTANT;
504 public static String isNull(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
505 String ctxLocation = parameters.get(INPUT_PARAM_KEY);
506 String str = ctx.getAttribute(ctxLocation);
508 return TRUE_CONSTANT;
510 return FALSE_CONSTANT;