CSIT Fix for SDC-2585
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / main / GetConsumersMenu.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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 fj.data.Either;
24 import org.openecomp.sdc.asdctool.cli.CLIToolData;
25 import org.openecomp.sdc.asdctool.cli.SpringCLITool;
26 import org.openecomp.sdc.asdctool.configuration.GetConsumersConfiguration;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.openecomp.sdc.be.model.operations.impl.ConsumerOperation;
29 import org.openecomp.sdc.be.resources.data.ConsumerData;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.List;
34
35 public class GetConsumersMenu extends SpringCLITool {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(GetConsumersMenu.class);
38
39     public static void main(String[] args) {
40         GetConsumersMenu getConsumersMenu = new GetConsumersMenu();
41         CLIToolData cliToolData = getConsumersMenu.init(args);
42         ConsumerOperation consumersService = cliToolData.getSpringApplicationContext().getBean(ConsumerOperation.class);
43         printConsumers(getConsumersMenu, consumersService);
44     }
45
46     private static void printConsumers(GetConsumersMenu getConsumersMenu, ConsumerOperation consumersService) {
47         Either<List<ConsumerData>, StorageOperationStatus> allConsumers = consumersService.getAll();
48         allConsumers.left().foreachDoEffect(getConsumersMenu::printConsumers);
49         allConsumers.right().foreachDoEffect(getConsumersMenu::printErr);
50     }
51
52     private void printConsumers(List<ConsumerData> consumers) {
53         System.out.println("SDC consumers: ");
54         consumers.forEach(consumer -> {
55             System.out.println("#########################");
56             System.out.println(consumer);
57         });
58         System.exit(0);
59     }
60
61     private void printErr(StorageOperationStatus err) {
62         String errMsg = String.format("failed to fetch consumers. reason: %s", err);
63         LOGGER.error(errMsg);
64         System.err.print(errMsg);
65         System.exit(1);
66     }
67
68     @Override
69     protected String commandName() {
70         return "get-consumers";
71     }
72
73     @Override
74     protected Class<?> getSpringConfigurationClass() {
75         return GetConsumersConfiguration.class;
76     }
77 }