[SDC-29] rebase continue work to align source
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / main / MigrationMenu.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.asdctool.main;
22
23 import java.util.Arrays;
24 import java.util.Optional;
25
26 import org.openecomp.sdc.asdctool.impl.PopulateComponentCache;
27 import org.openecomp.sdc.asdctool.impl.migration.v1604.AppConfig;
28 import org.openecomp.sdc.asdctool.impl.migration.v1604.DerivedFromAlignment;
29 import org.openecomp.sdc.asdctool.impl.migration.v1604.GroupsAlignment;
30 import org.openecomp.sdc.asdctool.impl.migration.v1604.ServiceMigration;
31 import org.openecomp.sdc.asdctool.impl.migration.v1604.VfcNamingAlignment;
32 import org.openecomp.sdc.asdctool.impl.migration.v1607.CsarMigration;
33 import org.openecomp.sdc.asdctool.impl.migration.v1610.TitanFixUtils;
34 import org.openecomp.sdc.asdctool.impl.migration.v1610.ToscaArtifactsAlignment;
35 import org.openecomp.sdc.asdctool.impl.migration.v1702.Migration1702;
36 import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707;
37 import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707Config;
38 import org.openecomp.sdc.asdctool.impl.migration.v1707.DistributionStatusUpdate;
39 import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707VnfFix;
40 import org.openecomp.sdc.asdctool.impl.migration.v1707.VfModulesPropertiesAdding;
41 import org.openecomp.sdc.be.config.ConfigurationManager;
42 import org.openecomp.sdc.common.api.ConfigurationSource;
43 import org.openecomp.sdc.common.impl.ExternalConfiguration;
44 import org.openecomp.sdc.common.impl.FSConfigurationSource;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
48
49 public class MigrationMenu {
50
51         private static Logger log = LoggerFactory.getLogger(MigrationMenu.class.getName());
52         private static final String SERVICE_MIGARTION_BEAN = "serviceMigrationBean";
53
54         private static enum MigrationOperationEnum {
55                 MIGRATION_1602_1604("migrate-1602-1604", SERVICE_MIGARTION_BEAN), 
56                 ALIGN_DERIVED_FROM_1604("align-derived-from-1604", "derivedFromAlignment"), 
57                 MIGRATE_1604_1607("migrate-1604-1607", SERVICE_MIGARTION_BEAN), 
58                 ALIGN_VFC_NAMES_1604("align-vfc-names-1604", "vfcNamingAlignmentBean"), 
59                 TEST_REMOVE_HEAT_PLACEHOLDERS("testremoveheatplaceholders",     SERVICE_MIGARTION_BEAN), 
60                 TEST_ADD_GROUP_UUIDS("testaddgroupuuids", SERVICE_MIGARTION_BEAN), 
61                 ALIGN_GROUPS("align-groups", "groupsAlignment"), 
62                 CLEAN_CSAR("clean-csar", "csarMigration"), 
63                 POPULATE_COMPONENT_CACHE("populate-component-cache", "populateComponentCache"), 
64                 FIX_PROPERTIES("fix-properties", "titanFixUtils"), 
65                 ALIGN_TOSCA_ARTIFACTS("align-tosca-artifacts", "toscaArtifactsAlignment"), 
66                 FIX_ICONS("fix-icons", "titanFixUtils"),
67                 MIGRATION_1610_1702("migrate-1610-1702", "migration1702"),
68                 MIGRATION_1702_1707("migrate-1702-1707", "migration1707"),
69                 DISTRIBUTION_STATUS_UPDATE_1707("distribution-status-update-1707", "distributionStatusUpdate"),
70                 VFMODULES_PROPERTIES_ADDING("vfModules-properties-adding", "vfModulesPropertiesAdding"),
71                 MIGRATION_1707_RELATIONS_FIX("fix-relations-after-migration-1707", "migration1707relationsFix"),
72                 MIGRATION_1707_VNF_FIX("fix-vnf-after-migration-1707", "migration1707vnfFix");
73                 // UPDATE_DATA_TYPES("update_data_types", "updateDataTypes");
74
75                 private String value, beanName;
76
77                 public static MigrationOperationEnum findByValue(String value) {
78                         Optional<MigrationOperationEnum> optionalFound = Arrays.asList(MigrationOperationEnum.values()).stream().filter(e -> e.getValue().equalsIgnoreCase(value)).findAny();
79                         return optionalFound.isPresent() ? optionalFound.get() : null;
80                 }
81
82                 MigrationOperationEnum(String value, String beanName) {
83                         this.value = value;
84                         this.beanName = beanName;
85                 }
86
87                 public String getValue() {
88                         return value;
89                 }
90
91                 public String getBeanName() {
92                         return beanName;
93                 }
94         };
95
96         //arguments will be [operation] [version] [config path] [align derived - optional]
97         //example : migrate-1610-1702 1702 /home/config
98         public static void main(String[] args) throws Exception {
99
100                 if (args == null || args.length < 3) {
101                         usageAndExit();
102                 }
103                 MigrationOperationEnum operationEnum = MigrationOperationEnum.findByValue(args[0]);
104                 String appConfigDir = args[2];
105                 String dataInputFileDir = null;
106                 if (operationEnum == MigrationOperationEnum.ALIGN_DERIVED_FROM_1604 ) {
107                         dataInputFileDir = args[3];
108                 }
109                 log.info("Run with configuration folder {}", appConfigDir);
110                 AnnotationConfigApplicationContext context = initContext(appConfigDir);
111                 try {
112                         ServiceMigration serviceMigration = (ServiceMigration) context.getBean(SERVICE_MIGARTION_BEAN);
113                         switch (operationEnum) {
114                         case MIGRATION_1602_1604:
115                                 log.info("Start Titan migration from 1602 version to 1604");
116                                 if (serviceMigration.migrate1602to1604(appConfigDir)) {
117                                         log.info("Titan migration from 1602 version to 1604 was finished successfull");
118                                         System.exit(0);
119                                 } else {
120                                         log.info("Titan migration from 1602 version to 1604 was failed");
121                                         System.exit(2);
122                                 }
123                                 break;
124                         case MIGRATE_1604_1607:
125                                 log.info("Start Titan migration from 1604 version to 1607");
126                                 if (serviceMigration.migrate1604to1607(appConfigDir)) {
127                                         log.info("Titan migration from 1604 version to 1607 was finished successfull");
128                                         System.exit(0);
129                                 } else {
130                                         log.info("Titan migration from 1604 version to 1607 was failed");
131                                         System.exit(2);
132                                 }
133                                 break;
134                         case ALIGN_VFC_NAMES_1604:
135                                 VfcNamingAlignment vfcNamingAlignment = (VfcNamingAlignment) context.getBean(operationEnum.getBeanName());
136                                 log.info("Start VFC naming alignment on 1604");
137                                 if (vfcNamingAlignment.alignVfcNames1604(appConfigDir)) {
138                                         log.info("VFC naming alignment on 1604 was finished successfull");
139                                         System.exit(0);
140                                 } else {
141                                         log.info("VFC naming alignment on 1604 was failed");
142                                         System.exit(2);
143                                 }
144                                 break;
145                         case TEST_REMOVE_HEAT_PLACEHOLDERS:
146                                 boolean check = serviceMigration.testRemoveHeatPlaceHolders(appConfigDir);
147                                 if (check == true) {
148                                         System.exit(0);
149                                 } else {
150                                         System.exit(2);
151                                 }
152                                 break;
153                         case TEST_ADD_GROUP_UUIDS:
154                                 check = serviceMigration.testAddGroupUuids(appConfigDir);
155                                 if (check == true) {
156                                         System.exit(0);
157                                 } else {
158                                         System.exit(2);
159                                 }
160                                 break;
161                         case ALIGN_DERIVED_FROM_1604:
162                                 DerivedFromAlignment derivedFromAlignment = (DerivedFromAlignment) context.getBean(operationEnum.getBeanName());
163                                 log.info("Start derived from alignment on 1604");
164                                 if (derivedFromAlignment.alignDerivedFrom1604(appConfigDir, dataInputFileDir)) {
165                                         log.info("Derived from alignment on 1604 was finished successfull");
166                                         System.exit(0);
167                                 } else {
168                                         log.info("Derived from alignment on 1604 was failed");
169                                         System.exit(2);
170                                 }
171                                 break;
172                         case ALIGN_GROUPS:
173                                 GroupsAlignment groupsAlignment = (GroupsAlignment) context.getBean(operationEnum.getBeanName());
174                                 log.debug("Start derived from alignment on 1604");
175                                 if (groupsAlignment.alignGroups(appConfigDir)) {
176                                         log.debug("Groups alignment was finished successfull");
177                                         System.exit(0);
178                                 } else {
179                                         log.debug("Groups alignment was failed");
180                                         System.exit(2);
181                                 }
182                                 break;
183                         case CLEAN_CSAR:
184                                 log.info("Start remove CSAR resources");
185                                 CsarMigration csarMigration = (CsarMigration) context.getBean(operationEnum.getBeanName());
186                                 if (csarMigration.removeCsarResources()) {
187                                         log.info("Remove CSAR resources finished successfully");
188                                         System.exit(0);
189                                 } else {
190                                         log.info("Remove CSAR resources failed");
191                                         System.exit(2);
192                                 }
193                                 break;
194                         case POPULATE_COMPONENT_CACHE:
195                                 PopulateComponentCache populateComponentCache = (PopulateComponentCache) context.getBean(operationEnum.getBeanName());
196                                 populateComponentCache.populateCache();
197                                 System.exit(0);
198                                 break;
199                         case FIX_PROPERTIES:
200                                 log.debug("Start fix capability properties types");
201                                 TitanFixUtils titanFixUtils = (TitanFixUtils) context.getBean(operationEnum.getBeanName());
202                                 if (titanFixUtils.fixCapabiltyPropertyTypes()) {
203                                         log.debug("Fix capability properties types finished successfully");
204                                         System.exit(0);
205                                 } else {
206                                         log.debug("Fix capability properties types failed");
207                                         System.exit(2);
208                                 }
209                                 break;
210                         case FIX_ICONS:
211                                 log.info("Start fix icons of vl and eline");
212                                 titanFixUtils = (TitanFixUtils) context.getBean(operationEnum.getBeanName());
213                                 if (titanFixUtils.fixIconsInNormatives()) {
214                                         log.info("Fix icons of vl and eline finished successfully");
215                                         System.exit(0);
216                                 } else {
217                                         log.info("Fix icons of vl and eline failed");
218                                         System.exit(2);
219                                 }
220                                 break;
221                         case ALIGN_TOSCA_ARTIFACTS:
222                                 log.info("Start align tosca artifacts");
223                                 ToscaArtifactsAlignment toscaArtifactsAlignment = (ToscaArtifactsAlignment) context.getBean(operationEnum.getBeanName());
224                                 boolean isSuccessful = toscaArtifactsAlignment.alignToscaArtifacts();
225                                 if (isSuccessful) {
226                                         log.info("Tosca Artifacts alignment was finished successfull");
227                                         System.exit(0);
228                                 } else {
229                                         log.info("Tosca Artifacts alignment has failed");
230                                         System.exit(2);
231                                 }
232                                 break;
233                         case MIGRATION_1610_1702:
234                                 log.info("Start ASDC migration from 1610 to 1702");
235                                 Migration1702 migration = (Migration1702) context.getBean(operationEnum.getBeanName());
236                                 isSuccessful = migration.migrate(appConfigDir);
237                                 if (isSuccessful) {
238                                         log.info("ASDC migration from 1610 to 1702 was finished successful");
239                                         System.exit(0);
240                                 } else{
241                                         log.info("ASDC migration from 1610 to 1702 has failed");
242                                         System.exit(2);
243                                 }
244                         
245                                 break;
246                         case MIGRATION_1702_1707://this migration is currently not needed, but will be commented out for production env
247 //                              log.info("Start ASDC migration from 1702 to 1707");
248 //                              Migration1707 migration1707 = (Migration1707) context.getBean(operationEnum.getBeanName());
249 //                              isSuccessful = migration1707.migrate();
250 //                              if (isSuccessful) {
251 //                                      log.info("SDC migration from 1702 to 1707 was finished successfully");
252 //                                      System.exit(0);
253 //                              } else{
254 //                                      log.info("SDC migration from 1702 to 1707 has failed");
255 //                                      System.exit(2);
256 //                              }
257                                 System.exit(0);
258                                 break;
259                         case VFMODULES_PROPERTIES_ADDING://this migration is currently not needed, but will be commented out for production env
260 //                              log.info("Start adding new properties to vfModules");
261 //                              VfModulesPropertiesAdding migrationVfModulesProperties = (VfModulesPropertiesAdding) context.getBean(operationEnum.getBeanName());
262 //                              isSuccessful = migrationVfModulesProperties.migrate(args[1]);
263 //                              if (isSuccessful) {
264 //                                      log.info("Adding new properties to vfModules was finished successfully");
265 //                                      System.exit(0);
266 //                              } else{
267 //                                      log.info("Adding new properties to vfModules has failed");
268 //                                      System.exit(2);
269 //                              }
270                                 System.exit(0);
271                                 break;
272                         case MIGRATION_1707_VNF_FIX://this migration is currently not needed, but will be commented out for production env
273 //                              log.info("Start fixing vnf after 1707 migration");
274 //                              Migration1707VnfFix migrationVnfFix = (Migration1707VnfFix) context.getBean(operationEnum.getBeanName());
275 //                              isSuccessful = migrationVnfFix.migrate();
276 //                              if (isSuccessful) {
277 //                                      log.info("Fixing VNFs after 1707 migration was finished successfully");
278 //                                      System.exit(0);
279 //                              } else{
280 //                                      log.info("Fixing VNFs after 1707 migration has failed");
281 //                                      System.exit(2);
282 //                              }
283                                 System.exit(0);
284                                 break;
285                         case DISTRIBUTION_STATUS_UPDATE_1707://not needed can be dropped
286 //                              log.info("Start Distribution status update 1707");
287 //                              DistributionStatusUpdate distStatusUpdate = (DistributionStatusUpdate) context.getBean(operationEnum.getBeanName());
288 //                              isSuccessful = distStatusUpdate.migrate();
289 //                              if (isSuccessful) {
290 //                                      log.info("ASDC Distribution status update 1707 was finished successful");
291 //                                      System.exit(0);
292 //                              } else{
293 //                                      log.info("ASDC Distribution status update 1707 has failed");
294 //                                      System.exit(2);
295 //                              }
296                                 System.exit(0);
297                                 break;
298                         case MIGRATION_1707_RELATIONS_FIX://not needed can be dropped
299 //                              log.info("Start fixing relations after 1707 migration");
300 //                              Migration migrationFix = (Migration1707RelationsFix) context.getBean(operationEnum.getBeanName());
301 //                              isSuccessful = migrationFix.migrate();
302 //                              if (isSuccessful) {
303 //                                      log.info("Fixing relations after 1707 migration was finished successfully");
304 //                                      System.exit(0);
305 //                              } else{
306 //                                      log.info("Fixing relations after 1707 migration has failed");
307 //                                      System.exit(2);
308 //                              }
309                                 System.exit(0);
310                                 break;
311                         default:
312                                 usageAndExit();
313                         }
314                 } catch (Exception t) {
315                         log.info("Failed {} with exception: {}", operationEnum, t.toString());
316                         t.printStackTrace();
317                         log.debug("Error while Running MigrationMenu {}", t.getMessage(), t);
318                         System.exit(3);
319                 } finally {
320                         context.close();
321                 }
322         }
323
324         private static AnnotationConfigApplicationContext initContext(String appConfigDir) {
325                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
326                 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
327                 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class, Migration1707Config.class);
328                 return context;
329         }
330
331         private static void usageAndExit() {
332                 MigrationUsage();
333                 System.exit(1);
334         }
335
336         private static void MigrationUsage() {
337                 System.out.println("Usage: migrate-1602-1604 <configuration dir>");
338                 System.out.println("Usage: migrate-1604-1607 <configuration dir>");
339                 System.out.println("Usage: align-vfc-names-1604 <configuration dir>");
340                 System.out.println("Usage: align-derived-from-1604 <configuration dir> <data_input_file dir>");
341                 System.out.println("Usage: align-groups <configuration dir>");
342                 System.out.println("Usage: fix-properties <configuration dir>");
343                 System.out.println("Usage: migrate-1610-1702 <configuration dir>");
344                 System.out.println("Usage: migrate-1702-1707 <configuration dir>");
345                 System.out.println("Usage: update_data_types <configuration dir> <data_types_input_file path>");
346                 System.out.println("Usage: distribution-status-update-1707");
347                 System.out.println("Usage: vfModules-properties-adding <group_types_input_file path> <configuration dir>");
348                 System.out.println("Usage: fix-relations-after-migration-1707 <configuration dir>");
349                 System.out.println("Usage: fix-vnf-after-migration-1707 <configuration dir>");
350         }
351 }