2 * ============LICENSE_START===================================================
3 * Copyright (c) 2018 Amdocs
4 * ============================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=====================================================
19 package org.onap.sdnc.apps.pomba.networkdiscovery.service.rs;
21 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.GENERAL_FAILURE;
22 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.MISSING_PARAM;
23 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.UNAUTHORIZED;
25 import java.util.List;
26 import java.util.UUID;
27 import javax.annotation.Resource;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.HttpHeaders;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.ResponseBuilder;
32 import javax.ws.rs.core.Response.Status;
33 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
34 import org.onap.logging.ref.slf4j.ONAPLogConstants;
35 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
36 import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
37 import org.onap.sdnc.apps.pomba.networkdiscovery.JerseyConfiguration;
38 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryResponse;
39 import org.onap.sdnc.apps.pomba.networkdiscovery.service.SpringService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
46 public class RestServiceImpl implements RestService {
48 private static Logger log = LoggerFactory.getLogger(RestService.class);
51 private SpringService service;
53 @Resource(name="basicAuthHeader")
54 private String basicAuthHeader;
57 public Response findbyResourceIdAndType(HttpServletRequest request,
64 List<String> resourceIds) throws ApplicationException {
66 ONAPLogAdapter adapter = new ONAPLogAdapter(log);
69 adapter.getServiceDescriptor().setServiceName(JerseyConfiguration.SERVICE_NAME);
70 adapter.entering(request);
72 validateInput(version, authorization, fromAppId, requestId, resourceType, resourceIds);
74 if (transactionId == null || transactionId.isEmpty()) {
75 transactionId = UUID.randomUUID().toString();
76 log.debug("transactionId is missing; using newly generated value: {}", transactionId);
79 NetworkDiscoveryResponse response = this.service.findbyResourceIdAndType(transactionId,
80 requestId, resourceType, resourceIds, adapter);
81 adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
82 Response returnedResponse = Response.ok(response).build();
83 adapter.unwrap().info("request at url = {} resulted in http status {} and response: {}", request.getServletPath(),
84 returnedResponse.getStatus(), returnedResponse.getEntity());
85 return returnedResponse;
87 } catch (ApplicationException x) {
88 adapter.getResponseDescriptor()
89 .setResponseCode(x.getResponseCode())
90 .setResponseStatus(ResponseStatus.ERROR);
91 log.error(x.getMessage());
92 ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
93 if (authorization == null) {
94 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"network-discovery\"");
96 return builder.build();
98 } catch (Exception x) {
99 adapter.getResponseDescriptor()
100 .setResponseCode(GENERAL_FAILURE.getResponseCode())
101 .setResponseStatus(ResponseStatus.ERROR);
102 log.error(GENERAL_FAILURE.getMessage(x), x);
103 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(x.getMessage()).build();
109 private void validateInput(String version, String authorization, String fromAppId, String requestId,
110 String resourceType, List<String> resourceIds) throws ApplicationException {
111 if (version == null) {
112 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "version");
115 if (authorization == null || !this.basicAuthHeader.equals(authorization)) {
116 throw new ApplicationException(UNAUTHORIZED, Status.UNAUTHORIZED);
118 if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
119 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
121 if (requestId == null || requestId.isEmpty()) {
122 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "requestId");
125 if (resourceType == null || resourceType.isEmpty()) {
126 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceType");
128 if (resourceIds == null || resourceIds.isEmpty()) {
129 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceIds");