Replaced all tabs with spaces in java and pom.xml
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / installer / VfResourceStructure.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.asdc.installer;
25
26 import java.io.IOException;
27 import java.io.UnsupportedEncodingException;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Map;
32 import org.onap.so.asdc.client.ASDCConfiguration;
33 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
34 import org.onap.so.asdc.util.ASDCNotificationLogging;
35 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
36 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
37 import org.onap.so.db.catalog.beans.Service;
38 import org.onap.so.db.catalog.beans.VnfResource;
39 import org.onap.sdc.api.IDistributionClient;
40 import org.onap.sdc.api.notification.IArtifactInfo;
41 import org.onap.sdc.api.notification.INotificationData;
42 import org.onap.sdc.api.notification.IResourceInstance;
43 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
44 import org.onap.so.logger.MessageEnum;
45 import com.fasterxml.jackson.core.JsonParseException;
46 import com.fasterxml.jackson.core.type.TypeReference;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * This structure exists to avoid having issues if the order of the vfResource/vfmodule artifact is not good (tree
54  * structure).
55  */
56 public class VfResourceStructure extends ResourceStructure {
57
58     protected static final Logger logger = LoggerFactory.getLogger(VfResourceStructure.class);
59
60     /**
61      * The list of VfModules defined for this resource.
62      */
63     private final List<VfModuleStructure> vfModulesStructureList;
64
65     /**
66      * The list of VfModulesMetadata defined for this resource.
67      */
68     private List<IVfModuleData> vfModulesMetadataList;
69
70     private VnfResource catalogVnfResource;
71
72     private NetworkResourceCustomization catalogNetworkResourceCustomization;
73
74     private AllottedResourceCustomization catalogResourceCustomization;
75
76     private Service catalogService;
77
78
79     public VfResourceStructure(INotificationData notificationdata, IResourceInstance resourceinstance) {
80         super(notificationdata, resourceinstance);
81         this.resourceType = ResourceType.VF_RESOURCE;
82         vfModulesStructureList = new LinkedList<>();
83     }
84
85     public void addArtifactToStructure(IDistributionClient distributionClient, IArtifactInfo artifactinfo,
86             IDistributionClientDownloadResult clientResult) throws UnsupportedEncodingException {
87         this.addArtifactToStructure(distributionClient, artifactinfo, clientResult, null);
88     }
89
90     public void addArtifactToStructure(IDistributionClient distributionClient, IArtifactInfo artifactinfo,
91             IDistributionClientDownloadResult clientResult, String modifiedHeatTemplate)
92             throws UnsupportedEncodingException {
93         VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactinfo, clientResult, modifiedHeatTemplate);
94         addArtifactByType(artifactinfo, clientResult, vfModuleArtifact);
95         if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifactinfo.getArtifactType())) {
96             logger.debug("VF_MODULE_ARTIFACT: " + new String(clientResult.getArtifactPayload(), "UTF-8"));
97             logger.debug(ASDCNotificationLogging.dumpVfModuleMetaDataList(vfModulesMetadataList));
98         }
99     }
100
101     protected void addArtifactByType(IArtifactInfo artifactinfo, IDistributionClientDownloadResult clientResult,
102             VfModuleArtifact vfModuleArtifact) throws UnsupportedEncodingException {
103
104         switch (artifactinfo.getArtifactType()) {
105             case ASDCConfiguration.HEAT:
106             case ASDCConfiguration.HEAT_ENV:
107             case ASDCConfiguration.HEAT_VOL:
108             case ASDCConfiguration.HEAT_NESTED: // For 1607 only 1 level tree is supported
109             case ASDCConfiguration.HEAT_ARTIFACT:
110             case ASDCConfiguration.HEAT_NET:
111             case ASDCConfiguration.OTHER:
112                 artifactsMapByUUID.put(artifactinfo.getArtifactUUID(), vfModuleArtifact);
113                 break;
114             case ASDCConfiguration.VF_MODULES_METADATA:
115                 vfModulesMetadataList = this.decodeVfModuleArtifact(clientResult.getArtifactPayload());
116                 break;
117             default:
118                 break;
119         }
120     }
121
122     public void prepareInstall() throws ArtifactInstallerException {
123         createVfModuleStructures();
124     }
125
126     public void createVfModuleStructures() throws ArtifactInstallerException {
127
128         // for vender tosca VNF there is no VFModule in VF
129         if (vfModulesMetadataList == null) {
130             logger.info("{} {} {} {}", MessageEnum.ASDC_GENERAL_INFO.toString(), "There is no VF mudules in the VF.",
131                     "ASDC", "createVfModuleStructures");
132             return;
133         }
134         for (IVfModuleData vfModuleMeta : vfModulesMetadataList) {
135             vfModulesStructureList.add(new VfModuleStructure(this, vfModuleMeta));
136         }
137         setNumberOfResources(vfModulesMetadataList.size());
138     }
139
140     public List<VfModuleStructure> getVfModuleStructure() {
141         return vfModulesStructureList;
142     }
143
144     public Map<String, VfModuleArtifact> getArtifactsMapByUUID() {
145         return artifactsMapByUUID;
146     }
147
148     public List<VfModuleStructure> getVfModulesStructureList() {
149         return vfModulesStructureList;
150     }
151
152     public VnfResource getCatalogVnfResource() {
153         return catalogVnfResource;
154     }
155
156     public void setCatalogVnfResource(VnfResource catalogVnfResource) {
157         this.catalogVnfResource = catalogVnfResource;
158     }
159
160     // Network Only
161     public NetworkResourceCustomization getCatalogNetworkResourceCustomization() {
162         return catalogNetworkResourceCustomization;
163     }
164
165     // Network Only
166     public void setCatalogNetworkResourceCustomization(
167             NetworkResourceCustomization catalogNetworkResourceCustomization) {
168         this.catalogNetworkResourceCustomization = catalogNetworkResourceCustomization;
169     }
170
171     public AllottedResourceCustomization getCatalogResourceCustomization() {
172         return catalogResourceCustomization;
173     }
174
175     public void setCatalogResourceCustomization(AllottedResourceCustomization catalogResourceCustomization) {
176         this.catalogResourceCustomization = catalogResourceCustomization;
177     }
178
179     public Service getCatalogService() {
180         return catalogService;
181     }
182
183     public void setCatalogService(Service catalogService) {
184         this.catalogService = catalogService;
185     }
186
187     public List<IVfModuleData> decodeVfModuleArtifact(byte[] arg0) {
188         try {
189             List<IVfModuleData> listVFModuleMetaData =
190                     new ObjectMapper().readValue(arg0, new TypeReference<List<VfModuleMetaData>>() {});
191             return listVFModuleMetaData;
192
193         } catch (JsonParseException e) {
194             logger.debug("JsonParseException : ", e);
195         } catch (JsonMappingException e) {
196             logger.debug("JsonMappingException : ", e);
197         } catch (IOException e) {
198             logger.debug("IOException : ", e);
199         }
200         return null;
201     }
202 }