aboutsummaryrefslogtreecommitdiffstats
path: root/sliPluginUtils/provider/src/main/java/org/openecomp/sdnc/sli/SliPluginUtils/SvcLogicContextList.java
blob: 89a4a98ffe31f8c0729024d633fcb23d7cf12369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdnc.sli.SliPluginUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ListIterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdnc.sli.SvcLogicContext;

/**
 * A utility class used to manage list manipulation in the context memory.
 * @see org.openecomp.sdnc.sli.SvcLogicContext
 */
public class SvcLogicContextList {
    /**
     * Internal flag indicating if list should be deleted from context memory
     * when it is copied into the SvcLogicContextList object.
     */
    private enum OperType {
        COPY, EXTRACT
    }

    // TODO: javadoc
    protected final String prefix;
    // TODO: javadoc
    protected final ArrayList<HashMap<String,String>> list;


    // TODO: javadoc
    public SvcLogicContextList( SvcLogicContext ctx, String list_prefix ) {
        this(ctx, list_prefix, OperType.COPY);
    }

    // TODO: javadoc
    private SvcLogicContextList( SvcLogicContext ctx, String list_prefix, OperType operation ) {
        this.prefix = list_prefix;

        // Initialize list
        int capacity = getCtxListLength(ctx, prefix);
        this.list = new ArrayList<HashMap<String, String>>(capacity);
        for( int i = 0; i < capacity; i++ ) {
            this.list.add(i, new HashMap<String,String>());
        }

        // Populate "elements" in list
        String prefix_bracket = this.prefix + '[';
        for (String key : new HashSet<String>(ctx.getAttributeKeySet())) {
            if( key.startsWith(prefix_bracket) ) {
                // Extract the index of the list
                int index = getCtxListIndex(key, this.prefix, capacity);

                // Store the
                String suffix = key.substring((prefix_bracket + index + ']').length());
                suffix = suffix.isEmpty() ? suffix : suffix.substring(1);
                this.list.get(index).put( suffix, ctx.getAttribute(key));

                // If flag to extract set, remove data from context memory as
                // it is read into this list
                if( operation == OperType.EXTRACT ) {
                    ctx.setAttribute(key, null);
                }
            }
        }

        // If flag to extract set, remove list _length value from cxt mem
        if( operation == OperType.EXTRACT ) {
            ctx.setAttribute(this.prefix + "_length", null);
        }
    }

    // TODO: javadoc
    public static SvcLogicContextList extract( SvcLogicContext ctx, String list_prefix ) {
        return new SvcLogicContextList(ctx, list_prefix, OperType.EXTRACT);
    }


    // ========== PUBLIC FUNCTIONS ==========

    // TODO: javadoc
    public HashMap<String,String> get( int index ) {
        return this.list.get(index);
    }

    // TODO: javadoc
    public HashMap<String,String> remove( int index ) {
        return this.list.remove(index);
    }

    // TODO: javadoc
    public void remove( String value ) {
        remove( "", value );
    }

    // TODO: javadoc
    public void remove( String key, String value ) {
        if( value == null ) {
            throw new IllegalArgumentException("value cannot be null");
        }

        ListIterator<HashMap<String,String>> itr = this.list.listIterator();
        while( itr.hasNext() ) {
            if( value.equals(itr.next().get(key)) ) {
                itr.remove();
            }
        }
    }

    // TODO javadoc
    public void remove( Map<String,String> primary_key ) {
        ListIterator<HashMap<String,String>> itr = this.list.listIterator();
        while( itr.hasNext() ) {
            boolean found = true;
            HashMap<String,String> list_element = itr.next();
            for( Map.Entry<String,String> key : primary_key.entrySet() ) {
                if( !key.getValue().equals(list_element.get(key.getKey())) ) {
                    found = false;
                    break;
                }
            }

            if( found ) {
                itr.remove();
            }
        }
    }

    // TODO: javadoc
    public int size() {
        return list.size();
    }

    // TODO: javadoc
    public void writeToContext( SvcLogicContext ctx ) {
        ctx.setAttribute( prefix + "_length", Integer.toString(this.list.size()) );

        for( int i = 0; i < this.list.size(); i++ ) {
            for( Map.Entry<String,String> entry : this.list.get(i).entrySet() ) {
                if( entry.getKey().equals("") ) {
                    ctx.setAttribute(prefix + '[' + i + ']', entry.getValue());
                } else {
                    ctx.setAttribute(prefix + '[' + i + "]." + entry.getKey(), entry.getValue());
                }
            }
        }
    }



    // ========== PRIVATE STATIC FUNCTIONS ==========

    // TODO: javadoc
    private static int getCtxListIndex( String key, String prefix, int list_size ) {
        int index = getCtxListIndex( key, prefix );
        if( index >= list_size ) {
            throw new IllegalArgumentException("Context memory list \"" + prefix + "[]\" contains an index >= the size of the list", new ArrayIndexOutOfBoundsException("index \"" + index + "\" is outside the bounds of the context memory list \"" + prefix + "[]. List Length = " + list_size));
        } else if (index < 0) {
            throw new IllegalArgumentException("Context memory list \"" + prefix + "[]\" contains a negative index", new NegativeArraySizeException("index \"" + index + "\" of context memory list is negative"));
        }

        return index;
    }

    // TODO: javadoc
    private static int getCtxListIndex( String key, String prefix ) {
        String ctx_index_str = StringUtils.substringBetween(key.substring(prefix.length()), "[", "]");
        try {
            return Integer.parseInt( ctx_index_str );
        } catch (NumberFormatException e) {
            throw new IllegalStateException("Could not parse index value \"" + ctx_index_str + "\" in context memory key \"" + key + "\"", e);
        }
    }

    // TODO: javadoc
    private static int getCtxListLength( SvcLogicContext ctx, String prefix ) {
        String _length_key = prefix + "_length";
        String _length_val_str = ctx.getAttribute(_length_key);
        try {
            return Integer.parseInt(_length_val_str);
        } catch (NumberFormatException e) {
            if( _length_val_str == null ) {
                throw new IllegalStateException( "Could not find list length \"" + _length_key + "\" in context memory." );
            } else {
                throw new IllegalStateException( "Could not parse index value \"" + _length_val_str + "\" of context memory list length \"" + _length_key + "\"" , e );
            }
        }
    }
}