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;
25 import java.io.FileOutputStream;
26 import java.io.PrintStream;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Properties;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
35 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class VnfTools implements SvcLogicJavaPlugin {
40 // ========== FIELDS ==========
42 private static final Logger LOG = LoggerFactory.getLogger(VnfTools.class);
44 // ========== CONSTRUCTORS ==========
46 public VnfTools(Properties props) {
48 LOG.debug("props is not null.");
53 public void checkIfActivateReady( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
54 LOG.debug("Checking if enough data is available to send the NCS Activate request...");
56 SliPluginUtils.checkParameters(parameters, new String[]{"return-key"}, LOG);
57 final String returnKey = parameters.get("return-key");
58 ctx.setAttribute(returnKey, "true");
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:
69 * <th>Description</th>
73 * <td>string_to_search</td>
74 * <td>String to perform java String.contains(String) on</td>
77 * <td>string_to_find</td>
78 * <td>String to find in the string_to_search</td>
81 * <td>result_ctx_string</td>
82 * <td>Context memory key to write the result ("true" or "false") to</td>
86 * @param ctx Reference to context memory
87 * @throws SvcLogicException
89 public void stringContains( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
90 SliPluginUtils.checkParameters(parameters, new String[]{"string_to_search","string_to_find","result_ctx_string"}, LOG);
91 ctx.setAttribute(parameters.get("result_ctx_string"), Boolean.toString(parameters.get("string_to_search").contains(parameters.get("string_to_find"))));
95 public void generateName( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
96 LOG.debug("generateName");
98 SliPluginUtils.checkParameters(parameters, new String[]{"base","suffix","return-path"}, LOG);
100 String base = parameters.get("base");
101 ctx.setAttribute( parameters.get("return-path"), base.substring(0, base.length() - 4) + parameters.get("suffix") + base.substring(base.length() - 2) );
105 private boolean matches(String str1, String str2) {
116 return str1.equals(str2);
121 private void setIfNotNull(String property, String value, SvcLogicContext ctx) {
123 LOG.debug("Setting " + property + " to " + value);
124 ctx.setAttribute(property, value);
129 * Moves an array element from one index to another
131 private void copyArrayEntry(String srcRoot, String destRoot, SvcLogicContext ctx) {
132 LOG.debug("copyArrayEntry called: srcRoot=" + srcRoot + ", destRoot=" + destRoot);
134 // Record all of the source keys
135 List<String> keysToMove = new ArrayList<String>();
136 for (String key : ctx.getAttributeKeySet()) {
137 if (key.startsWith(srcRoot)) {
142 // Now loop through and copy those keys to the destination, and then delete the source
143 for (String key : keysToMove) {
144 String suffix = key.substring(srcRoot.length());
145 LOG.debug("Move " + key + " to " + destRoot + suffix);
146 ctx.setAttribute(destRoot + suffix, ctx.getAttribute(key));
147 ctx.setAttribute(key, null);
152 public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
153 if (parameters == null) {
154 throw new SvcLogicException("no parameters passed");
157 String fileName = parameters.get("filename");
159 if ((fileName == null) || (fileName.length() == 0)) {
160 throw new SvcLogicException("printContext requires 'filename' parameter");
163 PrintStream pstr = null;
166 pstr = new PrintStream(new FileOutputStream(new File(fileName), true));
167 } catch (Exception e) {
168 throw new SvcLogicException("Cannot open file " + fileName, e);
170 pstr.println("#######################################");
171 for (String attr : ctx.getAttributeKeySet()) {
172 pstr.println(attr + " = " + ctx.getAttribute(attr));
178 static int getArrayLength( SvcLogicContext ctx, String key ) {
180 return Integer.parseInt(ctx.getAttribute(key));
181 } catch( NumberFormatException e ) {}
186 static int getArrayLength( SvcLogicContext ctx, String key, String debug ) {
188 return Integer.parseInt(ctx.getAttribute(key));
189 } catch( NumberFormatException e ) {
197 * Returns true if string is null or empty.
201 private static boolean stringIsBlank( String str ) {
202 return str == null || str.isEmpty();