[MSO-8] Update the maven dependency
[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         /**
90          * The list of artifacts existing in this resource hashed by UUID.
91          */
92         private final Map<String, VfModuleArtifact> artifactsMapByUUID;
93
94
95         public VfResourceStructure(INotificationData notificationdata, IResourceInstance resourceinstance) {
96                 notification=notificationdata;
97                 resourceInstance=resourceinstance;
98
99
100                 vfModulesStructureList = new LinkedList<VfModuleStructure>();
101                 artifactsMapByUUID =  new HashMap<String, VfModuleArtifact>();
102         }
103
104         //@Override
105         public void addArtifactToStructure(IDistributionClient distributionClient,IArtifactInfo artifactinfo,IDistributionClientDownloadResult clientResult) throws UnsupportedEncodingException {
106                 VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactinfo,clientResult);
107
108                 switch(artifactinfo.getArtifactType()) {
109                         case ASDCConfiguration.HEAT:
110                         case ASDCConfiguration.HEAT_ENV:
111                         case ASDCConfiguration.HEAT_VOL:
112                         case ASDCConfiguration.HEAT_NESTED:    // For 1607 only 1 level tree is supported
113                         case ASDCConfiguration.HEAT_ARTIFACT:
114                         case ASDCConfiguration.HEAT_NET:
115                         case ASDCConfiguration.OTHER:
116                                 artifactsMapByUUID.put(artifactinfo.getArtifactUUID(), vfModuleArtifact);
117                                 break;
118
119                         case ASDCConfiguration.VF_MODULES_METADATA:
120                                 vfModulesMetadataList = this.decodeVfModuleArtifact(clientResult.getArtifactPayload());
121                                 break;
122
123                         default:
124                                 break;
125
126                 }
127         }
128
129         public void createVfModuleStructures() throws ArtifactInstallerException {
130
131                 if (vfModulesMetadataList == null) {
132                         throw new ArtifactInstallerException("VfModule Meta DATA could not be decoded properly or was not present in the notification");
133                 }
134                 for (IVfModuleData vfModuleMeta:vfModulesMetadataList) {
135                         vfModulesStructureList.add(new VfModuleStructure(this,vfModuleMeta));
136                 }
137         }
138
139         public INotificationData getNotification() {
140                 return notification;
141         }
142
143         public IResourceInstance getResourceInstance() {
144                 return resourceInstance;
145         }
146
147         public List<VfModuleStructure> getVfModuleStructure() {
148                 return vfModulesStructureList;
149         }
150
151         public boolean isDeployedSuccessfully() {
152                 return isDeployedSuccessfully;
153         }
154
155         public void setSuccessfulDeployment() {
156                 isDeployedSuccessfully = true;
157         }
158
159         public Map<String, VfModuleArtifact> getArtifactsMapByUUID() {
160                 return artifactsMapByUUID;
161         }
162
163         public List<VfModuleStructure> getVfModulesStructureList() {
164                 return vfModulesStructureList;
165         }
166
167         public VnfResource getCatalogVnfResource() {
168                 return catalogVnfResource;
169         }
170
171         public void setCatalogVnfResource(VnfResource catalogVnfResource) {
172                 this.catalogVnfResource = catalogVnfResource;
173         }
174
175         // Network Only
176         public NetworkResourceCustomization getCatalogNetworkResourceCustomization() {
177                 return catalogNetworkResourceCustomization;
178         }
179         // Network Only
180         public void setCatalogNetworkResourceCustomization(NetworkResourceCustomization catalogNetworkResourceCustomization) {
181                 this.catalogNetworkResourceCustomization = catalogNetworkResourceCustomization;
182         }
183         // Network Only
184         public ServiceToNetworks getCatalogServiceToNetworks() {
185                 return catalogServiceToNetworks;
186         }
187         // Network Only
188         public void setCatalogServiceToNetworks(
189                         ServiceToNetworks catalogServiceToNetworks) {
190                 this.catalogServiceToNetworks = catalogServiceToNetworks;
191         }
192
193         public ServiceToAllottedResources getCatalogServiceToAllottedResources() {
194                 return catalogServiceToAllottedResources;
195         }
196
197         public void setCatalogServiceToAllottedResources(
198                         ServiceToAllottedResources catalogServiceToAllottedResources) {
199                 this.catalogServiceToAllottedResources = catalogServiceToAllottedResources;
200         }
201
202         public AllottedResourceCustomization getCatalogResourceCustomization() {
203                 return catalogResourceCustomization;
204         }
205
206         public void setCatalogResourceCustomization(
207                         AllottedResourceCustomization catalogResourceCustomization) {
208                 this.catalogResourceCustomization = catalogResourceCustomization;
209         }
210
211         public Service getCatalogService() {
212                 return catalogService;
213         }
214
215         public void setCatalogService(Service catalogService) {
216                 this.catalogService = catalogService;
217         }
218
219         public List<IVfModuleData> decodeVfModuleArtifact(byte[] arg0) {
220                 try {
221                         List<IVfModuleData> listVFModuleMetaData = new ObjectMapper().readValue(arg0, new TypeReference<List<VfModuleMetaData>>(){});
222                         return listVFModuleMetaData;
223
224                 } catch (JsonParseException e) {
225                         e.printStackTrace();
226                 } catch (JsonMappingException e) {
227                         e.printStackTrace();
228                 } catch (IOException e) {
229                         e.printStackTrace();
230                 }
231                 return null;
232         }
233 }