Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 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.openstacknode.cli; |
| 18 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknode.api.NodeState; |
| 25 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 26 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 27 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Initializes nodes for OpenStack node service. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 32 | @Service |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "openstack-node-init", |
| 34 | description = "Initializes nodes for OpenStack node service") |
| 35 | public class OpenstackNodeInitCommand extends AbstractShellCommand { |
| 36 | |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 37 | @Option(name = "-a", aliases = "--all", description = "Apply this command to all nodes", |
| 38 | required = false, multiValued = false) |
| 39 | private boolean isAll = false; |
| 40 | |
| 41 | @Option(name = "-i", aliases = "--incomplete", |
| 42 | description = "Apply this command to incomplete nodes", |
| 43 | required = false, multiValued = false) |
| 44 | private boolean isIncomplete = false; |
| 45 | |
| 46 | @Argument(index = 0, name = "hostnames", description = "Hostname(s) to apply this command", |
| 47 | required = false, multiValued = true) |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 48 | private String[] hostnames = null; |
| 49 | |
| 50 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 51 | protected void doExecute() { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 52 | OpenstackNodeService osNodeService = |
| 53 | AbstractShellCommand.get(OpenstackNodeService.class); |
| 54 | OpenstackNodeAdminService osNodeAdminService = |
| 55 | AbstractShellCommand.get(OpenstackNodeAdminService.class); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 56 | |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 57 | if ((!isAll && !isIncomplete && hostnames == null) || |
| 58 | (isAll && isIncomplete) || |
| 59 | (isIncomplete && hostnames != null) || |
| 60 | (hostnames != null && isAll)) { |
| 61 | print("Please specify one of hostname, --all, and --incomplete options."); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (isAll) { |
Jian Li | 5afbea4 | 2018-02-28 10:37:03 +0900 | [diff] [blame] | 66 | hostnames = osNodeService.nodes().stream() |
| 67 | .map(OpenstackNode::hostname).toArray(String[]::new); |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 68 | } else if (isIncomplete) { |
Jian Li | 5afbea4 | 2018-02-28 10:37:03 +0900 | [diff] [blame] | 69 | hostnames = osNodeService.nodes().stream() |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 70 | .filter(osNode -> osNode.state() != NodeState.COMPLETE) |
Jian Li | 5afbea4 | 2018-02-28 10:37:03 +0900 | [diff] [blame] | 71 | .map(OpenstackNode::hostname).toArray(String[]::new); |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 74 | for (String hostname : hostnames) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 75 | OpenstackNode osNode = osNodeService.node(hostname); |
| 76 | if (osNode == null) { |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 77 | print("Unable to find %s", hostname); |
| 78 | continue; |
| 79 | } |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 80 | print("Initializing %s", hostname); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 81 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 82 | osNodeAdminService.updateNode(updated); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 83 | } |
Hyunsun Moon | a946564 | 2017-06-29 16:28:58 +0900 | [diff] [blame] | 84 | print("Done."); |
Hyunsun Moon | 34bbe17 | 2016-06-28 19:18:40 -0700 | [diff] [blame] | 85 | } |
| 86 | } |