Calls to/from VNFM fail
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / SdcPackageProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  *  Modifications Copyright (c) 2019 Samsung
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.vnfmadapter.extclients;
24
25 import static com.google.common.base.Splitter.on;
26 import static com.google.common.collect.Iterables.filter;
27 import static com.google.common.io.ByteStreams.toByteArray;
28 import static java.lang.String.format;
29 import static org.apache.http.HttpHeaders.ACCEPT;
30 import static org.apache.http.HttpHeaders.AUTHORIZATION;
31 import static org.onap.so.adapters.vnfmadapter.NvfmAdapterUtils.abortOperation;
32 import static org.onap.so.adapters.vnfmadapter.NvfmAdapterUtils.child;
33 import static org.onap.so.adapters.vnfmadapter.NvfmAdapterUtils.childElement;
34 import static org.onap.so.adapters.vnfmadapter.NvfmAdapterUtils.children;
35 import static org.slf4j.LoggerFactory.getLogger;
36 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
37 import com.google.common.io.ByteStreams;
38 import com.google.gson.Gson;
39 import com.google.gson.JsonObject;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.charset.StandardCharsets;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.NoSuchElementException;
49 import java.util.Set;
50 import java.util.zip.ZipEntry;
51 import java.util.zip.ZipInputStream;
52 import org.apache.commons.codec.binary.Base64;
53 import org.apache.http.HttpEntity;
54 import org.apache.http.client.methods.CloseableHttpResponse;
55 import org.apache.http.client.methods.HttpGet;
56 import org.apache.http.impl.client.CloseableHttpClient;
57 import org.apache.http.impl.client.HttpClients;
58 import org.onap.so.utils.CryptoUtils;
59 import org.slf4j.Logger;
60 import org.springframework.beans.factory.annotation.Value;
61 import org.springframework.stereotype.Component;
62 import org.yaml.snakeyaml.Yaml;
63
64 @Component
65 public class SdcPackageProvider {
66     private static final String GET_PACKAGE_URL = "%s/sdc/v1/catalog/resources/%s/toscaModel";
67     @Value("${sdc.toscametapath:TOSCA-Metadata/TOSCA.meta}")
68     private List<String> toscaMetaPaths;
69     private static final String TOSCA_VNFD_KEY = "Entry-Definitions";
70     private static Logger logger = getLogger(SdcPackageProvider.class);
71
72     @Value("${sdc.username}")
73     private String sdcUsername;
74     @Value("${sdc.password}")
75     private String sdcPassword;
76     @Value("${sdc.key}")
77     private String sdcKey;
78     @Value("${sdc.endpoint}")
79     private String baseUrl;
80
81     public String getVnfdId(final String csarId) {
82         return getVnfNodeProperty(csarId, "descriptor_id");
83     }
84
85     private String getVnfNodeProperty(final String csarId, final String propertyName) {
86         logger.debug("Getting " + propertyName + " from " + csarId);
87         final byte[] onapPackage = getPackage(csarId);
88
89         try {
90             final String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(onapPackage));
91             final String onapVnfdContent = getFileInZip(new ByteArrayInputStream(onapPackage), vnfdLocation).toString();
92             logger.debug("VNFD CONTENTS: " + onapVnfdContent);
93             final JsonObject root = new Gson().toJsonTree(new Yaml().load(onapVnfdContent)).getAsJsonObject();
94
95             final JsonObject topologyTemplates = child(root, "topology_template");
96             final JsonObject nodeTemplates = child(topologyTemplates, "node_templates");
97             for (final JsonObject child : children(nodeTemplates)) {
98                 final String type = childElement(child, "type").getAsString();
99                 String propertyValue = null;
100                 if ("tosca.nodes.nfv.VNF".equals(type)) {
101                     final JsonObject properties = child(child, "properties");
102                     logger.debug("properties: " + properties.toString());
103
104                     propertyValue = properties.get(propertyName).getAsJsonPrimitive().getAsString();
105                 }
106                 if (propertyValue == null) {
107                     propertyValue = getValueFromNodeTypeDefinition(root, type, propertyName);
108                 }
109                 return propertyValue;
110             }
111
112         } catch (final Exception e) {
113             throw new IllegalArgumentException("Unable to extract " + propertyName + " from ONAP package", e);
114         }
115         throw new IllegalArgumentException("Unable to extract " + propertyName + " from ONAP package");
116     }
117
118     private String getValueFromNodeTypeDefinition(final JsonObject root, final String nodeTypeName,
119             final String propertyName) {
120         final JsonObject nodeTypes = child(root, "node_types");
121         final JsonObject nodeType = child(nodeTypes, nodeTypeName);
122
123         if ("tosca.nodes.nfv.VNF".equals(childElement(nodeType, "derived_from").getAsString())) {
124             final JsonObject properties = child(nodeType, "properties");
125             logger.debug("properties: " + properties.toString());
126             final JsonObject property = child(properties, propertyName);
127             logger.debug("property: " + property.toString());
128             logger.debug("property default: " + childElement(property, "default").toString());
129             return childElement(property, "default").getAsJsonPrimitive().getAsString();
130         }
131         return null;
132     }
133
134     private byte[] getPackage(final String csarId) {
135         final String SERVICE_NAME = "vnfm-adapter";
136         try (CloseableHttpClient client = HttpClients.createDefault()) {
137             final HttpGet httpget = new HttpGet(format(GET_PACKAGE_URL, baseUrl, csarId));
138             httpget.setHeader(ACCEPT, APPLICATION_OCTET_STREAM_VALUE);
139             httpget.setHeader("X-ECOMP-InstanceID", SERVICE_NAME);
140             httpget.setHeader("X-FromAppId", SERVICE_NAME);
141             final String auth = sdcUsername + ":" + CryptoUtils.decrypt(sdcPassword, sdcKey);
142             final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
143             final String authHeader = "Basic " + new String(encodedAuth);
144             httpget.setHeader(AUTHORIZATION, authHeader);
145             logger.debug("Fetching from SDC: " + httpget);
146             final CloseableHttpResponse response = client.execute(httpget);
147             final HttpEntity entity = response.getEntity();
148             final InputStream is = entity.getContent();
149             return toByteArray(is);
150         } catch (final Exception e) {
151             throw abortOperation("Unable to download " + csarId + " package from SDC", e);
152         }
153     }
154
155     private String getVnfdLocation(final InputStream stream) throws IOException {
156         final Iterator<String> pathIterator = toscaMetaPaths.iterator();
157         while (pathIterator.hasNext()) {
158             final String toscaMetadata = new String(getFileInZip(stream, pathIterator.next()).toByteArray());
159             if (!toscaMetadata.isEmpty()) {
160                 final String toscaVnfdLine =
161                         filter(on("\n").split(toscaMetadata), line -> line.contains(TOSCA_VNFD_KEY)).iterator().next();
162                 return toscaVnfdLine.replace(TOSCA_VNFD_KEY + ":", "").trim();
163             }
164         }
165         throw abortOperation("Unable to find valid Tosca Path");
166     }
167
168     private static ByteArrayOutputStream getFileInZip(final InputStream zip, final String path) throws IOException {
169         final ZipInputStream zipInputStream = new ZipInputStream(zip);
170         final ByteArrayOutputStream fileContent = getFileInZip(zipInputStream, path);
171         zipInputStream.close();
172         return fileContent;
173     }
174
175     private static ByteArrayOutputStream getFileInZip(final ZipInputStream zipInputStream, final String path)
176             throws IOException {
177         ZipEntry zipEntry;
178         final Set<String> items = new HashSet<>();
179         while ((zipEntry = zipInputStream.getNextEntry()) != null) {
180             items.add(zipEntry.getName());
181             if (zipEntry.getName().matches(path)) {
182                 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
183                 ByteStreams.copy(zipInputStream, byteArrayOutputStream);
184                 return byteArrayOutputStream;
185             }
186         }
187         logger.error("Unable to find the {} in archive found: {}", path, items);
188         throw new NoSuchElementException("Unable to find the " + path + " in archive found: " + items);
189     }
190
191     public String getFlavourId(final String csarId) {
192         return getVnfNodeProperty(csarId, "flavour_id");
193     }
194 }