org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / 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.controller;
22
23 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
24 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.openecomp.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                         secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));
109                         return secureServices;
110                 }
111                 catch (Exception t) {
112                         LOG.debug("Unexpected error while retrieving service definitions from A&AI: " + t.getMessage() + ":", t);
113                         t.printStackTrace();
114                         throw new VidServiceUnavailableException("Unexpected error while retrieving service definitions from A&AI: " + t.getMessage(), t);
115                 }
116         }
117
118
119
120         /**
121          * Gets the services.
122          *
123          * @param uuid the uuid
124          * @return the services
125          * @throws VidServiceUnavailableException the vid service unavailable exception
126          */
127         @RequestMapping(value={"/rest/models/services/{uuid}"}, method = RequestMethod.GET)
128         public ServiceModel getServices(@PathVariable("uuid") String uuid, HttpServletRequest request) throws VidServiceUnavailableException {
129                 try {
130                         return service.getService(uuid);
131                 } catch (AsdcCatalogException e) {
132                         LOG.error("Failed to retrieve service definitions from SDC", e);
133                         throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
134                 }
135         }
136
137
138         /**
139          * Gets the services view.
140          *
141          * @param request the request
142          * @return the services view
143          * @throws VidServiceUnavailableException the vid service unavailable exception
144          */
145         @RequestMapping(value={"/serviceModels"}, method=RequestMethod.GET)
146         public ModelAndView getServicesView(HttpServletRequest request) throws VidServiceUnavailableException {
147                 return new ModelAndView("serviceModels");
148         }
149 }