Roll versions to next Guilin snapshot
[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 keys:
66      * <ul>
67      * <li><b>string_to_sear</b> : String to perform java String.contains(String) on</li>
68      * <li><b>string_to_find</b> : String to find in the string_to_search</li>
69      * <li><b>result_ctx_string</b> : Context memory key to write the result ("true" or "false") to</li>
70      * </ul>
71      * @param ctx Reference to context memory
72      * @throws SvcLogicException when passed in parameter is not valid
73      */
74     public void stringContains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
75         SliPluginUtils.checkParameters(
76                 parameters, new String[]{STRING_TO_SEARCH, STRING_TO_FIND, RESULT_CTX_STRING}, LOG);
77         setIfNotNull(parameters.get(RESULT_CTX_STRING),
78                 Boolean.toString(parameters.get(STRING_TO_SEARCH).contains(parameters.get(STRING_TO_FIND))),
79                 ctx);
80     }
81
82
83     public void generateName(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
84         LOG.debug("generateName");
85
86         SliPluginUtils.checkParameters(parameters, new String[]{BASE, SUFFIX, RETURN_PATH}, LOG);
87
88         String base = parameters.get(BASE);
89         int baseLength = base.length();
90         if (baseLength < 4) {
91             String errorMessage = String.format("Parameter(%s) needs at least length 4 but only have %d",
92                     BASE, baseLength);
93             LOG.error(errorMessage);
94             throw new SvcLogicException(errorMessage);
95         }
96
97         setIfNotNull(parameters.get(RETURN_PATH), String.format("%s%s%s",
98                 base.substring(0, baseLength - 4), parameters.get(SUFFIX), base.substring(baseLength - 2)),
99                 ctx);
100     }
101
102     private void setIfNotNull(String property, String value, SvcLogicContext ctx) {
103         if (property != null && value != null) {
104             LOG.debug("Setting ", property, " to ",  value);
105             ctx.setAttribute(property, value);
106         }
107     }
108
109     public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
110         if (parameters == null) {
111             throw new SvcLogicException("no parameters passed");
112         }
113
114         String fileName = parameters.get(FILENAME);
115
116         if ((fileName == null) || (fileName.length() == 0)) {
117             throw new SvcLogicException("printContext requires 'filename' parameter");
118         }
119
120         PrintStream pstr = null;
121
122         try (FileOutputStream fileStream = new FileOutputStream(new File(fileName), true)){
123             pstr = new PrintStream(fileStream);
124         } catch (IOException e1) {
125             LOG.error("FileOutputStream close exception: ", e1);
126         }
127         catch (Exception e) {
128             throw new SvcLogicException("Cannot open file " + fileName, e);
129         } finally {
130             if (pstr != null) {
131                 pstr.println("#######################################");
132                 for (String attr : ctx.getAttributeKeySet()) {
133                     pstr.println(attr + " = " + ctx.getAttribute(attr));
134                 }
135
136                 pstr.flush();
137                 pstr.close();
138             }
139         }
140
141     }
142
143     static int getArrayLength(SvcLogicContext ctx, String key) {
144         String value = ctx.getAttribute(key);
145         try {
146             return Integer.parseInt(value);
147         } catch( NumberFormatException e ) {
148             LOG.debug(String.format("Ctx contained key(%s) value(%s) is not integer", key, value));
149         }
150
151         return 0;
152     }
153
154     static int getArrayLength(SvcLogicContext ctx, String key, String debug) {
155         try {
156             return Integer.parseInt(ctx.getAttribute(key));
157         } catch( NumberFormatException e ) {
158             LOG.debug(debug);
159         }
160
161         return 0;
162     }
163 }