2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.adapters.tenant;
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;
48 @WebService(serviceName = "TenantAdapter", endpointInterface = "org.onap.so.adapters.tenant.MsoTenantAdapter",
49 targetNamespace = "http://org.onap.so/tenant")
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 ";
60 private WebServiceContext wsContext;
63 private MsoTenantUtilsFactory tFactory;
64 private static Logger logger = LoggerFactory.getLogger(MsoTenantAdapterImpl.class);
67 * Health Check web method. Does nothing but return to show the adapter is deployed.
70 public void healthCheck() {
71 logger.debug("Health check call in Tenant Adapter");
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.
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.
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 {
88 logger.debug("Call to MSO createTenant adapter. Creating Tenant: {} in {}", tenantName, cloudSiteId);
90 // Start building up rollback object
91 TenantRollback tenantRollback = new TenantRollback();
92 tenantRollback.setCloudId(cloudSiteId);
93 tenantRollback.setMsoRequest(msoRequest);
95 MsoTenantUtils tUtils;
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);
104 MsoTenant newTenant = null;
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);
113 if (newTenant == null) {
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);
123 tenantRollback.setTenantId(newTenantId);
124 tenantRollback.setTenantCreated(true);
125 logger.debug("Tenant {} successfully created with ID {}", tenantName, newTenantId);
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());
133 newTenantId = newTenant.getTenantId();
134 tenantRollback.setTenantCreated(false);
135 logger.debug("Tenant {} already exists with ID {}", tenantName, newTenantId);
139 tenantId.value = newTenantId;
140 rollback.value = tenantRollback;
145 public void queryTenant(String cloudSiteId, String tenantNameOrId, MsoRequest msoRequest, Holder<String> tenantId,
146 Holder<String> tenantName, Holder<Map<String, String>> metadata) throws TenantException {
148 logger.debug("Querying Tenant {} in {}", tenantNameOrId, cloudSiteId);
150 MsoTenantUtils tUtils;
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);
159 MsoTenant qTenant = null;
161 qTenant = tUtils.queryTenant(tenantNameOrId, cloudSiteId);
162 if (qTenant == null) {
163 // Not found by ID, Try by name.
164 qTenant = tUtils.queryTenantByName(tenantNameOrId, cloudSiteId);
167 if (qTenant == null) {
168 logger.debug("QueryTenant: Tenant {} not found", tenantNameOrId);
169 tenantId.value = null;
170 tenantName.value = null;
171 metadata.value = null;
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();
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);
187 public void deleteTenant(String cloudSiteId, String tenantId, MsoRequest msoRequest, Holder<Boolean> tenantDeleted)
188 throws TenantException {
190 logger.debug("Deleting Tenant {} in {}", tenantId, cloudSiteId);
192 // Delete the Tenant.
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);
204 // On success, nothing is returned.
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.
213 * The rollback includes removing the VNF and deleting the tenant if the tenant did not exist prior to the VNF
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());
225 // Get the elements of the VnfRollback object for easier access
226 String cloudSiteId = rollback.getCloudId();
227 String tenantId = rollback.getTenantId();
229 logger.debug("Rolling Back Tenant {} in {}", rollback.getTenantId(), cloudSiteId);
231 if (rollback.getTenantCreated()) {
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);