9aba18ed575a10409636f75db6428cbc6b37a0f9
[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(TxtInformationArtifactConstants.HEADER);
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     textArtifact.append(TxtInformationArtifactConstants.FOOTER);
88     return textArtifact.toString();
89
90   }
91
92   private void addDataEntries(InformationArtifactData informationArtifactData) {
93     addEntryWithIndent(STORAGE_BACKUP_DETAILS, "", TAB);
94
95     Optional<StorageDataReplication> storageDataReplication =
96         Optional.of(informationArtifactData).map(InformationArtifactData::getVspQuestionnaire)
97             .map(VspQuestionnaire::getGeneral).map(General::getStorageDataReplication);
98     storageDataReplication
99         .ifPresent(replication -> addEntryWithIndent(IS_DATA_REPLICATION,
100             String.valueOf(replication.isStorageReplicationAcrossRegion()), TAB + TAB));
101
102
103     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_SIZE_TO_REP,
104         String.valueOf(rep.getStorageReplicationSize()), TAB + TAB));
105
106     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_REP_FREQUENCY,
107         String.valueOf(rep.getStorageReplicationFrequency()), TAB + TAB));
108
109     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_REP_SOURCE,
110         String.valueOf(rep.getStorageReplicationSource()), TAB + TAB));
111
112     storageDataReplication.ifPresent(rep -> addEntryWithIndent(DATA_REP_DEST,
113         String.valueOf(rep.getStorageReplicationDestination()), TAB + TAB));
114   }
115
116   private void addAvailabilityEntries() {
117     addEntryWithIndent(HIGH_AVAILABILITY, "", TAB);
118
119     Optional<Availability> availability =
120         Optional.of(new InformationArtifactData()).map(InformationArtifactData::getVspQuestionnaire)
121             .map(VspQuestionnaire::getGeneral).map(General::getAvailability);
122     availability
123         .ifPresent(availabilityVal -> addEntryWithIndent(USING_AVAILABILITY_ZONES, String.valueOf(
124             availabilityVal.isUseAvailabilityZonesForHighAvailability()), TAB + TAB));
125   }
126
127   private void addVspVlmEntries(InformationArtifactData informationArtifactData) {
128     addEntryWithIndent(TITLE, "", "");
129     Optional<VspDetails> vspDetails = Optional.of(informationArtifactData).map
130         (InformationArtifactData::getVspDetails);
131     addEntryWithIndent(VSP_NAME, informationArtifactData.getVspDetails().getName(), TAB);
132     addEntryWithIndent(VSP_DESC, informationArtifactData.getVspDetails().getDescription(), TAB);
133     addEntryWithIndent(VSP_VERSION,
134         roundVersionAsNeeded(informationArtifactData.getVspDetails().getVersion()), TAB);
135     addEntryWithIndent(VSP_VENDOR, informationArtifactData.getVspDetails().getVendorName(), TAB);
136     addEntryWithIndent(VSP_CATEGORY, informationArtifactData.getVspDetails().getCategory(), TAB);
137     addEntryWithIndent(LICENSE_DETAILS, "", TAB);
138     addEntryWithIndent(LICENSE_MODEL_VERSION,
139         informationArtifactData.getVspDetails().getVlmVersion() == null
140             ? ""
141             : informationArtifactData.getVspDetails().getVlmVersion().toString(),
142         TAB + TAB);
143     addEntryWithIndent(LICENSE_AGREEMENT_NAME, informationArtifactData.getVspDetails()
144         .getLicenseAgreement(), TAB + TAB);
145     addEntryWithIndent(LIST_OF_FEATURE_GROUPS, "", TAB + TAB);
146     vspDetails.ifPresent(vspDets -> addListEntriesWithIndent(vspDets
147         .getFeatureGroups(), TAB + TAB + TAB));
148   }
149
150   static String roundVersionAsNeeded(Version version) {
151     if (version.isFinal()) {
152       return version.toString();
153     } else {
154       return String.valueOf(Math.ceil(Double.valueOf(version.toString())));
155     }
156   }
157
158   private void addRecoveryEntriesPerComponent(ComponentQuestionnaire componentQuestionnaire) {
159     addEntryWithIndent(RECOVERY_DETAILS, "", TAB + TAB + TAB);
160     Optional<Recovery> recovery = Optional.of(componentQuestionnaire).map(
161         ComponentQuestionnaire::getGeneral).map(
162         org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.general
163             .General::getRecovery);
164     recovery.ifPresent(recoveryVal -> addEntryWithIndent(RECOVERY_DETAILS_POINT, String.valueOf(
165         recoveryVal.getPointObjective()), TAB + TAB + TAB + TAB));
166
167     recovery.ifPresent(recoveryVal -> addEntryWithIndent(RECOVERY_DETAILS_TIME, String.valueOf(
168         recoveryVal.getTimeObjective()), TAB + TAB + TAB + TAB));
169   }
170
171   private void addEntriesPerNic(NicQuestionnaire nicQuestionnaire) {
172     addEntryWithIndent(VNICS, "", TAB + TAB + TAB);
173     Optional<Network> networkOpt = Optional.of(nicQuestionnaire).map(
174         NicQuestionnaire::getNetwork);
175
176     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_NAME, network
177             .getNetworkDescription(),
178         TAB + TAB + TAB + TAB));
179     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_PURPOSE, network
180         .getNetworkDescription(), TAB + TAB + TAB + TAB));
181     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_INT_EXT, network
182         .getNetworkDescription(), TAB + TAB + TAB + TAB));
183     networkOpt.ifPresent(network -> addEntryWithIndent(VNICS_NETWORK, network.toString(),
184         TAB + TAB + TAB + TAB));
185     addEntryWithIndent(VNICS_PROTOCOLS, nicQuestionnaire.getProtocols() == null ? "" : nicQuestionnaire.getProtocols().toString(),
186         TAB + TAB + TAB + TAB);
187
188     Optional<IpConfiguration> ipconfigOpt = Optional.of(nicQuestionnaire).map
189         (NicQuestionnaire::getIpConfiguration);
190     ipconfigOpt.ifPresent(ipconfig -> addEntryWithIndent(VNICS_IPV4, String.valueOf(ipconfig
191         .isIpv4Required()), TAB + TAB + TAB + TAB));
192     ipconfigOpt.ifPresent(ipconfig -> addEntryWithIndent(VNICS_IPV6, String.valueOf(ipconfig
193         .isIpv6Required()), TAB + TAB + TAB + TAB));
194   }
195
196   private void addEntriesPerComponent(ComponentQuestionnaire componentQuestionnaire) {
197     addEntryWithIndent(VFC_NAME, "", TAB + TAB + TAB);
198     addEntryWithIndent(VFC_DESC, "", TAB + TAB + TAB);
199     addEntryWithIndent(VFC_IMAGES, "", TAB + TAB + TAB);
200     //todo component name +desc +img+vcpu
201     addEntryWithIndent(VFC_COMPUTE, "", TAB + TAB + TAB);
202     addEntryWithIndent(VFC_COMPUTE_VCPU, "", TAB + TAB + TAB + TAB);
203     addEntryWithIndent(VFC_COMPUTE_CPU_OVER_SUBSCRIPTION, "",
204         TAB + TAB + TAB + TAB);
205     addEntryWithIndent(VFC_COMPUTE_MEMORY, "", TAB + TAB + TAB + TAB);
206     addEntryWithIndent(VFC_COMPUTE_DISK, "", TAB + TAB + TAB + TAB);
207
208     addEntryWithIndent(HYPERVISOR_DETAILS, "", TAB + TAB + TAB);
209
210     Optional<Hypervisor> hypervisorOpt = Optional.of(componentQuestionnaire).map(
211         ComponentQuestionnaire::getGeneral).map(
212         org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.general
213             .General::getHypervisor);
214     hypervisorOpt.ifPresent(hypervisor -> addEntryWithIndent(HYPERVISOR_DETAILS_NAME, hypervisor
215         .getHypervisor(), TAB
216         + TAB + TAB + TAB));
217
218     hypervisorOpt.ifPresent(hypervisor -> addEntryWithIndent(HYPERVISOR_DETAILS_DRIVERS, hypervisor
219         .getDrivers(), TAB + TAB + TAB + TAB));
220
221     addEntryWithIndent(GUEST_OS_DETAILS, "", TAB + TAB + TAB);
222     Optional<GuestOS> guestOSOptional = Optional.of(componentQuestionnaire).map
223         (ComponentQuestionnaire::getCompute).map(Compute::getGuestOS);
224     guestOSOptional.ifPresent(guestOs -> addEntryWithIndent(GUEST_OS_NAME, guestOs.getName(),
225         TAB + TAB + TAB + TAB));
226
227
228     guestOSOptional
229         .ifPresent(guestOs -> addEntryWithIndent(GUEST_OS_BIT_SIZE, String.valueOf(guestOs
230             .getBitSize()), TAB + TAB + TAB + TAB));
231     guestOSOptional.ifPresent(guestOs -> addEntryWithIndent(GUEST_OS_TOOLS, guestOs.getTools
232         (), TAB + TAB + TAB + TAB));
233
234     addEntryWithIndent(VFC_INSTANCE_NUMBER, "",
235         TAB + TAB + TAB);
236
237     Optional<NumOfVMs> numVmsOpt = Optional.of(componentQuestionnaire).map
238         (ComponentQuestionnaire::getCompute).map(Compute::getNumOfVMs);
239     numVmsOpt.ifPresent(numVms -> addEntryWithIndent(VFC_INSTANCE_NUMBER_MIN, String.valueOf
240         (numVms.getMinimum()), TAB + TAB + TAB + TAB));
241
242     numVmsOpt.ifPresent(numVms -> addEntryWithIndent(VFC_INSTANCE_NUMBER_MAX, String.valueOf
243         (numVms.getMaximum()), TAB + TAB + TAB + TAB));
244   }
245
246   private void addListEntriesWithIndent(List<String> fieldValues, String indent) {
247     int counter = 1;
248     if (fieldValues == null) {
249       return;
250     }
251     for (String fieldValue : fieldValues) {
252       textArtifact.append(indent).append(counter++).append(".").append(TAB).append(fieldValue)
253           .append
254               (NL);
255     }
256
257   }
258
259
260   private void addEntryWithIndent(String fieldName, String fieldValue, String indent) {
261     textArtifact.append(indent).append(fieldName).append(SPACE).append(fieldValue).append(NL);
262   }
263 }