Use distribution json for workflow install
[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     public void addWorkflowArtifactToStructure(IArtifactInfo artifactinfo,
102             IDistributionClientDownloadResult clientResult) throws UnsupportedEncodingException {
103         WorkflowArtifact workflowArtifact = new WorkflowArtifact(artifactinfo, clientResult);
104         workflowArtifactsMapByUUID.put(artifactinfo.getArtifactUUID(), workflowArtifact);
105     }
106
107     protected void addArtifactByType(IArtifactInfo artifactinfo, IDistributionClientDownloadResult clientResult,
108             VfModuleArtifact vfModuleArtifact) throws UnsupportedEncodingException {
109
110         switch (artifactinfo.getArtifactType()) {
111             case ASDCConfiguration.HEAT:
112             case ASDCConfiguration.HEAT_ENV:
113             case ASDCConfiguration.HEAT_VOL:
114             case ASDCConfiguration.HEAT_NESTED: // For 1607 only 1 level tree is supported
115             case ASDCConfiguration.HEAT_ARTIFACT:
116             case ASDCConfiguration.HEAT_NET:
117             case ASDCConfiguration.OTHER:
118                 artifactsMapByUUID.put(artifactinfo.getArtifactUUID(), vfModuleArtifact);
119                 break;
120             case ASDCConfiguration.VF_MODULES_METADATA:
121                 vfModulesMetadataList = this.decodeVfModuleArtifact(clientResult.getArtifactPayload());
122                 break;
123             default:
124                 break;
125         }
126     }
127
128     public void prepareInstall() throws ArtifactInstallerException {
129         createVfModuleStructures();
130     }
131
132     public void createVfModuleStructures() throws ArtifactInstallerException {
133
134         // for vender tosca VNF there is no VFModule in VF
135         if (vfModulesMetadataList == null) {
136             logger.info("{} {} {} {}", MessageEnum.ASDC_GENERAL_INFO.toString(), "There is no VF mudules in the VF.",
137                     "ASDC", "createVfModuleStructures");
138             return;
139         }
140         for (IVfModuleData vfModuleMeta : vfModulesMetadataList) {
141             vfModulesStructureList.add(new VfModuleStructure(this, vfModuleMeta));
142         }
143         setNumberOfResources(vfModulesMetadataList.size());
144     }
145
146     public List<VfModuleStructure> getVfModuleStructure() {
147         return vfModulesStructureList;
148     }
149
150     public Map<String, VfModuleArtifact> getArtifactsMapByUUID() {
151         return artifactsMapByUUID;
152     }
153
154     public List<VfModuleStructure> getVfModulesStructureList() {
155         return vfModulesStructureList;
156     }
157
158     public VnfResource getCatalogVnfResource() {
159         return catalogVnfResource;
160     }
161
162     public void setCatalogVnfResource(VnfResource catalogVnfResource) {
163         this.catalogVnfResource = catalogVnfResource;
164     }
165
166     // Network Only
167     public NetworkResourceCustomization getCatalogNetworkResourceCustomization() {
168         return catalogNetworkResourceCustomization;
169     }
170
171     // Network Only
172     public void setCatalogNetworkResourceCustomization(
173             NetworkResourceCustomization catalogNetworkResourceCustomization) {
174         this.catalogNetworkResourceCustomization = catalogNetworkResourceCustomization;
175     }
176
177     public AllottedResourceCustomization getCatalogResourceCustomization() {
178         return catalogResourceCustomization;
179     }
180
181     public void setCatalogResourceCustomization(AllottedResourceCustomization catalogResourceCustomization) {
182         this.catalogResourceCustomization = catalogResourceCustomization;
183     }
184
185     public Service getCatalogService() {
186         return catalogService;
187     }
188
189     public void setCatalogService(Service catalogService) {
190         this.catalogService = catalogService;
191     }
192
193     public List<IVfModuleData> decodeVfModuleArtifact(byte[] arg0) {
194         try {
195             List<IVfModuleData> listVFModuleMetaData =
196                     new ObjectMapper().readValue(arg0, new TypeReference<List<VfModuleMetaData>>() {});
197             return listVFModuleMetaData;
198
199         } catch (JsonParseException e) {
200             logger.debug("JsonParseException : ", e);
201         } catch (JsonMappingException e) {
202             logger.debug("JsonMappingException : ", e);
203         } catch (IOException e) {
204             logger.debug("IOException : ", e);
205         }
206         return null;
207     }
208 }