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