added generic fabric support to SO
[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  * 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.onap.so.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 import org.onap.so.asdc.client.ASDCConfiguration;
31 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
32 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
33 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
34 import org.onap.so.db.catalog.beans.Service;
35 import org.onap.so.db.catalog.beans.VnfResource;
36 import org.onap.so.logger.MsoLogger;
37 import org.onap.sdc.api.IDistributionClient;
38 import org.onap.sdc.api.notification.IArtifactInfo;
39 import org.onap.sdc.api.notification.INotificationData;
40 import org.onap.sdc.api.notification.IResourceInstance;
41 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
42 import org.onap.so.logger.MessageEnum;
43 import com.fasterxml.jackson.core.JsonParseException;
44 import com.fasterxml.jackson.core.type.TypeReference;
45 import com.fasterxml.jackson.databind.JsonMappingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 /**
48  * This structure exists to avoid having issues if the order of the vfResource/vfmodule artifact is not good (tree structure).
49  *
50  */
51
52
53 public class VfResourceStructure {
54         
55         protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC, VfResourceStructure.class);
56
57         private boolean isDeployedSuccessfully=false;
58         /**
59          * The Raw notification data.
60          */
61         private final INotificationData notification;
62         /**
63          * The Raw notification data.
64          */
65         private boolean isAlreadyDeployed=false;
66
67         /**
68          * The resource we will try to deploy.
69          */
70         private final IResourceInstance resourceInstance;
71
72         /**
73          * The list of VfModules defined for this resource.
74          */
75         private final List<VfModuleStructure> vfModulesStructureList;
76
77         /**
78          * The list of VfModulesMetadata defined for this resource.
79          */
80         private List<IVfModuleData> vfModulesMetadataList;
81
82         private VnfResource catalogVnfResource;
83
84         private NetworkResourceCustomization catalogNetworkResourceCustomization;
85         
86         private AllottedResourceCustomization catalogResourceCustomization;
87
88         private Service catalogService;
89         
90         /**
91          * The list of artifacts existing in this resource hashed by UUID.
92          */
93         private final Map<String, VfModuleArtifact> artifactsMapByUUID;
94
95
96         public VfResourceStructure(INotificationData notificationdata, IResourceInstance resourceinstance) {
97                 notification=notificationdata;
98                 resourceInstance=resourceinstance;
99                 vfModulesStructureList = new LinkedList<>();
100                 artifactsMapByUUID = new HashMap<>();
101         }
102         
103         public void addArtifactToStructure(IDistributionClient distributionClient,IArtifactInfo artifactinfo,IDistributionClientDownloadResult clientResult) throws UnsupportedEncodingException {
104                 VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactinfo,clientResult);
105                 addArtifactByType(artifactinfo,clientResult,vfModuleArtifact);
106         }
107         
108         public void addArtifactToStructure(IDistributionClient distributionClient,IArtifactInfo artifactinfo,IDistributionClientDownloadResult clientResult, String modifiedHeatTemplate) throws UnsupportedEncodingException {
109                 VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactinfo,clientResult,modifiedHeatTemplate);
110                 addArtifactByType(artifactinfo,clientResult,vfModuleArtifact);
111         }
112         
113         protected void addArtifactByType(IArtifactInfo artifactinfo,IDistributionClientDownloadResult clientResult, VfModuleArtifact vfModuleArtifact) throws UnsupportedEncodingException {
114
115                 switch(artifactinfo.getArtifactType()) {
116                         case ASDCConfiguration.HEAT:
117                         case ASDCConfiguration.HEAT_ENV:
118                         case ASDCConfiguration.HEAT_VOL:
119                         case ASDCConfiguration.HEAT_NESTED:    // For 1607 only 1 level tree is supported
120                         case ASDCConfiguration.HEAT_ARTIFACT:
121                         case ASDCConfiguration.HEAT_NET:
122                         case ASDCConfiguration.OTHER:
123                                 artifactsMapByUUID.put(artifactinfo.getArtifactUUID(), vfModuleArtifact);
124                                 break;
125                         case ASDCConfiguration.VF_MODULES_METADATA:
126                                 vfModulesMetadataList = this.decodeVfModuleArtifact(clientResult.getArtifactPayload()); 
127                                 break;
128                         default:
129                                 break;
130                 }
131         }
132
133         public void createVfModuleStructures() throws ArtifactInstallerException {
134
135                 //for vender tosca VNF there is no VFModule in VF
136                 if (vfModulesMetadataList == null) {
137                     LOGGER.info(MessageEnum.ASDC_GENERAL_INFO,"There is no VF mudules in the VF.", "ASDC", "createVfModuleStructures");
138                     return;
139                 }
140                         for (IVfModuleData vfModuleMeta:vfModulesMetadataList) {
141                                 vfModulesStructureList.add(new VfModuleStructure(this,vfModuleMeta));
142                         }
143                 }
144
145         public INotificationData getNotification() {
146                 return notification;
147         }
148
149         public IResourceInstance getResourceInstance() {
150                 return resourceInstance;
151         }
152
153         public List<VfModuleStructure> getVfModuleStructure() {
154                 return vfModulesStructureList;
155         }
156
157         public boolean isDeployedSuccessfully() {
158                 return isDeployedSuccessfully;
159         }
160
161         public void setSuccessfulDeployment() {
162                 isDeployedSuccessfully = true;
163         }
164         
165         public boolean isAlreadyDeployed() {
166                 return isAlreadyDeployed;
167         }
168
169         public void setAlreadyDeployed(boolean isAlreadyDeployed) {
170                 this.isAlreadyDeployed = isAlreadyDeployed;
171         }
172
173         public Map<String, VfModuleArtifact> getArtifactsMapByUUID() {
174                 return artifactsMapByUUID;
175         }
176
177         public List<VfModuleStructure> getVfModulesStructureList() {
178                 return vfModulesStructureList;
179         }
180
181         public VnfResource getCatalogVnfResource() {
182                 return catalogVnfResource;
183         }
184
185         public void setCatalogVnfResource(VnfResource catalogVnfResource) {
186                 this.catalogVnfResource = catalogVnfResource;
187         }
188
189         // Network Only
190         public NetworkResourceCustomization getCatalogNetworkResourceCustomization() {
191                 return catalogNetworkResourceCustomization;
192         }
193         // Network Only
194         public void setCatalogNetworkResourceCustomization(NetworkResourceCustomization catalogNetworkResourceCustomization) {
195                 this.catalogNetworkResourceCustomization = catalogNetworkResourceCustomization;
196         }
197
198         public AllottedResourceCustomization getCatalogResourceCustomization() {
199                 return catalogResourceCustomization;
200         }
201
202         public void setCatalogResourceCustomization(
203                         AllottedResourceCustomization catalogResourceCustomization) {
204                 this.catalogResourceCustomization = catalogResourceCustomization;
205         }
206
207         public Service getCatalogService() {
208                 return catalogService;
209         }
210
211         public void setCatalogService(Service catalogService) {
212                 this.catalogService = catalogService;
213         }
214
215         public List<IVfModuleData> decodeVfModuleArtifact(byte[] arg0) {
216                 try {
217                         List<IVfModuleData> listVFModuleMetaData = new ObjectMapper().readValue(arg0, new TypeReference<List<VfModuleMetaData>>(){});
218                         return listVFModuleMetaData;
219
220                 } catch (JsonParseException e) {
221                         LOGGER.debug("JsonParseException : ",e);
222                 } catch (JsonMappingException e) {
223                         LOGGER.debug("JsonMappingException : ",e);
224                 } catch (IOException e) {
225                         LOGGER.debug("IOException : ",e);
226                 }
227                 return null;
228         }
229 }