Fix CloudSite creation for OOF Homing
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / util / OofInfraUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018. Intel Corp. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.common.util;
23
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.onap.so.bpmn.core.UrnPropertiesReader;
26 import org.onap.so.db.catalog.beans.CloudSite;
27 import org.onap.so.db.catalog.beans.HomingInstance;
28 import org.onap.so.db.catalog.client.CatalogDbClient;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import java.util.Optional;
32
33 public class OofInfraUtils {
34
35     private static final Logger logger = LoggerFactory.getLogger(OofInfraUtils.class);
36
37     /**
38      * This method creates a cloudsite in catalog database.
39      *
40      * @param cloudSite
41      *
42      * @return void
43      */
44     public void createCloudSite(CloudSite cloudSite, DelegateExecution execution) {
45         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
46         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
47         try {
48             CloudSite getCloudsite;
49
50             CatalogDbClient client = new CatalogDbClient(endpoint, auth);
51
52             getCloudsite = Optional.ofNullable(client.getCloudSite(cloudSite.getId(), endpoint + "/cloudSite/"))
53                     .orElse(new CloudSite());
54             if (!cloudSite.getId().equals(getCloudsite.getId())) {
55                 client.postOofHomingCloudSite(cloudSite);
56                 logger.debug("Did not findd cloudsite : {}", cloudSite.getId());
57                 logger.debug("Will create cloudSite: {}", cloudSite.toString());
58             } else {
59                 logger.debug("Found cloudsite : {}", cloudSite.getId());
60                 logger.debug("Will not create cloudSite: {}", cloudSite.toString());
61             }
62         } catch (Exception e) {
63             logger.debug("Error looking up or creating cloudsite : {}", cloudSite.getId());
64             logger.debug("CloudSite Lookup/Creation Error: " + e);
65         }
66
67
68     }
69
70     /**
71      * This method creates a HomingInstance in catalog database.
72      *
73      * @param homingInstance
74      *
75      * @return void
76      */
77     public void createHomingInstance(HomingInstance homingInstance, DelegateExecution execution) {
78         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
79         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
80
81         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
82         try {
83             client.postHomingInstance(homingInstance);
84         } catch (Exception exception) {
85             logger.debug("Could not create HomingInstance : {}", homingInstance.getServiceInstanceId());
86             logger.debug("HomingInstance Creation Error: {}", exception);
87         }
88
89     }
90
91     /**
92      * This method gets a HomingInstance in catalog database.
93      *
94      * @param serviceInstanceId
95      *
96      * @return HomingInstance
97      */
98     public HomingInstance getHomingInstance(String serviceInstanceId, DelegateExecution execution) {
99         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
100         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
101
102         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
103         try {
104             return client.getHomingInstance(serviceInstanceId, endpoint + "/homingInstance/");
105         } catch (Exception exception) {
106             logger.debug("Could not get HomingInstance for serviceInstanceId : {}", serviceInstanceId);
107             logger.debug("Get HomingInstance Error: {}", exception);
108         }
109         return null;
110     }
111 }