to correct the incorrect
[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  * 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.common.util;
21
22 import org.camunda.bpm.engine.delegate.DelegateExecution;
23 import org.onap.so.bpmn.core.UrnPropertiesReader;
24 import org.onap.so.db.catalog.beans.CloudSite;
25 import org.onap.so.db.catalog.beans.HomingInstance;
26 import org.onap.so.db.catalog.client.CatalogDbClient;
27 import org.onap.so.logger.MsoLogger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Component;
30
31 import java.util.Arrays;
32 import java.util.Optional;
33
34 public class OofInfraUtils {
35
36     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, OofInfraUtils.class);
37
38     /**
39      * This method creates a cloudsite in catalog database.
40      *
41      * @param cloudSite
42      *
43      * @return void
44      */
45     public void createCloudSite(CloudSite cloudSite, DelegateExecution execution) {
46         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
47         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
48         Optional <CloudSite> optCloudsite = Optional.empty();
49
50         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
51         try {
52             optCloudsite = Optional.ofNullable(client.getCloudSite(cloudSite.getId(), endpoint + "/cloudSite/"));
53         } catch (Exception e) {
54             LOGGER.debug("Could not find cloudsite : " + cloudSite.getId());
55             LOGGER.debug("Creating cloudSite: " + cloudSite.toString());
56         }
57         if (optCloudsite.isPresent() && (cloudSite.getId()) != optCloudsite.get().getId()) {
58             client.postCloudSite(cloudSite);
59         }
60     }
61
62     /**
63      * This method creates a HomingInstance in catalog database.
64      *
65      * @param homingInstance
66      *
67      * @return void
68      */
69     public void createHomingInstance(HomingInstance homingInstance, DelegateExecution execution) {
70         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
71         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
72
73         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
74         try {
75             client.postHomingInstance(homingInstance);
76         } catch (Exception exception) {
77             LOGGER.debug("Could not create HomingInstance : " + homingInstance.getServiceInstanceId());
78             LOGGER.debug("HomingInstance Creation Error: " + exception);
79         }
80
81     }
82
83     /**
84      * This method gets a HomingInstance in catalog database.
85      *
86      * @param serviceInstanceId
87      *
88      * @return HomingInstance
89      */
90     public HomingInstance getHomingInstance(String serviceInstanceId, DelegateExecution execution) {
91         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
92         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
93
94         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
95         try {
96             return client.getHomingInstance(serviceInstanceId, endpoint + "/homingInstance/");
97         } catch (Exception exception) {
98             LOGGER.debug("Could not get HomingInstance for serviceInstanceId : " + serviceInstanceId);
99             LOGGER.debug("Get HomingInstance Error: " + exception);
100         }
101         return null;
102     }
103 }