58169f6f92ae64d9af6492901ad0a6d343eae82e
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.adapters.tenant;
22
23
24 import java.util.Map;
25
26 import javax.annotation.Resource;
27 import javax.jws.WebService;
28 import javax.xml.ws.Holder;
29 import javax.xml.ws.WebServiceContext;
30
31 import org.openecomp.mso.openstack.exceptions.MsoCloudSiteNotFound;
32 import org.openecomp.mso.properties.MsoPropertiesFactory;
33 import org.openecomp.mso.adapters.tenant.exceptions.TenantAlreadyExists;
34 import org.openecomp.mso.adapters.tenant.exceptions.TenantException;
35 import org.openecomp.mso.adapters.tenantrest.TenantRollback;
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;
43
44 @WebService(serviceName = "TenantAdapter", endpointInterface = "org.openecomp.mso.adapters.tenant.MsoTenantAdapter", targetNamespace = "http://org.openecomp.mso/tenant")
45 public class MsoTenantAdapterImpl implements MsoTenantAdapter {
46
47         MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
48         MsoTenantUtilsFactory tFactory = new MsoTenantUtilsFactory(MSO_PROP_TENANT_ADAPTER);
49         
50         public static final String MSO_PROP_TENANT_ADAPTER="MSO_PROP_TENANT_ADAPTER";
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         
57     @Resource
58     WebServiceContext wsContext;
59
60     private static MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
61     /**
62      * Health Check web method. Does nothing but return to show the adapter is deployed.
63      */
64     @Override
65     public void healthCheck () {
66         logger.debug ("Health check call in Tenant Adapter");
67     }
68
69     /**
70      * This is the "Create Tenant" web service implementation. It will create
71      * a new Tenant in the specified cloud. If the tenant already exists, this
72      * can be considered a success or failure, depending on the value of the
73      * 'failIfExists' parameter.
74      *
75      * The method returns the tenantId (the Openstack ID), and a TenantRollback
76      * object. This last object can be passed as-is to the rollbackTenant method
77      * to undo what (if anything) was created. This is useful if a Tenant is
78      * successfully created but the orchestrator fails on a subsequent operation.
79      */
80     @Override
81     public void createTenant (String cloudSiteId,
82                               String tenantName,
83                               Map <String, String> metadata,
84                               Boolean failIfExists,
85                               Boolean backout,
86                               MsoRequest msoRequest,
87                               Holder <String> tenantId,
88                               Holder <TenantRollback> rollback) throws TenantException {
89         MsoLogger.setLogContext (msoRequest);
90         MsoLogger.setServiceName (CREATE_TENANT);
91
92         logger.debug ("Call to MSO createTenant adapter. Creating Tenant: " + tenantName
93                                       + "in "
94                                       + cloudSiteId);
95
96         // Will capture total time for metrics
97         long startTime = System.currentTimeMillis ();
98
99         // Start building up rollback object
100         TenantRollback tenantRollback = new TenantRollback ();
101         tenantRollback.setCloudId (cloudSiteId);
102         tenantRollback.setMsoRequest (msoRequest);
103
104         MsoTenantUtils tUtils;
105         MsoTenant newTenant = null;
106         String newTenantId;
107         long queryTenantStartTime = System.currentTimeMillis ();
108         try {
109             tUtils = tFactory.getTenantUtils (cloudSiteId);
110             newTenant = tUtils.queryTenantByName (tenantName, cloudSiteId);
111             logger.recordMetricEvent (queryTenantStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", OPENSTACK, QUERY_TENANT, null);
112
113         } catch (MsoException me) {
114             logger.recordMetricEvent (queryTenantStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with Open Stack", OPENSTACK, QUERY_TENANT, null);
115             String error = "Create Tenant " + tenantName + ": " + me;
116             logger.error (MessageEnum.RA_CREATE_TENANT_ERR, me.getMessage(), OPENSTACK, "createTenant", MsoLogger.ErrorCode.DataError, "Exception while communicate with Open Stack", me);
117             logger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
118             throw new TenantException (me);
119         }
120         if (newTenant == null) {
121             if (backout == null)
122                 backout = true;
123             long createTenantStartTime = System.currentTimeMillis ();
124             try {
125                 newTenantId = tUtils.createTenant (tenantName, cloudSiteId, metadata, backout.booleanValue ());
126                 logger.recordMetricEvent (createTenantStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", OPENSTACK, CREATE_TENANT, null);
127             } catch (MsoException me) {
128                 logger.recordMetricEvent (createTenantStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with Open Stack", OPENSTACK, CREATE_TENANT, null);
129                 String error = "Create Tenant " + tenantName + ": " + me;
130                 logger.error (MessageEnum.RA_CREATE_TENANT_ERR, me.getMessage(), OPENSTACK, "createTenant", MsoLogger.ErrorCode.DataError, "Exception while communicate with Open Stack", me);
131                 logger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
132                 throw new TenantException (me);
133             }
134             tenantRollback.setTenantId (newTenantId);
135             tenantRollback.setTenantCreated (true);
136             logger.debug ("Tenant " + tenantName + " successfully created with ID " + newTenantId);
137         } else {
138             if (failIfExists != null && failIfExists) {
139                 String error = CREATE_TENANT + ": Tenant " + tenantName + " already exists in " + cloudSiteId;
140                 logger.error (MessageEnum.RA_TENANT_ALREADY_EXIST, tenantName, cloudSiteId, OPENSTACK, "", MsoLogger.ErrorCode.DataError, CREATE_TENANT + ", Tenant already exists");
141                 logger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataError, error);
142                 throw new TenantAlreadyExists (tenantName, cloudSiteId, newTenant.getTenantId ());
143             }
144
145             newTenantId = newTenant.getTenantId ();
146             tenantRollback.setTenantCreated (false);
147             logger.debug ("Tenant " + tenantName + " already exists with ID " + newTenantId);
148         }
149
150
151         tenantId.value = newTenantId;
152         rollback.value = tenantRollback;
153         logger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully create tenant");
154         return;
155     }
156
157     @Override
158     public void queryTenant (String cloudSiteId,
159                              String tenantNameOrId,
160                              MsoRequest msoRequest,
161                              Holder <String> tenantId,
162                              Holder <String> tenantName,
163                              Holder <Map <String, String>> metadata) throws TenantException {
164         MsoLogger.setLogContext (msoRequest);
165         MsoLogger.setServiceName (QUERY_TENANT);
166         logger.debug ("Querying Tenant " + tenantNameOrId + " in " + cloudSiteId);
167
168         // Will capture execution time for metrics
169         long startTime = System.currentTimeMillis ();
170
171         MsoTenantUtils tUtils = null;
172         MsoTenant qTenant = null;
173         long subStartTime = System.currentTimeMillis ();
174         try {
175             tUtils = tFactory.getTenantUtils (cloudSiteId);
176             qTenant = tUtils.queryTenant (tenantNameOrId, cloudSiteId);
177             logger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from Open Stack", OPENSTACK, QUERY_TENANT, null);
178             if (qTenant == null) {
179                 // Not found by ID, Try by name.
180                 qTenant = tUtils.queryTenantByName (tenantNameOrId, cloudSiteId);
181             }
182
183             if (qTenant == null) {
184                 logger.debug ("QueryTenant: Tenant " + tenantNameOrId + " not found");
185                 tenantId.value = null;
186                 tenantName.value = null;
187                 metadata.value = null;
188             } else {
189                 logger.debug ("QueryTenant: Tenant " + tenantNameOrId + " found with ID " + qTenant.getTenantId ());
190                 tenantId.value = qTenant.getTenantId ();
191                 tenantName.value = qTenant.getTenantName ();
192                 metadata.value = qTenant.getMetadata ();
193             }
194         } catch (MsoException me) {
195             String error = "Query Tenant " + tenantNameOrId + ": " + me;
196             logger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, OPENSTACK, QUERY_TENANT, null);
197             logger.error (MessageEnum.RA_GENERAL_EXCEPTION, me.getMessage(), OPENSTACK, "", MsoLogger.ErrorCode.DataError, "Exception in queryTenant", me);
198             logger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
199             throw new TenantException (me);
200         }
201         logger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully query tenant");
202         return;
203     }
204
205     @Override
206     public void deleteTenant (String cloudSiteId,
207                               String tenantId,
208                               MsoRequest msoRequest,
209                               Holder <Boolean> tenantDeleted) throws TenantException {
210         MsoLogger.setLogContext (msoRequest);
211         MsoLogger.setServiceName (DELETE_TENANT);
212
213         logger.debug ("Deleting Tenant " + tenantId + " in " + cloudSiteId);
214
215         // Will capture execution time for metrics
216         long startTime = System.currentTimeMillis ();
217
218         // Delete the Tenant.
219         long subStartTime = System.currentTimeMillis ();
220         try {
221                 
222                 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
223             boolean deleted = tUtils.deleteTenant (tenantId, cloudSiteId);
224             tenantDeleted.value = deleted;
225             logger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully communicate with Open Stack", OPENSTACK, DELETE_TENANT, null);
226         } catch (MsoException me) {
227             String error = "Delete Tenant " + tenantId + ": " + me;
228             logger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, OPENSTACK, DELETE_TENANT, null);
229             logger.error (MessageEnum.RA_DELETE_TEMAMT_ERR, me.getMessage(), OPENSTACK, "", MsoLogger.ErrorCode.DataError, "Exception - DeleteTenant", me);
230             logger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
231             throw new TenantException (me);
232         }
233
234         // On success, nothing is returned.
235         logger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully delete tenant");
236         return;
237     }
238
239     /**
240      * This web service endpoint will rollback a previous Create VNF operation.
241      * A rollback object is returned to the client in a successful creation
242      * response. The client can pass that object as-is back to the rollbackVnf
243      * operation to undo the creation.
244      *
245      * The rollback includes removing the VNF and deleting the tenant if the
246      * tenant did not exist prior to the VNF creation.
247      */
248     @Override
249     public void rollbackTenant (TenantRollback rollback) throws TenantException {
250         long startTime = System.currentTimeMillis ();
251         MsoLogger.setServiceName (ROLLBACK_TENANT);
252         // rollback may be null (e.g. if stack already existed when Create was called)
253         if (rollback == null) {
254             logger.warn (MessageEnum.RA_ROLLBACK_NULL, OPENSTACK, "rollbackTenant", MsoLogger.ErrorCode.DataError, "rollbackTenant, rollback is null");
255             return;
256         }
257
258         // Get the elements of the VnfRollback object for easier access
259         String cloudSiteId = rollback.getCloudId ();
260         String tenantId = rollback.getTenantId ();
261
262         MsoLogger.setLogContext (rollback.getMsoRequest ());
263         logger.debug ("Rolling Back Tenant " + rollback.getTenantId () + " in " + cloudSiteId);
264
265         long subStartTime = System.currentTimeMillis ();
266         if (rollback.getTenantCreated ()) {
267             try {
268                  
269                 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
270                 tUtils.deleteTenant (tenantId, cloudSiteId);
271                 logger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully communicate with Open Stack", OPENSTACK, ROLLBACK_TENANT, null);
272             } catch (MsoException me) {
273                 me.addContext (ROLLBACK_TENANT);
274                 // Failed to delete the tenant.
275                 String error = "Rollback Tenant " + tenantId + ": " + me;
276                 logger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error, OPENSTACK, ROLLBACK_TENANT, null);
277                 logger.error (MessageEnum.RA_ROLLBACK_TENANT_ERR, me.getMessage(), OPENSTACK, "rollbackTenant", MsoLogger.ErrorCode.DataError, "Exception - rollbackTenant", me);
278                 logger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, error);
279                 throw new TenantException (me);
280             }
281         }
282         logger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully roll back tenant");
283         return;
284     }
285 }