2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP
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=========================================================
21 package org.onap.ccsdk.sli.core.slipluginutils;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * A SvcLogicJavaPlugin that exposes java.lang.String functions to DirectedGraph
37 public class SliStringUtils implements SvcLogicJavaPlugin {
38 private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);
40 public SliStringUtils() {}
43 * Provides split functionality to Directed Graphs.
44 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
46 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
48 * <tr><td>original_string</td><td>Mandatory</td><td>String to perform split on</td></tr>
49 * <tr><td>regex</td><td>Mandatory</td><td>the delimiting regular expression</td></tr>
50 * <tr><td>limit</td><td>Optional</td><td>result threshold. See String.split method for further description. Defaults to 0</td></tr>
51 * <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>
54 * @param ctx Reference to context memory
55 * @throws SvcLogicException
57 * @see String#split(String, int)
59 public void split( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
60 final String original_string = parameters.get("original_string");
61 LOG.trace("original_string = " + original_string);
62 final String regex = parameters.get("regex");
63 LOG.trace("regex = " + regex);
64 final String limit_str = parameters.get("limit");
65 LOG.trace("limit_str = " + limit_str);
66 final String ctx_memory_result_key = parameters.get("ctx_memory_result_key");
67 LOG.trace("ctx_memory_result_key = " + ctx_memory_result_key);
70 // Validation that parameters are not null
71 SliPluginUtils.checkParameters( parameters, new String[]{"original_string","regex","ctx_memory_result_key"}, LOG );
73 // Read limit from context memory. Default to 0 if null/empty
75 if( StringUtils.isNotEmpty(limit_str) ) {
77 limit = Integer.parseInt(limit_str);
79 catch( NumberFormatException e ) {
80 throw new IllegalArgumentException( "The limit parameter of the SliStringUtils.split() function must be a number, empty string, or null", e );
84 // Call String.split(regex,limit) on string passed in
85 String[] split_string = original_string.split(regex, limit);
87 // Populate context memory with results
88 for( int i = 0; i < split_string.length; i++ ) {
89 SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + '[' + i + ']', split_string[i], LOG, SliPluginUtils.LogLevel.DEBUG);
91 SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + "_length", new Integer(split_string.length), LOG, SliPluginUtils.LogLevel.DEBUG);
93 catch( Exception e ) {
94 // Have error message print parameters
95 throw new SvcLogicException( "An error occurred during SliStringUtils.split() where original_string = " + quotedOrNULL(regex) +
96 " regex = " + quotedOrNULL(regex) +
97 " limit = " + quotedOrNULL(regex) +
98 " ctx_memory_result_key = " + quotedOrNULL(regex), e );
102 private static String quotedOrNULL( String str ) {
103 return (str == null) ? "NULL" : '"' + str + '"';
107 * exposes equalsIgnoreCase to directed graph
108 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
109 * emits a true or false outcome
111 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
113 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
114 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
117 * @param ctx Reference to context memory
118 * @throws SvcLogicException
121 public static String equalsIgnoreCase(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
122 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
123 if(parameters.get("source").equalsIgnoreCase(parameters.get("target"))){
130 * exposes toUpperCase to directed graph
131 * writes an upperCase version of source to outputPath
132 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
134 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
136 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
137 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
140 * @param ctx Reference to context memory
141 * @throws SvcLogicException
144 public static void toUpper(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
145 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
146 ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").toUpperCase());
150 * exposes toLowerCase to directed graph
151 * writes a lowerCase version of source to outputPath
152 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
154 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
156 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
157 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
160 * @param ctx Reference to context memory
161 * @throws SvcLogicException
164 public static void toLower(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
165 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
166 ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").toLowerCase());
170 * exposes contains to directed graph to test if one string contains another
171 * tests if the source contains the target
172 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
173 * emits a true or false outcome
175 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
177 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
178 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
181 * @param ctx Reference to context memory
182 * @throws SvcLogicException
185 public static String contains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
186 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
187 if(parameters.get("source").contains(parameters.get("target"))){
194 * exposes endsWith to directed graph to test if one string endsWith another string
195 * tests if the source ends with the target
196 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
197 * emits a true or false outcome
199 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
201 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
202 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
205 * @param ctx Reference to context memory
206 * @throws SvcLogicException
209 public static String endsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
210 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
211 if(parameters.get("source").endsWith(parameters.get("target"))){
218 * exposes startsWith to directed graph to test if one string endsWith another string
219 * tests if the source ends with the target
220 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
221 * emits a true or false outcome
223 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
225 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
226 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
229 * @param ctx Reference to context memory
230 * @throws SvcLogicException
233 public static String startsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
234 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
235 if(parameters.get("source").startsWith(parameters.get("target"))){
242 * exposes trim to directed graph
243 * writes a trimmed version of the string to the outputPath
244 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
246 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
248 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
249 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
252 * @param ctx Reference to context memory
253 * @throws SvcLogicException
256 public static void trim(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
257 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
258 ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").trim());
262 * exposes String.length() to directed graph
263 * writes the length of source to outputPath
264 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
266 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
268 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
269 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
272 * @param ctx Reference to context memory
273 * @throws SvcLogicException
276 public static void getLength(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
277 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
278 ctx.setAttribute(parameters.get("outputPath"), String.valueOf(parameters.get("source").length()));
282 * exposes replace to directed graph
283 * writes the length of source to outputPath
284 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
286 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
288 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
289 * <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
290 * <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
291 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
294 * @param ctx Reference to context memory
295 * @throws SvcLogicException
298 public static void replace(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
299 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath","target","replacement"}, LOG);
300 ctx.setAttribute(parameters.get("outputPath"), (parameters.get("source").replace(parameters.get("target"), parameters.get("replacement"))));
304 * Provides substring functionality to Directed Graphs.
306 * Calls either String.substring(String beginIndex) or
307 * String.substring(String beginInded, String endIndex) if the end-index
309 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
311 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
313 * <tr><td>string</td><td>Mandatory</td><td>String to perform substring on</td></tr>
314 * <tr><td>result</td><td>Mandatory</td><td>Key in context memory to populate the resulting string in</td></tr>
315 * <tr><td>begin-index</td><td>Mandatory</td><td>Beginning index to pass to Java substring function</td></tr>
316 * <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>
319 * @param ctx Reference to context memory
320 * @throws SvcLogicException
323 public void substring( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
325 SliPluginUtils.checkParameters( parameters, new String[]{"string","begin-index","result"}, LOG );
326 final String string = parameters.get("string");
327 final String result = parameters.get("result");
328 final String begin = parameters.get("begin-index");
329 final String end = parameters.get("end-index");
330 if( StringUtils.isEmpty(end) ) {
331 ctx.setAttribute( result, string.substring(Integer.parseInt(begin)) );
334 ctx.setAttribute( result, string.substring(Integer.parseInt(begin), Integer.parseInt(end)) );
337 catch( Exception e ) {
338 throw new SvcLogicException( "An error occurred while the Directed Graph was performing a substring", e );
343 * Provides concat functionality to Directed Graphs.
345 * Will concat target to source and write the result to outputPath
346 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
348 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
350 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
351 * <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
352 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
355 * @param ctx Reference to context memory
356 * @throws SvcLogicException
359 public static void concat( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
360 SliPluginUtils.checkParameters( parameters, new String[]{"source","target","outputPath"}, LOG );
361 String result = parameters.get("source").concat(parameters.get("target"));
362 ctx.setAttribute(parameters.get("outputPath"), result);
366 * Provides url encoding functionality to Directed Graphs.
368 * Will url encode the source and write the result to outputPath
369 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
371 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
373 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
374 * <tr><td>encoding</td><td>Optional</td><td>the name of a supported character encoding, defaulted to UTF-8 if not supplied</td></tr>
375 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
378 * @param ctx Reference to context memory
379 * @throws SvcLogicException
381 public static void urlEncode(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
382 SliPluginUtils.checkParameters(parameters, new String[] { "source", "outputPath" }, LOG);
383 String encoding = parameters.get("encoding");
384 if (encoding == null) {
388 String result = URLEncoder.encode(parameters.get("source"), encoding);
389 ctx.setAttribute(parameters.get("outputPath"), result);
390 } catch (UnsupportedEncodingException e) {
391 throw new SvcLogicException("Url encode failed.", e);