Andrea Campanella | e1e3e44 | 2019-10-21 13:45:32 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.odtn.cli.impl; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Preconditions; |
| 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.Option; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.odtn.GnpyService; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | @Beta |
| 32 | @Service |
| 33 | @Command(scope = "onos", name = "odtn-connect-gnpy-command", |
| 34 | description = "show tapi context command") |
| 35 | public class ConnectGnpyCommand extends AbstractShellCommand { |
| 36 | |
| 37 | private static final Logger log = getLogger(ConnectGnpyCommand.class); |
| 38 | |
| 39 | @Argument(index = 0, name = "protocol", |
| 40 | description = "protocol for requests, e.g. HTTP or HTTPS", |
| 41 | required = true, multiValued = false) |
| 42 | String protocol = ""; |
| 43 | |
| 44 | @Argument(index = 1, name = "ip", |
| 45 | description = "Ip of GNPy instance", |
| 46 | required = true, multiValued = false) |
| 47 | String ip = ""; |
| 48 | |
| 49 | @Argument(index = 2, name = "port", |
| 50 | description = "Protocol of GNPy instance", |
| 51 | required = true, multiValued = false) |
| 52 | String port = ""; |
| 53 | |
| 54 | @Argument(index = 3, name = "username", |
| 55 | description = "Username of GNPy instance", |
| 56 | required = true, multiValued = false) |
| 57 | String username = ""; |
| 58 | |
| 59 | @Argument(index = 4, name = "password", |
| 60 | description = "Password of GNPy instance", |
| 61 | required = true, multiValued = false) |
| 62 | String password = ""; |
| 63 | |
| 64 | @Option(name = "-d", aliases = "--disconnect", |
| 65 | description = "If this argument is passed the connection with gNPY is removed.", |
| 66 | required = false, multiValued = false) |
| 67 | private boolean disconnect = false; |
| 68 | |
| 69 | @Override |
| 70 | protected void doExecute() { |
| 71 | GnpyService service = get(GnpyService.class); |
| 72 | String msg; |
| 73 | if (disconnect) { |
| 74 | msg = service.disconnectGnpy() ? "gnpy disconnect" : "error in disconnecting gnpy"; |
| 75 | } else { |
| 76 | Preconditions.checkNotNull(ip, "Ip must be specified"); |
| 77 | Preconditions.checkNotNull(password, "password must be specified"); |
| 78 | msg = service.connectGnpy(protocol, ip, port, username, password) ? "gnpy connected" : |
| 79 | "error in connecting gnpy, please check logs"; |
| 80 | } |
| 81 | print(msg); |
| 82 | } |
| 83 | |
| 84 | } |