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.MessageEnum;
37 import org.onap.so.logger.MsoLogger;
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 {
91 MsoLogger.setLogContext (msoRequest);
93 logger.debug("Call to MSO createTenant adapter. Creating Tenant: {} in {}", tenantName, cloudSiteId);
95 // Start building up rollback object
96 TenantRollback tenantRollback = new TenantRollback ();
97 tenantRollback.setCloudId (cloudSiteId);
98 tenantRollback.setMsoRequest (msoRequest);
100 MsoTenantUtils tUtils;
102 tUtils = tFactory.getTenantUtils (cloudSiteId);
103 } catch (MsoCloudSiteNotFound me) {
104 logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
105 MsoLogger.ErrorCode.DataError.getValue(), cloudSiteId, me);
106 throw new TenantException (me);
109 MsoTenant newTenant = null;
112 newTenant = tUtils.queryTenantByName (tenantName, cloudSiteId);
113 } catch (MsoException me) {
114 logger.error(OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR,
115 MsoLogger.ErrorCode.DataError.getValue(), me);
116 throw new TenantException (me);
118 if (newTenant == null) {
122 newTenantId = tUtils.createTenant (tenantName, cloudSiteId, metadata, backout.booleanValue ());
123 } catch (MsoException me) {
124 logger.error (OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR, MsoLogger.ErrorCode.DataError.getValue(), me);
125 throw new TenantException (me);
127 tenantRollback.setTenantId (newTenantId);
128 tenantRollback.setTenantCreated (true);
129 logger.debug ("Tenant {} successfully created with ID {}", tenantName, newTenantId);
131 if (failIfExists != null && failIfExists) {
132 logger.error("{} {} CreateTenant: Tenant {} already exists in {} ", MessageEnum.RA_TENANT_ALREADY_EXIST,
133 MsoLogger.ErrorCode.DataError.getValue(), tenantName, cloudSiteId);
134 throw new TenantAlreadyExists (tenantName, cloudSiteId, newTenant.getTenantId ());
137 newTenantId = newTenant.getTenantId ();
138 tenantRollback.setTenantCreated (false);
139 logger.debug("Tenant {} already exists with ID {}", tenantName, newTenantId);
143 tenantId.value = newTenantId;
144 rollback.value = tenantRollback;
149 public void queryTenant (String cloudSiteId,
150 String tenantNameOrId,
151 MsoRequest msoRequest,
152 Holder <String> tenantId,
153 Holder <String> tenantName,
154 Holder <Map <String, String>> metadata) throws TenantException {
155 MsoLogger.setLogContext (msoRequest);
156 logger.debug ("Querying Tenant {} in {}", tenantNameOrId, cloudSiteId);
158 MsoTenantUtils tUtils;
160 tUtils = tFactory.getTenantUtils (cloudSiteId);
161 } catch (MsoCloudSiteNotFound me) {
162 logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
163 MsoLogger.ErrorCode.DataError.getValue(), cloudSiteId, me);
164 throw new TenantException (me);
167 MsoTenant qTenant = null;
169 qTenant = tUtils.queryTenant (tenantNameOrId, cloudSiteId);
170 if (qTenant == null) {
171 // Not found by ID, Try by name.
172 qTenant = tUtils.queryTenantByName (tenantNameOrId, cloudSiteId);
175 if (qTenant == null) {
176 logger.debug ("QueryTenant: Tenant {} not found", tenantNameOrId);
177 tenantId.value = null;
178 tenantName.value = null;
179 metadata.value = null;
181 logger.debug("QueryTenant: Tenant {} found with ID {}", tenantNameOrId, qTenant.getTenantId());
182 tenantId.value = qTenant.getTenantId ();
183 tenantName.value = qTenant.getTenantName ();
184 metadata.value = qTenant.getMetadata ();
186 } catch (MsoException me) {
187 logger.error("Exception in queryTenant for {}: ", MessageEnum.RA_GENERAL_EXCEPTION,
188 MsoLogger.ErrorCode.DataError.getValue(), tenantNameOrId, me);
189 throw new TenantException (me);
195 public void deleteTenant (String cloudSiteId,
197 MsoRequest msoRequest,
198 Holder <Boolean> tenantDeleted) throws TenantException {
199 MsoLogger.setLogContext (msoRequest);
201 logger.debug ("Deleting Tenant {} in {}", tenantId, cloudSiteId);
203 // Delete the Tenant.
206 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
207 boolean deleted = tUtils.deleteTenant (tenantId, cloudSiteId);
208 tenantDeleted.value = deleted;
209 } catch (MsoException me) {
210 logger.error("{} {} Exception - DeleteTenant {}: ", MessageEnum.RA_DELETE_TEMAMT_ERR,
211 MsoLogger.ErrorCode.DataError.getValue(), tenantId, me);
212 throw new TenantException (me);
215 // On success, nothing is returned.
220 * This web service endpoint will rollback a previous Create VNF operation.
221 * A rollback object is returned to the client in a successful creation
222 * response. The client can pass that object as-is back to the rollbackVnf
223 * operation to undo the creation.
225 * The rollback includes removing the VNF and deleting the tenant if the
226 * tenant did not exist prior to the VNF creation.
229 public void rollbackTenant (TenantRollback rollback) throws TenantException {
230 // rollback may be null (e.g. if stack already existed when Create was called)
231 if (rollback == null) {
232 logger.warn("{} {} rollbackTenant, rollback is null", MessageEnum.RA_ROLLBACK_NULL,
233 MsoLogger.ErrorCode.DataError.getValue());
237 // Get the elements of the VnfRollback object for easier access
238 String cloudSiteId = rollback.getCloudId ();
239 String tenantId = rollback.getTenantId ();
241 MsoLogger.setLogContext (rollback.getMsoRequest ());
242 logger.debug("Rolling Back Tenant {} in {}", rollback.getTenantId(), cloudSiteId);
244 if (rollback.getTenantCreated ()) {
247 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
248 tUtils.deleteTenant (tenantId, cloudSiteId);
249 } catch (MsoException me) {
250 me.addContext (ROLLBACK_TENANT);
251 // Failed to delete the tenant.
252 logger.error("{} {} Exception - rollbackTenant {}: ", MessageEnum.RA_ROLLBACK_TENANT_ERR,
253 MsoLogger.ErrorCode.DataError.getValue(), tenantId, me);
254 throw new TenantException (me);