e9e39d766010fd818978a9abb7a8ebedd3849553
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / 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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.asdc.installer;
23
24 import java.io.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.codehaus.jackson.JsonParseException;
32 import org.codehaus.jackson.map.JsonMappingException;
33 import org.codehaus.jackson.map.ObjectMapper;
34 import org.codehaus.jackson.type.TypeReference;
35 import org.openecomp.mso.asdc.client.ASDCConfiguration;
36 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
37 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
38 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
39 import org.openecomp.mso.db.catalog.beans.Service;
40 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
41 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
42 import org.openecomp.mso.db.catalog.beans.VnfResource;
43 import org.openecomp.mso.logger.MessageEnum;
44 import org.openecomp.mso.logger.MsoLogger;
45 import org.openecomp.sdc.api.IDistributionClient;
46 import org.openecomp.sdc.api.notification.IArtifactInfo;
47 import org.openecomp.sdc.api.notification.INotificationData;
48 import org.openecomp.sdc.api.notification.IResourceInstance;
49 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
50 /**
51  * This structure exists to avoid having issues if the order of the vfResource/vfmodule artifact is not good (tree structure).
52  *
53  */
54 public final class VfResourceStructure {
55         
56         protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
57
58         private boolean isDeployedSuccessfully=false;
59         /**
60          * The Raw notification data.
61          */
62         private final INotificationData notification;
63
64         /**
65          * The resource we will try to deploy.
66          */
67         private final IResourceInstance resourceInstance;
68
69         /**
70          * The list of VfModules defined for this resource.
71          */
72         private final List<VfModuleStructure> vfModulesStructureList;
73
74         /**
75          * The list of VfModulesMetadata defined for this resource.
76          */
77         private List<IVfModuleData> vfModulesMetadataList;
78
79         private VnfResource catalogVnfResource;
80
81         private NetworkResourceCustomization catalogNetworkResourceCustomization;
82
83         private ServiceToNetworks catalogServiceToNetworks;
84
85         private ServiceToAllottedResources catalogServiceToAllottedResources;
86
87         private AllottedResourceCustomization catalogResourceCustomization;
88
89         private Service catalogService;
90
91         private List<String> vfArtifactUUIDList;
92         
93         /**
94          * The list of artifacts existing in this resource hashed by UUID.
95          */
96         private final Map<String, VfModuleArtifact> artifactsMapByUUID;
97
98
99         public VfResourceStructure(INotificationData notificationdata, IResourceInstance resourceinstance) {
100                 notification=notificationdata;
101                 resourceInstance=resourceinstance;
102
103
104                 vfModulesStructureList = new LinkedList<>();
105                 artifactsMapByUUID = new HashMap<>();
106         }
107
108         //@Override
109         public void addArtifactToStructure(IDistributionClient distributionClient,IArtifactInfo artifactinfo,IDistributionClientDownloadResult clientResult) throws UnsupportedEncodingException {
110                 VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactinfo,clientResult);
111
112                 switch(artifactinfo.getArtifactType()) {
113                         case ASDCConfiguration.HEAT:
114                         case ASDCConfiguration.HEAT_ENV:
115                         case ASDCConfiguration.HEAT_VOL:
116                         case ASDCConfiguration.HEAT_NESTED:    // For 1607 only 1 level tree is supported
117                         case ASDCConfiguration.HEAT_ARTIFACT:
118                         case ASDCConfiguration.HEAT_NET:
119                         case ASDCConfiguration.OTHER:
120                                 artifactsMapByUUID.put(artifactinfo.getArtifactUUID(), vfModuleArtifact);
121                                 break;
122
123                         case ASDCConfiguration.VF_MODULES_METADATA:
124                                 vfModulesMetadataList = this.decodeVfModuleArtifact(clientResult.getArtifactPayload());
125                                 
126                                 for(IVfModuleData moduleData : vfModulesMetadataList){
127                                         
128                                 }
129                                 
130                                 //vfArtifactUUIDList.add(artifactinfo.getArtifactUUID());
131                                 //vfModulesMetadataList = distributionClient.decodeVfModuleArtifact(clientResult.getArtifactPayload());
132                                 break;
133
134                         default:
135                                 break;
136
137                 }
138         }
139
140         public void createVfModuleStructures() throws ArtifactInstallerException {
141
142             //for vender tosca VNF there is no VFModule in VF
143                 if (vfModulesMetadataList == null) {
144                     LOGGER.info(MessageEnum.ASDC_GENERAL_INFO,"There is no VF mudules in the VF.", "ASDC", "createVfModuleStructures");
145                     return;
146                 }
147                 for (IVfModuleData vfModuleMeta:vfModulesMetadataList) {
148                         vfModulesStructureList.add(new VfModuleStructure(this,vfModuleMeta));
149                 }
150         }
151
152         public INotificationData getNotification() {
153                 return notification;
154         }
155
156         public IResourceInstance getResourceInstance() {
157                 return resourceInstance;
158         }
159
160         public List<VfModuleStructure> getVfModuleStructure() {
161                 return vfModulesStructureList;
162         }
163
164         public boolean isDeployedSuccessfully() {
165                 return isDeployedSuccessfully;
166         }
167
168         public void setSuccessfulDeployment() {
169                 isDeployedSuccessfully = true;
170         }
171
172         public Map<String, VfModuleArtifact> getArtifactsMapByUUID() {
173                 return artifactsMapByUUID;
174         }
175
176         public List<VfModuleStructure> getVfModulesStructureList() {
177                 return vfModulesStructureList;
178         }
179
180         public VnfResource getCatalogVnfResource() {
181                 return catalogVnfResource;
182         }
183
184         public void setCatalogVnfResource(VnfResource catalogVnfResource) {
185                 this.catalogVnfResource = catalogVnfResource;
186         }
187
188         // Network Only
189         public NetworkResourceCustomization getCatalogNetworkResourceCustomization() {
190                 return catalogNetworkResourceCustomization;
191         }
192         // Network Only
193         public void setCatalogNetworkResourceCustomization(NetworkResourceCustomization catalogNetworkResourceCustomization) {
194                 this.catalogNetworkResourceCustomization = catalogNetworkResourceCustomization;
195         }
196         // Network Only
197         public ServiceToNetworks getCatalogServiceToNetworks() {
198                 return catalogServiceToNetworks;
199         }
200         // Network Only
201         public void setCatalogServiceToNetworks(
202                         ServiceToNetworks catalogServiceToNetworks) {
203                 this.catalogServiceToNetworks = catalogServiceToNetworks;
204         }
205
206         public ServiceToAllottedResources getCatalogServiceToAllottedResources() {
207                 return catalogServiceToAllottedResources;
208         }
209
210         public void setCatalogServiceToAllottedResources(
211                         ServiceToAllottedResources catalogServiceToAllottedResources) {
212                 this.catalogServiceToAllottedResources = catalogServiceToAllottedResources;
213         }
214
215         public AllottedResourceCustomization getCatalogResourceCustomization() {
216                 return catalogResourceCustomization;
217         }
218
219         public void setCatalogResourceCustomization(
220                         AllottedResourceCustomization catalogResourceCustomization) {
221                 this.catalogResourceCustomization = catalogResourceCustomization;
222         }
223
224         public Service getCatalogService() {
225                 return catalogService;
226         }
227
228         public void setCatalogService(Service catalogService) {
229                 this.catalogService = catalogService;
230         }
231
232         public List<IVfModuleData> decodeVfModuleArtifact(byte[] arg0) {
233                 try {
234                         List<IVfModuleData> listVFModuleMetaData = new ObjectMapper().readValue(arg0, new TypeReference<List<VfModuleMetaData>>(){});
235                         return listVFModuleMetaData;
236
237                 } catch (JsonParseException e) {
238                         LOGGER.debug("JsonParseException : ",e);
239                 } catch (JsonMappingException e) {
240                         LOGGER.debug("JsonMappingException : ",e);
241                 } catch (IOException e) {
242                         LOGGER.debug("IOException : ",e);
243                 }
244                 return null;
245         }
246 }