Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / VidController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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.vid.controllers;
22
23 import org.onap.vid.asdc.AsdcCatalogException;
24 import org.onap.vid.asdc.beans.SecureServices;
25 import org.onap.vid.exceptions.VidServiceUnavailableException;
26 import org.onap.vid.model.PombaInstance.PombaRequest;
27 import org.onap.vid.model.ServiceModel;
28 import org.onap.vid.roles.Role;
29 import org.onap.vid.roles.RoleProvider;
30 import org.onap.vid.services.AaiService;
31 import org.onap.vid.services.PombaService;
32 import org.onap.vid.services.VidService;
33 import org.onap.portalsdk.core.controller.RestrictedBaseController;
34 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.web.bind.annotation.*;
38 import org.springframework.web.servlet.ModelAndView;
39
40 import javax.servlet.http.HttpServletRequest;
41 import java.util.List;
42
43 //import org.onap.vid.model.Service;
44
45 @RestController
46 public class VidController extends RestrictedBaseController {
47
48         private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidController.class);
49
50         private final VidService service;
51
52         @Autowired
53         public VidController(VidService vidService) {
54                 service = vidService;
55         }
56
57         @Autowired
58         private AaiService aaiService;
59
60         @Autowired
61         RoleProvider roleProvider;
62
63     @Autowired
64         private PombaService pombaService;
65
66 //      /**
67 //       * Gets the services.
68 //       *
69 //       * @param request the request
70 //       * @return the services
71 //       * @throws VidServiceUnavailableException the vid service unavailable exception
72 //       */
73 //      @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)
74 //      public SecureServices getServices(HttpServletRequest request) throws VidServiceUnavailableException {
75 //              try {
76 //                      AaiService aaiService = new AaiServiceImpl();
77 //                      LOG.info("Start API for browse ASDC was called");
78 //                      SecureServices secureServices = new SecureServices();
79 //                      Map<String, String[]> requestParams = request.getParameterMap();
80 //                      List<Role> roles = roleProvider.getUserRoles(request);
81 //                      secureServices.setServices(aaiService.getServicesByDistributionStatus());
82 //                      secureServices.setServices(service.getServices(requestParams));
83 //                      secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));
84 //                      return secureServices;
85 //              } catch (AsdcCatalogException e) {
86 //                      LOG.error("Failed to retrieve service definitions from SDC", e);
87 //                      throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
88 //              } catch (Throwable t) {
89 //                      LOG.debug("Unexpected error while retrieving service definitions from SDC: " + t.getMessage() + ":", t);
90 //                      t.printStackTrace();
91 //                      throw new VidServiceUnavailableException("Unexpected error while retrieving service definitions from SDC: " + t.getMessage(), t);
92 //              }
93 //      }
94
95         /**
96          * Gets the services.
97          *
98          * @param request the request
99          * @return the services
100          * @throws VidServiceUnavailableException the vid service unavailable exception
101          */
102         @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)
103         public SecureServices getServices(HttpServletRequest request) throws VidServiceUnavailableException {
104                 try {
105                         LOG.info("Start API for browse ASDC was called");
106                         SecureServices secureServices = new SecureServices();
107                         List<Role> roles = roleProvider.getUserRoles(request);
108                         secureServices.setServices(aaiService.getServicesByDistributionStatus());
109                         //Disable roles until AAF integration finishes
110                         //secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));
111                         secureServices.setReadOnly(false);
112                         return secureServices;
113                 }
114                 catch (Exception t) {
115                         LOG.debug("Unexpected error while retrieving service definitions from A&AI: " + t.getMessage() + ":", t);
116                         throw new VidServiceUnavailableException("Unexpected error while retrieving service definitions from A&AI: " + t.getMessage(), t);
117                 }
118         }
119
120
121
122         /**
123          * Gets the services.
124          *
125          * @param uuid the uuid
126          * @return the services
127          * @throws VidServiceUnavailableException the vid service unavailable exception
128          */
129         @RequestMapping(value={"/rest/models/services/{uuid}"}, method = RequestMethod.GET)
130         public ServiceModel getServices(@PathVariable("uuid") String uuid, HttpServletRequest request) throws VidServiceUnavailableException {
131                 try {
132                         return service.getService(uuid);
133                 } catch (AsdcCatalogException e) {
134                         LOG.error("Failed to retrieve service definitions from SDC. Error: "+e.getMessage() , e);
135                         throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
136                 }
137         }
138
139         @RequestMapping(value = "/rest/models/reset", method = RequestMethod.POST)
140         @ResponseStatus(HttpStatus.ACCEPTED)
141         public void invalidateServiceModelCache(HttpServletRequest request) {
142                 service.invalidateServiceCache();
143         }
144
145         /**
146          * Gets the services view.
147          *
148          * @param request the request
149          * @return the services view
150          * @throws VidServiceUnavailableException the vid service unavailable exception
151          */
152         @RequestMapping(value={"/serviceModels"}, method=RequestMethod.GET)
153     public ModelAndView getServicesView(HttpServletRequest request) {
154         return new ModelAndView("serviceModels");
155     }
156
157     @RequestMapping(value = {"/rest/models/services/verifyService"}, method = RequestMethod.POST)
158         public void verifyServiceInstance(HttpServletRequest request, @RequestBody PombaRequest pombaRequest) {
159                 pombaService.verify(pombaRequest);
160     }
161 }