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