Remove ECOMP in headers
[clamp.git] / src / main / java / org / onap / clamp / clds / client / SdcSendReqDelegate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * 
22  */
23
24 package org.onap.clamp.clds.client;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.io.IOException;
30 import java.security.GeneralSecurityException;
31 import java.util.List;
32
33 import org.apache.camel.Exchange;
34 import org.apache.camel.Handler;
35 import org.apache.commons.codec.DecoderException;
36 import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices;
37 import org.onap.clamp.clds.client.req.sdc.SdcRequests;
38 import org.onap.clamp.clds.config.ClampProperties;
39 import org.onap.clamp.clds.model.DcaeEvent;
40 import org.onap.clamp.clds.model.properties.Global;
41 import org.onap.clamp.clds.model.properties.ModelProperties;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * Send control loop model to dcae proxy.
47  */
48 @Component
49 public class SdcSendReqDelegate {
50
51     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcSendReqDelegate.class);
52     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
53     @Autowired
54     private SdcCatalogServices sdcCatalogServices;
55     @Autowired
56     private SdcRequests sdcReq;
57     @Autowired
58     private ClampProperties refProp;
59
60     /**
61      * Perform activity. Send to sdc proxy.
62      *
63      * @param camelExchange
64      *            The camel object that contains all fields
65      * @throws DecoderException
66      *             In case of issues with password decryption
67      * @throws GeneralSecurityException
68      *             In case of issues with password decryption
69      * @throws IOException
70      *             In case of issues with file opening
71      */
72     @Handler
73     public void execute(Exchange camelExchange) throws GeneralSecurityException, DecoderException, IOException {
74         String controlName = (String) camelExchange.getProperty("controlName");
75         String artifactLabel = sdcReq
76                 .normalizeResourceInstanceName(refProp.getStringValue("sdc.artifactLabel") + "-" + controlName);
77         String locationArtifactLabel = sdcReq
78                 .normalizeResourceInstanceName(refProp.getStringValue("sdc.locationArtifactLabel") + "-" + controlName);
79         String artifactType = refProp.getStringValue("sdc.artifactType");
80         String locationArtifactType = refProp.getStringValue("sdc.locationArtifactType");
81         String userid = (String) camelExchange.getProperty("userid");
82         String docText = (String) camelExchange.getProperty("docText");
83         String artifactName = (String) camelExchange.getProperty("controlName") + DcaeEvent.ARTIFACT_NAME_SUFFIX;
84         camelExchange.setProperty("artifactName", artifactName);
85         ModelProperties prop = ModelProperties.create(camelExchange);
86         String bluprintPayload;
87         bluprintPayload = sdcReq.formatBlueprint(prop, docText);
88         // no need to upload blueprint for Holmes, thus blueprintPayload for
89         // Holmes is empty
90         if (!bluprintPayload.isEmpty()) {
91             String formattedSdcReq = sdcReq.formatSdcReq(bluprintPayload, artifactName, artifactLabel, artifactType);
92             if (formattedSdcReq != null) {
93                 camelExchange.setProperty("formattedArtifactReq", formattedSdcReq.getBytes());
94             }
95             Global globalProps = prop.getGlobal();
96             if (globalProps != null && globalProps.getService() != null) {
97                 String serviceInvariantUUID = globalProps.getService();
98                 camelExchange.setProperty("serviceInvariantUUID", serviceInvariantUUID);
99             }
100             List<String> sdcReqUrlsList = sdcReq.getSdcReqUrlsList(prop);
101             String sdcLocationsPayload = sdcReq.formatSdcLocationsReq(prop, artifactName);
102             String locationArtifactName = (String) camelExchange.getProperty("controlName") + "-location.json";
103             String formattedSdcLocationReq = sdcReq.formatSdcReq(sdcLocationsPayload, locationArtifactName,
104                     locationArtifactLabel, locationArtifactType);
105             if (formattedSdcLocationReq != null) {
106                 camelExchange.setProperty("formattedLocationReq", formattedSdcLocationReq.getBytes());
107             }
108             sdcCatalogServices.uploadToSdc(prop, userid, sdcReqUrlsList, formattedSdcReq, formattedSdcLocationReq,
109                     artifactName, locationArtifactName);
110         }
111     }
112 }