Clean up hwvtep commands 11/104411/4
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 15 Feb 2023 17:49:08 +0000 (18:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 16 Feb 2023 06:52:58 +0000 (07:52 +0100)
Do not use Blueprint, but rather proper karaf tooling to implement the
commands.

Change-Id: I41c90a7d0f4273a2c8771d79745ab27f72e65768
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
hwvtepsouthbound/hwvtepsouthbound-impl/pom.xml
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepDisconnectCliCmd.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/TransactionHistoryCmd.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/resources/OSGI-INF/blueprint/hwvtepsouthbound.xml

index 438fdf4d12b142251628dbd05b335e842dedbd68..d9b3ee5da62284c5c82e1ceb8896d208e1be6f5d 100644 (file)
@@ -62,11 +62,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>library</artifactId>
       <version>${project.version}</version>
     </dependency>
-    <dependency>
-      <groupId>org.apache.karaf.shell</groupId>
-      <artifactId>org.apache.karaf.shell.console</artifactId>
-      <scope>provided</scope>
-    </dependency>
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>schema.hardwarevtep</artifactId>
@@ -87,11 +82,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>org.apache.karaf.shell.core</artifactId>
       <scope>provided</scope>
     </dependency>
-    <dependency>
-      <!-- FIXME: needed because of OsgiCommandSupport -->
-      <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.framework</artifactId>
-    </dependency>
 
     <!-- Testing Dependencies -->
     <dependency>
@@ -128,6 +118,10 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
           </argLine>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>org.apache.karaf.tooling</groupId>
+        <artifactId>karaf-services-maven-plugin</artifactId>
+      </plugin>
     </plugins>
   </build>
 
index af66d04ccacd7fbd47d8c3a55fe8dc817d6bf57a..9281e8a3eba5228007329ad5e8939343129ddb0b 100644 (file)
@@ -7,10 +7,12 @@
  */
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.commands.Option;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import java.util.concurrent.ExecutionException;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
@@ -25,39 +27,30 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
+@Service
 @Command(scope = "hwvtep", name = "disconnect", description = "Disconnect a node")
-public class HwvtepDisconnectCliCmd extends OsgiCommandSupport {
+public class HwvtepDisconnectCliCmd implements Action {
+    public static final TopologyId HWVTEP_TOPOLOGY_ID = new TopologyId(new Uri("hwvtep:1"));
 
+    @Reference
     private DataBroker dataBroker;
 
-    @Option(name = "-nodeid", description = "Node Id",
-        required = false, multiValued = false)
-    String nodeid;
-
-    public static final TopologyId HWVTEP_TOPOLOGY_ID = new TopologyId(new Uri("hwvtep:1"));
-
-    public void setDataBroker(DataBroker dataBroker) {
-        this.dataBroker = dataBroker;
-    }
+    @Option(name = "-nodeid", description = "Node Id", required = false, multiValued = false)
+    private String nodeid;
 
+    @SuppressWarnings("checkstyle:RegexpSinglelineJava")
     @Override
-    @SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")
-    protected Object doExecute() throws Exception {
+    public Object execute() throws InterruptedException, ExecutionException {
+        final var nodeKey = new NodeKey(new NodeId(new Uri(nodeid + "/disconnect")));
+
         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
-        tx.put(LogicalDatastoreType.CONFIGURATION, getIid(),
-            new NodeBuilder().setNodeId(new NodeId(new Uri(nodeid + "/disconnect"))).build());
+        tx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(NetworkTopology.class)
+            .child(Topology.class, new TopologyKey(HWVTEP_TOPOLOGY_ID))
+            .child(Node.class, nodeKey)
+            .build(),
+            new NodeBuilder().withKey(nodeKey).build());
         tx.commit().get();
-        session.getConsole().println("Successfully disconnected " + nodeid);
+        System.out.println("Successfully disconnected " + nodeid);
         return "";
     }
-
-    private InstanceIdentifier<Node> getIid() {
-        NodeId nodeId = new NodeId(new Uri(nodeid + "/disconnect"));
-        NodeKey nodeKey = new NodeKey(nodeId);
-        TopologyKey topoKey = new TopologyKey(HWVTEP_TOPOLOGY_ID);
-        return InstanceIdentifier.builder(NetworkTopology.class)
-            .child(Topology.class, topoKey)
-            .child(Node.class, nodeKey)
-            .build();
-    }
 }
index f02f17a35ed6fc7fe81f03a81a0d3ac46fc8c534..d216459b9af5f60f106af7c64846b3c979c593b5 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
+import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -14,78 +15,79 @@ import java.util.Map;
 import java.util.stream.Collectors;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
-import org.apache.karaf.shell.commands.Command;
-import org.apache.karaf.shell.commands.Option;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionHistory;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
+@Service
 @Command(scope = "hwvtep", name = "txlog", description = "prints hwvtep tx log")
-public class TransactionHistoryCmd extends OsgiCommandSupport {
-
-    @Option(name = "-nodeid", description = "Node Id",
-            required = false, multiValued = false)
-    String nodeid;
+public class TransactionHistoryCmd implements Action {
     private static final String SEPERATOR = "#######################################################";
 
-    private final HwvtepSouthboundProviderInfo hwvtepProvider;
+    @Reference
+    private HwvtepSouthboundProviderInfo hwvtepProvider;
 
-    public TransactionHistoryCmd(HwvtepSouthboundProvider hwvtepProvider) {
-        this.hwvtepProvider = hwvtepProvider;
-    }
+    @Option(name = "-nodeid", description = "Node Id", required = false, multiValued = false)
+    private String nodeid;
 
+    @SuppressWarnings("checkstyle:RegexpSinglelineJava")
     @Override
-    protected Object doExecute() throws Exception {
+    public Object execute() {
+        final PrintStream out = System.out;
+
         Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs = hwvtepProvider.getControllerTxHistory();
         Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs = hwvtepProvider.getDeviceUpdateHistory();
         if (nodeid != null) {
-            printLogs(controllerTxLogs, deviceUpdateLogs,
+            printLogs(out, controllerTxLogs, deviceUpdateLogs,
                 HwvtepSouthboundMapper.createInstanceIdentifier(new NodeId(nodeid)));
         } else {
             Map<InstanceIdentifier<Node>, TransactionHistory> txlogs
                     = controllerTxLogs.isEmpty() ? deviceUpdateLogs : controllerTxLogs;
-            txlogs.keySet().forEach(iid -> {
-                printLogs(controllerTxLogs, deviceUpdateLogs, iid);
-            });
-            session.getConsole().println("Device tx logs size " + deviceUpdateLogs.size());
+            txlogs.keySet().forEach(iid -> printLogs(out, controllerTxLogs, deviceUpdateLogs, iid));
+            out.println("Device tx logs size " + deviceUpdateLogs.size());
         }
         return null;
     }
 
-    private void printLogs(Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs,
-                           Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs,
-                           InstanceIdentifier<Node> iid) {
-        session.getConsole().println(SEPERATOR + " START " + SEPERATOR);
+    private static void printLogs(final PrintStream out,
+                                  final Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs,
+                                  final Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs,
+                                  final InstanceIdentifier<Node> iid) {
+        out.println(SEPERATOR + " START " + SEPERATOR);
         List<HwvtepTransactionLogElement> controllerTxLog = controllerTxLogs.get(iid).getElements()
                 .stream().map(ele -> new HwvtepTransactionLogElement(ele, false)).collect(Collectors.toList());
         List<HwvtepTransactionLogElement> deviceUpdateLog = deviceUpdateLogs.get(iid).getElements()
                 .stream().map(ele -> new HwvtepTransactionLogElement(ele, true)).collect(Collectors.toList());
         List<Pair<HwvtepTransactionLogElement, Boolean>> allLogs = mergeLogsByDate(controllerTxLog, deviceUpdateLog);
-        session.getConsole().print("Printing for Node :  ");
-        session.getConsole().println(iid.firstKeyOf(Node.class).getNodeId().getValue());
-        printLogs(allLogs);
-        session.getConsole().println(SEPERATOR + " END " + SEPERATOR);
-        session.getConsole().println();
+        out.print("Printing for Node :  ");
+        out.println(iid.firstKeyOf(Node.class).getNodeId().getValue());
+        printLogs(out, allLogs);
+        out.println(SEPERATOR + " END " + SEPERATOR);
+        out.println();
     }
 
-    private void printLogs(List<Pair<HwvtepTransactionLogElement, Boolean>> logs) {
+    private static void printLogs(final PrintStream out, final List<Pair<HwvtepTransactionLogElement, Boolean>> logs) {
         logs.forEach(pair -> {
             HwvtepTransactionLogElement log = pair.getLeft();
-            session.getConsole().print(new Date(log.getDate()));
-            session.getConsole().print(" ");
-            session.getConsole().print(pair.getRight() ? "CONTROLLER" : "DEVICE");
-            session.getConsole().print(" ");
-            session.getConsole().print(log.getTransactionType());
-            session.getConsole().print(" ");
-            session.getConsole().println(log.getData());
+            out.print(new Date(log.getDate()));
+            out.print(" ");
+            out.print(pair.getRight() ? "CONTROLLER" : "DEVICE");
+            out.print(" ");
+            out.print(log.getTransactionType());
+            out.print(" ");
+            out.println(log.getData());
         });
     }
 
     private static List<Pair<HwvtepTransactionLogElement, Boolean>> mergeLogsByDate(
-            List<HwvtepTransactionLogElement> logs1,
-            List<HwvtepTransactionLogElement> logs2) {
+            final List<HwvtepTransactionLogElement> logs1,
+            final List<HwvtepTransactionLogElement> logs2) {
 
         ArrayList<Pair<HwvtepTransactionLogElement, Boolean>> result = new ArrayList();
         int firstIdx = 0;
index 82852f7806466af366667199fe73adbfebdee5d7..dc6c001223020d7bc06d494c1b991702318c8766 100644 (file)
         <argument ref="bindingNormalizedNodeSerializer"/>
     </bean>
     <service ref="hwvtepSouthboundProvider" interface="org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundProviderInfo"/>
-
-    <!-- FIXME: convert these to proper Karaf commands -->
-    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
-    <command>
-        <action class="org.opendaylight.ovsdb.hwvtepsouthbound.TransactionHistoryCmd">
-            <argument ref="hwvtepSouthboundProvider" />
-        </action>
-    </command>
-    <command>
-        <action class="org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDisconnectCliCmd">
-            <property name="dataBroker" ref="dataBroker" />
-        </action>
-    </command>
-    </command-bundle>
 </blueprint>