41abdee5f1bd14c78dfd46c68ecdf5dab0aaa88a
[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.portalsdk.core.controller.RestrictedBaseController;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
26 import org.onap.vid.asdc.AsdcCatalogException;
27 import org.onap.vid.asdc.beans.SecureServices;
28 import org.onap.vid.exceptions.VidServiceUnavailableException;
29 import org.onap.vid.model.ServiceModel;
30 import org.onap.vid.roles.Role;
31 import org.onap.vid.roles.RoleProvider;
32 import org.onap.vid.services.AaiService;
33 import org.onap.vid.services.VidService;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.bind.annotation.RestController;
39 import org.springframework.web.servlet.ModelAndView;
40
41 import javax.servlet.http.HttpServletRequest;
42 import java.util.List;
43
44 //import org.onap.vid.model.Service;
45
46 @RestController
47 public class VidController extends RestrictedBaseController {
48
49         private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidController.class);
50
51         private final VidService service;
52
53         @Autowired
54         public VidController(VidService vidService) throws SdcToscaParserException{
55
56                 service = vidService;
57         }
58
59         @Autowired
60         private AaiService aaiService;
61
62         @Autowired
63         RoleProvider roleProvider;
64
65 //      /**
66 //       * Gets the services.
67 //       *
68 //       * @param request the request
69 //       * @return the services
70 //       * @throws VidServiceUnavailableException the vid service unavailable exception
71 //       */
72 //      @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)
73 //      public SecureServices getServices(HttpServletRequest request) throws VidServiceUnavailableException {
74 //              try {
75 //                      AaiService aaiService = new AaiServiceImpl();
76 //                      LOG.info("Start API for browse ASDC was called");
77 //                      SecureServices secureServices = new SecureServices();
78 //                      Map<String, String[]> requestParams = request.getParameterMap();
79 //                      List<Role> roles = roleProvider.getUserRoles(request);
80 //                      secureServices.setServices(aaiService.getServicesByDistributionStatus());
81 //                      secureServices.setServices(service.getServices(requestParams));
82 //                      secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));
83 //                      return secureServices;
84 //              } catch (AsdcCatalogException e) {
85 //                      LOG.error("Failed to retrieve service definitions from SDC", e);
86 //                      throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
87 //              } catch (Throwable t) {
88 //                      LOG.debug("Unexpected error while retrieving service definitions from SDC: " + t.getMessage() + ":", t);
89 //                      t.printStackTrace();
90 //                      throw new VidServiceUnavailableException("Unexpected error while retrieving service definitions from SDC: " + t.getMessage(), t);
91 //              }
92 //      }
93
94         /**
95          * Gets the services.
96          *
97          * @param request the request
98          * @return the services
99          * @throws VidServiceUnavailableException the vid service unavailable exception
100          */
101         @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)
102         public SecureServices getServices(HttpServletRequest request) throws VidServiceUnavailableException {
103                 try {
104                         LOG.info("Start API for browse ASDC was called");
105                         SecureServices secureServices = new SecureServices();
106                         List<Role> roles = roleProvider.getUserRoles(request);
107                         secureServices.setServices(aaiService.getServicesByDistributionStatus());
108                         //Disable roles until AAF integration finishes
109                         //secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));
110                         secureServices.setReadOnly(false);
111                         return secureServices;
112                 }
113                 catch (Exception t) {
114                         LOG.debug("Unexpected error while retrieving service definitions from A&AI: " + t.getMessage() + ":", t);
115                         t.printStackTrace();
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", e);
135                         throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
136                 }
137         }
138
139
140         /**
141          * Gets the services view.
142          *
143          * @param request the request
144          * @return the services view
145          * @throws VidServiceUnavailableException the vid service unavailable exception
146          */
147         @RequestMapping(value={"/serviceModels"}, method=RequestMethod.GET)
148         public ModelAndView getServicesView(HttpServletRequest request) throws VidServiceUnavailableException {
149                 return new ModelAndView("serviceModels");
150         }
151 }