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.IOException;
27 import java.io.PrintStream;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.Properties;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
36 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class VnfTools implements SvcLogicJavaPlugin {
41 // ========== FIELDS ==========
43 private static final Logger LOG = LoggerFactory.getLogger(VnfTools.class);
45 // ========== CONSTRUCTORS ==========
47 public VnfTools(Properties props) {
49 LOG.debug("props is not null.");
54 public void checkIfActivateReady( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
55 LOG.debug("Checking if enough data is available to send the NCS Activate request...");
57 SliPluginUtils.checkParameters(parameters, new String[]{"return-key"}, LOG);
58 final String returnKey = parameters.get("return-key");
59 ctx.setAttribute(returnKey, "true");
64 * DG node performs a java String.contains(String) and writes true or false
65 * to a key in context memory.
66 * @param parameters Hashmap in context memory must contain the following:
70 * <th>Description</th>
74 * <td>string_to_search</td>
75 * <td>String to perform java String.contains(String) on</td>
78 * <td>string_to_find</td>
79 * <td>String to find in the string_to_search</td>
82 * <td>result_ctx_string</td>
83 * <td>Context memory key to write the result ("true" or "false") to</td>
87 * @param ctx Reference to context memory
88 * @throws SvcLogicException
90 public void stringContains( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
91 SliPluginUtils.checkParameters(parameters, new String[]{"string_to_search","string_to_find","result_ctx_string"}, LOG);
92 ctx.setAttribute(parameters.get("result_ctx_string"), Boolean.toString(parameters.get("string_to_search").contains(parameters.get("string_to_find"))));
96 public void generateName( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
97 LOG.debug("generateName");
99 SliPluginUtils.checkParameters(parameters, new String[]{"base","suffix","return-path"}, LOG);
101 String base = parameters.get("base");
102 ctx.setAttribute( parameters.get("return-path"), base.substring(0, base.length() - 4) + parameters.get("suffix") + base.substring(base.length() - 2) );
106 private boolean matches(String str1, String str2) {
117 return str1.equals(str2);
122 private void setIfNotNull(String property, String value, SvcLogicContext ctx) {
124 LOG.debug("Setting " + property + " to " + value);
125 ctx.setAttribute(property, value);
130 * Moves an array element from one index to another
132 private void copyArrayEntry(String srcRoot, String destRoot, SvcLogicContext ctx) {
133 LOG.debug("copyArrayEntry called: srcRoot=" + srcRoot + ", destRoot=" + destRoot);
135 // Record all of the source keys
136 List<String> keysToMove = new ArrayList<String>();
137 for (String key : ctx.getAttributeKeySet()) {
138 if (key.startsWith(srcRoot)) {
143 // Now loop through and copy those keys to the destination, and then delete the source
144 for (String key : keysToMove) {
145 String suffix = key.substring(srcRoot.length());
146 LOG.debug("Move " + key + " to " + destRoot + suffix);
147 ctx.setAttribute(destRoot + suffix, ctx.getAttribute(key));
148 ctx.setAttribute(key, null);
153 public void printContext(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
154 if (parameters == null) {
155 throw new SvcLogicException("no parameters passed");
158 String fileName = parameters.get("filename");
160 if ((fileName == null) || (fileName.length() == 0)) {
161 throw new SvcLogicException("printContext requires 'filename' parameter");
164 PrintStream pstr = null;
165 FileOutputStream fileStream = null;
168 pstr = new PrintStream(fileStream = new FileOutputStream(new File(fileName), true));
169 } catch (Exception e) {
170 if (fileStream != null) {
173 } catch (IOException e1) {
174 LOG.error("FileOutputStream close exception: ", e1);
177 throw new SvcLogicException("Cannot open file " + fileName, e);
180 pstr.println("#######################################");
181 for (String attr : ctx.getAttributeKeySet()) {
182 pstr.println(attr + " = " + ctx.getAttribute(attr));
189 static int getArrayLength( SvcLogicContext ctx, String key ) {
191 return Integer.parseInt(ctx.getAttribute(key));
192 } catch( NumberFormatException e ) {}
197 static int getArrayLength( SvcLogicContext ctx, String key, String debug ) {
199 return Integer.parseInt(ctx.getAttribute(key));
200 } catch( NumberFormatException e ) {
208 * Returns true if string is null or empty.
212 private static boolean stringIsBlank( String str ) {
213 return str == null || str.isEmpty();