Add support for the new SFC shell 01/61401/5
authorDavid Suarez <[email protected]>
Wed, 9 Aug 2017 07:23:43 +0000 (09:23 +0200)
committerBrady Johnson <[email protected]>
Thu, 10 Aug 2017 19:11:35 +0000 (19:11 +0000)
Add support for the new SFC shell. This is the first commit of a series
adding all the new CLI commands to the Karaf console to show the
provisioned SFC's model entities as described in [1].

[1] https://git.opendaylight.org/gerrit/#/c/61059/

Change-Id: I04498cbbe23aa0ab3ffb22aba87432217b0fe34b
Signed-off-by: David Suarez <[email protected]>
features/features4-sfc/pom.xml
features/odl-sfc-shell/pom.xml [new file with mode: 0644]
features/pom.xml
pom.xml
sfc-provider/src/main/java/org/opendaylight/sfc/provider/api/SfcProviderServiceFunctionAPI.java
sfc-shell/pom.xml [new file with mode: 0644]
sfc-shell/src/main/java/org/opendaylight/sfc/shell/AbstractCommand.java [new file with mode: 0644]
sfc-shell/src/main/java/org/opendaylight/sfc/shell/ServiceFunctionsCommand.java [new file with mode: 0644]

index a4f55d3baed51a5dce4d67a23642f3a363ee81ca..88872e64b04c08ac78b433dd89bd443ca424ddc4 100644 (file)
     <name>ODL :: sfc :: ${project.artifactId}</name>
 
     <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>odl-sfc-shell</artifactId>
+            <version>${project.version}</version>
+            <type>xml</type>
+            <classifier>features</classifier>
+        </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>odl-sfc-genius</artifactId>
diff --git a/features/odl-sfc-shell/pom.xml b/features/odl-sfc-shell/pom.xml
new file mode 100644 (file)
index 0000000..d18ce2a
--- /dev/null
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- vi: set et smarttab sw=4 tabstop=4: -->
+
+<!--
+ Copyright (c) Ericsson Inc. and others. All rights reserved.
+
+ This program and the accompanying materials are made available under the
+ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ and is available at http://www.eclipse.org/legal/epl-v10.html
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.opendaylight.odlparent</groupId>
+    <artifactId>single-feature-parent</artifactId>
+    <version>2.0.2</version>
+    <relativePath />
+  </parent>
+
+  <groupId>org.opendaylight.sfc</groupId>
+  <artifactId>odl-sfc-shell</artifactId>
+  <version>0.6.0-SNAPSHOT</version>
+  <packaging>feature</packaging>
+  <name>ODL :: sfc :: ${project.artifactId}</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>odl-mdsal-broker</artifactId>
+      <version>1.6.0-SNAPSHOT</version>
+      <type>xml</type>
+      <classifier>features</classifier>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.sfc</groupId>
+      <artifactId>sfc-shell</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+  </dependencies>
+</project>
index 81bd048ed983b76bcbb08a5e404da89f44942a2d..8a32003a32d0311bf3e318cb37891d39dfb64479 100644 (file)
@@ -48,6 +48,7 @@
         <module>odl-sfc-test-consumer</module>
         <module>odl-sfc-vnfm-tacker</module>
         <module>odl-sfc-genius</module>
+        <module>odl-sfc-shell</module>
     </modules>
 
     <scm>
diff --git a/pom.xml b/pom.xml
index 565837bed7697f00314bf7ee0a58d786e8c242bd..c941e914633178be81aa26276826d4bbc0df75a6 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -51,6 +51,7 @@
     <module>sfc-karaf</module>
     <module>sfc-renderers</module>
     <module>sfc-genius</module>
