aboutsummaryrefslogtreecommitdiffstats
path: root/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java
blob: 63c750cb84cc2203fd7b6ec78af655f6a79f26de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.core.slipluginutils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A SvcLogicJavaPlugin that exposes java.lang.String functions to DirectedGraph
 */
public class SliStringUtils implements SvcLogicJavaPlugin {
	private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);

	public static String INPUT_PARAM_SOURCE = "source";
	public static String INPUT_PARAM_TARGET = "target";
	
	public SliStringUtils() {}

	/**
	 * Provides split functionality to Directed Graphs.
	 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
	 * <table border="1">
	 * 	<thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
	 * 	<tbody>
	 * 		<tr><td>original_string</td><td>Mandatory</td><td>String to perform split on</td></tr>
	 * 		<tr><td>regex</td><td>Mandatory</td><td>the delimiting regular expression</td></tr>
	 * 		<tr><td>limit</td><td>Optional</td><td>result threshold. See String.split method for further description. Defaults to 0</td></tr>
	 * 		<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>
	 * 	</tbody>
	 * </table>
	 * @param ctx Reference to context memory
	 * @throws SvcLogicException
	 * @since 11.0.2
	 * @see String#split(String, int)
	 */
	public void split( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
		final String original_string = parameters.get("original_string");
		LOG.trace("original_string = " + original_string);
		final String regex = parameters.get("regex");
		LOG.trace("regex = " + regex);
		final String limit_str = parameters.get("limit");
		LOG.trace("limit_str = " + limit_str);
		final String ctx_memory_result_key = parameters.get("ctx_memory_result_key");
		LOG.trace("ctx_memory_result_key = " + ctx_memory_result_key);

		try {
			// Validation that parameters are not null
			SliPluginUtils.checkParameters( parameters, new String[]{"original_string","regex","ctx_memory_result_key"}, LOG );

			// Read limit from context memory. Default to 0 if null/empty
			int limit = 0;
			if( StringUtils.isNotEmpty(limit_str) ) {
				try {
					limit = Integer.parseInt(limit_str);
				}
				catch( NumberFormatException e ) {
					throw new IllegalArgumentException( "The limit parameter of the SliStringUtils.split() function must be a number, empty string, or null", e );
				}
			}

			// Call String.split(regex,limit) on string passed in
			String[] split_string = original_string.split(regex, limit);

			// Populate context memory with results
			for( int i = 0; i < split_string.length; i++ ) {
				SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + '[' + i + ']', split_string[i], LOG, SliPluginUtils.LogLevel.DEBUG);
			}
			SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + "_length", new Integer(split_string.length), LOG, SliPluginUtils.LogLevel.DEBUG);
		}
		catch( Exception e ) {
			// Have error message print parameters
			throw new SvcLogicException( "An error occurred during SliStringUtils.split() where original_string = " + quotedOrNULL(regex) +
					" regex = " + quotedOrNULL(regex) +
					" limit = " + quotedOrNULL(regex) +
					" ctx_memory_result_key = " + quotedOrNULL(regex), e );
		}
	}

	public static String quotedOrNULL( String str ) {
		return (str == null) ? "NULL" : '"' + str + '"';
	}

    /**
     * exposes equalsIgnoreCase to directed graph
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * emits a true or false outcome
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static String equalsIgnoreCase(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
        if(parameters.get("source").equalsIgnoreCase(parameters.get("target"))){
            return "true";
        }
        return "false";
    }

    /**
     * exposes toUpperCase to directed graph
     * writes an upperCase version of source to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void toUpper(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
        ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").toUpperCase());
    }

    /**
     * exposes toLowerCase to directed graph
     * writes a lowerCase version of source to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void toLower(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
        ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").toLowerCase());
    }

    /**
     * exposes contains to directed graph to test if one string contains another
     * tests if the source contains the target
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * emits a true or false outcome
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static String contains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
        if(parameters.get("source").contains(parameters.get("target"))){
            return "true";
        }
        return "false";
    }

    /**
     * exposes endsWith to directed graph to test if one string endsWith another string
     * tests if the source ends with the target
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * emits a true or false outcome
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static String endsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
        if(parameters.get("source").endsWith(parameters.get("target"))){
            return "true";
        }
        return "false";
    }

    /**
     * exposes startsWith to directed graph to test if one string endsWith another string
     * tests if the source ends with the target
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * emits a true or false outcome
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static String startsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
        if(parameters.get("source").startsWith(parameters.get("target"))){
            return "true";
        }
        return "false";
    }

    /**
     * exposes trim to directed graph
     * writes a trimmed version of the string to the outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void trim(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
        ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").trim());
    }

    /**
     * exposes String.length() to directed graph
     * writes the length of source to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void getLength(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
        ctx.setAttribute(parameters.get("outputPath"), String.valueOf(parameters.get("source").length()));
    }

    /**
     * exposes replace to directed graph
     * writes the length of source to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
     *      <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void replace(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath","target","replacement"}, LOG);
        ctx.setAttribute(parameters.get("outputPath"), (parameters.get("source").replace(parameters.get("target"), parameters.get("replacement"))));
    }

    /**
     * exposes replaceAll to directed graph
     * writes the length of source to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>This should be a valid regular expression</td></tr>
     *      <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void replaceAll(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath","target","replacement"}, LOG);
        ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").replaceAll(parameters.get("target"), parameters.get("replacement")));
    }
    
    /**
     * Provides substring functionality to Directed Graphs.
     * <p>
     * Calls either String.substring(String beginIndex) or
     * String.substring(String beginInded, String endIndex) if the end-index
     * is present or not.
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>string</td><td>Mandatory</td><td>String to perform substring on</td></tr>
     *      <tr><td>result</td><td>Mandatory</td><td>Key in context memory to populate the resulting string in</td></tr>
     *      <tr><td>begin-index</td><td>Mandatory</td><td>Beginning index to pass to Java substring function</td></tr>
     *      <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>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public void substring( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
        try {
            SliPluginUtils.checkParameters( parameters, new String[]{"string","begin-index","result"}, LOG );
            final String string = parameters.get("string");
            final String result = parameters.get("result");
            final String begin = parameters.get("begin-index");
            final String end = parameters.get("end-index");
            if( StringUtils.isEmpty(end) ) {
                ctx.setAttribute( result, string.substring(Integer.parseInt(begin)) );
            }
            else {
                ctx.setAttribute( result, string.substring(Integer.parseInt(begin), Integer.parseInt(end)) );
            }
        }
        catch( Exception e ) {
            throw new SvcLogicException( "An error occurred while the Directed Graph was performing a substring", e );
        }
    }

    /**
     * Provides concat functionality to Directed Graphs.
     * <p>
     * Will concat target to source and write the result to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     * @since 11.0.2
     */
    public static void concat( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
            SliPluginUtils.checkParameters( parameters, new String[]{"source","target","outputPath"}, LOG );
            String result = parameters.get("source").concat(parameters.get("target"));
            ctx.setAttribute(parameters.get("outputPath"), result);
    }

    /**
     * Provides url encoding functionality to Directed Graphs.
     * <p>
     * Will url encode the source and write the result to outputPath
     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
     * <table border="1">
     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
     *  <tbody>
     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
     *      <tr><td>encoding</td><td>Optional</td><td>the name of a supported character encoding, defaulted to UTF-8 if not supplied</td></tr>
     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
     *  </tbody>
     * </table>
     * @param ctx Reference to context memory
     * @throws SvcLogicException
     */
    public static void urlEncode(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
        SliPluginUtils.checkParameters(parameters, new String[] { "source", "outputPath" }, LOG);
        String encoding = parameters.get("encoding");
        if (encoding == null) {
            encoding = "UTF-8";
        }
        try {
            String result = URLEncoder.encode(parameters.get("source"), encoding);
            ctx.setAttribute(parameters.get("outputPath"), result);
        } catch (UnsupportedEncodingException e) {
            throw new SvcLogicException("Url encode failed.", e);
        }
    }

	/**
	 * xmlEscapeText() will be used to format input xml with text.
	 *
	 * @param inParams
	 *            accepts the instance of {@link Map} holds the input xml in string
	 *            format.
	 * @param ctx
	 *            accepts the instance of {@link SvcLogicContext} holds the service
	 *            logic context.
	 *
	 */
	public static void xmlEscapeText(Map<String, String> inParams, SvcLogicContext ctx) {
		String source = inParams.get(INPUT_PARAM_SOURCE);
		String target = inParams.get(INPUT_PARAM_TARGET);
		source = StringEscapeUtils.escapeXml(source);
		ctx.setAttribute(target, source);
	}
}