a16c3fb5cc27616280ed5416fb7bd98c5f3653bd
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.rest;
22
23 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
24 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmHelper;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.*;
27 import org.onap.vnfmadapter.v1.model.Tenant;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.*;
35 import javax.ws.rs.core.MediaType;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.UUID;
39 import static org.onap.so.adapters.vnfmadapter.Constants.BASE_URL;
40
41 @Controller
42 @RequestMapping(value = BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
43 public class Sol003GrantController {
44
45     private static final String SEPARATOR = "_";
46     private static final String VIM_TYPE = "OPENSTACK";
47     private static final String CLOUD_OWNER = "myTestCloudOwner";
48     private static final String REGION = "myTestRegion";
49     private static final String TENANT_ID = "myTestTenantId";
50     private static final Logger logger = LoggerFactory.getLogger(Sol003GrantController.class);
51     public final AaiServiceProvider aaiServiceProvider;
52     public final AaiHelper aaiHelper;
53     public final VnfmHelper vnfmHelper;
54
55     @Autowired
56     public Sol003GrantController(final AaiServiceProvider aaiServiceProvider, final AaiHelper aaiHelper,
57             final VnfmHelper vnfmHelper) {
58         this.aaiServiceProvider = aaiServiceProvider;
59         this.aaiHelper = aaiHelper;
60         this.vnfmHelper = vnfmHelper;
61     }
62
63     @GetMapping(value = "/grants/{grantId}")
64     public ResponseEntity<InlineResponse201> grantsGrantIdGet(@PathVariable("grantId") final String grantId) {
65         logger.info("Get grant received from VNFM, grant id: " + grantId);
66         return new ResponseEntity<InlineResponse201>(HttpStatus.NOT_IMPLEMENTED);
67     }
68
69     @PostMapping(value = "/grants")
70     public ResponseEntity<InlineResponse201> grantsPost(@RequestBody final GrantRequest grantRequest) {
71         logger.info("Grant request received from VNFM: " + grantRequest);
72
73         final InlineResponse201 grantResponse = createGrantResponse(grantRequest);
74         logger.info("Grant request returning to VNFM: " + grantResponse);
75         return new ResponseEntity<InlineResponse201>(grantResponse, HttpStatus.CREATED);
76     }
77
78     private InlineResponse201 createGrantResponse(final GrantRequest grantRequest) {
79         final InlineResponse201 grantResponse = new InlineResponse201();
80         grantResponse.setId(UUID.randomUUID().toString());
81         grantResponse.setVnfInstanceId(grantRequest.getVnfInstanceId());
82         grantResponse.setVnfLcmOpOccId(grantRequest.getVnfLcmOpOccId());
83         final Tenant tenant =
84                 aaiHelper.getAssignedTenant(aaiServiceProvider.invokeGetGenericVnf((grantRequest.getVnfInstanceId())));
85
86         String vimConnectionId = "";
87         final InlineResponse201VimConnections vimConnection = vnfmHelper.getVimConnections(tenant);
88         grantResponse.addVimConnectionsItem(vimConnection);
89         vimConnectionId = vimConnection.getId();
90
91         if (grantRequest.getOperation().equals(GrantRequest.OperationEnum.INSTANTIATE)) {
92             grantResponse.addResources(getResources(grantRequest.getAddResources(), vimConnectionId));
93         } else if (grantRequest.getOperation().equals(GrantRequest.OperationEnum.TERMINATE)) {
94             grantResponse.addResources(getResources(grantRequest.getRemoveResources(), vimConnectionId));
95         }
96         return grantResponse;
97     }
98
99     private InlineResponse201VimConnections getVimConnectionsItem(final Tenant tenant) {
100         final InlineResponse201VimConnections vimConnection = new InlineResponse201VimConnections();
101         vimConnection.setId(createVimConnectionId(tenant.getCloudOwner(), tenant.getRegionName()));
102         vimConnection.setVimId(vimConnection.getId());
103         vimConnection.setVimType(VIM_TYPE);
104         return vimConnection;
105     }
106
107     private List<InlineResponse201AddResources> getResources(final List<GrantsAddResources> requestResources,
108             final String vimId) {
109         final List<InlineResponse201AddResources> resources = new ArrayList<>();
110         for (final GrantsAddResources requestResource : requestResources) {
111             final InlineResponse201AddResources responseResource = new InlineResponse201AddResources();
112             responseResource.setResourceDefinitionId(requestResource.getId());
113             responseResource.setVimConnectionId(vimId);
114             resources.add(responseResource);
115         }
116         return resources;
117     }
118
119     private String createVimConnectionId(String cloudOwner, String cloudRegionId) {
120         return cloudOwner + SEPARATOR + cloudRegionId;
121     }
122 }