a937bd9499cc0152c5ebeda35bad88d68c71b2c8
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / 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  * 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
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
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=========================================================
22  */
23
24 package org.onap.so.adapters.tenant;
25
26
27 import java.util.Map;
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;
47
48 @WebService(serviceName = "TenantAdapter", endpointInterface = "org.onap.so.adapters.tenant.MsoTenantAdapter", targetNamespace = "http://org.onap.so/tenant")
49 @Component
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 ";
57     @Resource
58     private WebServiceContext wsContext;
59
60     @Autowired
61         private MsoTenantUtilsFactory tFactory;
62     private static Logger logger = LoggerFactory.getLogger(MsoTenantAdapterImpl.class);
63     /**
64      * Health Check web method. Does nothing but return to show the adapter is deployed.
65      */
66     @Override
67     public void healthCheck () {
68         logger.debug ("Health check call in Tenant Adapter");
69     }
70
71     /**
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.
76      *
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.
81      */
82     @Override
83     public void createTenant (String cloudSiteId,
84                               String tenantName,
85                               Map <String, String> metadata,
86                               Boolean failIfExists,
87                               Boolean backout,
88                               MsoRequest msoRequest,
89                               Holder <String> tenantId,
90                               Holder <TenantRollback> rollback) throws TenantException {
91         MsoLogger.setLogContext (msoRequest);
92
93         logger.debug("Call to MSO createTenant adapter. Creating Tenant: {} in {}", tenantName, cloudSiteId);
94
95         // Start building up rollback object
96         TenantRollback tenantRollback = new TenantRollback ();
97         tenantRollback.setCloudId (cloudSiteId);
98         tenantRollback.setMsoRequest (msoRequest);
99         
100         MsoTenantUtils tUtils;
101                 try {
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);
107                 }
108
109         MsoTenant newTenant = null;
110         String newTenantId;
111         try {
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);
117         }
118         if (newTenant == null) {
119             if (backout == null)
120                 backout = true;
121             try {
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);
126             }
127             tenantRollback.setTenantId (newTenantId);
128             tenantRollback.setTenantCreated (true);
129             logger.debug ("Tenant {} successfully created with ID {}", tenantName, newTenantId);
130         } else {
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 ());
135             }
136
137             newTenantId = newTenant.getTenantId ();
138             tenantRollback.setTenantCreated (false);
139             logger.debug("Tenant {} already exists with ID {}", tenantName, newTenantId);
140         }
141
142
143         tenantId.value = newTenantId;
144         rollback.value = tenantRollback;
145         return;
146     }
147
148     @Override
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);
157
158         MsoTenantUtils tUtils;
159                 try {
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);
165                 }
166         
167         MsoTenant qTenant = null;
168         try {
169             qTenant = tUtils.queryTenant (tenantNameOrId, cloudSiteId);
170             if (qTenant == null) {
171                 // Not found by ID, Try by name.
172                 qTenant = tUtils.queryTenantByName (tenantNameOrId, cloudSiteId);
173             }
174
175             if (qTenant == null) {
176                 logger.debug ("QueryTenant: Tenant {} not found", tenantNameOrId);
177                 tenantId.value = null;
178                 tenantName.value = null;
179                 metadata.value = null;
180             } else {
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 ();
185             }
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);
190         }
191         return;
192     }
193
194     @Override
195     public void deleteTenant (String cloudSiteId,
196                               String tenantId,
197                               MsoRequest msoRequest,
198                               Holder <Boolean> tenantDeleted) throws TenantException {
199         MsoLogger.setLogContext (msoRequest);
200
201         logger.debug ("Deleting Tenant {} in {}", tenantId, cloudSiteId);
202
203         // Delete the Tenant.
204         try {
205                 
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);
213         }
214
215         // On success, nothing is returned.
216         return;
217     }
218
219     /**
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.
224      *
225      * The rollback includes removing the VNF and deleting the tenant if the
226      * tenant did not exist prior to the VNF creation.
227      */
228     @Override
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());
234             return;
235         }
236
237         // Get the elements of the VnfRollback object for easier access
238         String cloudSiteId = rollback.getCloudId ();
239         String tenantId = rollback.getTenantId ();
240
241         MsoLogger.setLogContext (rollback.getMsoRequest ());
242         logger.debug("Rolling Back Tenant {} in {}", rollback.getTenantId(), cloudSiteId);
243
244         if (rollback.getTenantCreated ()) {
245             try {
246                  
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);
255             }
256         }
257         return;
258     }
259 }