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