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