bba883e727e901ddfc38474acc6d0bbe90c1926e
[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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import java.util.Optional;
23
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.onap.so.client.exception.ExceptionBuilder;
26 import org.onap.so.db.catalog.client.CatalogDbClient;
27 import org.onap.so.db.catalog.beans.CloudSite;
28 import org.onap.so.logger.MsoLogger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class CloudSiteCatalogUtils {
34
35         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CloudSiteCatalogUtils.class);
36         @Autowired
37         private ExceptionBuilder exceptionUtil;
38         
39         @Autowired
40         private CatalogDbClient catalogDbClient;
41         
42         
43         public void getIdentityUrlFromCloudSite(DelegateExecution execution) {
44                 String cloudRegionId = (String) execution.getVariable("lcpCloudRegionId");
45         
46                 if (cloudRegionId != null) {
47                         Optional<CloudSite> cloudSite = getCloudSite(cloudRegionId);
48                         if (!cloudSite.isPresent()) {
49                                 msoLogger.debug("Cloud Region with cloudRegionId " + cloudRegionId + " not found in Catalog DB");
50                                 exceptionUtil.buildAndThrowWorkflowException(execution, 404, "Cloud Region with cloudRegionId " + cloudRegionId + " not found in Catalog DB");
51                         }
52                         
53                         if (cloudSite.get().getIdentityService() == null)        {
54                                 msoLogger.debug("No identityService found for Cloud Region with cloudRegionId " + cloudRegionId + " in Catalog DB");
55                                 exceptionUtil.buildAndThrowWorkflowException(execution, 404, "No identityService found for Cloud Region with cloudRegionId " + cloudRegionId + " in Catalog DB");
56                         }                       
57                         String identityUrl = cloudSite.get().getIdentityService().getIdentityUrl();
58                         
59                         msoLogger.debug("identityUrl from Catalog DB is: " + identityUrl);
60                         execution.setVariable("identityUrl", identityUrl);
61                 }
62         }
63         
64         protected Optional<CloudSite> getCloudSite(String id) {
65                 if (id == null) {
66                         return Optional.empty();
67                 }
68                 CloudSite cloudSite = catalogDbClient.getCloudSite(id);
69
70                 if (cloudSite != null) {
71                         return Optional.of(cloudSite);
72                 } else {
73                         return(Optional.of(catalogDbClient.getCloudSiteByClliAndAicVersion(id,"2.5")));                 
74                 }
75         }
76 }