2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.sdnc.vnftools;
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;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
36 import java.util.Properties;
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";
49 private static final Logger LOG = LoggerFactory.getLogger(VnfTools.class);
51 public VnfTools(Properties props) {
53 LOG.debug("props is not null.");
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...");
60 SliPluginUtils.checkParameters(parameters, new String[]{RETURN_KEY}, LOG);
61 setIfNotNull(parameters.get(RETURN_KEY), TRUE_STRING, ctx);
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:
71 * <th>Description</th>
75 * <td>string_to_search</td>
76 * <td>String to perform java String.contains(String) on</td>
79 * <td>string_to_find</td>
80 * <td>String to find in the string_to_search</td>
83 * <td>result_ctx_string</td>
84 * <td>Context memory key to write the result ("true" or "false") to</td>
88 * @param ctx Reference to context memory
89 * @throws SvcLogicException when passed in parameter is not valid
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))),
100 public void generateName(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
101 LOG.debug("generateName");
103 SliPluginUtils.checkParameters(parameters, new String[]{BASE, SUFFIX, RETURN_PATH}, LOG);
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",
110 LOG.error(errorMessage);
111 throw new SvcLogicException(errorMessage);
114 setIfNotNull(parameters.get(RETURN_PATH), String.format("%s%s%s",
115 base.substring(0, baseLength - 4), parameters.get(SUFFIX), base.substring(baseLength - 2)),
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);
126 public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
127 if (parameters == null) {
128 throw new SvcLogicException("no parameters passed");
131 String fileName = parameters.get(FILENAME);
133 if ((fileName == null) || (fileName.length() == 0)) {
134 throw new SvcLogicException("printContext requires 'filename' parameter");
137 PrintStream pstr = null;
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);
144 catch (Exception e) {
145 throw new SvcLogicException("Cannot open file " + fileName, e);
148 pstr.println("#######################################");
149 for (String attr : ctx.getAttributeKeySet()) {
150 pstr.println(attr + " = " + ctx.getAttribute(attr));
160 static int getArrayLength(SvcLogicContext ctx, String key) {
161 String value = ctx.getAttribute(key);
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));
171 static int getArrayLength(SvcLogicContext ctx, String key, String debug) {
173 return Integer.parseInt(ctx.getAttribute(key));
174 } catch( NumberFormatException e ) {