blob: 5cc3285c92588c6d3e8e62fe3faa77e546767889 [file] [log] [blame]
Hyunsun Moon34bbe172016-06-28 19:18:40 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moon34bbe172016-06-28 19:18:40 -07003 *
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
17package org.onosproject.openstacknode.cli;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Hyunsun Moona9465642017-06-29 16:28:58 +090021import org.apache.karaf.shell.commands.Option;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070022import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon0d457362017-06-27 17:19:41 +090023import org.onosproject.openstacknode.api.NodeState;
24import org.onosproject.openstacknode.api.OpenstackNode;
25import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
26import org.onosproject.openstacknode.api.OpenstackNodeService;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070027
Hyunsun Moona9465642017-06-29 16:28:58 +090028import java.util.List;
29import java.util.stream.Collectors;
30
Hyunsun Moon34bbe172016-06-28 19:18:40 -070031/**
32 * Initializes nodes for OpenStack node service.
33 */
34@Command(scope = "onos", name = "openstack-node-init",
35 description = "Initializes nodes for OpenStack node service")
36public class OpenstackNodeInitCommand extends AbstractShellCommand {
37
Hyunsun Moona9465642017-06-29 16:28:58 +090038 @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 Moon34bbe172016-06-28 19:18:40 -070049 private String[] hostnames = null;
50
51 @Override
52 protected void execute() {
Hyunsun Moon0d457362017-06-27 17:19:41 +090053 OpenstackNodeService osNodeService =
54 AbstractShellCommand.get(OpenstackNodeService.class);
55 OpenstackNodeAdminService osNodeAdminService =
56 AbstractShellCommand.get(OpenstackNodeAdminService.class);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070057
Hyunsun Moona9465642017-06-29 16:28:58 +090058 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 Moon34bbe172016-06-28 19:18:40 -070079 for (String hostname : hostnames) {
Hyunsun Moon0d457362017-06-27 17:19:41 +090080 OpenstackNode osNode = osNodeService.node(hostname);
81 if (osNode == null) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070082 print("Unable to find %s", hostname);
83 continue;
84 }
Hyunsun Moona9465642017-06-29 16:28:58 +090085 print("Initializing %s", hostname);
Hyunsun Moon0d457362017-06-27 17:19:41 +090086 OpenstackNode updated = osNode.updateState(NodeState.INIT);
87 osNodeAdminService.updateNode(updated);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070088 }
Hyunsun Moona9465642017-06-29 16:28:58 +090089 print("Done.");
Hyunsun Moon34bbe172016-06-28 19:18:40 -070090 }
91}