VfModule and VolumeGroup RequestParameters: introduce objects hierarchy
[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 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright 2018 - 2019 Nokia
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.controller;
23
24 import org.onap.portalsdk.core.controller.RestrictedBaseController;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
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.PombaInstance.PombaRequest;
30 import org.onap.vid.model.ServiceModel;
31 import org.onap.vid.roles.Role;
32 import org.onap.vid.roles.RoleProvider;
33 import org.onap.vid.services.AaiService;
34 import org.onap.vid.services.PombaService;
35 import org.onap.vid.services.VidService;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.web.bind.annotation.*;
39 import org.springframework.web.servlet.ModelAndView;
40
41 import javax.servlet.http.HttpServletRequest;
42 import java.util.List;
43
44 @RestController
45 public class VidController extends RestrictedBaseController {
46
47     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidController.class);
48
49     private final VidService vidService;
50     private final AaiService aaiService;
51     private final RoleProvider roleProvider;
52     private final PombaService pombaService;
53
54     @Autowired
55     public VidController(VidService vidService, AaiService aaiService, RoleProvider roleProvider,
56         PombaService pombaService) {
57         this.vidService = vidService;
58         this.aaiService = aaiService;
59         this.roleProvider = roleProvider;
60         this.pombaService = pombaService;
61     }
62
63     /**
64      * @param request the request
65      * @return the services
66      */
67     @RequestMapping(value = {"/rest/models/services"}, method = RequestMethod.GET)
68     public SecureServices getServices(HttpServletRequest request) {
69         LOG.info("Start API for browse SDC was called");
70         SecureServices secureServices = new SecureServices();
71         List<Role> roles = roleProvider.getUserRoles(request);
72         secureServices.setServices(aaiService.getServicesByDistributionStatus());
73                 secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));
74         return secureServices;
75     }
76
77
78     /**
79      * @param uuid the uuid
80      * @return the services
81      * @throws VidServiceUnavailableException the vid service unavailable exception
82      */
83     @RequestMapping(value = {"/rest/models/services/{uuid}"}, method = RequestMethod.GET)
84     public ServiceModel getService(@PathVariable("uuid") String uuid) throws VidServiceUnavailableException {
85         try {
86             return vidService.getService(uuid);
87         } catch (AsdcCatalogException e) {
88             LOG.error("Failed to retrieve service definitions from SDC. Error: " + e.getMessage(), e);
89             throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
90         }
91     }
92
93     @RequestMapping(value = "/rest/models/reset", method = RequestMethod.POST)
94     @ResponseStatus(HttpStatus.ACCEPTED)
95     public void invalidateServiceModelCache() {
96         vidService.invalidateServiceCache();
97     }
98
99     /**
100      * @return the services view
101      * @throws VidServiceUnavailableException the vid service unavailable exception
102      */
103     // FIX ME: Circular view path [serviceModels]: would dispatch back to the current handler URL [/serviceModels] again.
104     @RequestMapping(value = {"/serviceModels"}, method = RequestMethod.GET)
105     public ModelAndView getServicesView() {
106         return new ModelAndView("serviceModels");
107     }
108
109     @RequestMapping(value = {"/rest/models/services/verifyService"}, method = RequestMethod.POST)
110     public void verifyServiceInstance(@RequestBody PombaRequest pombaRequest) {
111         pombaService.verify(pombaRequest);
112     }
113 }