Change openecomp to onap and update license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / RESTAPI.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.restcore;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.ws.rs.core.HttpHeaders;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.UriInfo;
33
34 import org.onap.aai.db.props.AAIProperties;
35 import org.onap.aai.dbmap.DBConnectionType;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.tools.CreateUUID;
40 import org.onap.aai.introspection.tools.DefaultFields;
41 import org.onap.aai.introspection.tools.InjectKeysFromURI;
42 import org.onap.aai.introspection.tools.IntrospectorValidator;
43 import org.onap.aai.introspection.tools.Issue;
44 import org.onap.aai.introspection.tools.RemoveNonVisibleProperty;
45 import org.onap.aai.logging.ErrorLogHelper;
46 import org.onap.aai.logging.LoggingContext;
47 import org.onap.aai.util.AAIConfig;
48 import org.onap.aai.util.FormatDate;
49
50 import com.att.eelf.configuration.EELFLogger;
51 import com.att.eelf.configuration.EELFManager;
52 import com.google.common.base.Joiner;
53
54
55 /**
56  * Base class for AAI REST API classes.
57  * Provides method to validate header information
58  * TODO should authenticate caller and authorize them for the API they are calling
59  * TODO should store the transaction
60  
61  *
62  */
63 public class RESTAPI {
64         
65         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(RESTAPI.class);
66
67         protected final String COMPONENT = "aairest";
68
69         /**
70          * The Enum Action.
71          */
72         public enum Action {
73                 GET, PUT, POST, DELETE
74         };
75
76         /**
77          * Gets the from app id.
78          *
79          * @param headers the headers
80          * @param logline the logline
81          * @return the from app id
82          * @throws AAIException the AAI exception
83          */
84         protected String getFromAppId(HttpHeaders headers) throws AAIException { 
85                 String fromAppId = null;
86                 if (headers != null) {
87                         List<String> fromAppIdHeader = headers.getRequestHeader("X-FromAppId");
88                         if (fromAppIdHeader != null) {
89                                 for (String fromAppIdValue : fromAppIdHeader) {
90                                         fromAppId = fromAppIdValue;
91                                 }
92                         } 
93                 }
94
95                 if (fromAppId == null) {
96                         throw new AAIException("AAI_4009");
97                 }
98
99                 LoggingContext.partnerName(fromAppId);
100
101                 return fromAppId;
102         }
103         
104         /**
105          * Gets the trans id.
106          *
107          * @param headers the headers
108          * @param logline the logline
109          * @return the trans id
110          * @throws AAIException the AAI exception
111          */
112         protected String getTransId(HttpHeaders headers) throws AAIException { 
113                 String transId = null;
114                 if (headers != null) {
115                         List<String> transIdHeader = headers.getRequestHeader("X-TransactionId");
116                         if (transIdHeader != null) {
117                                 for (String transIdValue : transIdHeader) {
118                                         transId = transIdValue;
119                                 }
120                         }
121                 }
122
123                 if (transId == null) {
124                         throw new AAIException("AAI_4010");
125                 }
126
127                 LoggingContext.requestId(transId);
128
129                 return transId;
130         }
131         
132         
133         /**
134          * Gen date.
135          *
136          * @return the string
137          */
138         protected String genDate() {
139                 FormatDate fd = new FormatDate( "YYMMdd-HH:mm:ss:SSS");
140                 
141                 return fd.getDateTime();
142         }
143
144         /**
145          * Gets the media type.
146          *
147          * @param mediaTypeList the media type list
148          * @return the media type
149          */
150         protected String getMediaType(List <MediaType> mediaTypeList) {
151                 String mediaType = MediaType.APPLICATION_JSON;  // json is the default    
152                 for (MediaType mt : mediaTypeList) {
153                         if (MediaType.APPLICATION_XML_TYPE.isCompatible(mt)) {
154                                 mediaType = MediaType.APPLICATION_XML;
155                         } 
156                 }
157                 return mediaType;
158         }
159         
160
161         /* ----------helpers for common consumer actions ----------- */
162         
163         /**
164          * Sets the depth.
165          *
166          * @param depthParam the depth param
167          * @return the int
168          * @throws AAIException the AAI exception
169          */
170         protected int setDepth(String depthParam) throws AAIException {
171                 int depth = AAIProperties.MAXIMUM_DEPTH; //default 
172                 if (depthParam != null && depthParam.length() > 0 && !depthParam.equals("all")){
173                         try {
174                                 depth = Integer.valueOf(depthParam);
175                         } catch (Exception e) {
176                                 throw new AAIException("AAI_4016");
177                         }
178                 }
179                 return depth;
180         }
181
182         /**
183          * Consumer exception response generator.
184          *
185          * @param headers the headers
186          * @param info the info
187          * @param templateAction the template action
188          * @param e the e
189          * @return the response
190          */
191         protected Response consumerExceptionResponseGenerator(HttpHeaders headers, UriInfo info, HttpMethod templateAction, AAIException e) {
192                 ArrayList<String> templateVars = new ArrayList<String>();
193                 templateVars.add(templateAction.toString()); //GET, PUT, etc
194                 templateVars.add(info.getPath().toString());
195                 templateVars.addAll(e.getTemplateVars());
196
197                 return Response
198                                 .status(e.getErrorObject().getHTTPResponseCode())
199                                 .entity(ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers.getAcceptableMediaTypes(), e, templateVars))
200                                 .build();
201         }
202         
203         /**
204          * Validate introspector.
205          *
206          * @param obj the obj
207          * @param loader the loader
208          * @param uri the uri
209          * @param validateRequired the validate required
210          * @throws AAIException the AAI exception
211          * @throws UnsupportedEncodingException the unsupported encoding exception
212          */
213         protected void validateIntrospector(Introspector obj, Loader loader, URI uri, HttpMethod method) throws AAIException, UnsupportedEncodingException {
214                 
215                 int maximumDepth = AAIProperties.MAXIMUM_DEPTH;
216                 boolean validateRequired = true;
217                 if (method.equals(HttpMethod.MERGE_PATCH)) {
218                         validateRequired = false;
219                         maximumDepth = 0;
220                 }
221                 IntrospectorValidator validator = new IntrospectorValidator.Builder()
222                                 .validateRequired(validateRequired)
223                                 .restrictDepth(maximumDepth)
224                                 .addResolver(new RemoveNonVisibleProperty())
225                                 .addResolver(new CreateUUID())
226                                 .addResolver(new DefaultFields())
227                                 .addResolver(new InjectKeysFromURI(loader, uri))
228                                 .build();
229                 boolean result = validator.validate(obj);
230                 if (!result) {
231                         result = validator.resolveIssues();
232                 }
233                 if (!result) {
234                         List<String> messages = new ArrayList<>();
235                         for (Issue issue : validator.getIssues()) {
236                                 if (!issue.isResolved()) {
237                                         messages.add(issue.getDetail());
238                                 }
239                         }
240                         String errors = Joiner.on(",").join(messages);
241                         throw new AAIException("AAI_3000", errors);
242                 }
243                 //check that key in payload and key in request uri are the same
244         String objURI = obj.getURI();
245         //if requested object is a parent objURI will have a leading slash the input uri will lack
246         //this adds that leading slash for the comparison
247         String testURI = "/" + uri.getRawPath();
248         if (!testURI.endsWith(objURI)) {
249                 throw new AAIException("AAI_3000", "uri and payload keys don't match");
250         }
251         }
252         
253         protected DBConnectionType determineConnectionType(String fromAppId, String realTime) {
254                 DBConnectionType type = DBConnectionType.REALTIME;
255                 boolean isRealTimeClient = AAIConfig.get("aai.realtime.clients", "").contains(fromAppId);
256                 if (isRealTimeClient || realTime != null) {
257                         type = DBConnectionType.REALTIME;
258                 } else {
259                         type = DBConnectionType.CACHED;
260                 }
261                 
262                 return type;
263         }
264         
265         /**
266          * Gets the input media type.
267          *
268          * @param mediaType the media type
269          * @return the input media type
270          */
271         protected String getInputMediaType(MediaType mediaType) {
272                 String result = mediaType.getType() + "/" + mediaType.getSubtype();
273                 
274                 return result;
275                 
276         }
277
278 }