Change sliapi package names
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / main / java / org / onap / ccsdk / sli / core / slipluginutils / SliStringUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.slipluginutils;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.util.Map;
26
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;
33
34 /**
35  * A SvcLogicJavaPlugin that exposes java.lang.String functions to DirectedGraph
36  */
37 public class SliStringUtils implements SvcLogicJavaPlugin {
38         private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);
39
40         public SliStringUtils() {}
41
42         /**
43          * Provides split functionality to Directed Graphs.
44          * @param parameters HashMap<String,String> of parameters passed by the DG to this function
45          * <table border="1">
46          *      <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
47          *      <tbody>
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>
52          *      </tbody>
53          * </table>
54          * @param ctx Reference to context memory
55          * @throws SvcLogicException
56          * @since 11.0.2
57          * @see String#split(String, int)
58          */
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);
68
69                 try {
70                         // Validation that parameters are not null
71                         SliPluginUtils.checkParameters( parameters, new String[]{"original_string","regex","ctx_memory_result_key"}, LOG );
72
73                         // Read limit from context memory. Default to 0 if null/empty
74                         int limit = 0;
75                         if( StringUtils.isNotEmpty(limit_str) ) {
76                                 try {
77                                         limit = Integer.parseInt(limit_str);
78                                 }
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 );
81                                 }
82                         }
83
84                         // Call String.split(regex,limit) on string passed in
85                         String[] split_string = original_string.split(regex, limit);
86
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);
90                         }
91                         SliPluginUtils.ctxSetAttribute(ctx, ctx_memory_result_key + "_length", new Integer(split_string.length), LOG, SliPluginUtils.LogLevel.DEBUG);
92                 }
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 );
99                 }
100         }
101
102         private static String quotedOrNULL( String str ) {
103                 return (str == null) ? "NULL" : '"' + str + '"';
104         }
105
106     /**
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
110      * <table border="1">
111      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
112      *  <tbody>
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>
115      *  </tbody>
116      * </table>
117      * @param ctx Reference to context memory
118      * @throws SvcLogicException
119      * @since 11.0.2
120      */
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"))){
124             return "true";
125         }
126         return "false";
127     }
128
129     /**
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
133      * <table border="1">
134      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
135      *  <tbody>
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>
138      *  </tbody>
139      * </table>
140      * @param ctx Reference to context memory
141      * @throws SvcLogicException
142      * @since 11.0.2
143      */
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());
147     }
148
149     /**
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
153      * <table border="1">
154      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
155      *  <tbody>
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>
158      *  </tbody>
159      * </table>
160      * @param ctx Reference to context memory
161      * @throws SvcLogicException
162      * @since 11.0.2
163      */
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());
167     }
168
169     /**
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
174      * <table border="1">
175      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
176      *  <tbody>
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>
179      *  </tbody>
180      * </table>
181      * @param ctx Reference to context memory
182      * @throws SvcLogicException
183      * @since 11.0.2
184      */
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"))){
188             return "true";
189         }
190         return "false";
191     }
192
193     /**
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
198      * <table border="1">
199      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
200      *  <tbody>
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>
203      *  </tbody>
204      * </table>
205      * @param ctx Reference to context memory
206      * @throws SvcLogicException
207      * @since 11.0.2
208      */
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"))){
212             return "true";
213         }
214         return "false";
215     }
216
217     /**
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
222      * <table border="1">
223      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
224      *  <tbody>
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>
227      *  </tbody>
228      * </table>
229      * @param ctx Reference to context memory
230      * @throws SvcLogicException
231      * @since 11.0.2
232      */
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"))){
236             return "true";
237         }
238         return "false";
239     }
240
241     /**
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
245      * <table border="1">
246      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
247      *  <tbody>
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>
250      *  </tbody>
251      * </table>
252      * @param ctx Reference to context memory
253      * @throws SvcLogicException
254      * @since 11.0.2
255      */
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());
259     }
260
261     /**
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
265      * <table border="1">
266      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
267      *  <tbody>
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>
270      *  </tbody>
271      * </table>
272      * @param ctx Reference to context memory
273      * @throws SvcLogicException
274      * @since 11.0.2
275      */
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()));
279     }
280
281     /**
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
285      * <table border="1">
286      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
287      *  <tbody>
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>
292      *  </tbody>
293      * </table>
294      * @param ctx Reference to context memory
295      * @throws SvcLogicException
296      * @since 11.0.2
297      */
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"))));
301     }
302
303     /**
304      * Provides substring functionality to Directed Graphs.
305      * <p>
306      * Calls either String.substring(String beginIndex) or
307      * String.substring(String beginInded, String endIndex) if the end-index
308      * is present or not.
309      * @param parameters HashMap<String,String> of parameters passed by the DG to this function
310      * <table border="1">
311      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
312      *  <tbody>
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>
317      *  </tbody>
318      * </table>
319      * @param ctx Reference to context memory
320      * @throws SvcLogicException
321      * @since 11.0.2
322      */
323     public void substring( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
324         try {
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)) );
332             }
333             else {
334                 ctx.setAttribute( result, string.substring(Integer.parseInt(begin), Integer.parseInt(end)) );
335             }
336         }
337         catch( Exception e ) {
338             throw new SvcLogicException( "An error occurred while the Directed Graph was performing a substring", e );
339         }
340     }
341
342     /**
343      * Provides concat functionality to Directed Graphs.
344      * <p>
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
347      * <table border="1">
348      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
349      *  <tbody>
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>
353      *  </tbody>
354      * </table>
355      * @param ctx Reference to context memory
356      * @throws SvcLogicException
357      * @since 11.0.2
358      */
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);
363     }
364
365     /**
366      * Provides url encoding functionality to Directed Graphs.
367      * <p>
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
370      * <table border="1">
371      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
372      *  <tbody>
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>
376      *  </tbody>
377      * </table>
378      * @param ctx Reference to context memory
379      * @throws SvcLogicException
380      */
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) {
385             encoding = "UTF-8";
386         }
387         try {
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);
392         }
393     }
394
395 }