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