94c7492f94d157fe1485645c03a420fa25da1297
[sdnc/apps.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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=====================================================
17  */
18 package org.onap.sdnc.apps.pomba.networkdiscovery.service.rs;
19
20 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.GENERAL_FAILURE;
21 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.MISSING_PARAM;
22 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.UNAUTHORIZED;
23
24 import java.util.List;
25 import java.util.UUID;
26 import javax.annotation.Resource;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.HttpHeaders;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.ResponseBuilder;
31 import javax.ws.rs.core.Response.Status;
32 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
33 import org.onap.logging.ref.slf4j.ONAPLogConstants;
34 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
35 import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
36 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryResponse;
37 import org.onap.sdnc.apps.pomba.networkdiscovery.service.SpringService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class RestServiceImpl implements RestService {
45
46     private static Logger log = LoggerFactory.getLogger(RestService.class);
47
48     @Autowired
49     private SpringService service;
50
51     @Resource(name="basicAuthHeader")
52     private String basicAuthHeader;
53
54     public RestServiceImpl() {
55     }
56
57     @Override
58     public Response findbyResourceIdAndType(HttpServletRequest request,
59                                             String version,
60                                             String authorization,
61                                             String fromAppId,
62                                             String transactionId,
63                                             String requestId,
64                                             String resourceType,
65                                             List<String> resourceIds,
66                                             String notificationURL) throws ApplicationException {
67
68         ONAPLogAdapter adapter = new ONAPLogAdapter(log);
69         adapter.getServiceDescriptor().setServiceName(SERVICE_NAME);
70         adapter.entering(request);
71         try {
72
73             if (version == null) {
74                 // only unit tests can pass null here
75                 // url matching will guarantee non-null in real server
76                 version = "v1";
77             }
78
79             if (!this.basicAuthHeader.equals(authorization)) {
80                 throw new ApplicationException(UNAUTHORIZED, Status.UNAUTHORIZED);
81             }
82             if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
83                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
84             }
85             if (requestId == null || requestId.isEmpty()) {
86                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "requestId");
87             }
88             if (notificationURL == null) {
89                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "notificationURL");
90             }
91             if (resourceType == null || resourceType.isEmpty()) {
92                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceType");
93             }
94             if (resourceIds == null || resourceIds.isEmpty()) {
95                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceIds");
96             }
97
98             if (transactionId == null || transactionId.isEmpty()) {
99                 transactionId = UUID.randomUUID().toString();
100                 log.debug("transactionId is missing; using newly generated value: " + transactionId);
101             }
102
103             // just reuse received Authorization header in callback
104             NetworkDiscoveryResponse response = this.service.findbyResourceIdAndType(transactionId,
105                     requestId, resourceType, resourceIds, notificationURL, authorization, adapter);
106             adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
107             return Response.ok(response).build();
108
109         } catch (ApplicationException x) {
110             adapter.getResponseDescriptor()
111                     .setResponseCode(x.getResponseCode())
112                     .setResponseStatus(ResponseStatus.ERROR);
113             log.error(x.getMessage(), x);
114             ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
115             if (authorization == null) {
116                 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"network-discovery\"");
117             }
118             return builder.build();
119
120         } catch (Exception x) {
121             adapter.getResponseDescriptor()
122                     .setResponseCode(GENERAL_FAILURE.getResponseCode())
123                     .setResponseStatus(ResponseStatus.ERROR);
124             log.error(GENERAL_FAILURE.getMessage(x), x);
125             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(x.getMessage()).build();
126         } finally {
127             adapter.exiting();
128         }
129
130     }
131
132 }