Fix vnftools load issues
[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 org.onap.ccsdk.sli.core.sli.SvcLogicContext;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
27 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.util.Map;
36 import java.util.Properties;
37
38 public class VnfTools implements SvcLogicJavaPlugin {
39     static final String BASE = "base";
40     static final String FILENAME = "filename";
41     static final String RESULT_CTX_STRING = "result_ctx_string";
42     static final String RETURN_KEY = "return-key";
43     static final String RETURN_PATH = "return-path";
44     static final String STRING_TO_FIND = "string_to_find";
45     static final String STRING_TO_SEARCH = "string_to_search";
46     static final String SUFFIX = "suffix";
47     static final String TRUE_STRING = "true";
48
49     private static final Logger LOG = LoggerFactory.getLogger(VnfTools.class);
50
51     public VnfTools() {
52
53     }
54
55     public void checkIfActivateReady(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
56         LOG.debug("Checking if enough data is available to send the NCS Activate request...");
57
58         SliPluginUtils.checkParameters(parameters, new String[]{RETURN_KEY}, LOG);
59         setIfNotNull(parameters.get(RETURN_KEY), TRUE_STRING, ctx);
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 when passed in parameter is not valid
88      */
89     public void stringContains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
90         SliPluginUtils.checkParameters(
91                 parameters, new String[]{STRING_TO_SEARCH, STRING_TO_FIND, RESULT_CTX_STRING}, LOG);
92         setIfNotNull(parameters.get(RESULT_CTX_STRING),
93                 Boolean.toString(parameters.get(STRING_TO_SEARCH).contains(parameters.get(STRING_TO_FIND))),
94                 ctx);
95     }
96
97
98     public void generateName(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
99         LOG.debug("generateName");
100
101         SliPluginUtils.checkParameters(parameters, new String[]{BASE, SUFFIX, RETURN_PATH}, LOG);
102
103         String base = parameters.get(BASE);
104         int baseLength = base.length();
105         if (baseLength < 4) {
106             String errorMessage = String.format("Parameter(%s) needs at least length 4 but only have %d",
107                     BASE, baseLength);
108             LOG.error(errorMessage);
109             throw new SvcLogicException(errorMessage);
110         }
111
112         setIfNotNull(parameters.get(RETURN_PATH), String.format("%s%s%s",
113                 base.substring(0, baseLength - 4), parameters.get(SUFFIX), base.substring(baseLength - 2)),
114                 ctx);
115     }
116
117     private void setIfNotNull(String property, String value, SvcLogicContext ctx) {
118         if (property != null && value != null) {
119             LOG.debug("Setting ", property, " to ",  value);
120             ctx.setAttribute(property, value);
121         }
122     }
123
124     public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
125         if (parameters == null) {
126             throw new SvcLogicException("no parameters passed");
127         }
128
129         String fileName = parameters.get(FILENAME);
130
131         if ((fileName == null) || (fileName.length() == 0)) {
132             throw new SvcLogicException("printContext requires 'filename' parameter");
133         }
134
135         PrintStream pstr = null;
136
137         try (FileOutputStream fileStream = new FileOutputStream(new File(fileName), true)){
138             pstr = new PrintStream(fileStream);
139         } catch (IOException e1) {
140             LOG.error("FileOutputStream close exception: ", e1);
141         }
142         catch (Exception e) {
143             throw new SvcLogicException("Cannot open file " + fileName, e);
144         } finally {
145             if (pstr != null) {
146                 pstr.println("#######################################");
147                 for (String attr : ctx.getAttributeKeySet()) {
148                     pstr.println(attr + " = " + ctx.getAttribute(attr));
149                 }
150
151                 pstr.flush();
152                 pstr.close();
153             }
154         }
155
156     }
157
158     static int getArrayLength(SvcLogicContext ctx, String key) {
159         String value = ctx.getAttribute(key);
160         try {
161             return Integer.parseInt(value);
162         } catch( NumberFormatException e ) {
163             LOG.debug(String.format("Ctx contained key(%s) value(%s) is not integer", key, value));
164         }
165
166         return 0;
167     }
168
169     static int getArrayLength(SvcLogicContext ctx, String key, String debug) {
170         try {
171             return Integer.parseInt(ctx.getAttribute(key));
172         } catch( NumberFormatException e ) {
173             LOG.debug(debug);
174         }
175
176         return 0;
177     }
178 }