aboutsummaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/CarrierTechnologyParametersJSONAdapter.java
blob: 38abd3c6ca8545513002f3a3baece0b03d486d32 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. 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.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.service.parameters.carriertechnology;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorCarrierTechnologyParameters;
import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FILECarrierTechnologyParameters;
import org.onap.policy.apex.service.parameters.ApexParameterRuntimeException;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * This class deserialises various type of carrier technology parameters from JSON.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class CarrierTechnologyParametersJSONAdapter
        implements JsonSerializer<CarrierTechnologyParameters>, JsonDeserializer<CarrierTechnologyParameters> {
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJSONAdapter.class);

    private static final String PARAMETER_CLASS_NAME = "parameterClassName";

    private static final String CARRIER_TECHNOLOGY_TOKEN = "carrierTechnology";
    private static final String CARRIER_TECHNOLOGY_PARAMETERS = "parameters";

    // Built in technology parameters
    private static final Map<String, String> BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP = new HashMap<>();
    
    static {
        BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("FILE",
                FILECarrierTechnologyParameters.class.getCanonicalName());
        BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("EVENT_REQUESTOR",
                EventRequestorCarrierTechnologyParameters.class.getCanonicalName());
    }

    /*
     * (non-Javadoc)
     *
     * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
     * com.google.gson.JsonSerializationContext)
     */
    @Override
    public JsonElement serialize(final CarrierTechnologyParameters src, final Type typeOfSrc,
            final JsonSerializationContext context) {
        final String returnMessage = "serialization of Apex carrier technology parameters to Json is not supported";
        LOGGER.error(returnMessage);
        throw new ApexParameterRuntimeException(returnMessage);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
     * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
     */
    @Override
    public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {
        final JsonObject jsonObject = json.getAsJsonObject();

        // Get the carrier technology label primitive
        final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);

        // Check if we found our carrier technology
        if (labelJsonPrimitive == null) {
            LOGGER.warn("carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" not found in JSON file");
            return null;
        }

        // Get and check the carrier technology label
        final String carrierTechnologyLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
        if (carrierTechnologyLabel == null || carrierTechnologyLabel.length() == 0) {
            final String errorMessage = "carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" value \""
                    + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
            LOGGER.warn(errorMessage);
            throw new ApexParameterRuntimeException(errorMessage);
        }

        // We now get the technology carrier parameter class
        String carrierTechnologyParameterClassName = null;

        // Get the technology carrier parameter class for the carrier technology plugin class from
        // the configuration parameters
        final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);

        // If no technology carrier parameter class was specified, we try to use a built in carrier
        // technology
        if (classNameJsonPrimitive == null) {
            carrierTechnologyParameterClassName =
                    BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
        } else {
            // We use the specified one
            carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
        }

        // Check the carrier technology parameter class
        if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
            final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" parameter \""
                    + PARAMETER_CLASS_NAME + "\" value \""
                    + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
                    + "\" invalid in JSON file";
            LOGGER.warn(errorMessage);
            throw new ApexParameterRuntimeException(errorMessage);
        }

        // Get the class for the carrier technology
        Class<?> carrierTechnologyParameterClass = null;
        try {
            carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
        } catch (final ClassNotFoundException e) {
            final String errorMessage =
                    "carrier technology \"" + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
                            + "\" value \"" + carrierTechnologyParameterClassName + "\", could not find class";
            LOGGER.warn(errorMessage, e);
            throw new ApexParameterRuntimeException(errorMessage, e);
        }

        // Deserialise the class
        CarrierTechnologyParameters carrierTechnologyParameters =
                context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
        if (carrierTechnologyParameters == null) {
            // OK no parameters for the carrier technology have been specified, just instantiate the
            // default parameters
            try {
                carrierTechnologyParameters =
                        (CarrierTechnologyParameters) carrierTechnologyParameterClass.newInstance();
            } catch (final Exception e) {
                final String errorMessage = "could not create default parameters for carrier technology \""
                        + carrierTechnologyLabel + "\"\n" + e.getMessage();
                LOGGER.warn(errorMessage, e);
                throw new ApexParameterRuntimeException(errorMessage, e);
            }
        }

        // Check that the carrier technology label matches the label in the carrier technology
        // parameters object
        if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
            final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" does not match plugin \""
                    + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
                    + "\", specify correct carrier technology parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
                    + "\"";
            LOGGER.warn(errorMessage);
            throw new ApexParameterRuntimeException(errorMessage);
        }

        return carrierTechnologyParameters;
    }
}