Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / main / java / org / openecomp / policy / xacml / custom / EcompFunctionDefinitionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.policy.xacml.custom;
21
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Modifier;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.openecomp.policy.xacml.pdp.std.functions.FunctionDefinitionCustomRegexpMatch;
28
29 import com.att.research.xacml.api.Identifier;
30 import com.att.research.xacml.std.IdentifierImpl;
31 import com.att.research.xacml.std.datatypes.DataTypes;
32 import com.att.research.xacmlatt.pdp.policy.FunctionDefinition;
33 import com.att.research.xacmlatt.pdp.policy.FunctionDefinitionFactory;
34 import com.att.research.xacmlatt.pdp.std.StdFunctions;
35
36 public class EcompFunctionDefinitionFactory extends FunctionDefinitionFactory {
37         private static Map<Identifier,FunctionDefinition>       mapFunctionDefinitions  = new HashMap<>();
38         private static boolean                                                          needMapInit                             = true;
39
40         public static final Identifier ID_FUNCTION_CUSTOM_REGEXP_MATCH                                          = new IdentifierImpl("org.openecomp.function.regex-match");
41         
42         private static final FunctionDefinition FD_CUSTOM_REGEXP_MATCH = new FunctionDefinitionCustomRegexpMatch<>(ID_FUNCTION_CUSTOM_REGEXP_MATCH, DataTypes.DT_STRING);
43
44         private static void register(FunctionDefinition functionDefinition) {
45                 mapFunctionDefinitions.put(functionDefinition.getId(), functionDefinition);
46         }
47                 
48         private static void initMap() {
49                 if (needMapInit) {
50                         synchronized(mapFunctionDefinitions) {
51                                 if (needMapInit) {
52                                         needMapInit     = false;
53                                         Field[] declaredFields  = StdFunctions.class.getDeclaredFields();
54                                         for (Field field : declaredFields) {
55                                                 if (Modifier.isStatic(field.getModifiers()) && 
56                                                         field.getName().startsWith(StdFunctions.FD_PREFIX) &&
57                                                         FunctionDefinition.class.isAssignableFrom(field.getType()) &&
58                                                         Modifier.isPublic(field.getModifiers())
59                                                 ) {
60                                                         try {
61                                                                 register((FunctionDefinition)(field.get(null)));
62                                                         } catch (IllegalAccessException ex) {
63                                                                 
64                                                         }
65                                                 }
66                                         }
67                                         //
68                                         // Our custom function
69                                         //
70                                         //register(FunctionDefinitionCustomRegexpMatch);
71                                         register(FD_CUSTOM_REGEXP_MATCH);
72                                 }
73                         }
74                 }
75         }
76         
77         public EcompFunctionDefinitionFactory() {
78                 initMap();
79         }
80
81         @Override
82         public FunctionDefinition getFunctionDefinition(Identifier functionId) {
83                 return mapFunctionDefinitions.get(functionId);
84         }
85 }