[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / client / SdcSendReqDelegate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.client;
25
26 import org.onap.clamp.clds.client.req.SdcReq;
27 import org.onap.clamp.clds.model.CldsAsdcServiceDetail;
28 import org.onap.clamp.clds.model.DcaeEvent;
29 import org.onap.clamp.clds.model.prop.Global;
30 import org.onap.clamp.clds.model.prop.ModelProperties;
31 import org.onap.clamp.clds.model.refprop.RefProp;
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.camunda.bpm.engine.delegate.JavaDelegate;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37
38 import java.util.List;
39
40 /**
41  * Send control loop model to dcae proxy.
42  */
43 public class SdcSendReqDelegate implements JavaDelegate {
44     // currently uses the java.util.logging.Logger like the Camunda engine
45     private static final Logger logger = LoggerFactory.getLogger(SdcSendReqDelegate.class);
46
47     @Autowired
48     private RefProp refProp;
49
50     @Autowired
51     private SdcCatalogServices asdcCatalogServices;
52
53     private String baseUrl;
54     private String artifactType;
55     private String locationArtifactType;
56     private String artifactLabel;
57     private String locationArtifactLabel;
58
59     /**
60      * Perform activity.  Send to asdc proxy.
61      *
62      * @param execution
63      */
64     public void execute(DelegateExecution execution) throws Exception {
65         String userid = (String) execution.getVariable("userid");
66         logger.info("userid=" + userid);
67         String docText = (String) execution.getVariable("docText");
68         String artifactName = execution.getVariable("controlName") + DcaeEvent.ARTIFACT_NAME_SUFFIX;
69         execution.setVariable("artifactName", artifactName);
70         getAsdcAttributes();
71         ModelProperties prop = ModelProperties.create(execution);
72         String bluprintPayload = SdcReq.formatBlueprint(refProp, prop, docText);
73         String formatttedAsdcReq = SdcReq.formatAsdcReq(bluprintPayload, artifactName, artifactLabel, artifactType);
74         if (formatttedAsdcReq != null) {
75             execution.setVariable("formattedArtifactReq", formatttedAsdcReq.getBytes());
76         }
77         List<String> asdcReqUrlsList = SdcReq.getAsdcReqUrlsList(prop, baseUrl, asdcCatalogServices, execution);
78
79         String asdcLocationsPayload = SdcReq.formatAsdcLocationsReq(prop, artifactName);
80         String locationArtifactName = execution.getVariable("controlName") + "-location.json";
81         String formattedAsdcLocationReq = SdcReq.formatAsdcReq(asdcLocationsPayload, locationArtifactName, locationArtifactLabel, locationArtifactType);
82         if (formattedAsdcLocationReq != null) {
83             execution.setVariable("formattedLocationReq", formattedAsdcLocationReq.getBytes());
84         }
85         String serviceInvariantUUID = getServiceInvariantUUIDFromProps(prop);
86         uploadToAsdc(prop, serviceInvariantUUID, userid, asdcReqUrlsList, formatttedAsdcReq, formattedAsdcLocationReq, artifactName, locationArtifactName);
87     }
88
89     private String getServiceInvariantUUIDFromProps(ModelProperties props) {
90         String invariantUUID = "";
91         Global globalProps = props.getGlobal();
92         if (globalProps != null) {
93             if (globalProps.getService() != null) {
94                 invariantUUID = globalProps.getService();
95             }
96         }
97         return invariantUUID;
98     }
99
100     private void uploadToAsdc(ModelProperties prop, String serviceInvariantUUID, String userid, List<String> asdcReqUrlsList, String formatttedAsdcReq, String formattedAsdcLocationReq, String artifactName, String locationArtifactName) throws Exception {
101         logger.info("userid=" + userid);
102         if (asdcReqUrlsList != null && asdcReqUrlsList.size() > 0) {
103             for (String url : asdcReqUrlsList) {
104                 if (url != null) {
105                     String originalServiceUUID = asdcCatalogServices.getServiceUUIDFromServiceInvariantID(serviceInvariantUUID);
106                     logger.info("ServiceUUID used before upload in url:" + originalServiceUUID);
107                     String asdcServicesInformation = asdcCatalogServices.getAsdcServicesInformation(originalServiceUUID);
108                     CldsAsdcServiceDetail cldsAsdcServiceDetail = asdcCatalogServices.getCldsAsdcServiceDetailFromJson(asdcServicesInformation);
109                     String uploadedArtifactUUID = asdcCatalogServices.getArtifactIdIfArtifactAlreadyExists(cldsAsdcServiceDetail, artifactName);
110                     // Upload artifacts to asdc
111                     String updateUrl = uploadedArtifactUUID != null ? url + "/" + uploadedArtifactUUID : url;
112                     String responseStr = asdcCatalogServices.uploadArtifactToAsdc(prop, userid, updateUrl, formatttedAsdcReq);
113                     logger.info("value of asdc Response of uploading to asdc :" + responseStr);
114                     String updatedServiceUUID = asdcCatalogServices.getServiceUUIDFromServiceInvariantID(serviceInvariantUUID);
115                     if (!originalServiceUUID.equalsIgnoreCase(updatedServiceUUID)) {
116                         url = url.replace(originalServiceUUID, updatedServiceUUID);
117                     }
118                     logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUUID);
119                     asdcServicesInformation = asdcCatalogServices.getAsdcServicesInformation(updatedServiceUUID);
120                     cldsAsdcServiceDetail = asdcCatalogServices.getCldsAsdcServiceDetailFromJson(asdcServicesInformation);
121                     uploadedArtifactUUID = asdcCatalogServices.getArtifactIdIfArtifactAlreadyExists(cldsAsdcServiceDetail, locationArtifactName);
122                     //  To send location information also to asdc
123                     updateUrl = uploadedArtifactUUID != null ? url + "/" + uploadedArtifactUUID : url;
124                     responseStr = asdcCatalogServices.uploadArtifactToAsdc(prop, userid, updateUrl, formattedAsdcLocationReq);
125                     logger.info("value of asdc Response of uploading location to asdc :" + responseStr);
126                 }
127             }
128         }
129     }
130
131     private void getAsdcAttributes() {
132         baseUrl = refProp.getStringValue("asdc.serviceUrl");
133         artifactLabel = refProp.getStringValue("asdc.artifactLabel");
134         locationArtifactLabel = refProp.getStringValue("asdc.locationArtifactLabel");
135         artifactType = refProp.getStringValue("asdc.artifactType");
136         locationArtifactType = refProp.getStringValue("asdc.locationArtifactType");
137     }
138 }