RoleValidatorByOwningEntity permits by PermissionPropertiesOwningEntity
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / AaiController2.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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 java.util.List;
24 import java.util.stream.Collectors;
25 import javax.servlet.http.HttpServletRequest;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.vid.aai.AaiClientInterface;
28 import org.onap.vid.aai.AaiGetVnfResponse;
29 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
30 import org.onap.vid.aai.model.ModelVer;
31 import org.onap.vid.aai.model.Permissions;
32 import org.onap.vid.model.aaiTree.Network;
33 import org.onap.vid.model.aaiTree.RelatedVnf;
34 import org.onap.vid.model.aaiTree.VpnBinding;
35 import org.onap.vid.properties.Features;
36 import org.onap.vid.roles.PermissionPropertiesSubscriberAndServiceType;
37 import org.onap.vid.roles.RoleProvider;
38 import org.onap.vid.services.AaiService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.ResponseStatus;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.togglz.core.manager.FeatureManager;
50
51 /**
52  * Controller to handle a&ai new requests.
53  */
54
55 @RestController
56 public class AaiController2 extends VidRestrictedBaseController {
57
58     private final AaiService aaiService;
59     private final RoleProvider roleProvider;
60     private final AaiClientInterface aaiClient;
61     private final FeatureManager featureManager;
62
63     @Autowired
64     public AaiController2(AaiService aaiService, RoleProvider roleProvider, AaiClientInterface aaiClient, FeatureManager featureManager) {
65         this.aaiService = aaiService;
66         this.roleProvider = roleProvider;
67         this.aaiClient = aaiClient;
68         this.featureManager = featureManager;
69     }
70
71     @RequestMapping(value = "/aai_get_homing_by_vfmodule/{vnfInstanceId}/{vfModuleId}", method = RequestMethod.GET)
72     public GetTenantsResponse getHomingDataByVfModule(@PathVariable("vnfInstanceId") String vnfInstanceId,
73                                                       @PathVariable("vfModuleId") String vfModuleId){
74         return aaiService.getHomingDataByVfModule(vnfInstanceId, vfModuleId);
75     }
76
77     @RequestMapping(value = "/aai_get_service_instance_topology/{subscriberId}/{serviceType}/{serviceInstanceId}",
78             method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
79     public String getServiceInstanceTree(@PathVariable("subscriberId") String globalCustomerId,
80                                          @PathVariable("serviceType") String serviceInstanceType,
81                                          @PathVariable("serviceInstanceId") String serviceInstanceId) {
82         return aaiService.getAAIServiceTree(globalCustomerId, serviceInstanceType, serviceInstanceId);
83     }
84
85     @RequestMapping(value = "/aai_reset_cache/{cacheName}", method = RequestMethod.DELETE)
86     @ResponseStatus(HttpStatus.ACCEPTED)
87     public void resetCache(@PathVariable("cacheName") String cacheName) {
88         aaiClient.resetCache(cacheName);
89     }
90
91     @RequestMapping(value = "/roles/service_permissions", method = RequestMethod.GET)
92     public Permissions servicePermissions(HttpServletRequest request,
93                                           @RequestParam(value = "subscriberId") String subscriberId,
94                                           @RequestParam(value = "serviceType") String serviceType) {
95
96         final boolean isEditPermitted = roleProvider
97                 .getUserRolesValidator(request)
98                 .isServicePermitted(new PermissionPropertiesSubscriberAndServiceType(subscriberId, serviceType));
99
100         return new Permissions(isEditPermitted);
101     }
102
103     @RequestMapping(value = "/aai_search_group_members",
104             method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
105     public List<RelatedVnf> searchGroupMembers(@RequestParam("subscriberId") String globalCustomerId,
106                                                @RequestParam("serviceType") String serviceType,
107                                                @RequestParam("serviceInvariantId") String invariantId,
108                                                @RequestParam("groupType") String groupType,
109                                                @RequestParam("groupRole") String groupRole) {
110         return aaiService.searchGroupMembers(globalCustomerId, serviceType, invariantId, groupType, groupRole);
111     }
112
113     @RequestMapping(value = "/aai_get_vpn_list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
114     public List<VpnBinding> getVpnList() {
115         return aaiService.getVpnListByVpnType("SERVICE-INFRASTRUCTURE");
116     }
117
118     @RequestMapping(value = "/aai_get_active_networks",
119         method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
120     public List<Network> getActiveNetworkList(
121         @RequestParam("cloudRegion") String cloudRegion,
122         @RequestParam("tenantId") String tenantId,
123         @RequestParam(value = "networkRole", required = false) String networkRole) {
124         return aaiService.getL3NetworksByCloudRegion(cloudRegion, tenantId, networkRole)
125             .stream()
126             .filter(Network::isBoundToVpn)
127             .filter(network -> StringUtils.isNotEmpty(network.getInstanceName()))
128             .filter(network -> StringUtils.equalsIgnoreCase(network.getOrchStatus(), "active"))
129             .collect(Collectors.toList());
130     }
131
132     @RequestMapping(value = "/aai_get_newest_model_version_by_invariant/{invariantId}",
133         method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
134     public ModelVer getNewestModelVersionByInvariant(@PathVariable("invariantId") String invariantId) {
135         return aaiService.getNewestModelVersionByInvariantId(invariantId);
136     }
137
138     @GetMapping(value = "/get_vnf_data_by_globalid_and_service_type/{globalCustomerId}/{serviceType}")
139     public AaiGetVnfResponse getVnfDataByGlobalIdAndServiceType(
140         @PathVariable("globalCustomerId") String globalCustomerId,
141         @PathVariable("serviceType") String serviceType,
142         @RequestParam(name="nfRole", required = false) String nfRole,
143         @RequestParam(name="cloudRegion", required = false) String cloudRegion) {
144         if (featureManager.isActive(Features.FLAG_FLASH_REDUCED_RESPONSE_CHANGEMG)){
145             return aaiClient.getVnfsByParamsForChangeManagement(globalCustomerId, serviceType, nfRole, cloudRegion).getT();
146         }
147         return aaiService.getVNFData(globalCustomerId, serviceType).getT();
148     }
149 }