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