Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / tenant / MsoTenantAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.tenant;
25
26
27 import java.util.Map;
28 import javax.annotation.Resource;
29 import javax.jws.WebService;
30 import javax.xml.ws.Holder;
31 import javax.xml.ws.WebServiceContext;
32 import org.onap.so.adapters.tenant.exceptions.TenantAlreadyExists;
33 import org.onap.so.adapters.tenant.exceptions.TenantException;
34 import org.onap.so.adapters.tenantrest.TenantRollback;
35 import org.onap.so.entity.MsoRequest;
36 import org.onap.so.logger.ErrorCode;
37 import org.onap.so.logger.MessageEnum;
38 import org.onap.so.openstack.beans.MsoTenant;
39 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
40 import org.onap.so.openstack.exceptions.MsoException;
41 import org.onap.so.openstack.utils.MsoTenantUtils;
42 import org.onap.so.openstack.utils.MsoTenantUtilsFactory;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47
48 @WebService(serviceName = "TenantAdapter", endpointInterface = "org.onap.so.adapters.tenant.MsoTenantAdapter",
49         targetNamespace = "http://org.onap.so/tenant")
50 @Component
51 public class MsoTenantAdapterImpl implements MsoTenantAdapter {
52     public static final String CREATE_TENANT = "createTenant";
53     public static final String OPENSTACK = "OpenStack";
54     public static final String QUERY_TENANT = "QueryTenant";
55     public static final String DELETE_TENANT = "DeleteTenant";
56     public static final String ROLLBACK_TENANT = "RollbackTenant";
57     private static final String OPENSTACK_COMMUNICATE_EXCEPTION_MSG =
58             "{} {} Exception while communicate with Open Stack ";
59     @Resource
60     private WebServiceContext wsContext;
61
62     @Autowired
63     private MsoTenantUtilsFactory tFactory;
64     private static Logger logger = LoggerFactory.getLogger(MsoTenantAdapterImpl.class);
65
66     /**
67      * Health Check web method. Does nothing but return to show the adapter is deployed.
68      */
69     @Override
70     public void healthCheck() {
71         logger.debug("Health check call in Tenant Adapter");
72     }
73
74     /**
75      * This is the "Create Tenant" web service implementation. It will create a new Tenant in the specified cloud. If
76      * the tenant already exists, this can be considered a success or failure, depending on the value of the
77      * 'failIfExists' parameter.
78      *
79      * The method returns the tenantId (the Openstack ID), and a TenantRollback object. This last object can be passed
80      * as-is to the rollbackTenant method to undo what (if anything) was created. This is useful if a Tenant is
81      * successfully created but the orchestrator fails on a subsequent operation.
82      */
83     @Override
84     public void createTenant(String cloudSiteId, String tenantName, Map<String, String> metadata, Boolean failIfExists,
85             Boolean backout, MsoRequest msoRequest, Holder<String> tenantId, Holder<TenantRollback> rollback)
86             throws TenantException {
87
88         logger.debug("Call to MSO createTenant adapter. Creating Tenant: {} in {}", tenantName, cloudSiteId);
89
90         // Start building up rollback object
91         TenantRollback tenantRollback = new TenantRollback();
92         tenantRollback.setCloudId(cloudSiteId);
93         tenantRollback.setMsoRequest(msoRequest);
94
95         MsoTenantUtils tUtils;
96         try {
97             tUtils = tFactory.getTenantUtils(cloudSiteId);
98         } catch (MsoCloudSiteNotFound me) {
99             logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
100                     ErrorCode.DataError.getValue(), cloudSiteId, me);
101             throw new TenantException(me);
102         }
103
104         MsoTenant newTenant = null;
105         String newTenantId;
106         try {
107             newTenant = tUtils.queryTenantByName(tenantName, cloudSiteId);
108         } catch (MsoException me) {
109             logger.error(OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR,
110                     ErrorCode.DataError.getValue(), me);
111             throw new TenantException(me);
112         }
113         if (newTenant == null) {
114             if (backout == null)
115                 backout = true;
116             try {
117                 newTenantId = tUtils.createTenant(tenantName, cloudSiteId, metadata, backout.booleanValue());
118             } catch (MsoException me) {
119                 logger.error(OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR,
120                         ErrorCode.DataError.getValue(), me);
121                 throw new TenantException(me);
122             }
123             tenantRollback.setTenantId(newTenantId);
124             tenantRollback.setTenantCreated(true);
125             logger.debug("Tenant {} successfully created with ID {}", tenantName, newTenantId);
126         } else {
127             if (failIfExists != null && failIfExists) {
128                 logger.error("{} {} CreateTenant: Tenant {} already exists in {} ", MessageEnum.RA_TENANT_ALREADY_EXIST,
129                         ErrorCode.DataError.getValue(), tenantName, cloudSiteId);
130                 throw new TenantAlreadyExists(tenantName, cloudSiteId, newTenant.getTenantId());
131             }
132
133             newTenantId = newTenant.getTenantId();
134             tenantRollback.setTenantCreated(false);
135             logger.debug("Tenant {} already exists with ID {}", tenantName, newTenantId);
136         }
137
138
139         tenantId.value = newTenantId;
140         rollback.value = tenantRollback;
141         return;
142     }
143
144     @Override
145     public void queryTenant(String cloudSiteId, String tenantNameOrId, MsoRequest msoRequest, Holder<String> tenantId,
146             Holder<String> tenantName, Holder<Map<String, String>> metadata) throws TenantException {
147
148         logger.debug("Querying Tenant {} in {}", tenantNameOrId, cloudSiteId);
149
150         MsoTenantUtils tUtils;
151         try {
152             tUtils = tFactory.getTenantUtils(cloudSiteId);
153         } catch (MsoCloudSiteNotFound me) {
154             logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
155                     ErrorCode.DataError.getValue(), cloudSiteId, me);
156             throw new TenantException(me);
157         }
158
159         MsoTenant qTenant = null;
160         try {
161             qTenant = tUtils.queryTenant(tenantNameOrId, cloudSiteId);
162             if (qTenant == null) {
163                 // Not found by ID, Try by name.
164                 qTenant = tUtils.queryTenantByName(tenantNameOrId, cloudSiteId);
165             }
166
167             if (qTenant == null) {
168                 logger.debug("QueryTenant: Tenant {} not found", tenantNameOrId);
169                 tenantId.value = null;
170                 tenantName.value = null;
171                 metadata.value = null;
172             } else {
173                 logger.debug("QueryTenant: Tenant {} found with ID {}", tenantNameOrId, qTenant.getTenantId());
174                 tenantId.value = qTenant.getTenantId();
175                 tenantName.value = qTenant.getTenantName();
176                 metadata.value = qTenant.getMetadata();
177             }
178         } catch (MsoException me) {
179             logger.error("Exception in queryTenant for {}: ", MessageEnum.RA_GENERAL_EXCEPTION,
180                     ErrorCode.DataError.getValue(), tenantNameOrId, me);
181             throw new TenantException(me);
182         }
183         return;
184     }
185
186     @Override
187     public void deleteTenant(String cloudSiteId, String tenantId, MsoRequest msoRequest, Holder<Boolean> tenantDeleted)
188             throws TenantException {
189
190         logger.debug("Deleting Tenant {} in {}", tenantId, cloudSiteId);
191
192         // Delete the Tenant.
193         try {
194
195             MsoTenantUtils tUtils = tFactory.getTenantUtils(cloudSiteId);
196             boolean deleted = tUtils.deleteTenant(tenantId, cloudSiteId);
197             tenantDeleted.value = deleted;
198         } catch (MsoException me) {
199             logger.error("{} {} Exception - DeleteTenant {}: ", MessageEnum.RA_DELETE_TEMAMT_ERR,
200                     ErrorCode.DataError.getValue(), tenantId, me);
201             throw new TenantException(me);
202         }
203
204         // On success, nothing is returned.
205         return;
206     }
207
208     /**
209      * This web service endpoint will rollback a previous Create VNF operation. A rollback object is returned to the
210      * client in a successful creation response. The client can pass that object as-is back to the rollbackVnf operation
211      * to undo the creation.
212      *
213      * The rollback includes removing the VNF and deleting the tenant if the tenant did not exist prior to the VNF
214      * creation.
215      */
216     @Override
217     public void rollbackTenant(TenantRollback rollback) throws TenantException {
218         // rollback may be null (e.g. if stack already existed when Create was called)
219         if (rollback == null) {
220             logger.warn("{} {} rollbackTenant, rollback is null", MessageEnum.RA_ROLLBACK_NULL,
221                     ErrorCode.DataError.getValue());
222             return;
223         }
224
225         // Get the elements of the VnfRollback object for easier access
226         String cloudSiteId = rollback.getCloudId();
227         String tenantId = rollback.getTenantId();
228
229         logger.debug("Rolling Back Tenant {} in {}", rollback.getTenantId(), cloudSiteId);
230
231         if (rollback.getTenantCreated()) {
232             try {
233
234                 MsoTenantUtils tUtils = tFactory.getTenantUtils(cloudSiteId);
235                 tUtils.deleteTenant(tenantId, cloudSiteId);
236             } catch (MsoException me) {
237                 me.addContext(ROLLBACK_TENANT);
238                 // Failed to delete the tenant.
239                 logger.error("{} {} Exception - rollbackTenant {}: ", MessageEnum.RA_ROLLBACK_TENANT_ERR,
240                         ErrorCode.DataError.getValue(), tenantId, me);
241                 throw new TenantException(me);
242             }
243         }
244         return;
245     }
246 }