aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-nfvo-ns-lcm/so-etsi-nfvo-ns-lcm-bpmn-flows/src/main/java/org/onap/so/etsi/nfvo/ns/lcm/bpmn/flows/nsd/parser/NetworkServiceDescriptorParser.java
blob: cc9223475060c353e58e9b7e25adb3363b5d4b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.parser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.FileEntry;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.NetworkServiceDescriptor;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.ToscaMetadata;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.VirtualNetworkFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@Service
public class NetworkServiceDescriptorParser {
    public static final String NS_NODE_TYPE = "tosca.nodes.nfv.NS";
    private static final String NODE_TYPE = "node_type";
    private static final String SUBSTITUTION_MAPPINGS = "substitution_mappings";
    private static final Logger logger = LoggerFactory.getLogger(NetworkServiceDescriptorParser.class);
    private static final String VNF_TYPE = "tosca.nodes.nfv.VNF";
    private static final String PROPERTIES = "properties";
    private static final String TYPE = "type";
    private static final String NODE_TEMPLATES = "node_templates";
    private static final String TOPOLOGY_TEMPLATE = "topology_template";
    private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
    private static final String TOSCA_META_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
    private final ToscaMetadataParser toscaMetadataParser;
    private final FileParser fileParser;

    @Autowired
    public NetworkServiceDescriptorParser(final ToscaMetadataParser toscaMetadataParser, final FileParser fileParser) {
        this.toscaMetadataParser = toscaMetadataParser;
        this.fileParser = fileParser;
    }

    public Optional<NetworkServiceDescriptor> parse(final byte[] zipBytes) {
        try {
            final Map<String, FileEntry> files = getZipContent(zipBytes);
            if (isMetaFilePresent(files)) {
                final Optional<ToscaMetadata> optional =
                        toscaMetadataParser.parse(files.get(TOSCA_META_PATH_FILE_NAME));
                if (optional.isPresent()) {
                    final ToscaMetadata toscaMetadata = optional.get();
                    logger.info("Parsed ToscaMetadata {}", toscaMetadata);
                    final String entryDefinitionFile = toscaMetadata.getEntry(ENTRY_DEFINITIONS);
                    if (entryDefinitionFile != null && files.containsKey(entryDefinitionFile)) {
                        final Map<String, Object> fileContent =
                                fileParser.getFileContent(files.get(entryDefinitionFile));
                        final Map<String, Object> topologyTemplates = getTopologyTemplates(fileContent);
                        final Map<String, Object> nodeTemplates = getNodeTemplates(topologyTemplates);

                        final Optional<NetworkServiceDescriptor> nsdOptional =
                                getNetworkServiceDescriptor(topologyTemplates);
                        if (nsdOptional.isPresent()) {
                            final NetworkServiceDescriptor networkServiceDescriptor = nsdOptional.get();
                            networkServiceDescriptor.setVnfs(getVirtualNetworkFunctions(nodeTemplates));
                            return Optional.of(networkServiceDescriptor);
                        }

                    }
                }

            }

            logger.error("Unable to find {} file in {}", TOSCA_META_PATH_FILE_NAME, files);
        } catch (final Exception exception) {
            logger.error("Unable to parse nsd zip content", exception);
        }
        return Optional.empty();
    }

    @SuppressWarnings("unchecked")
    private Optional<NetworkServiceDescriptor> getNetworkServiceDescriptor(
            final Map<String, Object> topologyTemplates) {
        final Map<String, Object> substitutionMappings =
                (Map<String, Object>) topologyTemplates.get(SUBSTITUTION_MAPPINGS);
        final Object nodeType = substitutionMappings.get(NODE_TYPE);
        if (substitutionMappings != null && NS_NODE_TYPE.equals(nodeType)) {
            final NetworkServiceDescriptor networkServiceDescriptor = new NetworkServiceDescriptor();
            networkServiceDescriptor.setType(nodeType.toString());
            networkServiceDescriptor.setProperties((Map<String, Object>) substitutionMappings.get(PROPERTIES));
            return Optional.of(networkServiceDescriptor);
        }
        logger.error("No {} found in fileContent: {}", SUBSTITUTION_MAPPINGS, topologyTemplates);

        return Optional.empty();
    }

