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