e69ca8d149edf866936f704ebeb6db7bade45038
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.vendorsoftwareproduct.informationArtifact.impl;
22
23 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
24 import org.openecomp.sdc.vendorsoftwareproduct.factory.QuestionnnaireDataServiceFactory;
25 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactData;
26 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
27 import org.openecomp.sdc.vendorsoftwareproduct.questionnaire.QuestionnaireDataService;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.ComponentQuestionnaire;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.compute.Compute;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.compute.GuestOS;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.compute.NumOfVMs;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.general.Hypervisor;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.general.Recovery;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.nic.IpConfiguration;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.nic.Network;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.nic.NicQuestionnaire;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.vsp.VspQuestionnaire;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.vsp.general.Availability;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.vsp.general.General;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.vsp.general.StorageDataReplication;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42
43 import java.util.List;
44 import java.util.Optional;
45
46 import static org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.impl.TxtInformationArtifactConstants.*;
47
48 /**
49  * @author katyr
50  * @since November 23, 2016
51  */
52
53 public class TxtInformationArtifactGeneratorImpl implements InformationArtifactGenerator {
54   private QuestionnaireDataService questionnaireDataService = QuestionnnaireDataServiceFactory
55       .getInstance().createInterface();
56   private StringBuilder textArtifact;
57   @Override
58   public String generate(String vspId, Version version) {
59     InformationArtifactData informationArtifactData =
60         questionnaireDataService.generateQuestionnaireDataForInformationArtifact(vspId, version);
61     return createTxtArtifact(informationArtifactData);
62   }
63
64   private String createTxtArtifact(InformationArtifactData informationArtifactData) {
65     textArtifact = new StringBuilder();
66     addVspVlmEntries(informationArtifactData);
67     addAvailabilityEntries();
68     addDataEntries(informationArtifactData);
69     addEntryWithIndent(LIST_OF_VFCS, "", TAB);
70     addEntryWithIndent(FOR_EACH_VFC, "", TAB + TAB);
71     List<ComponentQuestionnaire> componentQuestionnaires =
72         informationArtifactData.getComponentQuestionnaires();
73
74     for (ComponentQuestionnaire componentQuestionnaire : componentQuestionnaires) {
75       addEntriesPerComponent(componentQuestionnaire);
76
77     }
78
79     List<NicQuestionnaire> nicQuestionnaires = informationArtifactData.getNicQuestionnaires();
80     for (NicQuestionnaire nicQuestionnaire : nicQuestionnaires) {
81       addEntriesPerNic(nicQuestionnaire);
82
83     }
84     for (ComponentQuestionnaire componentQuestionnaire : componentQuestionnaires) {
85       addRecoveryEntriesPerComponent(componentQuestionnaire);
86     }
87     return textArtifact.toString();
88
89   }
90
91   private void addDataEntries(InformationArtifactData informationArtifactData) {
92     addEntryWithIndent(STORAGE_BACKUP_DETAILS, "", TAB);
93
94     Optional<StorageDataReplication> storageDataReplication =
95         Optional.of(informationArtifactData).map(InformationArtifactData::getVspQuestionnaire)
96             .map(VspQuestionnaire::getGeneral).map(General::getStorageDataReplication);
97     storageDataReplication
98         .ifPresent(replication -> addEntryWithIndent(IS_DATA_REPLICATION,
99             String.valueOf(replication.isStorageReplicationAcrossRegion()), TAB + TAB));
100
101
102     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_SIZE_TO_REP,
103         String.valueOf(rep.getStorageReplicationSize()), TAB + TAB));
104
105     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_REP_FREQUENCY,
106         String.valueOf(rep.getStorageReplicationFrequency()), TAB + TAB));
107
108     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_REP_SOURCE,
109         String.valueOf(rep.getStorageReplicationSource()), TAB + TAB));
110
111     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_REP_DEST,
112         String.valueOf(rep.getStorageReplicationDestination()), TAB + TAB));
113   }
114
115   private void addAvailabilityEntries() {
116     addEntryWithIndent(HIGH_AVAILABILITY, "", TAB);
117
118     Optional<Availability> availability =
119         Optional.of(new InformationArtifactData()).map(InformationArtifactData::getVspQuestionnaire)
120             .map(VspQuestionnaire::getGeneral).map(General::getAvailability);
121     availability
122         .ifPresent(availabilityVal -> addEntryWithIndent(USING_AVAILABILITY_ZONES, String.valueOf(
123             availabilityVal.isUseAvailabilityZonesForHighAvailability()), TAB + TAB));
124   }
125
126   private void addVspVlmEntries(InformationArtifactData informationArtifactData) {
127     addEntryWithIndent(TITLE, "", "");
128     Optional<VspDetails> vspDetails = Optional.of(informationArtifactData).map
129         (InformationArtifactData::getVspDetails);
130     addEntryWithIndent(VSP_NAME, informationArtifactData.getVspDetails().getName(), TAB);
131     addEntryWithIndent(VSP_DESC, informationArtifactData.getVspDetails().getDescription(), TAB);
132     addEntryWithIndent(VSP_VERSION,
133         roundVersionAsNeeded(informationArtifactData.getVspDetails().getVersion()), TAB);
134     addEntryWithIndent(VSP_VENDOR, informationArtifactData.getVspDetails().getVendorName(), TAB);
135     addEntryWithIndent(VSP_CATEGORY, informationArtifactData.getVspDetails().getCategory(), TAB);
136     addEntryWithIndent(LICENSE_DETAILS, "", TAB);
137     addEntryWithIndent(LICENSE_MODEL_VERSION,
138         informationArtifactData.getVspDetails().getVlmVersion() == null
139             ? ""
140             : informationArtifactData.getVspDetails().getVlmVersion().toString(),
141         TAB + TAB);
142     addEntryWithIndent(LICENSE_AGREEMENT_NAME, informationArtifactData.getVspDetails()
143         .getLicenseAgreement(), TAB + TAB);
144     addEntryWithIndent(LIST_OF_FEATURE_GROUPS, "", TAB + TAB);
145     vspDetails.ifPresent(vspDets -> addListEntriesWithIndent(vspDets
146         .getFeatureGroups(), TAB + TAB + TAB));
147   }
148
149   static String roundVersionAsNeeded(Version version) {
150     if (version.isFinal()) {
151       return version.toString();
152     } else {
153       return String.valueOf(Math.ceil(Double.valueOf(version.toString())));
154     }
155   }
156
157   private void addRecoveryEntriesPerComponent(ComponentQuestionnaire componentQuestionnaire) {
158     addEntryWithIndent(RECOVERY_DETAILS, "", TAB + TAB + TAB);
159     Optional<Recovery> recovery = Optional.of(componentQuestionnaire).map(
160         ComponentQuestionnaire::getGeneral).map(
161         org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.general
162             .General::getRecovery);
163     recovery.ifPresent(recoveryVal -> addEntryWithIndent(RECOVERY_DETAILS_POINT, String.valueOf(
164         recoveryVal.getPointObjective()), TAB + TAB + TAB + TAB));
165
166     recovery.ifPresent(recoveryVal -> addEntryWithIndent(RECOVERY_DETAILS_TIME, String.valueOf(
167         recoveryVal.getTimeObjective()), TAB + TAB + TAB + TAB));
168   }
169
170   private void addEntriesPerNic(NicQuestionnaire nicQuestionnaire) {
171     addEntryWithIndent(VNICS, "", TAB + TAB + TAB);
172     Optional<Network> networkOpt = Optional.of(nicQuestionnaire).map(
173         NicQuestionnaire::getNetwork);
174
175     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_NAME, network
176             .getNetworkDescription(),
177         TAB + TAB + TAB + TAB));
178     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_PURPOSE, network
179         .getNetworkDescription(), TAB + TAB + TAB + TAB));
180     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_INT_EXT, network
181         .getNetworkDescription(), TAB + TAB + TAB + TAB));
182     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_NETWORK, network.toString(),
183         TAB + TAB + TAB + TAB));
184     addEntryWithIndent(VNICS_PROTOCOLS, nicQuestionnaire.getProtocols() == null ? "" : nicQuestionnaire.getProtocols().toString(),
185         TAB + TAB + TAB + TAB);
186
187     Optional<IpConfiguration> ipconfigOpt = Optional.of(nicQuestionnaire).map
188         (NicQuestionnaire::getIpConfiguration);
189     ipconfigOpt.ifPresent(ipconfig -> addEntryWithIndent(VNICS_IPV4, String.valueOf(ipconfig
190         .isIpv4Required()), TAB + TAB + TAB + TAB));
191     ipconfigOpt.ifPresent(ipconfig -> addEntryWithIndent(VNICS_IPV6, String.valueOf(ipconfig
192         .isIpv6Required()), TAB + TAB + TAB + TAB));
193   }
194
195   private void addEntriesPerComponent(ComponentQuestionnaire componentQuestionnaire) {
196     addEntryWithIndent(VFC_NAME, "", TAB + TAB + TAB);
197     addEntryWithIndent(VFC_DESC, "", TAB + TAB + TAB);
198     addEntryWithIndent(VFC_IMAGES, "", TAB + TAB + TAB);
199     //todo component name +desc +img+vcpu
200     addEntryWithIndent(VFC_COMPUTE, "", TAB + TAB + TAB);
201     addEntryWithIndent(VFC_COMPUTE_VCPU, "", TAB + TAB + TAB + TAB);
202     addEntryWithIndent(VFC_COMPUTE_CPU_OVER_SUBSCRIPTION, "",
203         TAB + TAB + TAB + TAB);
204     addEntryWithIndent(VFC_COMPUTE_MEMORY, "", TAB + TAB + TAB + TAB);
205     addEntryWithIndent(VFC_COMPUTE_DISK, "", TAB + TAB + TAB + TAB);
206
207     addEntryWithIndent(HYPERVISOR_DETAILS, "", TAB + TAB + TAB);
208
209     Optional<Hypervisor> hypervisorOpt = Optional.of(componentQuestionnaire).map(
210         ComponentQuestionnaire::getGeneral).map(
211         org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.general
212             .General::getHypervisor);
213     hypervisorOpt.ifPresent(hypervisor -> addEntryWithIndent(HYPERVISOR_DETAILS_NAME, hypervisor
214         .getHypervisor(), TAB
215         + TAB + TAB + TAB));
216
217     hypervisorOpt.ifPresent(hypervisor -> addEntryWithIndent(HYPERVISOR_DETAILS_DRIVERS, hypervisor
218         .getDrivers(), TAB + TAB + TAB + TAB));
219
220     addEntryWithIndent(GUEST_OS_DETAILS, "", TAB + TAB + TAB);
221     Optional<GuestOS> guestOSOptional = Optional.of(componentQuestionnaire).map
222         (ComponentQuestionnaire::getCompute).map(Compute::getGuestOS);
223     guestOSOptional.ifPresent(guestOs -> addEntryWithIndent(GUEST_OS_NAME, guestOs.getName(),
224         TAB + TAB + TAB + TAB));
225
226
227     guestOSOptional
228         .ifPresent(guestOs -> addEntryWithIndent(GUEST_OS_BIT_SIZE, String.valueOf(guestOs
229             .getBitSize()), TAB + TAB + TAB + TAB));
230     guestOSOptional.ifPresent(guestOs -> addEntryWithIndent(GUEST_OS_TOOLS, guestOs.getTools
231         (), TAB + TAB + TAB + TAB));
232
233     addEntryWithIndent(VFC_INSTANCE_NUMBER, "",
234         TAB + TAB + TAB);
235
236     Optional<NumOfVMs> numVmsOpt = Optional.of(componentQuestionnaire).map
237         (ComponentQuestionnaire::getCompute).map(Compute::getNumOfVMs);
238     numVmsOpt.ifPresent(numVms -> addEntryWithIndent(VFC_INSTANCE_NUMBER_MIN, String.valueOf
239         (numVms.getMinimum()), TAB + TAB + TAB + TAB));
240
241     numVmsOpt.ifPresent(numVms -> addEntryWithIndent(VFC_INSTANCE_NUMBER_MAX, String.valueOf
242         (numVms.getMaximum()), TAB + TAB + TAB + TAB));
243   }
244
245   private void addListEntriesWithIndent(List<String> fieldValues, String indent) {
246     int counter = 1;
247     if (fieldValues == null) {
248       return;
249     }
250     for (String fieldValue : fieldValues) {
251       textArtifact.append(indent).append(counter++).append(".").append(TAB).append(fieldValue)
252           .append
253               (NL);
254     }
255
256   }
257
258
259   private void addEntryWithIndent(String fieldName, String fieldValue, String indent) {
260     textArtifact.append(indent).append(fieldName).append(SPACE).append(fieldValue).append(NL);
261   }
262 }