replaced String.repeat with static final strings
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / onap / so / adapters / sdnc / sdncrest / MapTypedRequestTunablesData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.sdnc.sdncrest;
25
26 import org.onap.so.logger.LoggingAnchor;
27 import org.onap.so.adapters.sdnc.exception.SDNCAdapterException;
28 import org.onap.so.adapters.sdnc.impl.Constants;
29 import org.onap.so.logger.ErrorCode;
30 import org.onap.so.logger.MessageEnum;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.core.env.Environment;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class MapTypedRequestTunablesData {
39
40     private static Logger logger = LoggerFactory.getLogger(MapTypedRequestTunablesData.class);
41
42     private static final String MISSING_CONFIGURATION_ERROR_MSG = "Missing configuration for: ";
43     private static final String MISSING_CONFIG_PARAM_ERROR_MSG = "Missing config param";
44     private static final String MSO_INTERNAL_ERROR = "MsoInternalError";
45
46     @Autowired
47     private Environment env;
48
49
50     public TypedRequestTunables setTunables(TypedRequestTunables reqTunableOriginal) throws SDNCAdapterException {
51         TypedRequestTunables reqTunable = new TypedRequestTunables(reqTunableOriginal);
52
53         String error;
54         String value = env.getProperty(reqTunable.getKey().toLowerCase(), "");
55
56         if ("".equals(value)) {
57             error = MISSING_CONFIGURATION_ERROR_MSG + reqTunable.getKey();
58             logger.error(LoggingAnchor.FIVE, MessageEnum.RA_SDNC_MISS_CONFIG_PARAM.toString(), reqTunable.getKey(),
59                     "SDNC", ErrorCode.DataError.getValue(), MISSING_CONFIG_PARAM_ERROR_MSG);
60
61             throw new SDNCAdapterException(error);
62         }
63
64         String[] parts = value.split("\\|");
65
66         if (parts.length != 5) {
67             error = "Invalid configuration for: " + reqTunable.getKey();
68             logger.error(LoggingAnchor.SIX, MessageEnum.RA_SDNC_INVALID_CONFIG.toString(), reqTunable.getKey(), value,
69                     "SDNC", ErrorCode.DataError.getValue(), "Invalid config");
70             throw new SDNCAdapterException(error);
71         }
72
73         reqTunable.setReqMethod(parts[0]);
74         logger.trace("Request Method is set to: {}", reqTunable.getReqMethod());
75
76         reqTunable.setTimeout(parts[1]);
77         logger.trace("Timeout is set to: {}", reqTunable.getTimeout());
78
79         String urlPropKey = Constants.REQUEST_TUNABLES + "." + parts[2];
80         reqTunable.setSdncUrl(env.getProperty(urlPropKey, ""));
81
82         if ("".equals(reqTunable.getSdncUrl())) {
83             error = MISSING_CONFIGURATION_ERROR_MSG + urlPropKey;
84             logger.error(LoggingAnchor.FIVE, MessageEnum.RA_SDNC_MISS_CONFIG_PARAM.toString(), urlPropKey, "SDNC",
85                     ErrorCode.DataError.getValue(), MISSING_CONFIG_PARAM_ERROR_MSG);
86
87             throw new SDNCAdapterException(error);
88         }
89
90         logger.trace("SDNC Url is set to: {}", reqTunable.getSdncUrl());
91
92         reqTunable.setHeaderName(parts[3]);
93         logger.trace("Header Name is set to: {}", reqTunable.getHeaderName());
94
95         reqTunable.setNamespace(parts[4]);
96         logger.trace("Namespace is set to: {}", reqTunable.getNamespace());
97
98         reqTunable.setMyUrl(env.getProperty(Constants.MY_URL_PROP, ""));
99
100         if ("".equals(reqTunable.getMyUrl())) {
101             error = MISSING_CONFIGURATION_ERROR_MSG + Constants.MY_URL_PROP;
102             logger.error(LoggingAnchor.FIVE, MessageEnum.RA_SDNC_MISS_CONFIG_PARAM.toString(), Constants.MY_URL_PROP,
103                     "SDNC", ErrorCode.DataError.getValue(), MISSING_CONFIG_PARAM_ERROR_MSG);
104
105             throw new SDNCAdapterException(error);
106         }
107
108         while (reqTunable.getMyUrl().endsWith("/")) {
109             reqTunable.setMyUrl(reqTunable.getMyUrl().substring(0, reqTunable.getMyUrl().length() - 1));
110         }
111
112         reqTunable.setMyUrl(reqTunable.getMyUrl().concat(reqTunable.getMyUrlSuffix()));
113
114         logger.debug(reqTunable.toString());
115         return reqTunable;
116     }
117
118 }