2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.adapters.tenant;
26 import javax.annotation.Resource;
27 import javax.jws.WebService;
28 import javax.xml.ws.Holder;
29 import javax.xml.ws.WebServiceContext;
31 import org.openecomp.mso.properties.MsoPropertiesFactory;
32 import org.openecomp.mso.adapters.tenant.exceptions.TenantAlreadyExists;
33 import org.openecomp.mso.adapters.tenant.exceptions.TenantException;
34 import org.openecomp.mso.adapters.tenantrest.TenantRollback;
35 import org.openecomp.mso.cloud.CloudConfigFactory;
36 import org.openecomp.mso.entity.MsoRequest;
37 import org.openecomp.mso.logger.MessageEnum;
38 import org.openecomp.mso.logger.MsoLogger;
39 import org.openecomp.mso.openstack.beans.MsoTenant;
40 import org.openecomp.mso.openstack.exceptions.MsoException;
41 import org.openecomp.mso.openstack.utils.MsoTenantUtils;
42 import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory;
44 @WebService(serviceName = "TenantAdapter", endpointInterface = "org.openecomp.mso.adapters.tenant.MsoTenantAdapter", targetNamespace = "http://org.openecomp.mso/tenant")
45 public class MsoTenantAdapterImpl implements MsoTenantAdapter {
47 MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
48 MsoTenantUtilsFactory tFactory = new MsoTenantUtilsFactory(MSO_PROP_TENANT_ADAPTER);
50 public static final String MSO_PROP_TENANT_ADAPTER="MSO_PROP_TENANT_ADAPTER";
53 WebServiceContext wsContext;
55 private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
57 * Health Check web method. Does nothing but return to show the adapter is deployed.
60 public void healthCheck () {
61 LOGGER.debug ("Health check call in Tenant Adapter");
65 * This is the "Create Tenant" web service implementation. It will create
66 * a new Tenant in the specified cloud. If the tenant already exists, this
67 * can be considered a success or failure, depending on the value of the
68 * 'failIfExists' parameter.
70 * The method returns the tenantId (the Openstack ID), and a TenantRollback
71 * object. This last object can be passed as-is to the rollbackTenant method
72 * to undo what (if anything) was created. This is useful if a Tenant is
73 * successfully created but the orchestrator fails on a subsequent operation.
76 public void createTenant (String cloudSiteId,
78 Map <String, String> metadata,
81 MsoRequest msoRequest,
82 Holder <String> tenantId,
83 Holder <TenantRollback> rollback) throws TenantException, TenantAlreadyExists {
84 MsoLogger.setLogContext (msoRequest);
85 MsoLogger.setServiceName ("CreateTenant");
87 LOGGER.debug ("Call to MSO createTenant adapter. Creating Tenant: " + tenantName
91 // Will capture total time for metrics
92 long startTime = System.currentTimeMillis ();
94 // Start building up rollback object
95 TenantRollback tenantRollback = new TenantRollback ();
96 tenantRollback.setCloudId (cloudSiteId);
97 tenantRollback.setMsoRequest (msoRequest);
99 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
101 MsoTenant newTenant = null;
102 String newTenantId = null;
103 long queryTenantStartTime = System.currentTimeMillis ();
105 newTenant = tUtils.queryTenantByName (tenantName, cloudSiteId);
106 LOGGER.recordMetricEvent (queryTenantStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "QueryTenant", null);
107 } catch (MsoException me) {
108 LOGGER.recordMetricEvent (queryTenantStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with Open Stack", "OpenStack", "QueryTenant", null);
109 String error = "Create Tenant " + tenantName + ": " + me;
110 LOGGER.error (MessageEnum.RA_CREATE_TENANT_ERR, me.getMessage(), "OpenStack", "createTenant", MsoLogger.ErrorCode.DataError, "Exception while communicate with Open Stack", me);
111 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
112 throw new TenantException (me);
114 if (newTenant == null) {
117 long createTenantStartTime = System.currentTimeMillis ();
119 newTenantId = tUtils.createTenant (tenantName, cloudSiteId, metadata, backout.booleanValue ());
120 LOGGER.recordMetricEvent (createTenantStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "CreateTenant", null);
121 } catch (MsoException me) {
122 LOGGER.recordMetricEvent (createTenantStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with Open Stack", "OpenStack", "CreateTenant", null);
123 String error = "Create Tenant " + tenantName + ": " + me;
124 LOGGER.error (MessageEnum.RA_CREATE_TENANT_ERR, me.getMessage(), "OpenStack", "createTenant", MsoLogger.ErrorCode.DataError, "Exception while communicate with Open Stack", me);
125 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
126 throw new TenantException (me);
128 tenantRollback.setTenantId (newTenantId);
129 tenantRollback.setTenantCreated (true);
130 LOGGER.debug ("Tenant " + tenantName + " successfully created with ID " + newTenantId);
132 if (failIfExists != null && failIfExists) {
133 String error = "CreateTenant: Tenant " + tenantName + " already exists in " + cloudSiteId;
134 LOGGER.error (MessageEnum.RA_TENANT_ALREADY_EXIST, tenantName, cloudSiteId, "OpenStack", "", MsoLogger.ErrorCode.DataError, "CreateTenant, Tenant already exists");
135 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
136 throw new TenantAlreadyExists (tenantName, cloudSiteId, newTenant.getTenantId ());
139 newTenantId = newTenant.getTenantId ();
140 tenantRollback.setTenantCreated (false);
141 LOGGER.debug ("Tenant " + tenantName + " already exists with ID " + newTenantId);
145 tenantId.value = newTenantId;
146 rollback.value = tenantRollback;
147 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully create tenant");
152 public void queryTenant (String cloudSiteId,
153 String tenantNameOrId,
154 MsoRequest msoRequest,
155 Holder <String> tenantId,
156 Holder <String> tenantName,
157 Holder <Map <String, String>> metadata) throws TenantException {
158 MsoLogger.setLogContext (msoRequest);
159 MsoLogger.setServiceName ("QueryTenant");
160 LOGGER.debug ("Querying Tenant " + tenantNameOrId + " in " + cloudSiteId);
162 // Will capture execution time for metrics
163 long startTime = System.currentTimeMillis ();
165 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
167 MsoTenant qTenant = null;
168 long subStartTime = System.currentTimeMillis ();
170 qTenant = tUtils.queryTenant (tenantNameOrId, cloudSiteId);
171 LOGGER.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", "OpenStack", "QueryTenant", null);
172 if (qTenant == null) {
173 // Not found by ID, Try by name.
174 qTenant = tUtils.queryTenantByName (tenantNameOrId, cloudSiteId);
177 if (qTenant == null) {
178 LOGGER.debug ("QueryTenant: Tenant " + tenantNameOrId + " not found");
179 tenantId.value = null;
180 tenantName.value = null;
181 metadata.value = null;
183 LOGGER.debug ("QueryTenant: Tenant " + tenantNameOrId + " found with ID " + qTenant.getTenantId ());
184 tenantId.value = qTenant.getTenantId ();
185 tenantName.value = qTenant.getTenantName ();
186 metadata.value = qTenant.getMetadata ();
188 } catch (MsoException me) {
189 String error = "Query Tenant " + tenantNameOrId + ": " + me;
190 LOGGER.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "QueryTenant", null);
191 LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, me.getMessage(), "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception in queryTenant", me);
192 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
193 throw new TenantException (me);
195 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully query tenant");
200 public void deleteTenant (String cloudSiteId,
202 MsoRequest msoRequest,
203 Holder <Boolean> tenantDeleted) throws TenantException {
204 MsoLogger.setLogContext (msoRequest);
205 MsoLogger.setServiceName ("DeleteTenant");
207 LOGGER.debug ("Deleting Tenant " + tenantId + " in " + cloudSiteId);
209 // Will capture execution time for metrics
210 long startTime = System.currentTimeMillis ();
212 // Delete the Tenant.
213 long subStartTime = System.currentTimeMillis ();
216 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
217 boolean deleted = tUtils.deleteTenant (tenantId, cloudSiteId);
218 tenantDeleted.value = deleted;
219 LOGGER.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully communicate with Open Stack", "OpenStack", "DeleteTenant", null);
220 } catch (MsoException me) {
221 String error = "Delete Tenant " + tenantId + ": " + me;
222 LOGGER.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "DeleteTenant", null);
223 LOGGER.error (MessageEnum.RA_DELETE_TEMAMT_ERR, me.getMessage(), "OpenStack", "", MsoLogger.ErrorCode.DataError, "Exception - DeleteTenant", me);
224 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
225 throw new TenantException (me);
228 // On success, nothing is returned.
229 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully delete tenant");
234 * This web service endpoint will rollback a previous Create VNF operation.
235 * A rollback object is returned to the client in a successful creation
236 * response. The client can pass that object as-is back to the rollbackVnf
237 * operation to undo the creation.
239 * The rollback includes removing the VNF and deleting the tenant if the
240 * tenant did not exist prior to the VNF creation.
243 public void rollbackTenant (TenantRollback rollback) throws TenantException {
244 long startTime = System.currentTimeMillis ();
245 MsoLogger.setServiceName ("RollbackTenant");
246 // rollback may be null (e.g. if stack already existed when Create was called)
247 if (rollback == null) {
248 LOGGER.warn (MessageEnum.RA_ROLLBACK_NULL, "OpenStack", "rollbackTenant", MsoLogger.ErrorCode.DataError, "rollbackTenant, rollback is null");
252 // Get the elements of the VnfRollback object for easier access
253 String cloudSiteId = rollback.getCloudId ();
254 String tenantId = rollback.getTenantId ();
256 MsoLogger.setLogContext (rollback.getMsoRequest ());
257 LOGGER.debug ("Rolling Back Tenant " + rollback.getTenantId () + " in " + cloudSiteId);
259 long subStartTime = System.currentTimeMillis ();
260 if (rollback.getTenantCreated ()) {
263 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
264 tUtils.deleteTenant (tenantId, cloudSiteId);
265 LOGGER.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully communicate with Open Stack", "OpenStack", "RollbackTenant", null);
266 } catch (MsoException me) {
267 me.addContext ("RollbackTenant");
268 // Failed to delete the tenant.
269 String error = "Rollback Tenant " + tenantId + ": " + me;
270 LOGGER.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, "OpenStack", "RollbackTenant", null);
271 LOGGER.error (MessageEnum.RA_ROLLBACK_TENANT_ERR, me.getMessage(), "OpenStack", "rollbackTenant", MsoLogger.ErrorCode.DataError, "Exception - rollbackTenant", me);
272 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
273 throw new TenantException (me);
276 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully roll back tenant");