a452a4ab00f7dce2da211e4c0e272e7c41d99119
[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(Properties props) {
52         if (props != null) {
53             LOG.debug("props is not null.");
54         }
55     }
56
57     public void checkIfActivateReady(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
58         LOG.debug("Checking if enough data is available to send the NCS Activate request...");
59
60         SliPluginUtils.checkParameters(parameters, new String[]{RETURN_KEY}, LOG);
61         setIfNotNull(parameters.get(RETURN_KEY), TRUE_STRING, ctx);
62     }
63
64     /**
65      * DG node performs a java String.contains(String) and writes true or false
66      * to a key in context memory.
67      * @param parameters HashMap in context memory must contain the following:
68      * <table border='1'>
69      * <thead>
70      *     <th>Key</th>
71      *     <th>Description</th>
72      * </thead>
73      * <tbody>
74      *     <tr>
75      *         <td>string_to_search</td>
76      *         <td>String to perform java String.contains(String) on</td>
77      *     </tr>
78      *  <tr>
79      *         <td>string_to_find</td>
80      *         <td>String to find in the string_to_search</td>
81      *     </tr>
82      *  <tr>
83      *         <td>result_ctx_string</td>
84      *         <td>Context memory key to write the result ("true" or "false") to</td>
85      *     </tr>
86      * </tbody>
87      * </table>
88      * @param ctx Reference to context memory
89      * @throws SvcLogicException when passed in parameter is not valid
90      */
91     public void stringContains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
92         SliPluginUtils.checkParameters(
93                 parameters, new String[]{STRING_TO_SEARCH, STRING_TO_FIND, RESULT_CTX_STRING}, LOG);
94         setIfNotNull(parameters.get(RESULT_CTX_STRING),
95                 Boolean.toString(parameters.get(STRING_TO_SEARCH).contains(parameters.get(STRING_TO_FIND))),
96                 ctx);
97     }
98
99
100     public void generateName(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
101         LOG.debug("generateName");
102
103         SliPluginUtils.checkParameters(parameters, new String[]{BASE, SUFFIX, RETURN_PATH}, LOG);
104
105         String base = parameters.get(BASE);
106         int baseLength = base.length();
107         if (baseLength < 4) {
108             String errorMessage = String.format("Parameter(%s) needs at least length 4 but only have %d",
109                     BASE, baseLength);
110             LOG.error(errorMessage);
111             throw new SvcLogicException(errorMessage);
112         }
113
114         setIfNotNull(parameters.get(RETURN_PATH), String.format("%s%s%s",
115                 base.substring(0, baseLength - 4), parameters.get(SUFFIX), base.substring(baseLength - 2)),
116                 ctx);
117     }
118
119     private void setIfNotNull(String property, String value, SvcLogicContext ctx) {
120         if (property != null && value != null) {
121             LOG.debug("Setting ", property, " to ",  value);
122             ctx.setAttribute(property, value);
123         }
124     }
125
126     public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
127         if (parameters == null) {
128             throw new SvcLogicException("no parameters passed");
129         }
130
131         String fileName = parameters.get(FILENAME);
132
133         if ((fileName == null) || (fileName.length() == 0)) {
134             throw new SvcLogicException("printContext requires 'filename' parameter");
135         }
136
137         PrintStream pstr = null;
138
139         try (FileOutputStream fileStream = new FileOutputStream(new File(fileName), true)){
140             pstr = new PrintStream(fileStream);
141         } catch (IOException e1) {
142             LOG.error("FileOutputStream close exception: ", e1);
143         }
144         catch (Exception e) {
145             throw new SvcLogicException("Cannot open file " + fileName, e);
146         } finally {
147             if (pstr != null) {
148                 pstr.println("#######################################");
149                 for (String attr : ctx.getAttributeKeySet()) {
150                     pstr.println(attr + " = " + ctx.getAttribute(attr));
151                 }
152
153                 pstr.flush();
154                 pstr.close();
155             }
156         }
157
158     }
159
160     static int getArrayLength(SvcLogicContext ctx, String key) {
161         String value = ctx.getAttribute(key);
162         try {
163             return Integer.parseInt(value);
164         } catch( NumberFormatException e ) {
165             LOG.debug(String.format("Ctx contained key(%s) value(%s) is not integer", key, value));
166         }
167
168         return 0;
169     }
170
171     static int getArrayLength(SvcLogicContext ctx, String key, String debug) {
172         try {
173             return Integer.parseInt(ctx.getAttribute(key));
174         } catch( NumberFormatException e ) {
175             LOG.debug(debug);
176         }
177
178         return 0;
179     }
180 }