a93b065dbe83acff141bbf4a9514af8b61f30ff5
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / installer / VfResourceStructure.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.asdc.installer;
22
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.codehaus.jackson.JsonParseException;
31 import org.codehaus.jackson.map.JsonMappingException;
32 import org.codehaus.jackson.map.ObjectMapper;
33 import org.codehaus.jackson.type.TypeReference;
34
35 import org.openecomp.sdc.api.IDistributionClient;
36 import org.openecomp.sdc.api.notification.IArtifactInfo;
37 import org.openecomp.sdc.api.notification.INotificationData;
38 import org.openecomp.sdc.api.notification.IResourceInstance;
39 import org.openecomp.sdc.api.notification.IVfModuleMetadata;
40 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
41 import org.openecomp.mso.asdc.client.ASDCConfiguration;
42 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
43 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
44 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
45 import org.openecomp.mso.db.catalog.beans.Service;
46 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
47 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
48 import org.openecomp.mso.db.catalog.beans.VnfResource;
49
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         private boolean isDeployedSuccessfully=false;
57         /**
58          * The Raw notification data.
59          */
60         private final INotificationData notification;
61
62         /**
63          * The resource we will try to deploy.
64          */
65         private final IResourceInstance resourceInstance;
66
67         /**
68          * The list of VfModules defined for this resource.
69          */
70         private final List<VfModuleStructure> vfModulesStructureList;
71
72         /**
73          * The list of VfModulesMetadata defined for this resource.
74          */
75         private List<IVfModuleData> vfModulesMetadataList;
76
77         private VnfResource catalogVnfResource;
78
79         private NetworkResourceCustomization catalogNetworkResourceCustomization;
80
81         private ServiceToNetworks catalogServiceToNetworks;
82
83         private ServiceToAllottedResources catalogServiceToAllottedResources;
84
85         private AllottedResourceCustomization catalogResourceCustomization;
86
87         private Service catalogService;
88
89         private List<String> vfArtifactUUIDList;
90         
91         /**
92          * The list of artifacts existing in this resource hashed by UUID.
93          */
94         private final Map<String, VfModuleArtifact> artifactsMapByUUID;
95
96
97         public VfResourceStructure(INotificationData notificationdata, IResourceInstance resourceinstance) {
98                 notification=notificationdata;
99                 resourceInstance=resourceinstance;
100
101
102                 vfModulesStructureList = new LinkedList<VfModuleStructure>();
103                 artifactsMapByUUID =  new HashMap<String, VfModuleArtifact>();
104         }
105
106         //@Override
107         public void addArtifactToStructure(IDistributionClient distributionClient,IArtifactInfo artifactinfo,IDistributionClientDownloadResult clientResult) throws UnsupportedEncodingException {
108                 VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactinfo,clientResult);
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
121                         case ASDCConfiguration.VF_MODULES_METADATA:
122                                 vfModulesMetadataList = this.decodeVfModuleArtifact(clientResult.getArtifactPayload());
123                                 
124                                 for(IVfModuleData moduleData : vfModulesMetadataList){
125                                         
126                                 }
127                                 
128                                 //vfArtifactUUIDList.add(artifactinfo.getArtifactUUID());
129                                 //vfModulesMetadataList = distributionClient.decodeVfModuleArtifact(clientResult.getArtifactPayload());
130                                 break;
131
132                         default:
133                                 break;
134
135                 }
136         }
137
138         public void createVfModuleStructures() throws ArtifactInstallerException {
139
140                 if (vfModulesMetadataList == null) {
141                         throw new ArtifactInstallerException("VfModule Meta DATA could not be decoded properly or was not present in the notification");
142                 }
143                 for (IVfModuleData vfModuleMeta:vfModulesMetadataList) {
144                         vfModulesStructureList.add(new VfModuleStructure(this,vfModuleMeta));
145                 }
146         }
147
148         public INotificationData getNotification() {
149                 return notification;
150         }
151
152         public IResourceInstance getResourceInstance() {
153                 return resourceInstance;
154         }
155
156         public List<VfModuleStructure> getVfModuleStructure() {
157                 return vfModulesStructureList;
158         }
159
160         public boolean isDeployedSuccessfully() {
161                 return isDeployedSuccessfully;
162         }
163
164         public void setSuccessfulDeployment() {
165                 isDeployedSuccessfully = true;
166         }
167
168         public Map<String, VfModuleArtifact> getArtifactsMapByUUID() {
169                 return artifactsMapByUUID;
170         }
171
172         public List<VfModuleStructure> getVfModulesStructureList() {
173                 return vfModulesStructureList;
174         }
175
176         public VnfResource getCatalogVnfResource() {
177                 return catalogVnfResource;
178         }
179
180         public void setCatalogVnfResource(VnfResource catalogVnfResource) {
181                 this.catalogVnfResource = catalogVnfResource;
182         }
183
184         // Network Only
185         public NetworkResourceCustomization getCatalogNetworkResourceCustomization() {
186                 return catalogNetworkResourceCustomization;
187         }
188         // Network Only
189         public void setCatalogNetworkResourceCustomization(NetworkResourceCustomization catalogNetworkResourceCustomization) {
190                 this.catalogNetworkResourceCustomization = catalogNetworkResourceCustomization;
191         }
192         // Network Only
193         public ServiceToNetworks getCatalogServiceToNetworks() {
194                 return catalogServiceToNetworks;
195         }
196         // Network Only
197         public void setCatalogServiceToNetworks(
198                         ServiceToNetworks catalogServiceToNetworks) {
199                 this.catalogServiceToNetworks = catalogServiceToNetworks;
200         }
201
202         public ServiceToAllottedResources getCatalogServiceToAllottedResources() {
203                 return catalogServiceToAllottedResources;
204         }
205
206         public void setCatalogServiceToAllottedResources(
207                         ServiceToAllottedResources catalogServiceToAllottedResources) {
208                 this.catalogServiceToAllottedResources = catalogServiceToAllottedResources;
209         }
210
211         public AllottedResourceCustomization getCatalogResourceCustomization() {
212                 return catalogResourceCustomization;
213         }
214
215         public void setCatalogResourceCustomization(
216                         AllottedResourceCustomization catalogResourceCustomization) {
217                 this.catalogResourceCustomization = catalogResourceCustomization;
218         }
219
220         public Service getCatalogService() {
221                 return catalogService;
222         }
223
224         public void setCatalogService(Service catalogService) {
225                 this.catalogService = catalogService;
226         }
227
228         public List<IVfModuleData> decodeVfModuleArtifact(byte[] arg0) {
229                 try {
230                         List<IVfModuleData> listVFModuleMetaData = new ObjectMapper().readValue(arg0, new TypeReference<List<VfModuleMetaData>>(){});
231                         return listVFModuleMetaData;
232
233                 } catch (JsonParseException e) {
234                         e.printStackTrace();
235                 } catch (JsonMappingException e) {
236                         e.printStackTrace();
237                 } catch (IOException e) {
238                         e.printStackTrace();
239                 }
240                 return null;
241         }
242 }