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);
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...");
58 SliPluginUtils.checkParameters(parameters, new String[]{RETURN_KEY}, LOG);
59 setIfNotNull(parameters.get(RETURN_KEY), TRUE_STRING, ctx);
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:
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>
71 * @param ctx Reference to context memory
72 * @throws SvcLogicException when passed in parameter is not valid
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))),
83 public void generateName(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
84 LOG.debug("generateName");
86 SliPluginUtils.checkParameters(parameters, new String[]{BASE, SUFFIX, RETURN_PATH}, LOG);
88 String base = parameters.get(BASE);
89 int baseLength = base.length();
91 String errorMessage = String.format("Parameter(%s) needs at least length 4 but only have %d",
93 LOG.error(errorMessage);
94 throw new SvcLogicException(errorMessage);
97 setIfNotNull(parameters.get(RETURN_PATH), String.format("%s%s%s",
98 base.substring(0, baseLength - 4), parameters.get(SUFFIX), base.substring(baseLength - 2)),
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);
109 public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
110 if (parameters == null) {
111 throw new SvcLogicException("no parameters passed");
114 String fileName = parameters.get(FILENAME);
116 if ((fileName == null) || (fileName.length() == 0)) {
117 throw new SvcLogicException("printContext requires 'filename' parameter");
120 PrintStream pstr = null;
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);
127 catch (Exception e) {
128 throw new SvcLogicException("Cannot open file " + fileName, e);
131 pstr.println("#######################################");
132 for (String attr : ctx.getAttributeKeySet()) {
133 pstr.println(attr + " = " + ctx.getAttribute(attr));
143 static int getArrayLength(SvcLogicContext ctx, String key) {
144 String value = ctx.getAttribute(key);
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));
154 static int getArrayLength(SvcLogicContext ctx, String key, String debug) {
156 return Integer.parseInt(ctx.getAttribute(key));
157 } catch( NumberFormatException e ) {