+    <module>sfc-shell</module>
   </modules>
 
   <properties>
         <artifactId>sfc-genius</artifactId>
         <version>${project.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.opendaylight.sfc</groupId>
+        <artifactId>sfc-shell</artifactId>
+        <version>${project.version}</version>
+      </dependency>
 
       <!-- YANG tools -->
       <dependency>
index 96f72b2df525e22d3041a3dd0315a9f4b4d87520..cf6d4e67b8904b35def01a426f9bb9e066f63e7f 100755 (executable)
@@ -313,7 +313,7 @@ public final class SfcProviderServiceFunctionAPI {
         return sf;
     }
 
-    protected static ServiceFunctions readAllServiceFunctions() {
+    public static ServiceFunctions readAllServiceFunctions() {
         ServiceFunctions sfs;
         printTraceStart(LOG);
         InstanceIdentifier<ServiceFunctions> sfsIID = InstanceIdentifier.builder(ServiceFunctions.class).build();
diff --git a/sfc-shell/pom.xml b/sfc-shell/pom.xml
new file mode 100644 (file)
index 0000000..1ee59a3
--- /dev/null
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- vi: set et smarttab sw=4 tabstop=4: --><!--
+Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+
+This program and the accompanying materials are made available under the
+terms of the Eclipse Public License v1.0 which accompanies this distribution,
+and is available at http://www.eclipse.org/legal/epl-v10.html
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.opendaylight.sfc</groupId>
+    <artifactId>sfc-parent</artifactId>
+    <version>0.6.0-SNAPSHOT</version>
+  </parent>
+
+
+  <artifactId>sfc-shell</artifactId>
+  <groupId>org.opendaylight.sfc</groupId>
+  <packaging>bundle</packaging>
+  <name>ODL :: sfc :: ${project.artifactId}</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.opendaylight.sfc</groupId>
+      <artifactId>sfc-provider</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.shell</groupId>
+      <artifactId>org.apache.karaf.shell.console</artifactId>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <!-- This bundle works with Karaf 3 and 4.0 -->
+            <Import-Package>
+              org.apache.karaf.shell.commands;version="[3.0.0,4.1)",
+              org.apache.karaf.shell.console;version="[3.0.0,4.1)",
+              *
+            </Import-Package>
+            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+            <Karaf-Commands>*</Karaf-Commands>
+          </instructions>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <configuration>
+          <propertyExpansion>checkstyle.violationSeverity=error</propertyExpansion>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/sfc-shell/src/main/java/org/opendaylight/sfc/shell/AbstractCommand.java b/sfc-shell/src/main/java/org/opendaylight/sfc/shell/AbstractCommand.java
new file mode 100644 (file)
index 0000000..7d453ef
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * Copyright (c) 2017 Ericsson S.A. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.sfc.shell;
+
+import java.io.PrintStream;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.console.Session;
+
+/**
+ * Abstract command with some common functionality to be inherited by the rest
+ * of the commands.
+ *
+ * @author David Suárez ([email protected])
+ *
+ */
+public abstract class AbstractCommand implements Action {
+
+    // Default value for fields that are not present
+    protected static final String NO_VALUE = "------";
+
+    @Reference
+    protected Session session;
+
+    public Session getSession() {
+        return session;
+    }
+
+    public PrintStream getConsole() {
+        return getSession().getConsole();
+    }
+}
diff --git a/sfc-shell/src/main/java/org/opendaylight/sfc/shell/ServiceFunctionsCommand.java b/sfc-shell/src/main/java/org/opendaylight/sfc/shell/ServiceFunctionsCommand.java
new file mode 100644 (file)
index 0000000..26e9401
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2017 Ericsson S.A. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.sfc.shell;
+
+import java.util.List;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.support.table.Col;
+import org.apache.karaf.shell.support.table.ShellTable;
+import org.opendaylight.sfc.provider.api.SfcProviderServiceFunctionAPI;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.ServiceFunctions;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Karaf command to show the Service Function provisioned.
+ *
+ * @author David Suárez ([email protected])
+ *
+ */
+@Service
+@Command(scope = "sfc", name = "sf-list", description = "Show the provisioned Service Functions")
+public class ServiceFunctionsCommand extends AbstractCommand {
+
+    @Option(name = "-name", aliases = {
+            "--name" }, description = "Name of the Service Function", required = false, multiValued = false)
+    private String name;
+
+    private final ShellTable table;
+
+    private static final Logger LOG = LoggerFactory.getLogger(ServiceFunctionsCommand.class);
+
+    public ServiceFunctionsCommand() {
+        table = new ShellTable();
+        table.column(new Col("Name"));
+        table.column(new Col("Type"));
+        table.column(new Col("Mgmt. Addr."));
+        table.column(new Col("REST Uri"));
+        table.column(new Col("DPL Type"));
+    }
+
+    @Override
+    public Object execute() throws Exception {
+        LOG.debug("Service Function name: {}", name);
+
+        if (name != null) {
+            renderContent(SfcProviderServiceFunctionAPI.readServiceFunction(new SfName(name)));
+        } else {
+            LOG.debug("Getting the list of Service Functions");
+            ServiceFunctions sfs = SfcProviderServiceFunctionAPI.readAllServiceFunctions();
+            if (sfs != null) {
+                List<ServiceFunction> serviceFunctions = sfs.getServiceFunction();
+                serviceFunctions.forEach(this::renderContent);
+            }
+        }
+        table.print(getConsole());
+        return null;
+    }
+
+    private void renderContent(ServiceFunction serviceFunction) {
+        if (serviceFunction != null) {
+            LOG.debug("Service Function data: {}", serviceFunction);
+            table.addRow().addContent(serviceFunction.getName().getValue(),
+                    serviceFunction.getType() == null ? NO_VALUE : serviceFunction.getType().getValue(),
+                    serviceFunction.getIpMgmtAddress() == null ? NO_VALUE
+                            : serviceFunction.getIpMgmtAddress().getIpv4Address().getValue(),
+                    serviceFunction.getRestUri() == null ? NO_VALUE : serviceFunction.getRestUri().getValue(),
+                    serviceFunction.getSfDataPlaneLocator() == null
+                            || serviceFunction.getSfDataPlaneLocator().get(0) == null ? NO_VALUE
+                                    : serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType());
+        }
+    }
+}