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