Removed MsoLogger class
[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.ErrorCode;
37 import org.onap.so.logger.MessageEnum;
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
92         logger.debug("Call to MSO createTenant adapter. Creating Tenant: {} in {}", tenantName, cloudSiteId);
93
94         // Start building up rollback object
95         TenantRollback tenantRollback = new TenantRollback ();
96         tenantRollback.setCloudId (cloudSiteId);
97         tenantRollback.setMsoRequest (msoRequest);
98         
99         MsoTenantUtils tUtils;
100                 try {
101                         tUtils = tFactory.getTenantUtils (cloudSiteId);
102                 } catch (MsoCloudSiteNotFound me) {
103         logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
104             ErrorCode.DataError.getValue(), cloudSiteId, me);
105             throw new TenantException (me);
106                 }
107
108         MsoTenant newTenant = null;
109         String newTenantId;
110         try {
111             newTenant = tUtils.queryTenantByName (tenantName, cloudSiteId);
112         } catch (MsoException me) {
113             logger.error(OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR,
114                 ErrorCode.DataError.getValue(), me);
115             throw new TenantException (me);
116         }
117         if (newTenant == null) {
118             if (backout == null)
119                 backout = true;
120             try {
121                 newTenantId = tUtils.createTenant (tenantName, cloudSiteId, metadata, backout.booleanValue ());
122             } catch (MsoException me) {
123                 logger.error (OPENSTACK_COMMUNICATE_EXCEPTION_MSG, MessageEnum.RA_CREATE_TENANT_ERR, ErrorCode.DataError.getValue(), me);
124                 throw new TenantException (me);
125             }
126             tenantRollback.setTenantId (newTenantId);
127             tenantRollback.setTenantCreated (true);
128             logger.debug ("Tenant {} successfully created with ID {}", tenantName, newTenantId);
129         } else {
130             if (failIfExists != null && failIfExists) {
131                 logger.error("{} {} CreateTenant: Tenant {} already exists in {} ", MessageEnum.RA_TENANT_ALREADY_EXIST,
132                     ErrorCode.DataError.getValue(), tenantName, cloudSiteId);
133                 throw new TenantAlreadyExists (tenantName, cloudSiteId, newTenant.getTenantId ());
134             }
135
136             newTenantId = newTenant.getTenantId ();
137             tenantRollback.setTenantCreated (false);
138             logger.debug("Tenant {} already exists with ID {}", tenantName, newTenantId);
139         }
140
141
142         tenantId.value = newTenantId;
143         rollback.value = tenantRollback;
144         return;
145     }
146
147     @Override
148     public void queryTenant (String cloudSiteId,
149                              String tenantNameOrId,
150                              MsoRequest msoRequest,
151                              Holder <String> tenantId,
152                              Holder <String> tenantName,
153                              Holder <Map <String, String>> metadata) throws TenantException {
154
155         logger.debug ("Querying Tenant {} in {}", tenantNameOrId, cloudSiteId);
156
157         MsoTenantUtils tUtils;
158                 try {
159                         tUtils = tFactory.getTenantUtils (cloudSiteId);
160                 } catch (MsoCloudSiteNotFound me) {
161         logger.error("{} {} no implementation found for {}: ", MessageEnum.RA_CREATE_TENANT_ERR,
162             ErrorCode.DataError.getValue(), cloudSiteId, me);
163             throw new TenantException (me);
164                 }
165         
166         MsoTenant qTenant = null;
167         try {
168             qTenant = tUtils.queryTenant (tenantNameOrId, cloudSiteId);
169             if (qTenant == null) {
170                 // Not found by ID, Try by name.
171                 qTenant = tUtils.queryTenantByName (tenantNameOrId, cloudSiteId);
172             }
173
174             if (qTenant == null) {
175                 logger.debug ("QueryTenant: Tenant {} not found", tenantNameOrId);
176                 tenantId.value = null;
177                 tenantName.value = null;
178                 metadata.value = null;
179             } else {
180                 logger.debug("QueryTenant: Tenant {} found with ID {}", tenantNameOrId, qTenant.getTenantId());
181                 tenantId.value = qTenant.getTenantId ();
182                 tenantName.value = qTenant.getTenantName ();
183                 metadata.value = qTenant.getMetadata ();
184             }
185         } catch (MsoException me) {
186             logger.error("Exception in queryTenant for {}: ", MessageEnum.RA_GENERAL_EXCEPTION,
187                 ErrorCode.DataError.getValue(), tenantNameOrId, me);
188             throw new TenantException (me);
189         }
190         return;
191     }
192
193     @Override
194     public void deleteTenant (String cloudSiteId,
195                               String tenantId,
196                               MsoRequest msoRequest,
197                               Holder <Boolean> tenantDeleted) throws TenantException {
198
199         logger.debug ("Deleting Tenant {} in {}", tenantId, cloudSiteId);
200
201         // Delete the Tenant.
202         try {
203                 
204                 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
205             boolean deleted = tUtils.deleteTenant (tenantId, cloudSiteId);
206             tenantDeleted.value = deleted;
207         } catch (MsoException me) {
208             logger.error("{} {} Exception - DeleteTenant {}: ", MessageEnum.RA_DELETE_TEMAMT_ERR,
209                 ErrorCode.DataError.getValue(), tenantId, me);
210             throw new TenantException (me);
211         }
212
213         // On success, nothing is returned.
214         return;
215     }
216
217     /**
218      * This web service endpoint will rollback a previous Create VNF operation.
219      * A rollback object is returned to the client in a successful creation
220      * response. The client can pass that object as-is back to the rollbackVnf
221      * operation to undo the creation.
222      *
223      * The rollback includes removing the VNF and deleting the tenant if the
224      * tenant did not exist prior to the VNF creation.
225      */
226     @Override
227     public void rollbackTenant (TenantRollback rollback) throws TenantException {
228         // rollback may be null (e.g. if stack already existed when Create was called)
229         if (rollback == null) {
230             logger.warn("{} {} rollbackTenant, rollback is null", MessageEnum.RA_ROLLBACK_NULL,
231                 ErrorCode.DataError.getValue());
232             return;
233         }
234
235         // Get the elements of the VnfRollback object for easier access
236         String cloudSiteId = rollback.getCloudId ();
237         String tenantId = rollback.getTenantId ();
238
239         logger.debug("Rolling Back Tenant {} in {}", rollback.getTenantId(), cloudSiteId);
240
241         if (rollback.getTenantCreated ()) {
242             try {
243                  
244                 MsoTenantUtils tUtils = tFactory.getTenantUtils (cloudSiteId);
245                 tUtils.deleteTenant (tenantId, cloudSiteId);
246             } catch (MsoException me) {
247                 me.addContext (ROLLBACK_TENANT);
248                 // Failed to delete the tenant.
249                 logger.error("{} {} Exception - rollbackTenant {}: ", MessageEnum.RA_ROLLBACK_TENANT_ERR,
250                     ErrorCode.DataError.getValue(), tenantId, me);
251                 throw new TenantException (me);
252             }
253         }
254         return;
255     }
256 }