8a1654785cdc93f45fe0d128ca442900109ac9f6
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / SdcPackageProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
17
18 import com.google.gson.Gson;
19 import com.google.gson.JsonObject;
20 import org.apache.http.HttpEntity;
21 import org.apache.http.client.methods.CloseableHttpResponse;
22 import org.apache.http.client.methods.HttpGet;
23 import org.apache.http.impl.client.CloseableHttpClient;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
28 import org.slf4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.context.annotation.Conditional;
32 import org.springframework.stereotype.Component;
33 import org.yaml.snakeyaml.Yaml;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.InputStream;
37
38 import static com.google.common.io.ByteStreams.toByteArray;
39 import static java.lang.String.format;
40 import static org.apache.http.HttpHeaders.ACCEPT;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.*;
43 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
44 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
45 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getVnfdLocation;
46 import static org.slf4j.LoggerFactory.getLogger;
47 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
48
49 /**
50  * Responsible for providing access to AAI APIs.
51  * Handles authentication and mandatory parameters.
52  */
53 @Component
54 @Conditional(value = Conditions.UseForDirect.class)
55 public class SdcPackageProvider implements IPackageProvider {
56     private static final String SDC_MSB_NAME = "sdc";
57     private static final String SDC_MSB_VERSION = "v1";
58     private static final String GET_PACKAGE_URL = "%s/sdc/v1/catalog/resources/%s/toscaModel";
59     private static Logger logger = getLogger(SdcPackageProvider.class);
60     private final MsbApiProvider msbApiProvider;
61     private final DriverProperties driverProperties;
62     @Value("${sdcUsername}")
63     private String sdcUsername;
64     @Value("${sdcPassword}")
65     private String sdcPassword;
66
67     @Autowired
68     SdcPackageProvider(MsbApiProvider msbApiProvider, DriverProperties driverProperties) {
69         this.msbApiProvider = msbApiProvider;
70         this.driverProperties = driverProperties;
71     }
72
73     @Override
74     public byte[] getPackage(String csarId) {
75         String baseUrl = msbApiProvider.getMicroServiceUrl(SDC_MSB_NAME, SDC_MSB_VERSION);
76         try {
77             CloseableHttpClient client = systemFunctions().getHttpClient();
78             HttpGet httpget = new HttpGet(format(GET_PACKAGE_URL, baseUrl, csarId));
79             httpget.setHeader(ACCEPT, APPLICATION_OCTET_STREAM_VALUE);
80             httpget.setHeader("X-ECOMP-InstanceID", driverProperties.getVnfmId());
81             httpget.setHeader("X-FromAppId", SERVICE_NAME);
82             CloseableHttpResponse response = client.execute(httpget);
83             HttpEntity entity = response.getEntity();
84             InputStream is = entity.getContent();
85             byte[] bytes = toByteArray(is);
86             client.close();
87             return bytes;
88         } catch (Exception e) {
89             throw buildFatalFailure(logger, "Unable to download " + csarId + " package from SDC", e);
90         }
91     }
92
93     @Override
94     public String getCbamVnfdId(String csarId) {
95         byte[] onapPackage = getPackage(csarId);
96         try {
97             String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(onapPackage));
98             String onapVnfdContent = getFileInZip(new ByteArrayInputStream(onapPackage), vnfdLocation).toString();
99             JsonObject root = new Gson().toJsonTree(new Yaml().load(onapVnfdContent)).getAsJsonObject();
100             return childElement(child(root, "metadata"), "resourceVendorModelNumber").getAsString();
101         } catch (Exception e) {
102             throw buildFatalFailure(logger, "Unable to extract CBAM VNFD id from ONAP package", e);
103         }
104     }
105 }