    private List<VirtualNetworkFunction> getVirtualNetworkFunctions(final Map<String, Object> nodeTemplates) {
        final List<VirtualNetworkFunction> vnfs = new ArrayList<>();
        for (final Entry<String, Object> entry : nodeTemplates.entrySet()) {
            @SuppressWarnings("unchecked")
            final Map<String, Object> entryValue = (Map<String, Object>) entry.getValue();
            final Object type = entryValue.get(TYPE);
            if (type != null && type.equals(VNF_TYPE)) {
                @SuppressWarnings("unchecked")
                final Map<String, Object> vnfProperties = (Map<String, Object>) entryValue.get(PROPERTIES);
                final VirtualNetworkFunction vnf = new VirtualNetworkFunction();
                vnf.setVnfName(entry.getKey());

                if (vnfProperties != null && !vnfProperties.isEmpty()) {
                    final Object vnfDescriptorId = vnfProperties.get("descriptor_id");
                    @SuppressWarnings("unchecked")
                    final List<String> vnfmInfoList = (List<String>) vnfProperties.get("vnfm_info");
                    if (vnfDescriptorId != null && vnfmInfoList != null) {
                        vnf.setVnfmInfoList(vnfmInfoList);
                        vnf.setVnfdId(vnfDescriptorId.toString());
                        vnf.setProperties(vnfProperties);
                        vnfs.add(vnf);
                    } else {
                        logger.warn("descriptor_id missing {}", entryValue);
                    }
                }
            }

        }
        return vnfs;
    }

    private Map<String, Object> getNodeTemplates(final Map<String, Object> topologyTemplates) {
        @SuppressWarnings("unchecked")
        final Map<String, Object> nodeTemplates = (Map<String, Object>) topologyTemplates.get(NODE_TEMPLATES);
        if (nodeTemplates != null) {
            logger.debug("Found nodeTemplates: {}", topologyTemplates);
            return nodeTemplates;
        }
        logger.error("No {} found in fileContent: {}", NODE_TEMPLATES, topologyTemplates);
        return Collections.emptyMap();
    }

    private Map<String, Object> getTopologyTemplates(final Map<String, Object> fileContent) {
        @SuppressWarnings("unchecked")
        final Map<String, Object> topologyTemplates = (Map<String, Object>) fileContent.get(TOPOLOGY_TEMPLATE);
        if (topologyTemplates != null) {
            logger.debug("Found {}: {}", TOPOLOGY_TEMPLATE, topologyTemplates);

            return topologyTemplates;
        }
        logger.error("No {} found in fileContent: {}", TOPOLOGY_TEMPLATE, fileContent);
        return Collections.emptyMap();
    }

    private boolean isMetaFilePresent(final Map<String, FileEntry> files) {
        return files.containsKey(TOSCA_META_PATH_FILE_NAME);
    }

    private Map<String, FileEntry> getZipContent(final byte[] zipBytes) {
        final Map<String, FileEntry> files = new HashMap<>();
        try (final ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));) {
            ZipEntry zipEntry;
            while ((zipEntry = inputZipStream.getNextEntry()) != null) {
                logger.info("{} : {}", zipEntry.getName(), zipEntry.isDirectory());
                if (files.get(zipEntry.getName()) != null) {
                    logger.warn("{} File entry already exists ...", zipEntry.getName());
                } else {
                    final FileEntry fileEntry = new FileEntry().filePath(zipEntry.getName())
                            .fileContent(getBytes(inputZipStream)).isDirectory(zipEntry.isDirectory());
                    files.put(zipEntry.getName(), fileEntry);

                }

            }
            return files;
        } catch (final Exception exception) {
            logger.error("Unable to parse nsd zip content", exception);
            return Collections.emptyMap();
        }
    }

    private byte[] getBytes(final ZipInputStream inputZipStream) throws IOException {
        try {
            return IOUtils.toByteArray(inputZipStream);
        } catch (final IOException exception) {
            logger.error("Could not read bytes from file", exception);
            throw exception;
        }
    }


}