Containerization feature of SO
[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  * 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.so.adapters.sdnc.sdncrest;
22
23 import org.onap.so.adapters.sdnc.exception.SDNCAdapterException;
24 import org.onap.so.adapters.sdnc.impl.Constants;
25 import org.onap.so.logger.MessageEnum;
26 import org.onap.so.logger.MsoAlarmLogger;
27 import org.onap.so.logger.MsoLogger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.core.env.Environment;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class MapTypedRequestTunablesData {
34         
35         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA,MapTypedRequestTunablesData.class);
36         
37         private static final MsoAlarmLogger alarmLogger = new MsoAlarmLogger(); 
38                 
39         @Autowired
40         private Environment env;
41
42
43         public TypedRequestTunables setTunables(TypedRequestTunables reqTunableOriginal) throws SDNCAdapterException {  
44                 TypedRequestTunables reqTunable = new TypedRequestTunables(reqTunableOriginal);         
45                 
46                 String error;
47                 String value = env.getProperty(reqTunable.getKey().toLowerCase(), "");
48
49                 if ("".equals(value)) {
50                         error= "Missing configuration for: " + reqTunable.getKey();
51                         msoLogger.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, reqTunable.getKey(), "SDNC", "", MsoLogger.ErrorCode.DataError, "Missing config param");
52                         alarmLogger.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, reqTunable.getError());              
53                         throw new SDNCAdapterException(error);
54                 }
55
56                 String[] parts = value.split("\\|");
57
58                 if (parts.length != 5) {
59                         error="Invalid configuration for: " + reqTunable.getKey();
60                         msoLogger.error(MessageEnum.RA_SDNC_INVALID_CONFIG, reqTunable.getKey(), value, "SDNC", "", MsoLogger.ErrorCode.DataError, "Invalid config");
61                         alarmLogger.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, reqTunable.getError());      
62                         throw new SDNCAdapterException(error);
63                 }
64
65                 reqTunable.setReqMethod(parts[0]);
66                 msoLogger.trace("Request Method is set to: " + reqTunable.getReqMethod());
67
68                 reqTunable.setTimeout(parts[1]);
69                 msoLogger.trace("Timeout is set to: " + reqTunable.getTimeout());
70
71                 String urlPropKey = Constants.REQUEST_TUNABLES + "." + parts[2];
72                 reqTunable.setSdncUrl(env.getProperty(urlPropKey, ""));
73
74                 if ("".equals(reqTunable.getSdncUrl())) {
75                         error="Missing configuration for: " + urlPropKey;
76                         msoLogger.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, urlPropKey, "SDNC", "", MsoLogger.ErrorCode.DataError, "Missing config param");
77                         alarmLogger.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, reqTunable.getError());
78                         throw new SDNCAdapterException(error);
79                 }
80
81                 msoLogger.trace("SDNC Url is set to: " + reqTunable.getSdncUrl());
82
83                 reqTunable.setHeaderName(parts[3]);
84                 msoLogger.trace("Header Name is set to: " + reqTunable.getHeaderName());
85
86                 reqTunable.setNamespace(parts[4]);
87                 msoLogger.trace("Namespace is set to: " + reqTunable.getNamespace());
88
89                 reqTunable.setMyUrl(env.getProperty(Constants.MY_URL_PROP, ""));
90
91                 if ("".equals(reqTunable.getMyUrl())) {
92                         error="Missing configuration for: " + Constants.MY_URL_PROP;
93                         msoLogger.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, Constants.MY_URL_PROP, "SDNC", "",
94                                 MsoLogger.ErrorCode.DataError, "Missing config param");
95                         alarmLogger.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, reqTunable.getError());              
96                         throw new SDNCAdapterException(error);
97                 }
98
99                 while (reqTunable.getMyUrl().endsWith("/")) {
100                         reqTunable.setMyUrl(reqTunable.getMyUrl().substring(0, reqTunable.getMyUrl().length()-1));
101                 }
102
103                 reqTunable.setMyUrl(reqTunable.getMyUrl().concat(reqTunable.getMyUrlSuffix()));
104
105                 msoLogger.debug(reqTunable.toString()); 
106                 return reqTunable;
107         }
108
109 }