2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdnc.sli.SliPluginUtils;
24 import java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdnc.sli.SvcLogicContext;
30 import org.openecomp.sdnc.sli.SvcLogicException;
31 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * A SvcLogicJavaPlugin that exposes java.lang.String functions to DirectedGraph
38 public class SliStringUtils implements SvcLogicJavaPlugin {
39 private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);
41 public SliStringUtils() {}
44 * Provides split functionality to Directed Graphs.
45 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
47 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
49 * <tr><td>original_string</td><td>Mandatory</td><td>String to perform split on</td></tr>
50 * <tr><td>regex</td><td>Mandatory</td><td>the delimiting regular expression</td></tr>
51 * <tr><td>limit</td><td>Optional</td><td>result threshold. See String.split method for further description. Defaults to 0</td></tr>
52 * <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>
55 * @param ctx Reference to context memory
56 * @throws SvcLogicException
58 * @see String#split(String, int)
60 public void split( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
61 final String original_string = parameters.get("original_string");
62 LOG.trace("original_string = " + original_string);
63 final String regex = parameters.get("regex");
64 LOG.trace("regex = " + regex);
65 final String limit_str = parameters.get("limit");
66 LOG.trace("limit_str = " + limit_str);
67 final String ctx_memory_result_key = parameters.get("ctx_memory_result_key");
68 LOG.trace("ctx_memory_result_key = " + ctx_memory_result_key);
71 // Validation that parameters are not null
72 SliPluginUtils.checkParameters( parameters, new String[]{"original_string","regex","ctx_memory_result_key"}, LOG );
74 // Read limit from context memory. Default to 0 if null/empty
76 if( StringUtils.isNotEmpty(limit_str) ) {
78 limit = Integer.parseInt(limit_str);
80 catch( NumberFormatException e ) {
81 throw new IllegalArgumentException( "The limit parameter of the SliStringUtils.split() function must be a number, empty string, or null", e );
85 // Call String.split(regex,limit) on string passed in
86 String[] split_string = original_string.split(regex, limit);
88 // Populate context memory with results
89 for( int i = 0; i < split_string.length; i++ ) {
90 SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + '[' + i + ']', split_string[i], LOG, SliPluginUtils.LogLevel.DEBUG);
92 SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + "_length", new Integer(split_string.length), LOG, SliPluginUtils.LogLevel.DEBUG);
94 catch( Exception e ) {
95 // Have error message print parameters
96 throw new SvcLogicException( "An error occurred during SliStringUtils.split() where original_string = " + quotedOrNULL(regex) +
97 " regex = " + quotedOrNULL(regex) +
98 " limit = " + quotedOrNULL(regex) +
99 " ctx_memory_result_key = " + quotedOrNULL(regex), e );
103 private static String quotedOrNULL( String str ) {
104 return (str == null) ? "NULL" : '"' + str + '"';
108 * exposes equalsIgnoreCase to directed graph
109 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
110 * emits a true or false outcome
112 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
114 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
115 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
118 * @param ctx Reference to context memory
119 * @throws SvcLogicException
122 public static String equalsIgnoreCase(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
123 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
124 if(parameters.get("source").equalsIgnoreCase(parameters.get("target"))){
131 * exposes toUpperCase to directed graph
132 * writes an upperCase version of source to outputPath
133 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
135 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
137 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
138 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
141 * @param ctx Reference to context memory
142 * @throws SvcLogicException
145 public static void toUpper(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
146 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
147 ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").toUpperCase());
151 * exposes toLowerCase to directed graph
152 * writes a lowerCase version of source to outputPath
153 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
155 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
157 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
158 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
161 * @param ctx Reference to context memory
162 * @throws SvcLogicException
165 public static void toLower(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
166 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
167 ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").toLowerCase());
171 * exposes contains to directed graph to test if one string contains another
172 * tests if the source contains the target
173 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
174 * emits a true or false outcome
176 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
178 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
179 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
182 * @param ctx Reference to context memory
183 * @throws SvcLogicException
186 public static String contains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
187 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
188 if(parameters.get("source").contains(parameters.get("target"))){
195 * exposes endsWith to directed graph to test if one string endsWith another string
196 * tests if the source ends with the target
197 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
198 * emits a true or false outcome
200 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
202 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
203 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
206 * @param ctx Reference to context memory
207 * @throws SvcLogicException
210 public static String endsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
211 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
212 if(parameters.get("source").endsWith(parameters.get("target"))){
219 * exposes startsWith to directed graph to test if one string endsWith another string
220 * tests if the source ends with the target
221 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
222 * emits a true or false outcome
224 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
226 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
227 * <tr><td>target</td><td>Mandatory</td><td>target string</td></tr>
230 * @param ctx Reference to context memory
231 * @throws SvcLogicException
234 public static String startsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
235 SliPluginUtils.checkParameters(parameters, new String[]{"source","target"}, LOG);
236 if(parameters.get("source").startsWith(parameters.get("target"))){
243 * exposes trim to directed graph
244 * writes a trimmed version of the string to the outputPath
245 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
247 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
249 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
250 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
253 * @param ctx Reference to context memory
254 * @throws SvcLogicException
257 public static void trim(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
258 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
259 ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").trim());
263 * exposes String.length() to directed graph
264 * writes the length of source to outputPath
265 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
267 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
269 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
270 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
273 * @param ctx Reference to context memory
274 * @throws SvcLogicException
277 public static void getLength(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
278 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath"}, LOG);
279 ctx.setAttribute(parameters.get("outputPath"), String.valueOf(parameters.get("source").length()));
283 * exposes replace to directed graph
284 * writes the length of source to outputPath
285 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
287 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
289 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
290 * <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
291 * <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
292 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
295 * @param ctx Reference to context memory
296 * @throws SvcLogicException
299 public static void replace(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
300 SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath","target","replacement"}, LOG);
301 ctx.setAttribute(parameters.get("outputPath"), (parameters.get("source").replace(parameters.get("target"), parameters.get("replacement"))));
305 * Provides substring functionality to Directed Graphs.
307 * Calls either String.substring(String beginIndex) or
308 * String.substring(String beginInded, String endIndex) if the end-index
310 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
312 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
314 * <tr><td>string</td><td>Mandatory</td><td>String to perform substring on</td></tr>
315 * <tr><td>result</td><td>Mandatory</td><td>Key in context memory to populate the resulting string in</td></tr>
316 * <tr><td>begin-index</td><td>Mandatory</td><td>Beginning index to pass to Java substring function</td></tr>
317 * <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>
320 * @param ctx Reference to context memory
321 * @throws SvcLogicException
324 public void substring( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
326 SliPluginUtils.checkParameters( parameters, new String[]{"string","begin-index","result"}, LOG );
327 final String string = parameters.get("string");
328 final String result = parameters.get("result");
329 final String begin = parameters.get("begin-index");
330 final String end = parameters.get("end-index");
331 if( StringUtils.isEmpty(end) ) {
332 ctx.setAttribute( result, string.substring(Integer.parseInt(begin)) );
335 ctx.setAttribute( result, string.substring(Integer.parseInt(begin), Integer.parseInt(end)) );
338 catch( Exception e ) {
339 throw new SvcLogicException( "An error occurred while the Directed Graph was performing a substring", e );
344 * Provides concat functionality to Directed Graphs.
346 * Will concat target to source and write the result to outputPath
347 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
349 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
351 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
352 * <tr><td>target</td><td>Mandatory</td><td>The sequence of char values to be replaced</td></tr>
353 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
356 * @param ctx Reference to context memory
357 * @throws SvcLogicException
360 public static void concat( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
361 SliPluginUtils.checkParameters( parameters, new String[]{"source","target","outputPath"}, LOG );
362 String result = parameters.get("source").concat(parameters.get("target"));
363 ctx.setAttribute(parameters.get("outputPath"), result);
367 * Provides url encoding functionality to Directed Graphs.
369 * Will url encode the source and write the result to outputPath
370 * @param parameters HashMap<String,String> of parameters passed by the DG to this function
372 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
374 * <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
375 * <tr><td>encoding</td><td>Optional</td><td>the name of a supported character encoding, defaulted to UTF-8 if not supplied</td></tr>
376 * <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
379 * @param ctx Reference to context memory
380 * @throws SvcLogicException
382 public static void urlEncode(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
383 SliPluginUtils.checkParameters(parameters, new String[] { "source", "outputPath" }, LOG);
384 String encoding = parameters.get("encoding");
385 if (encoding == null) {
389 String result = URLEncoder.encode(parameters.get("source"), encoding);
390 ctx.setAttribute(parameters.get("outputPath"), result);
391 } catch (UnsupportedEncodingException e) {
392 throw new SvcLogicException("Url encode failed.", e);