Change the header to SO
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / sdncrest / TypedRequestTunables.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 package org.openecomp.mso.adapters.sdnc.sdncrest;
21
22 import org.openecomp.mso.adapters.sdnc.impl.Constants;
23 import org.openecomp.mso.logger.MessageEnum;
24 import org.openecomp.mso.logger.MsoAlarmLogger;
25 import org.openecomp.mso.logger.MsoLogger;
26 import org.openecomp.mso.properties.MsoJavaProperties;
27 import org.openecomp.mso.properties.MsoPropertiesException;
28 import org.openecomp.mso.properties.MsoPropertiesFactory;
29
30 /**
31  * Typed Request Tunables.  Each entry is identified by a TYPE in the property name.
32  * Different types can have different keys.
33  * <p>
34  * General format:
35  * <pre>
36  * org.openecomp.mso.adapters.sdnc.TYPE.KEY1[.KEY2...]=METHOD|TIMEOUT|URL|HEADER|NAMESPACE
37  * </pre>
38  * Currently supported type(s): service
39  * <pre>
40  * org.openecomp.mso.adapters.sdnc.service.SERVICE.OPERATION=METHOD|TIMEOUT|URL|HEADER|NAMESPACE
41  * </pre>
42  */
43 public class TypedRequestTunables {
44
45         private static final String MSO_PROPERTIES_ID = "MSO_PROP_SDNC_ADAPTER";
46
47         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
48         private static final MsoAlarmLogger ALARMLOGGER = new MsoAlarmLogger();
49
50         private final MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
51
52         private final String reqId;
53         private final String myUrlSuffix;
54         private String key = null;
55         private String error = null;
56
57         // tunables (all are required)
58         private String reqMethod = null;
59         private String timeout = null;
60         private String sdncUrl = null;
61         private String headerName = null;
62         private String namespace = null;
63         private String myUrl = null;
64
65         public TypedRequestTunables(String reqId, String myUrlSuffix) {
66                 this.reqId = reqId;
67                 this.myUrlSuffix = myUrlSuffix;
68         }
69
70         /**
71          * Sets the key for a service request:
72          * <pre>
73          * org.openecomp.mso.adapters.sdnc.service.SERVICE.OPERATION
74          * </pre>
75          * @param service the sdncService
76          * @param operation the sdncOperation
77          */
78         public void setServiceKey(String service, String operation) {
79                 key = Constants.REQUEST_TUNABLES + ".service." + service + "." + operation;
80                 LOGGER.debug("Generated " + getClass().getSimpleName() + " key: " + key);
81         }
82
83         /**
84          * Gets the SDNC request ID.
85          */
86         public String getReqId() {
87                 return reqId;
88         }
89
90         /**
91          * Gets the generated key.
92          */
93         public String getKey() {
94                 return key;
95         }
96
97         /**
98          * Gets the most recent error, or null if there was no error.
99          */
100         public String getError() {
101                 return error;
102         }
103
104         public String getReqMethod() {
105                 return reqMethod;
106         }
107
108         public String getTimeout() {
109                 return timeout;
110         }
111
112         public String getSdncUrl() {
113                 return sdncUrl;
114         }
115
116         public String getHeaderName() {
117                 return headerName;
118         }
119
120         public String getNamespace() {
121                 return namespace;
122         }
123
124         /**
125          * Gets the SDNC adapter notification URL, trimmed of trailing '/' characters.
126          */
127         public String getMyUrl() {
128                 return myUrl;
129         }
130
131         /**
132          * Returns true if successful.  If there is an error, it is logged and alarmed.
133          * The error description may be retrieved by calling getError().
134          */
135         public boolean setTunables() {
136                 error = null;
137                 MsoJavaProperties properties;
138
139                 try {
140                         properties = msoPropertiesFactory.getMsoJavaProperties(MSO_PROPERTIES_ID);
141                 } catch (MsoPropertiesException e) {
142                         error = "Mso Properties ID not found in cache: " + MSO_PROPERTIES_ID;
143                         LOGGER.error(MessageEnum.LOAD_PROPERTIES_FAIL, "Unknown. " +  error, "SDNC", "",
144                                 MsoLogger.ErrorCode.DataError, "Exception - Mso Properties ID not found in cache", e);
145                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
146                         return false;
147                 }
148
149                 String value = properties.getProperty(key, "");
150
151                 if (value.equals("")) {
152                         error = "Missing configuration for: " + key;
153                         LOGGER.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, key, "SDNC", "", MsoLogger.ErrorCode.DataError, "Missing config param");
154                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
155                         return false;
156                 }
157
158                 String[] parts = value.split("\\|");
159
160                 if (parts.length != 5) {
161                         error = "Invalid configuration for: " + key;
162                         LOGGER.error(MessageEnum.RA_SDNC_INVALID_CONFIG, key, value, "SDNC", "", MsoLogger.ErrorCode.DataError, "Invalid config");
163                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
164                         return false;
165                 }
166
167                 reqMethod = parts[0];
168                 LOGGER.debug("Request Method is set to: " + reqMethod);
169
170                 timeout = parts[1];
171                 LOGGER.debug("Timeout is set to: " + timeout);
172
173                 String urlPropKey = Constants.REQUEST_TUNABLES + "." + parts[2];
174                 sdncUrl = properties.getProperty(urlPropKey, "");
175
176                 if (sdncUrl.equals("")) {
177                         error = "Missing configuration for: " + urlPropKey;
178                         LOGGER.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, urlPropKey, "SDNC", "", MsoLogger.ErrorCode.DataError, "Missing config param");
179                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
180                         return false;
181                 }
182
183                 LOGGER.debug("SDNC Url is set to: " + sdncUrl);
184
185                 headerName = parts[3];
186                 LOGGER.debug("Header Name is set to: " + headerName);
187
188                 namespace = parts[4];
189                 LOGGER.debug("Namespace is set to: " + namespace);
190
191                 myUrl = properties.getProperty(Constants.MY_URL_PROP, "");
192
193                 if (myUrl.equals("")) {
194                         error = "Missing configuration for: " + Constants.MY_URL_PROP;
195                         LOGGER.error(MessageEnum.RA_SDNC_MISS_CONFIG_PARAM, Constants.MY_URL_PROP, "SDNC", "",
196                                 MsoLogger.ErrorCode.DataError, "Missing config param");
197                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
198                         return false;
199                 }
200
201                 while (myUrl.endsWith("/")) {
202                         myUrl = myUrl.substring(0, myUrl.length()-1);
203                 }
204
205                 myUrl += myUrlSuffix;
206
207                 LOGGER.debug(toString());
208                 return true;
209         }
210
211         @Override
212         public String toString() {
213                 return getClass().getSimpleName() + "["
214                         + "reqId=" + reqId
215                         + (key == null ? "" : ", key=" + key)
216                         + (reqMethod == null ? "" : ", reqMethod=" + reqMethod)
217                         + (sdncUrl == null ? "" : ", sdncUrl=" + sdncUrl)
218                         + (timeout == null ? "" : ", timeout=" + timeout)
219                         + (headerName == null ? "" : ", headerName=" + headerName)
220                         + (namespace == null ? "" : ", namespace=" + namespace)
221                         + (myUrl == null ? "" : ", myUrl=" + myUrl)
222                         + "]";
223         }
224 }