Change the header to SO
[so.git] / adapters / mso-tenant-adapter / src / main / java / org / openecomp / mso / adapters / tenant / MsoTenantAdapterImpl.java
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.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;
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         
52     @Resource
53     WebServiceContext wsContext;
54
55     private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
56     /**
57      * Health Check web method. Does nothing but return to show the adapter is deployed.
58      */
59     @Override
60     public void healthCheck () {
61         LOGGER.debug ("Health check call in Tenant Adapter");
62     }
63
64     /**
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.
69      *
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.
74      */
75     @Override
76     public void createTenant (String cloudSiteId,
77                               String tenantName,
78                               Map <String, String> metadata,
79                               Boolean failIfExists,
80                               Boolean backout,
81                               MsoRequest msoRequest,
82                               Holder <String> tenantId,
83                               Holder <TenantRollback> rollback) throws TenantException, TenantAlreadyExists {
84         MsoLogger.setLogContext (msoRequest);
85         MsoLogger.setServiceName ("CreateTenant");
86
87         LOGGER.debug ("Call to MSO createTenant adapter. Creating Tenant: " + tenantName
88                                       + "in "
89                                       + cloudSiteId);
90
91         // Will capture total time for metrics
92         long startTime = System.currentTimeMillis ();
93
94         // Start building up rollback object
95         TenantRollback tenantRollback = new TenantRollback ();
96         tenantRollback.setCloudId (cloudSiteId);
97         tenantRollback.setMsoRequest (msoRequest);
98         
99         MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
100
101         MsoTenant newTenant = null;
102         String newTenantId = null;
103         long queryTenantStartTime = System.currentTimeMillis ();
104         try {
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);
113         }
114         if (newTenant == null) {
115             if (backout == null)
116                 backout = true;
117             long createTenantStartTime = System.currentTimeMillis ();
118             try {
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);
127             }
128             tenantRollback.setTenantId (newTenantId);
129             tenantRollback.setTenantCreated (true);
130             LOGGER.debug ("Tenant " + tenantName + " successfully created with ID " + newTenantId);
131         } else {
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 ());
137             }
138
139             newTenantId = newTenant.getTenantId ();
140             tenantRollback.setTenantCreated (false);
141             LOGGER.debug ("Tenant " + tenantName + " already exists with ID " + newTenantId);
142         }
143
144
145         tenantId.value = newTenantId;
146         rollback.value = tenantRollback;
147         LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully create tenant");
148         return;
149     }
150
151     @Override
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);
161
162         // Will capture execution time for metrics
163         long startTime = System.currentTimeMillis ();
164
165         MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
166         
167         MsoTenant qTenant = null;
168         long subStartTime = System.currentTimeMillis ();
169         try {
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);
175             }
176
177             if (qTenant == null) {
178                 LOGGER.debug ("QueryTenant: Tenant " + tenantNameOrId + " not found");
179                 tenantId.value = null;
180                 tenantName.value = null;
181                 metadata.value = null;
182             } else {
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 ();
187             }
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);
194         }
195         LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully query tenant");
196         return;
197     }
198
199     @Override
200     public void deleteTenant (String cloudSiteId,
201                               String tenantId,
202                               MsoRequest msoRequest,
203                               Holder <Boolean> tenantDeleted) throws TenantException {
204         MsoLogger.setLogContext (msoRequest);
205         MsoLogger.setServiceName ("DeleteTenant");
206
207         LOGGER.debug ("Deleting Tenant " + tenantId + " in " + cloudSiteId);
208
209         // Will capture execution time for metrics
210         long startTime = System.currentTimeMillis ();
211
212         // Delete the Tenant.
213         long subStartTime = System.currentTimeMillis ();
214         try {
215                 
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);
226         }
227
228         // On success, nothing is returned.
229         LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully delete tenant");
230         return;
231     }
232
233     /**
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.
238      *
239      * The rollback includes removing the VNF and deleting the tenant if the
240      * tenant did not exist prior to the VNF creation.
241      */
242     @Override
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");
249             return;
250         }
251
252         // Get the elements of the VnfRollback object for easier access
253         String cloudSiteId = rollback.getCloudId ();
254         String tenantId = rollback.getTenantId ();
255
256         MsoLogger.setLogContext (rollback.getMsoRequest ());
257         LOGGER.debug ("Rolling Back Tenant " + rollback.getTenantId () + " in " + cloudSiteId);
258
259         long subStartTime = System.currentTimeMillis ();
260         if (rollback.getTenantCreated ()) {
261             try {
262                  
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);
274             }
275         }
276         LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully roll back tenant");
277         return;
278     }
279 }