summaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/Util.java
blob: b885b5c2561c2dafd1958a295ba4b5d0b2f219ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.controlloop.actorserviceprovider;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Actor utilities.
 */
public class Util {
    private static final Logger logger = LoggerFactory.getLogger(Util.class);

    private Util() {
        // do nothing
    }

    /**
     * Extracts an object's identity by invoking {@link Object#toString()} and returning
     * the portion starting with "@". Extraction is done on-demand, when toString() is
     * called on the result. This is typically used when logging.
     *
     * @param object object whose identity is to be extracted
     * @return an object that will extract the source object's identity when this object's
     *         toString() method is called
     */
    public static Object ident(Object object) {
        return new DelayedIdentString(object);
    }

    /**
     * Runs a function and logs a message if it throws an exception. Does <i>not</i>
     * re-throw the exception.
     *
     * @param function function to be run
     * @param exceptionMessage message to log if an exception is thrown
     * @param exceptionArgs arguments to be passed to the logger
     */
    public static void runFunction(Runnable function, String exceptionMessage, Object... exceptionArgs) {
        try {
            function.run();

        } catch (RuntimeException ex) {
            // create a new array containing the original arguments plus the exception
            Object[] allArgs = Arrays.copyOf(exceptionArgs, exceptionArgs.length + 1);
            allArgs[exceptionArgs.length] = ex;

            logger.warn(exceptionMessage, allArgs);
        }
    }

    /**
     * Translates parameters from one class to another, typically from a Map to a POJO or
     * vice versa.
     *
     * @param identifier identifier of the actor/operation being translated; used to build
     *        an exception message
     * @param source source object to be translated
     * @param clazz target class
     * @return the translated object
     */
    public static <T> T translate(String identifier, Object source, Class<T> clazz) {
        Coder coder = new StandardCoder();

        try {
            String json = coder.encode(source);
            return coder.decode(json, clazz);

        } catch (CoderException | RuntimeException e) {
            throw new IllegalArgumentException("cannot translate parameters for " + identifier, e);
        }
    }

    /**
     * Translates parameters to a Map.
     *
     * @param identifier identifier of the actor/operation being translated; used to build
     *        an exception message
     * @param source source parameters
     * @return the parameters, as a Map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> translateToMap(String identifier, Object source) {
        if (source == null) {
            return null;
        }

        return translate(identifier, source, LinkedHashMap.class);
    }
}