Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / custom / CustomFunctionDefinitionFactory.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
21 package org.openecomp.policy.pdp.test.custom;
22
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Modifier;
25 import java.security.PrivateKey;
26 import java.security.PublicKey;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import com.att.research.xacml.api.Identifier;
31 import com.att.research.xacml.std.IdentifierImpl;
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 import com.att.research.xacmlatt.pdp.std.functions.FunctionDefinitionBagOneAndOnly;
36
37 public class CustomFunctionDefinitionFactory extends FunctionDefinitionFactory {
38         private static Map<Identifier,FunctionDefinition>       mapFunctionDefinitions  = new HashMap<>();
39         private static boolean                                                          needMapInit                             = true;
40         
41         public static final Identifier ID_FUNCTION_PRIVATEKEY_ONE_AND_ONLY = new IdentifierImpl("urn:com:att:research:xacml:custom:function:3.0:rsa:privatekey-one-and-only");
42         public static final Identifier ID_FUNCTION_PUBLICKEY_ONE_AND_ONLY = new IdentifierImpl("urn:com:att:research:xacml:custom:function:3.0:rsa:publickey-one-and-only");
43         
44         public static final FunctionDefinition  FD_PRIVATEKEY_ONE_AND_ONLY      = new FunctionDefinitionBagOneAndOnly<PrivateKey>(ID_FUNCTION_PRIVATEKEY_ONE_AND_ONLY, DataTypePrivateKey.newInstance());
45         public static final FunctionDefinition  FD_PUBLICKEY_ONE_AND_ONLY       = new FunctionDefinitionBagOneAndOnly<PublicKey>(ID_FUNCTION_PUBLICKEY_ONE_AND_ONLY, DataTypePublicKey.newInstance());
46
47         private static void register(FunctionDefinition functionDefinition) {
48                 mapFunctionDefinitions.put(functionDefinition.getId(), functionDefinition);
49         }
50                 
51         private static void initMap() {
52                 if (needMapInit) {
53                         synchronized(mapFunctionDefinitions) {
54                                 if (needMapInit) {
55                                         needMapInit     = false;
56                                         Field[] declaredFields  = StdFunctions.class.getDeclaredFields();
57                                         for (Field field : declaredFields) {
58                                                 if (Modifier.isStatic(field.getModifiers()) && 
59                                                         field.getName().startsWith(StdFunctions.FD_PREFIX) &&
60                                                         FunctionDefinition.class.isAssignableFrom(field.getType()) &&
61                                                         Modifier.isPublic(field.getModifiers())
62                                                 ) {
63                                                         try {
64                                                                 register((FunctionDefinition)(field.get(null)));
65                                                         } catch (IllegalAccessException ex) {
66                                                                 
67                                                         }
68                                                 }
69                                         }
70                                         //
71                                         // Our custom function
72                                         //
73                                         register(FunctionDefinitionDecrypt.newInstance());
74                                         register(FD_PRIVATEKEY_ONE_AND_ONLY);
75                                         register(FD_PUBLICKEY_ONE_AND_ONLY);
76                                 }
77                         }
78                 }
79         }
80         
81         public CustomFunctionDefinitionFactory() {
82                 initMap();
83         }
84
85         @Override
86         public FunctionDefinition getFunctionDefinition(Identifier functionId) {
87                 return mapFunctionDefinitions.get(functionId);
88         }
89
90 }