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