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