33acace93474b18da8e3096e4079002df4df4a34
[sdnc/northbound.git] / vnftools / provider / src / main / java / org / openecomp / sdnc / vnftools / VnfTools.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.vnftools;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.PrintStream;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Properties;
31
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import org.openecomp.sdnc.sli.SvcLogicException;
34 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
35 import org.openecomp.sdnc.sli.SliPluginUtils.SliPluginUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class VnfTools implements SvcLogicJavaPlugin {
40         // ========== FIELDS ==========
41
42         private static final Logger LOG = LoggerFactory.getLogger(VnfTools.class);
43
44         // ========== CONSTRUCTORS ==========
45
46         public VnfTools(Properties props) {
47                 if (props != null) {
48                         LOG.debug("props is not null.");
49                 }
50         }
51
52
53         public void checkIfActivateReady( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
54                 LOG.debug("Checking if enough data is available to send the NCS Activate request...");
55
56                 SliPluginUtils.checkParameters(parameters, new String[]{"return-key"}, LOG);
57                 final String returnKey = parameters.get("return-key");
58                 ctx.setAttribute(returnKey, "true");
59
60         }
61
62         /**
63          * DG node performs a java String.contains(String) and writes true or false
64          * to a key in context memory.
65          * @param parameters Hashmap in context memory must contain the following:
66          * <table border='1'>
67          * <thead>
68          *      <th>Key</th>
69          *      <th>Description</th>
70          * </thead>
71          * <tbody>
72          *      <tr>
73          *              <td>string_to_search</td>
74          *              <td>String to perform java String.contains(String) on</td>
75          *      </tr>
76          *  <tr>
77          *              <td>string_to_find</td>
78          *              <td>String to find in the string_to_search</td>
79          *      </tr>
80          *  <tr>
81          *              <td>result_ctx_string</td>
82          *              <td>Context memory key to write the result ("true" or "false") to</td>
83          *      </tr>
84          * </tbody>
85          * </table>
86          * @param ctx Reference to context memory
87          * @throws SvcLogicException
88          */
89         public void stringContains( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
90                 SliPluginUtils.checkParameters(parameters, new String[]{"string_to_search","string_to_find","result_ctx_string"}, LOG);
91                 ctx.setAttribute(parameters.get("result_ctx_string"), Boolean.toString(parameters.get("string_to_search").contains(parameters.get("string_to_find"))));
92         }
93
94
95         public void generateName( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
96                 LOG.debug("generateName");
97
98                 SliPluginUtils.checkParameters(parameters, new String[]{"base","suffix","return-path"}, LOG);
99
100                 String base = parameters.get("base");
101                 ctx.setAttribute( parameters.get("return-path"), base.substring(0, base.length() - 4) + parameters.get("suffix") + base.substring(base.length() - 2) );
102         }
103
104
105         private boolean matches(String str1, String str2) {
106                 if (str1 == null) {
107                         if (str2 == null) {
108                                 return true;
109                         } else {
110                                 return false;
111                         }
112                 } else {
113                         if (str2 == null) {
114                                 return false;
115                         } else {
116                                 return str1.equals(str2);
117                         }
118                 }
119         }
120
121         private void setIfNotNull(String property, String value, SvcLogicContext ctx) {
122                 if (value != null) {
123                         LOG.debug("Setting " + property + " to " + value);
124                         ctx.setAttribute(property, value);
125                 }
126         }
127
128         /*
129          * Moves an array element from one index to another
130          */
131         private void copyArrayEntry(String srcRoot, String destRoot, SvcLogicContext ctx) {
132                 LOG.debug("copyArrayEntry called: srcRoot=" + srcRoot + ", destRoot=" + destRoot);
133
134                 // Record all of the source keys
135                 List<String> keysToMove = new ArrayList<String>();
136                 for (String key : ctx.getAttributeKeySet()) {
137                         if (key.startsWith(srcRoot)) {
138                                 keysToMove.add(key);
139                         }
140                 }
141
142                 // Now loop through and copy those keys to the destination, and then delete the source
143                 for (String key : keysToMove) {
144                         String suffix = key.substring(srcRoot.length());
145                         LOG.debug("Move " + key + " to " + destRoot + suffix);
146                         ctx.setAttribute(destRoot + suffix, ctx.getAttribute(key));
147                         ctx.setAttribute(key, null);
148                 }
149
150         }
151
152         public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
153                 if (parameters == null) {
154                         throw new SvcLogicException("no parameters passed");
155                 }
156
157                 String fileName = parameters.get("filename");
158
159                 if ((fileName == null) || (fileName.length() == 0)) {
160                         throw new SvcLogicException("printContext requires 'filename' parameter");
161                 }
162
163                 PrintStream pstr = null;
164
165                 try {
166                         pstr = new PrintStream(new FileOutputStream(new File(fileName), true));
167                 } catch (Exception e) {
168                         throw new SvcLogicException("Cannot open file " + fileName, e);
169                 }
170                 pstr.println("#######################################");
171                 for (String attr : ctx.getAttributeKeySet()) {
172                         pstr.println(attr + " = " + ctx.getAttribute(attr));
173                 }
174                 pstr.flush();
175                 pstr.close();
176         }
177
178         static int getArrayLength( SvcLogicContext ctx, String key ) {
179                 try {
180                         return Integer.parseInt(ctx.getAttribute(key));
181                 } catch( NumberFormatException e ) {}
182
183                 return 0;
184         }
185
186         static int getArrayLength( SvcLogicContext ctx, String key, String debug ) {
187                 try {
188                         return Integer.parseInt(ctx.getAttribute(key));
189                 } catch( NumberFormatException e ) {
190                         LOG.debug(debug);
191                 }
192
193                 return 0;
194         }
195
196         /**
197          * Returns true if string is null or empty.
198          * @param str
199          * @return
200          */
201         private static boolean stringIsBlank( String str ) {
202                 return str == null || str.isEmpty();
203         }
204
205 }