Bump upstreams to 2022.09 Chlorine
[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.commands.Argument;
12 import org.apache.karaf.shell.commands.Command;
13 import org.apache.karaf.shell.console.OsgiCommandSupport;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.OdlSrmRpcsService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityNameBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeBase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeService;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @Command(scope = "srm", name = "reinstall", description = "Reinstall service or instance")
27 public class ReinstallCommand extends OsgiCommandSupport {
28     private static final Logger LOG = LoggerFactory.getLogger(ReinstallCommand.class);
29
30     @Argument(index = 0, name = "name", description = "EntityName of type service, required",
31         required = false, multiValued = false)
32     String name;
33
34     private final OdlSrmRpcsService srmRpcService;
35     private final EntityTypeBase entityType = EntityTypeService.VALUE;
36
37     public ReinstallCommand(OdlSrmRpcsService srmRpcService) {
38         this.srmRpcService = srmRpcService;
39     }
40
41     @Override
42     @Deprecated
43     protected @Nullable Object doExecute() throws Exception {
44         ReinstallInput input = getInput();
45         if (input == null) {
46             // We've already shown the relevant error msg
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     private void printResult(RpcResult<ReinstallOutput> reinstallResult) {
56         StringBuilder strResult = new StringBuilder("");
57         if (reinstallResult.isSuccessful()) {
58             strResult.append("RPC call to reinstall was successful");
59             LOG.trace("RPC Result: {}", reinstallResult.getResult());
60         } else {
61             strResult.append("RPC Call to reinstall failed.\n")
62                 .append("ErrorMsg: ").append(reinstallResult.getResult().getMessage());
63             LOG.trace("RPC Result: {}", reinstallResult.getResult());
64         }
65         session.getConsole().println(strResult.toString());
66     }
67
68     private @Nullable ReinstallInput getInput() {
69         EntityNameBase entityName = SrmCliUtils.getEntityName(entityType, name);
70         if (entityName == null) {
71             session.getConsole().println(SrmCliUtils.getNameHelp(entityType));
72             return null;
73         }
74         ReinstallInputBuilder inputBuilder = new ReinstallInputBuilder();
75         inputBuilder.setEntityType(entityType);
76         inputBuilder.setEntityName(entityName);
77         return inputBuilder.build();
78     }
79 }