6b178bd215941aeb0b9b99e110c5d0ea7d25f501
[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
32 import java.util.Optional;
33
34 public class OofInfraUtils {
35
36     private static final Logger logger = LoggerFactory.getLogger(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         try {
49             CloudSite getCloudsite;
50
51             CatalogDbClient client = new CatalogDbClient(endpoint, auth);
52
53             getCloudsite = Optional.ofNullable(client.getCloudSite(cloudSite.getId(), endpoint + "/cloudSite/")).orElse(new CloudSite());
54             if (!cloudSite.getId().equals(getCloudsite.getId())) {
55                 client.postCloudSite(cloudSite);
56                 logger.debug("Did not findd cloudsite : {}", cloudSite.getId());
57                 logger.debug("Will create cloudSite: {}", cloudSite.toString());
58             }
59             else {
60                 logger.debug("Found cloudsite : {}", cloudSite.getId());
61                 logger.debug("Will not create cloudSite: {}", cloudSite.toString());
62             }
63         } catch (Exception e) {
64             logger.debug("Error looking up or creating cloudsite : {}", cloudSite.getId());
65             logger.debug("CloudSite Lookup/Creation Error: " + e);
66         }
67
68
69     }
70
71     /**
72      * This method creates a HomingInstance in catalog database.
73      *
74      * @param homingInstance
75      *
76      * @return void
77      */
78     public void createHomingInstance(HomingInstance homingInstance, DelegateExecution execution) {
79         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
80         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
81
82         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
83         try {
84             client.postHomingInstance(homingInstance);
85         } catch (Exception exception) {
86             logger.debug("Could not create HomingInstance : {}", homingInstance.getServiceInstanceId());
87             logger.debug("HomingInstance Creation Error: {}", exception);
88         }
89
90     }
91
92     /**
93      * This method gets a HomingInstance in catalog database.
94      *
95      * @param serviceInstanceId
96      *
97      * @return HomingInstance
98      */
99     public HomingInstance getHomingInstance(String serviceInstanceId, DelegateExecution execution) {
100         String endpoint = UrnPropertiesReader.getVariable("mso.catalog.db.spring.endpoint", execution);
101         String auth = UrnPropertiesReader.getVariable("mso.db.auth", execution);
102
103         CatalogDbClient client = new CatalogDbClient(endpoint, auth);
104         try {
105             return client.getHomingInstance(serviceInstanceId, endpoint + "/homingInstance/");
106         } catch (Exception exception) {
107             logger.debug("Could not get HomingInstance for serviceInstanceId : {}", serviceInstanceId);
108             logger.debug("Get HomingInstance Error: {}", exception);
109         }
110         return null;
111     }
112 }