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", targetNamespace = "http://org.onap.so/tenant")
50 public class MsoTenantAdapterImpl implements MsoTenantAdapter {
51 public static final String CREATE_TENANT = "createTenant";
52 public static final String OPENSTACK = "OpenStack";
53 public static final String QUERY_TENANT = "QueryTenant";
54 public static final String DELETE_TENANT = "DeleteTenant";
55 public static final String ROLLBACK_TENANT = "RollbackTenant";
56 private static final String OPENSTACK_COMMUNICATE_EXCEPTION_MSG = "{} {} Exception while communicate with Open Stack ";
58 private WebServiceContext wsContext;
61 private MsoTenantUtilsFactory tFactory;
62 private static Logger logger = LoggerFactory.getLogger(MsoTenantAdapterImpl.class);
64 * Health Check web method. Does nothing but return to show the adapter is deployed.
67 public void healthCheck () {
68 logger.debug ("Health check call in Tenant Adapter");
72 * This is the "Create Tenant" web service implementation. It will create
73 * a new Tenant in the specified cloud. If the tenant already exists, this
74 * can be considered a success or failure, depending on the value of the
75 * 'failIfExists' parameter.
77 * The method returns the tenantId (the Openstack ID), and a TenantRollback
78 * object. This last object can be passed as-is to the rollbackTenant method
79 * to undo what (if anything) was created. This is useful if a Tenant is
80 * successfully created but the orchestrator fails on a subsequent operation.
83 public void createTenant (String cloudSiteId,
85 Map <String, String> metadata,
88 MsoRequest msoRequest,
89 Holder <String> tenantId,
90 Holder <TenantRollback> rollback) throws TenantException {
92 logger.debug("Call to MSO createTenant adapter. Creating Tenant: {} in {}", tenantName, cloudSiteId);
94 // Start building up rollback object
95 TenantRollback tenantRollback = new TenantRollback ();
96 tenantRollback.setCloudId (cloudSiteId);
97 tenantRollback.setMsoRequest (msoRequest);
99 MsoTenantUtils tUtils;
101 tUtils = tFactory.getTenantUtils (cloudSiteId);
102 } catch (MsoCloudSiteNotFound me) {
103 logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
104 ErrorCode.DataError.getValue(), cloudSiteId, me);
105 throw new TenantException (me);
108 MsoTenant newTenant = null;
111 newTenant = tUtils.queryTenantByName (tenantName, cloudSiteId);
112 } catch (MsoException me) {
113 logger.error(OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR,
114 ErrorCode.DataError.getValue(), me);
115 throw new TenantException (me);
117 if (newTenant == null) {
121 newTenantId = tUtils.createTenant (tenantName, cloudSiteId, metadata, backout.booleanValue ());
122 } catch (MsoException me) {
123 logger.error (OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR, ErrorCode.DataError.getValue(), me);
124 throw new TenantException (me);
126 tenantRollback.setTenantId (newTenantId);
127 tenantRollback.setTenantCreated (true);
128 logger.debug ("Tenant {} successfully created with ID {}", tenantName, newTenantId);
130 if (failIfExists != null && failIfExists) {
131 logger.error("{} {} CreateTenant: Tenant {} already exists in {} ", MessageEnum.RA_TENANT_ALREADY_EXIST,
132 ErrorCode.DataError.getValue(), tenantName, cloudSiteId);
133 throw new TenantAlreadyExists (tenantName, cloudSiteId, newTenant.getTenantId ());
136 newTenantId = newTenant.getTenantId ();
137 tenantRollback.setTenantCreated (false);
138 logger.debug("Tenant {} already exists with ID {}", tenantName, newTenantId);
142 tenantId.value = newTenantId;
143 rollback.value = tenantRollback;
148 public void queryTenant (String cloudSiteId,
149 String tenantNameOrId,
150 MsoRequest msoRequest,
151 Holder <String> tenantId,
152 Holder <String> tenantName,
153 Holder <Map <String, String>> metadata) throws TenantException {
155 logger.debug ("Querying Tenant {} in {}", tenantNameOrId, cloudSiteId);
157 MsoTenantUtils tUtils;
159 tUtils = tFactory.getTenantUtils (cloudSiteId);
160 } catch (MsoCloudSiteNotFound me) {
161 logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
162 ErrorCode.DataError.getValue(), cloudSiteId, me);
163 throw new TenantException (me);
166 MsoTenant qTenant = null;
168 qTenant = tUtils.queryTenant (tenantNameOrId, cloudSiteId);
169 if (qTenant == null) {
170 // Not found by ID, Try by name.
171 qTenant = tUtils.queryTenantByName (tenantNameOrId, cloudSiteId);
174 if (qTenant == null) {
175 logger.debug ("QueryTenant: Tenant {} not found", tenantNameOrId);
176 tenantId.value = null;
177 tenantName.value = null;
178 metadata.value = null;
180 logger.debug("QueryTenant: Tenant {} found with ID {}", tenantNameOrId, qTenant.getTenantId());
181 tenantId.value = qTenant.getTenantId ();
182 tenantName.value = qTenant.getTenantName ();
183 metadata.value = qTenant.getMetadata ();
185 } catch (MsoException me) {
186 logger.error("Exception in queryTenant for {}: ", MessageEnum.RA_GENERAL_EXCEPTION,
187 ErrorCode.DataError.getValue(), tenantNameOrId, me);
188 throw new TenantException (me);
194 public void deleteTenant (String cloudSiteId,
196 MsoRequest msoRequest,
197 Holder <Boolean> tenantDeleted) throws TenantException {
199 logger.debug ("Deleting Tenant {} in {}", tenantId, cloudSiteId);
201 // Delete the Tenant.
204 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
205 boolean deleted = tUtils.deleteTenant (tenantId, cloudSiteId);
206 tenantDeleted.value = deleted;
207 } catch (MsoException me) {
208 logger.error("{} {} Exception - DeleteTenant {}: ", MessageEnum.RA_DELETE_TEMAMT_ERR,
209 ErrorCode.DataError.getValue(), tenantId, me);
210 throw new TenantException (me);
213 // On success, nothing is returned.
218 * This web service endpoint will rollback a previous Create VNF operation.
219 * A rollback object is returned to the client in a successful creation
220 * response. The client can pass that object as-is back to the rollbackVnf
221 * operation to undo the creation.
223 * The rollback includes removing the VNF and deleting the tenant if the
224 * tenant did not exist prior to the VNF creation.
227 public void rollbackTenant (TenantRollback rollback) throws TenantException {
228 // rollback may be null (e.g. if stack already existed when Create was called)
229 if (rollback == null) {
230 logger.warn("{} {} rollbackTenant, rollback is null", MessageEnum.RA_ROLLBACK_NULL,
231 ErrorCode.DataError.getValue());
235 // Get the elements of the VnfRollback object for easier access
236 String cloudSiteId = rollback.getCloudId ();
237 String tenantId = rollback.getTenantId ();
239 logger.debug("Rolling Back Tenant {} in {}", rollback.getTenantId(), cloudSiteId);
241 if (rollback.getTenantCreated ()) {
244 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
245 tUtils.deleteTenant (tenantId, cloudSiteId);
246 } catch (MsoException me) {
247 me.addContext (ROLLBACK_TENANT);
248 // Failed to delete the tenant.
249 logger.error("{} {} Exception - rollbackTenant {}: ", MessageEnum.RA_ROLLBACK_TENANT_ERR,
250 ErrorCode.DataError.getValue(), tenantId, me);
251 throw new TenantException (me);