More changes to actor code
[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     private static final Coder coder = new StandardCoder();
38
39     private Util() {
40         // do nothing
41     }
42
43     /**
44      * Extracts an object's identity by invoking {@link Object#toString()} and returning
45      * the portion starting with "@". Extraction is done on-demand, when toString() is
46      * called on the result. This is typically used when logging.
47      *
48      * @param object object whose identity is to be extracted
49      * @return an object that will extract the source object's identity when this object's
50      *         toString() method is called
51      */
52     public static Object ident(Object object) {
53         return new DelayedIdentString(object);
54     }
55
56     /**
57      * Runs a function and logs a message if it throws an exception. Does <i>not</i>
58      * re-throw the exception.
59      *
60      * @param function function to be run
61      * @param exceptionMessage message to log if an exception is thrown
62      * @param exceptionArgs arguments to be passed to the logger
63      */
64     public static void runFunction(Runnable function, String exceptionMessage, Object... exceptionArgs) {
65         try {
66             function.run();
67
68         } catch (RuntimeException ex) {
69             // create a new array containing the original arguments plus the exception
70             Object[] allArgs = Arrays.copyOf(exceptionArgs, exceptionArgs.length + 1);
71             allArgs[exceptionArgs.length] = ex;
72
73             logger.warn(exceptionMessage, allArgs);
74         }
75     }
76
77     /**
78      * Translates parameters from one class to another, typically from a Map to a POJO or
79      * vice versa.
80      *
81      * @param identifier identifier of the actor/operation being translated; used to build
82      *        an exception message
83      * @param source source object to be translated
84      * @param clazz target class
85      * @return the translated object
86      */
87     public static <T> T translate(String identifier, Object source, Class<T> clazz) {
88         try {
89             return coder.convert(source, clazz);
90
91         } catch (CoderException | RuntimeException e) {
92             throw new IllegalArgumentException("cannot translate parameters for " + identifier, e);
93         }
94     }
95
96     /**
97      * Translates parameters to a Map.
98      *
99      * @param identifier identifier of the actor/operation being translated; used to build
100      *        an exception message
101      * @param source source parameters
102      * @return the parameters, as a Map
103      */
104     @SuppressWarnings("unchecked")
105     public static Map<String, Object> translateToMap(String identifier, Object source) {
106         return translate(identifier, source, LinkedHashMap.class);
107     }
108 }