Add more code to facilitate actor implementation
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.controlloop.actorserviceprovider;
22
23 import java.util.Arrays;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import org.onap.policy.common.utils.coder.Coder;
27 import org.onap.policy.common.utils.coder.CoderException;
28 import org.onap.policy.common.utils.coder.StandardCoder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Actor utilities.
34  */
35 public class Util {
36     private static final Logger logger = LoggerFactory.getLogger(Util.class);
37
38     private Util() {
39         // do nothing
40     }
41
42     /**
43      * Extracts an object's identity by invoking {@link Object#toString()} and returning
44      * the portion starting with "@". Extraction is done on-demand, when toString() is
45      * called on the result. This is typically used when logging.
46      *
47      * @param object object whose identity is to be extracted
48      * @return an object that will extract the source object's identity when this object's
49      *         toString() method is called
50      */
51     public static Object ident(Object object) {
52         return new DelayedIdentString(object);
53     }
54
55     /**
56      * Runs a function and logs a message if it throws an exception. Does <i>not</i>
57      * re-throw the exception.
58      *
59      * @param function function to be run
60      * @param exceptionMessage message to log if an exception is thrown
61      * @param exceptionArgs arguments to be passed to the logger
62      */
63     public static void runFunction(Runnable function, String exceptionMessage, Object... exceptionArgs) {
64         try {
65             function.run();
66
67         } catch (RuntimeException ex) {
68             // create a new array containing the original arguments plus the exception
69             Object[] allArgs = Arrays.copyOf(exceptionArgs, exceptionArgs.length + 1);
70             allArgs[exceptionArgs.length] = ex;
71
72             logger.warn(exceptionMessage, allArgs);
73         }
74     }
75
76     /**
77      * Translates parameters from one class to another, typically from a Map to a POJO or
78      * vice versa.
79      *
80      * @param identifier identifier of the actor/operation being translated; used to build
81      *        an exception message
82      * @param source source object to be translated
83      * @param clazz target class
84      * @return the translated object
85      */
86     public static <T> T translate(String identifier, Object source, Class<T> clazz) {
87         Coder coder = new StandardCoder();
88
89         try {
90             String json = coder.encode(source);
91             return coder.decode(json, clazz);
92
93         } catch (CoderException | RuntimeException e) {
94             throw new IllegalArgumentException("cannot translate parameters for " + identifier, e);
95         }
96     }
97
98     /**
99      * Translates parameters to a Map.
100      *
101      * @param identifier identifier of the actor/operation being translated; used to build
102      *        an exception message
103      * @param source source parameters
104      * @return the parameters, as a Map
105      */
106     @SuppressWarnings("unchecked")
107     public static Map<String, Object> translateToMap(String identifier, Object source) {
108         if (source == null) {
109             return null;
110         }
111
112         return translate(identifier, source, LinkedHashMap.class);
113     }
114 }