blob: 155ecfcceae8e0723342747f7fa82667fee0c166 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070023import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon0d457362017-06-27 17:19:41 +090024import org.onosproject.openstacknode.api.NodeState;
25import org.onosproject.openstacknode.api.OpenstackNode;
26import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
27import org.onosproject.openstacknode.api.OpenstackNodeService;
Hyunsun Moon34bbe172016-06-28 19:18:40 -070028
29/**
30 * Initializes nodes for OpenStack node service.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Hyunsun Moon34bbe172016-06-28 19:18:40 -070033@Command(scope = "onos", name = "openstack-node-init",
34 description = "Initializes nodes for OpenStack node service")
35public class OpenstackNodeInitCommand extends AbstractShellCommand {
36
Hyunsun Moona9465642017-06-29 16:28:58 +090037 @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 Moon34bbe172016-06-28 19:18:40 -070048 private String[] hostnames = null;
49
50 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 protected void doExecute() {
Hyunsun Moon0d457362017-06-27 17:19:41 +090052 OpenstackNodeService osNodeService =
53 AbstractShellCommand.get(OpenstackNodeService.class);
54 OpenstackNodeAdminService osNodeAdminService =
55 AbstractShellCommand.get(OpenstackNodeAdminService.class);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070056
Hyunsun Moona9465642017-06-29 16:28:58 +090057 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 Li5afbea42018-02-28 10:37:03 +090066 hostnames = osNodeService.nodes().stream()
67 .map(OpenstackNode::hostname).toArray(String[]::new);
Hyunsun Moona9465642017-06-29 16:28:58 +090068 } else if (isIncomplete) {
Jian Li5afbea42018-02-28 10:37:03 +090069 hostnames = osNodeService.nodes().stream()
Hyunsun Moona9465642017-06-29 16:28:58 +090070 .filter(osNode -> osNode.state() != NodeState.COMPLETE)
Jian Li5afbea42018-02-28 10:37:03 +090071 .map(OpenstackNode::hostname).toArray(String[]::new);
Hyunsun Moona9465642017-06-29 16:28:58 +090072 }
73
Hyunsun Moon34bbe172016-06-28 19:18:40 -070074 for (String hostname : hostnames) {
Hyunsun Moon0d457362017-06-27 17:19:41 +090075 OpenstackNode osNode = osNodeService.node(hostname);
76 if (osNode == null) {
Hyunsun Moon34bbe172016-06-28 19:18:40 -070077 print("Unable to find %s", hostname);
78 continue;
79 }
Hyunsun Moona9465642017-06-29 16:28:58 +090080 print("Initializing %s", hostname);
Hyunsun Moon0d457362017-06-27 17:19:41 +090081 OpenstackNode updated = osNode.updateState(NodeState.INIT);
82 osNodeAdminService.updateNode(updated);
Hyunsun Moon34bbe172016-06-28 19:18:40 -070083 }
Hyunsun Moona9465642017-06-29 16:28:58 +090084 print("Done.");
Hyunsun Moon34bbe172016-06-28 19:18:40 -070085 }
86}