35080d396d0fedb1a4d5d4a6af3891a05f964deb
[policy/engine.git] / ONAP-PDP / src / main / java / org / onap / policy / xacml / custom / OnapFunctionDefinitionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
21 package org.onap.policy.xacml.custom;
22
23 import com.att.research.xacml.api.Identifier;
24 import com.att.research.xacml.std.IdentifierImpl;
25 import com.att.research.xacml.std.datatypes.DataTypes;
26 import com.att.research.xacmlatt.pdp.policy.FunctionDefinition;
27 import com.att.research.xacmlatt.pdp.policy.FunctionDefinitionFactory;
28 import com.att.research.xacmlatt.pdp.std.StdFunctions;
29
30 import java.lang.reflect.Field;
31 import java.lang.reflect.Modifier;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.xacml.pdp.std.functions.FunctionDefinitionCustomRegexpMatch;
38
39 public class OnapFunctionDefinitionFactory extends FunctionDefinitionFactory {
40     private static Logger logger = FlexLogger.getLogger(OnapFunctionDefinitionFactory.class);
41     private static Map<Identifier, FunctionDefinition> mapFunctionDefinitions = new HashMap<>();
42     private static boolean needMapInit = true;
43
44     public static final Identifier ID_FUNCTION_CUSTOM_REGEXP_MATCH =
45             new IdentifierImpl("org.onap.function.regex-match");
46
47     private static final FunctionDefinition FD_CUSTOM_REGEXP_MATCH =
48             new FunctionDefinitionCustomRegexpMatch<>(ID_FUNCTION_CUSTOM_REGEXP_MATCH, DataTypes.DT_STRING);
49
50     private static void register(final FunctionDefinition functionDefinition) {
51         mapFunctionDefinitions.put(functionDefinition.getId(), functionDefinition);
52     }
53
54     private static void initMap() {
55
56         synchronized (mapFunctionDefinitions) {
57             if (needMapInit) {
58                 needMapInit = false;
59                 final Field[] declaredFields = StdFunctions.class.getDeclaredFields();
60                 for (final Field field : declaredFields) {
61                     if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith(StdFunctions.FD_PREFIX)
62                             && FunctionDefinition.class.isAssignableFrom(field.getType())
63                             && Modifier.isPublic(field.getModifiers())) {
64                         try {
65                             register((FunctionDefinition) (field.get(null)));
66                         } catch (final IllegalAccessException ex) {
67                             logger.error(ex.getMessage() + ex);
68                         }
69                     }
70                 }
71                 //
72                 // Our custom function
73                 //
74                 // register(FunctionDefinitionCustomRegexpMatch);
75                 register(FD_CUSTOM_REGEXP_MATCH);
76             }
77         }
78     }
79
80     public OnapFunctionDefinitionFactory() {
81         initMap();
82     }
83
84     @Override
85     public FunctionDefinition getFunctionDefinition(final Identifier functionId) {
86         return mapFunctionDefinitions.get(functionId);
87     }
88 }