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