43af6f59a078bff8fbd7ee7557505bd6e3399294
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / main / java / org / openecomp / sdnc / sli / SliPluginUtils / SliStringUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.SliPluginUtils;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.Map;
27
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;
34
35 /**
36  * A SvcLogicJavaPlugin that exposes java.lang.String functions to DirectedGraph
37  */
38 public class SliStringUtils implements SvcLogicJavaPlugin {
39         private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);
40
41         public SliStringUtils() {}
42
43         /**
44          * Provides split functionality to Directed Graphs.
45          * @param parameters HashMap<String,String> of parameters passed by the DG to this function
46          * <table border="1">
47          *      <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
48          *      <tbody>
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>
53          *      </tbody>
54          * </table>
55          * @param ctx Reference to context memory
56          * @throws SvcLogicException
57          * @since 11.0.2
58          * @see String#split(String, int)
59          */
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);
69
70                 try {
71                         // Validation that parameters are not null
72                         SliPluginUtils.checkParameters( parameters, new String[]{"original_string","regex","ctx_memory_result_key"}, LOG );
73
74                         // Read limit from context memory. Default to 0 if null/empty
75                         int limit = 0;
76                         if( StringUtils.isNotEmpty(limit_str) ) {
77                                 try {
78                                         limit = Integer.parseInt(limit_str);
79                                 }
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 );
82                                 }
83                         }
84
85                         // Call String.split(regex,limit) on string passed in
86                         String[] split_string = original_string.split(regex, limit);
87
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);
91                         }
92                         SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + "_length", new Integer(split_string.length), LOG, SliPluginUtils.LogLevel.DEBUG);
93                 }
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 );
100                 }
101         }
102
103         private static String quotedOrNULL( String str ) {
104                 return (str == null) ? "NULL" : '"' + str + '"';
105         }
106
107     /**
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
111      * <table border="1">
112      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
113      *  <tbody>
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>
116      *  </tbody>
117      * </table>
118      * @param ctx Reference to context memory
119      * @throws SvcLogicException
120      * @since 11.0.2
121      */
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"))){
125             return "true";
126         }
127         return "false";
128     }
129
130     /**
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
134      * <table border="1">
135      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
136      *  <tbody>
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>
139      *  </tbody>
140      * </table>
141      * @param ctx Reference to context memory
142      * @throws SvcLogicException
143      * @since 11.0.2
144      */
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());
148     }
149
150     /**
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
154      * <table border="1">
155      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
156      *  <tbody>
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>
159      *  </tbody>
160      * </table>
161      * @param ctx Reference to context memory
162      * @throws SvcLogicException
163      * @since 11.0.2
164      */
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());
168     }
169
170     /**
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
175      * <table border="1">
176      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
177      *  <tbody>
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>
180      *  </tbody>
181      * </table>
182      * @param ctx Reference to context memory
183      * @throws SvcLogicException
184      * @since 11.0.2
185      */
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"))){
189             return "true";
190         }
191         return "false";
192     }
193
194     /**
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
199      * <table border="1">
200      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
201      *  <tbody>
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>
204      *  </tbody>
205      * </table>
206      * @param ctx Reference to context memory
207      * @throws SvcLogicException
208      * @since 11.0.2
209      */
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"))){
213             return "true";
214         }
215         return "false";
216     }
217
218     /**
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
223      * <table border="1">
224      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
225      *  <tbody>
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>
228      *  </tbody>
229      * </table>
230      * @param ctx Reference to context memory
231      * @throws SvcLogicException
232      * @since 11.0.2
233      */
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"))){
237             return "true";
238         }
239         return "false";
240     }
241
242     /**
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
246      * <table border="1">
247      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
248      *  <tbody>
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>
251      *  </tbody>
252      * </table>
253      * @param ctx Reference to context memory
254      * @throws SvcLogicException
255      * @since 11.0.2
256      */
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());
260     }
261
262     /**
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
266      * <table border="1">
267      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
268      *  <tbody>
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>
271      *  </tbody>
272      * </table>
273      * @param ctx Reference to context memory
274      * @throws SvcLogicException
275      * @since 11.0.2
276      */
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()));
280     }
281
282     /**
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
286      * <table border="1">
287      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
288      *  <tbody>
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>
293      *  </tbody>
294      * </table>
295      * @param ctx Reference to context memory
296      * @throws SvcLogicException
297      * @since 11.0.2
298      */
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"))));
302     }
303
304     /**
305      * Provides substring functionality to Directed Graphs.
306      * <p>
307      * Calls either String.substring(String beginIndex) or
308      * String.substring(String beginInded, String endIndex) if the end-index
309      * is present or not.
310      * @param parameters HashMap<String,String> of parameters passed by the DG to this function
311      * <table border="1">
312      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
313      *  <tbody>
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>
318      *  </tbody>
319      * </table>
320      * @param ctx Reference to context memory
321      * @throws SvcLogicException
322      * @since 11.0.2
323      */
324     public void substring( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
325         try {
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)) );
333             }
334             else {
335                 ctx.setAttribute( result, string.substring(Integer.parseInt(begin), Integer.parseInt(end)) );
336             }
337         }
338         catch( Exception e ) {
339             throw new SvcLogicException( "An error occurred while the Directed Graph was performing a substring", e );
340         }
341     }
342
343     /**
344      * Provides concat functionality to Directed Graphs.
345      * <p>
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
348      * <table border="1">
349      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
350      *  <tbody>
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>
354      *  </tbody>
355      * </table>
356      * @param ctx Reference to context memory
357      * @throws SvcLogicException
358      * @since 11.0.2
359      */
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);
364     }
365
366     /**
367      * Provides url encoding functionality to Directed Graphs.
368      * <p>
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
371      * <table border="1">
372      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
373      *  <tbody>
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>
377      *  </tbody>
378      * </table>
379      * @param ctx Reference to context memory
380      * @throws SvcLogicException
381      */
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) {
386             encoding = "UTF-8";
387         }
388         try {
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);
393         }
394     }
395
396 }