1e9f9b6a159c1a1c818f5729c070fb565ec35fb3
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / util / BundleFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.oam.util;
26
27 import org.osgi.framework.Bundle;
28
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.regex.Pattern;
33
34
35
36 /**
37  *
38  * Utility Class that splits a given bundleSet into two sets: bundleToStopSet and
39  * bundleToNotStopSet
40  *
41  * The bundleToStopSet is defined as: all bundles which match at least one of
42  * the stopRegexes but exceptRegexes none of the
43  *
44  * The bundleToNotStopSet is defined as all bundles which are not a member of
45  * the bundleToStopSet
46  *
47  */
48 class BundleFilter {
49
50     private final Map<String, Bundle> bundleToStopSet;
51     private final Map<String, Bundle> bundleToNotStopSet;
52
53
54     /**
55      * BundleFilter a bundle filter
56      * @param stopRegexes  - An array of regular expression used to pick out which bundles are candidates for stopping
57      * @param exceptRegexes - An array of regular expression used to override which bundles are candidates for stopping
58      * @param bundles - An array of the bundle to be split into {@link #getBundlesToStop()} {@link #getBundlesToNotStop()}
59      */
60     BundleFilter(String[] stopRegexes, String[] exceptRegexes, Bundle[] bundles) {
61
62         Pattern[] stopPatterns = toPattern(stopRegexes);
63         Pattern[] exceptPatterns = toPattern(exceptRegexes);
64
65         Map<String, Bundle> bundleToStop = new HashMap<>();
66         Map<String, Bundle>  bundleToNotStop = new HashMap<>();
67
68         for (Bundle bundle : bundles) {
69             String symbolicName = bundle.getSymbolicName();
70             if (isMatch(symbolicName,stopPatterns) && !isMatch(symbolicName,exceptPatterns)) {
71                 bundleToStop.put(symbolicName, bundle);
72             } else {
73                 bundleToNotStop.put(symbolicName, bundle);
74             }
75         }
76
77         this.bundleToStopSet = Collections.unmodifiableMap(bundleToStop);
78         this.bundleToNotStopSet = Collections.unmodifiableMap(bundleToNotStop);
79     }
80
81     /**
82      * Determines if the value matches any of the regular expressions.
83      *
84      * @param value
85      *            - the value that is to be matched
86      * @param patterns
87      *            - the array of {@link Pattern} to match the value against
88      * @return boolean true if there is a match
89      */
90     private boolean isMatch(String value,Pattern[] patterns) {
91         for (Pattern pattern : patterns) {
92             if (pattern.matcher(value).matches()) {
93                 return true;
94             }
95         }
96         return false;
97     }
98
99     /**
100      * This method converts an Array of regular expression in String form into a
101      * Array of {@link Pattern}
102      *
103      * @param regex
104      *            - A string array of regular expressions
105      * @return Pattern Array of compiled regular expressions
106      */
107     private Pattern[] toPattern(String[] regex) {
108         Pattern[] pattern = new Pattern[regex.length];
109         for (int i = 0; i < regex.length; i++ ) {
110             pattern[i] = Pattern.compile(regex[i]);
111         }
112         return pattern;
113     }
114
115
116     /**@return Map of bundles that are to be stopped  */
117     Map<String, Bundle> getBundlesToStop(){
118         return bundleToStopSet;
119     }
120
121     /**
122      *
123      * @return Map of bundles that are not to be stopped
124      */
125     Map<String, Bundle> getBundlesToNotStop() {
126         return bundleToNotStopSet;
127     }
128 }