1+ /**
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ * <p>
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ * <p>
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+ package org .apache .hadoop .hdds .scm .cli ;
20+
21+ import org .apache .hadoop .hdds .cli .HddsVersionProvider ;
22+ import org .apache .hadoop .hdds .protocol .proto .HddsProtos ;
23+ import org .apache .hadoop .hdds .scm .client .ScmClient ;
24+ import picocli .CommandLine ;
25+ import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState .DEAD ;
26+ import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState .DECOMMISSIONED ;
27+ import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState .DECOMMISSIONING ;
28+ import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState .HEALTHY ;
29+ import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState .STALE ;
30+
31+ import java .util .ArrayList ;
32+ import java .util .List ;
33+ import java .util .concurrent .Callable ;
34+
35+ /**
36+ * Handler of printTopology command.
37+ */
38+ @ CommandLine .Command (
39+ name = "printTopology" ,
40+ description = "Print a tree of the network topology as reported by SCM" ,
41+ mixinStandardHelpOptions = true ,
42+ versionProvider = HddsVersionProvider .class )
43+ public class TopologySubcommand implements Callable <Void > {
44+
45+ @ CommandLine .ParentCommand
46+ private SCMCLI parent ;
47+
48+ private static List <HddsProtos .NodeState > stateArray = new ArrayList <>();
49+
50+ static {
51+ stateArray .add (HEALTHY );
52+ stateArray .add (STALE );
53+ stateArray .add (DEAD );
54+ stateArray .add (DECOMMISSIONING );
55+ stateArray .add (DECOMMISSIONED );
56+ }
57+
58+ @ Override
59+ public Void call () throws Exception {
60+ try (ScmClient scmClient = parent .createScmClient ()) {
61+ for (HddsProtos .NodeState state : stateArray ) {
62+ List <HddsProtos .Node > nodes = scmClient .queryNode (state ,
63+ HddsProtos .QueryScope .CLUSTER , "" );
64+ if (nodes != null && nodes .size () > 0 ) {
65+ // show node state
66+ System .out .println ("State = " + state .toString ());
67+ // format "hostname/ipAddress networkLocation"
68+ nodes .forEach (node -> {
69+ System .out .print (node .getNodeID ().getHostName () + "/" +
70+ node .getNodeID ().getIpAddress ());
71+ System .out .println (" " +
72+ (node .getNodeID ().getNetworkLocation () != null ?
73+ node .getNodeID ().getNetworkLocation () : "NA" ));
74+ });
75+ }
76+ }
77+ return null ;
78+ }
79+ }
80+ }
0 commit comments