401f5c5b2b147a48cc6821f47d2dcc60bf044678
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / onap / so / adapters / sdnc / impl / MapRequestTunables.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.impl;
25
26 import org.onap.so.logger.ErrorCode;
27 import org.onap.so.logger.MessageEnum;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
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 MapRequestTunables {
36         
37         private static Logger logger = LoggerFactory.getLogger(MapRequestTunables.class);
38         public static final String GENERATED_KEY = "Generated key: ";
39         
40         @Autowired
41         private Environment env;
42
43         public RequestTunables setTunables(RequestTunables reqTunableOriginal)
44         { 
45                 RequestTunables reqTunable = new RequestTunables(reqTunableOriginal);
46                 String error = null;
47                 String key;
48                 if ("query".equals(reqTunable.getAction())) { //due to variable format for reqTunable.getOperation() eg services/layer3-service-list/8fe4ba4f-35cf-4d9b-a04a-fd3f5d4c5cc9
49                         key = Constants.REQUEST_TUNABLES + "." + reqTunable.getMsoAction() + ".." + reqTunable.getAction();
50                         logger.debug(GENERATED_KEY + key);
51                 }
52                 else if ("put".equals(reqTunable.getAction())  || "restdelete".equals(reqTunable.getAction())) { //due to variable format for reqTunable.getOperation() eg services/layer3-service-list/8fe4ba4f-35cf-4d9b-a04a-fd3f5d4c5cc9
53                         key = Constants.REQUEST_TUNABLES + "..." + reqTunable.getAction();
54                         logger.debug(GENERATED_KEY + key);
55                 } else {
56                         key = Constants.REQUEST_TUNABLES + "." + reqTunable.getMsoAction() + "." + reqTunable.getOperation() +"."  + reqTunable.getAction();
57                         logger.debug(GENERATED_KEY + key);
58                 }
59
60                 String value;   
61                 value = env.getProperty(key, "");       
62
63                 if (value != null && value.length() > 0) {
64
65                         String[] parts = value.split("\\|"); //escape pipe
66                         if (parts.length < 3) {
67                                 logger.warn("{} {} {} {} {} {}", MessageEnum.RA_SDNC_INVALID_CONFIG.toString(), key, value, "SDNC",
68                                         ErrorCode.DataError.getValue(), "Invalid config");
69                         }
70
71                         for (int i = 0; i < parts.length; i++) {
72                                 if (i == 0) {
73                                         reqTunable.setReqMethod(parts[i]) ;
74                                         logger.debug("Request Method is set to: {}", reqTunable.getReqMethod());
75                                 } else if (i == 1) {
76                                         reqTunable.setTimeout( parts[i]);
77                                         logger.debug("Timeout is set to: {}", reqTunable.getTimeout());
78                                 } else if (i == 2) {
79                                         reqTunable.setSdncUrl(env.getProperty(Constants.REQUEST_TUNABLES + "." + parts[i],""));
80                                         if (reqTunable.getOperation() != null && reqTunable.getSdncUrl() != null) {
81                                                 reqTunable.setSdncUrl(reqTunable.getSdncUrl()  + reqTunable.getOperation());
82                                         }
83                                         logger.debug("SDNC Url is set to: {}", reqTunable.getSdncUrl());
84                                 } else if  (i == 3) {
85                                         reqTunable.setHeaderName(parts[i]);
86                                         logger.debug("HeaderName is set to: {}", reqTunable.getHeaderName());
87                                 } else if  (i == 4) {
88                                         reqTunable.setNamespace(parts[i]);
89                                         logger.debug("NameSpace is set to: {}", reqTunable.getNamespace());
90                                 } else if  (i == 5) {
91                                         reqTunable.setAsyncInd(parts[i]);
92                                         logger.debug("AsyncInd is set to: {}", reqTunable.getAsyncInd());
93                                 }
94                         }
95
96                         if (reqTunable.getSdncUrl() == null || ("").equals(reqTunable.getSdncUrl())) {
97                                 error = "Invalid configuration, sdncUrl required for:" + key + " value:" + value;
98                         }
99                 } else {
100                         error = "Missing configuration for:" + key;
101                 }
102                 if (error != null) {
103                         logger.error("{} {} {} {} {}", MessageEnum.RA_SDNC_MISS_CONFIG_PARAM.toString(), key, "SDNC",
104                                 ErrorCode.DataError.getValue(), "Missing config param");
105                 }
106                 logger.debug("RequestTunables Key:{} Value:{} Tunables:{}", key, value, this.toString());
107                 return reqTunable;
108         }
109 }