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.openecomp.sdnc.sli.SliPluginUtils;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.ListIterator;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
34 * A utility class used to manage list manipulation in the context memory.
35 * @see org.openecomp.sdnc.sli.SvcLogicContext
37 public class SvcLogicContextList {
39 * Internal flag indicating if list should be deleted from context memory
40 * when it is copied into the SvcLogicContextList object.
42 private enum OperType {
47 protected final String prefix;
49 protected final ArrayList<HashMap<String,String>> list;
53 public SvcLogicContextList( SvcLogicContext ctx, String list_prefix ) {
54 this(ctx, list_prefix, OperType.COPY);
58 private SvcLogicContextList( SvcLogicContext ctx, String list_prefix, OperType operation ) {
59 this.prefix = list_prefix;
62 int capacity = getCtxListLength(ctx, prefix);
63 this.list = new ArrayList<>( capacity );
64 for( int i = 0; i < capacity; i++ ) {
65 this.list.add(i, new HashMap<String,String>());
68 // Populate "elements" in list
69 String prefix_bracket = this.prefix + '[';
70 for( String key : new HashSet<>(ctx.getAttributeKeySet()) ) {
71 if( key.startsWith(prefix_bracket) ) {
72 // Extract the index of the list
73 int index = getCtxListIndex(key, this.prefix, capacity);
76 String suffix = key.substring((prefix_bracket + index + ']').length());
77 suffix = suffix.isEmpty() ? suffix : suffix.substring(1);
78 this.list.get(index).put( suffix, ctx.getAttribute(key));
80 // If flag to extract set, remove data from context memory as
81 // it is read into this list
82 if( operation == OperType.EXTRACT ) {
83 ctx.setAttribute(key, null);
88 // If flag to extract set, remove list _length value from cxt mem
89 if( operation == OperType.EXTRACT ) {
90 ctx.setAttribute(this.prefix + "_length", null);
95 public static SvcLogicContextList extract( SvcLogicContext ctx, String list_prefix ) {
96 return new SvcLogicContextList(ctx, list_prefix, OperType.EXTRACT);
100 // ========== PUBLIC FUNCTIONS ==========
103 public HashMap<String,String> get( int index ) {
104 return this.list.get(index);
108 public HashMap<String,String> remove( int index ) {
109 return this.list.remove(index);
113 public void remove( String value ) {
118 public void remove( String key, String value ) {
119 if( value == null ) {
120 throw new IllegalArgumentException("value cannot be null");
123 ListIterator<HashMap<String,String>> itr = this.list.listIterator();
124 while( itr.hasNext() ) {
125 if( value.equals(itr.next().get(key)) ) {
132 public void remove( Map<String,String> primary_key ) {
133 ListIterator<HashMap<String,String>> itr = this.list.listIterator();
134 while( itr.hasNext() ) {
135 boolean found = true;
136 HashMap<String,String> list_element = itr.next();
137 for( Map.Entry<String,String> key : primary_key.entrySet() ) {
138 if( !key.getValue().equals(list_element.get(key.getKey())) ) {
156 public void writeToContext( SvcLogicContext ctx ) {
157 ctx.setAttribute( prefix + "_length", Integer.toString(this.list.size()) );
159 for( int i = 0; i < this.list.size(); i++ ) {
160 for( Map.Entry<String,String> entry : this.list.get(i).entrySet() ) {
161 if( entry.getKey().equals("") ) {
162 ctx.setAttribute(prefix + '[' + i + ']', entry.getValue());
165 ctx.setAttribute(prefix + '[' + i + "]." + entry.getKey(), entry.getValue());
173 // ========== PRIVATE STATIC FUNCTIONS ==========
176 private static int getCtxListIndex( String key, String prefix, int list_size ) {
177 int index = getCtxListIndex( key, prefix );
178 if( index >= list_size ) {
179 throw new IllegalArgumentException(
180 "Context memory list \"" + prefix + "[]\" contains an index >= the size of the list",
181 new ArrayIndexOutOfBoundsException( "index \"" + index + "\" is outside the bounds of the context memory list \"" + prefix + "[]. List Length = " + list_size )
184 else if( index < 0 ) {
185 throw new IllegalArgumentException(
186 "Context memory list \"" + prefix + "[]\" contains a negative index",
187 new NegativeArraySizeException( "index \"" + index + "\" of context memory list is negative" )
195 private static int getCtxListIndex( String key, String prefix ) {
196 String ctx_index_str = StringUtils.substringBetween(key.substring(prefix.length()), "[", "]");
198 return Integer.parseInt( ctx_index_str );
200 catch( NumberFormatException e ) {
201 throw new IllegalStateException("Could not parse index value \"" + ctx_index_str + "\" in context memory key \"" + key + "\"", e);
206 private static int getCtxListLength( SvcLogicContext ctx, String prefix ) {
207 String _length_key = prefix + "_length";
208 String _length_val_str = ctx.getAttribute(_length_key);
210 return Integer.parseInt(_length_val_str);
212 catch( NumberFormatException e ) {
213 if( _length_val_str == null ) {
214 throw new IllegalStateException( "Could not find list length \"" + _length_key + "\" in context memory." );
217 throw new IllegalStateException( "Could not parse index value \"" + _length_val_str + "\" of context memory list length \"" + _length_key + "\"" , e );