a8280b82cc8ab4d60196e4b559585b75bd008349
[serviceutils.git] / srm / shell / src / main / java / org / opendaylight / serviceutils / srm / shell / ReinstallCommand.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.serviceutils.srm.shell;
9
10 import java.util.concurrent.Future;
11 import org.apache.karaf.shell.api.action.Action;
12 import org.apache.karaf.shell.api.action.Argument;
13 import org.apache.karaf.shell.api.action.Command;
14 import org.apache.karaf.shell.api.action.lifecycle.Reference;
15 import org.apache.karaf.shell.api.action.lifecycle.Service;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.OdlSrmRpcsService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityNameBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeService;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Implementation class of "srm:reinstall" Karaf shell command.
30  */
31 @Service
32 @Command(scope = "srm", name = "reinstall", description = "Reinstall service or instance")
33 public class ReinstallCommand implements Action {
34     private static final Logger LOG = LoggerFactory.getLogger(ReinstallCommand.class);
35     private static final EntityTypeBase ENTITY_TYPE = EntityTypeService.VALUE;
36
37     @Argument(index = 0, name = "name", description = "EntityName of type service, required",
38         required = false, multiValued = false)
39     private String name;
40     @Reference
41     private OdlSrmRpcsService srmRpcService;
42
43     @Override
44     public @Nullable Object execute() throws Exception {
45         ReinstallInput input = getInput();
46         if (input == null || srmRpcService == null) {
47             return null;
48         }
49         Future<RpcResult<ReinstallOutput>> result = srmRpcService.reinstall(input);
50         RpcResult<ReinstallOutput> reinstallResult = result.get();
51         printResult(reinstallResult);
52         return null;
53     }
54
55     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
56     private static void printResult(RpcResult<ReinstallOutput> reinstallResult) {
57         var sb = new StringBuilder("");
58         if (reinstallResult.isSuccessful()) {
59             sb.append("RPC call to reinstall was successful");
60             LOG.trace("RPC Result: {}", reinstallResult.getResult());
61         } else {
62             sb.append("RPC Call to reinstall failed.\n")
63                 .append("ErrorMsg: ").append(reinstallResult.getResult().getMessage());
64             LOG.trace("RPC Result: {}", reinstallResult.getResult());
65         }
66         System.out.println(sb.toString());
67     }
68
69     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
70     private @Nullable ReinstallInput getInput() {
71         EntityNameBase entityName = SrmCliUtils.getEntityName(ENTITY_TYPE, name);
72         if (entityName == null) {
73             System.out.println(SrmCliUtils.getNameHelp(ENTITY_TYPE));
74             return null;
75         }
76         return new ReinstallInputBuilder()
77             .setEntityType(ENTITY_TYPE)
78             .setEntityName(entityName)
79             .build();
80     }
81 }