Uni Add cmd CLI implmentation 69/29569/7
authorMohamed El-Serngawy <melserngawy@inocybe.com>
Wed, 11 Nov 2015 22:05:51 +0000 (17:05 -0500)
committerMohamed El-Serngawy <melserngawy@inocybe.com>
Tue, 17 Nov 2015 14:57:19 +0000 (09:57 -0500)
Change-Id: Iaa1183e31dc2c15fc6e2e0f2c15fb64a2ca16263
Signed-off-by: Mohamed El-Serngawy <melserngawy@inocybe.com>
cli/src/main/java/org/opendaylight/unimgr/cli/UniAddShellCommand.java
cli/src/main/java/org/opendaylight/unimgr/cli/UniListShellCommand.java
cli/src/main/java/org/opendaylight/unimgr/cli/UniRemoveShellCommand.java
cli/src/main/java/org/opendaylight/unimgr/cli/UniShowShellCommand.java
impl/src/main/java/org/opendaylight/unimgr/api/IUnimgrConsoleProvider.java
impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrConstants.java
impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrMapper.java
impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrProvider.java
impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrUtils.java
impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/unimgr/impl/rev151012/UnimgrModule.java
impl/src/main/resources/OSGI-INF/blueprint/blueprint.xml [new file with mode: 0644]

index 9b5835e4c810f77c864badd6ec7669057421314e..8de1a77513f14d9096b9c946edb940fbdaa74d2a 100755 (executable)
@@ -13,40 +13,37 @@ import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-@Command(name = "add",
+@Command(name = "uni-add",
          scope = "uni",
          description = "Adds an uni to the controller.")
 public class UniAddShellCommand extends OsgiCommandSupport {
 
+    private static final Logger LOG = LoggerFactory.getLogger(UniAddShellCommand.class);
     protected IUnimgrConsoleProvider provider;
 
-    @Option(name = "-id",
-            aliases = { "--uni-id" },
-            description = "The id.\n-id / --id <id>",
-            required = true,
-            multiValued = false)
-    private String uniId = "any";
-
     @Option(name = "-pm",
             aliases = { "--physical-medium" },
             description = "The physical medium.\n-pm / --physical-medium <physical-medium>",
             required = false,
             multiValued = false)
-    private String physicalMedium = "any";
+    private String physicalMedium = "UNI TypeFull Duplex 2 Physical Interface";
 
     @Option(name = "-ma",
             aliases = { "--mac-address" },
             description = "The mac address.\n-ma / --mac-address <mac-address>",
-            required = false,
+            required = true,
             multiValued = false)
     private String macAddress = "any";
 
@@ -55,64 +52,67 @@ public class UniAddShellCommand extends OsgiCommandSupport {
             description = "The mode.\n-m / --mode <mode>",
             required = false,
             multiValued = false)
-    private String mode = "any";
+    private String mode = "Full Duplex";
 
     @Option(name = "-ml",
             aliases = { "--mac-layer" },
             description = "The mac layer.\n-ml / --mac-layer <mac-layer",
             required = false,
             multiValued = false)
-    private String macLayer = "any";
+    private String macLayer = "IEEE 802.3-2005";
 
     @Option(name = "-t",
             aliases = { "--type" },
             description = "The type.\n-t / --type <type>",
             required = false,
             multiValued = false)
-    private String type = "any";
+    private String type = "";
 
     @Option(name = "-ms",
             aliases = { "--mtu-size" },
             description = "The mtu size.\n-ms / --mtu-size <mtu-size>",
             required = false,
             multiValued = false)
-    private String mtuSize;
+    private String mtuSize = "0";
 
     @Option(name = "-s",
             aliases = { "--speed" },
             description = "Spped.\n-s / --speed 10M/100M/1G/10G",
-            required = true,
+            required = false,
             multiValued = true)
-    private String speed = "any";
+    private String speed = "";
+
+    @Option(name = "-ip",
+            aliases = { "--ipAddress" },
+            description = "IpAddress of the Uni",
+            required = true,
+            multiValued = false)
+    private String ipAddress = "any";
 
     public UniAddShellCommand(IUnimgrConsoleProvider provider) {
         this.provider = provider;
     }
 
     private Object getSpeed() {
-
-        System.out.println(speed);
-
         Object speedObject = null;
         if (speed.equals("10M")) {
             speedObject = new Speed10MBuilder().build();
         }
-        if (speed.equals("100M")) {
+        else if (speed.equals("100M")) {
             speedObject = new Speed100MBuilder().build();
         }
-        if (speed.equals("1G")) {
+        else if (speed.equals("1G")) {
             speedObject = new Speed1GBuilder().build();
         }
-        if (speed.equals("10G")) {
+        else if (speed.equals("10G")) {
             speedObject = new Speed10GBuilder().build();
         }
-        System.out.println(speedObject);
         return speedObject;
     }
 
     @Override
     protected Object doExecute() throws Exception {
-        Uni uni = new UniAugmentationBuilder()
+        UniAugmentation uni = new UniAugmentationBuilder()
                         .setMacAddress(new MacAddress(macAddress))
                         .setMacLayer(macLayer)
                         .setMode(mode)
@@ -120,12 +120,13 @@ public class UniAddShellCommand extends OsgiCommandSupport {
                         .setPhysicalMedium(physicalMedium)
                         .setSpeed((Speed) getSpeed())
                         .setType(type)
+                        .setIpAddress(new IpAddress(ipAddress.toCharArray()))
                         .build();
 
         if (provider.addUni(uni)) {
-            return String.format("Uni created {}", uni.getIpAddress().getIpv4Address());
+            return new String("Uni with ip " +ipAddress+" created");
         } else {
-            return new String("Error creating new uni");
+            return new String("Error creating new Uni");
         }
     }
 }
index 2cda310501f2ea1de455408ec2cca01a04717c1a..a210b898908c8b7d4308cbb77f1e819f5dd6ae1d 100755 (executable)
@@ -13,6 +13,7 @@ import java.util.List;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
 
index 7b8cc7baa068a73d3e33bb98ec1d4a878d3d0f4f..977c94840742003bb2f1104ba39669cf0c15ccde 100755 (executable)
@@ -11,14 +11,15 @@ import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 
 @Command(name = "remove", scope = "uni", description = "Removes an Uni from the controller.")
 public class UniRemoveShellCommand extends OsgiCommandSupport {
 
     protected IUnimgrConsoleProvider provider;
 
-    @Argument(index = 0, name = "id", description = "Uni Id", required = true, multiValued = false)
-    String id;
+    @Argument(index = 0, name = "ip", description = "Uni ipAddress", required = true, multiValued = false)
+    String ipAddress;
 
     public UniRemoveShellCommand(IUnimgrConsoleProvider provider) {
         this.provider = provider;
@@ -26,7 +27,8 @@ public class UniRemoveShellCommand extends OsgiCommandSupport {
 
     @Override
     protected Object doExecute() throws Exception {
-        if (provider.removeUni(id)) {
+        IpAddress ipAddre = new IpAddress(ipAddress.toCharArray());
+        if (provider.removeUni(ipAddre)) {
             return String.format("Uni successfully removed");
         } else {
             return String.format("Error removing Uni");
index e1373ce2f1bd547d81cdc7768d1ed19bce4034fd..eeb02e2d7ef35d0b5daecabadaf66004b8608a53 100755 (executable)
@@ -11,6 +11,7 @@ import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
 
 @Command(name = "show", scope = "uni", description = "Shows detailed information about an uni.")
@@ -18,8 +19,8 @@ public class UniShowShellCommand extends OsgiCommandSupport {
 
     protected IUnimgrConsoleProvider provider;
 
-    @Argument(index = 0, name = "id", description = "Uni Id", required = true, multiValued = false)
-    String id;
+    @Argument(index = 0, name = "ip", description = "Uni ipAddress", required = true, multiValued = false)
+    String ipAddress;
 
     public UniShowShellCommand(IUnimgrConsoleProvider provider) {
         this.provider = provider;
@@ -28,7 +29,8 @@ public class UniShowShellCommand extends OsgiCommandSupport {
     @Override
     protected Object doExecute() throws Exception {
         StringBuilder sb = new StringBuilder();
-        Uni uni = provider.getUni(id);
+        IpAddress ipAddre = new IpAddress(ipAddress.toCharArray());
+        Uni uni = provider.getUni(ipAddre);
 
         if (uni != null) {
             //sb.append(String.format("Uni Id: <%s>\n", uni.getUniId()));
index 717ae27985f10d2bdb016a3510eb94099d55d379..1346649dd8b9006be684c13cf6fd4767844d9e36 100755 (executable)
@@ -9,18 +9,21 @@ package org.opendaylight.unimgr.api;
 
 import java.util.List;
 
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
 
 public interface IUnimgrConsoleProvider extends AutoCloseable {
 
-    boolean addUni(Uni uni);
+    boolean addUni(UniAugmentation uni);
 
-    boolean removeUni(String uuid);
+    boolean removeUni(IpAddress ipAddress);
 
-    List<Uni> listUnis(boolean isConfigurationDatastore);
+    List<Uni> listUnis(Boolean isConfigurationData);
 
-    Uni getUni(String uuid);
+    Uni getUni(IpAddress ipAddress);
 
     boolean removeEvc(String uuid);
 
index 0f39295ecd82d1ed176dc09ae3b82a20234c2a7c..38fd796b94b00b7d162e0adbad20b1c98b975a14 100644 (file)
@@ -19,6 +19,8 @@ public class UnimgrConstants {
 
     public static final TopologyId EVC_TOPOLOGY_ID = new TopologyId(new Uri("unimgr:evc"));
 
+    public static final String UNI_PREFIX = "uni://";
+
     public static final String OVSDB_PREFIX = "ovsdb://";
 
     public static final Integer OVSDB_PORT = new Integer(6640);
index 74a744c90d177143bf854795c3146b435a4af0d4..ff2293bb61c53e18d21c50e2c41469bfca296506 100755 (executable)
@@ -282,4 +282,19 @@ public class UnimgrMapper {
         return nodePath;
     }
 
+    /**
+     * Generates an Instance Identifier for a specific UNI node by
+     * using the node Id
+     * @param nodeId The node ID of a specific UNI node.
+     * @return An Instance Identifier for a specific UNI node.
+     */
+    public static InstanceIdentifier<Node> getUniNodeIid(NodeId nodeId) {
+    InstanceIdentifier<Node> nodePath = InstanceIdentifier
+                                            .create(NetworkTopology.class)
+                                            .child(Topology.class,
+                                                    new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
+                                            .child(Node.class,
+                                                    new NodeKey(nodeId));
+    return nodePath;
+}
 }
index f0871b6387fa1a926784a0a45d4efd16df83b2f9..beb31dc66e698740b5a0a6349f22f1ad828c314c 100755 (executable)
@@ -18,8 +18,10 @@ import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderCo
 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
 import org.opendaylight.unimgr.command.TransactionInvoker;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Evc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
@@ -46,6 +48,10 @@ public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUni
     private DataBroker dataBroker;
     private ServiceRegistration<IUnimgrConsoleProvider> unimgrConsoleRegistration;
 
+    public UnimgrProvider() {
+        LOG.info("Unimgr provider initialized");
+    }
+
     @Override
     public void onSessionInitiated(ProviderContext session) {
         LOG.info("UnimgrProvider Session Initiated");
@@ -123,38 +129,32 @@ public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUni
     }
 
     @Override
-    public boolean addUni(Uni uni) {
-        //TODO This code was left commented as an example
-        if (uni.getIpAddress() == null || uni.getMacAddress() == null) {
+    public boolean addUni(UniAugmentation uniAug) {
+        if (uniAug == null || uniAug.getIpAddress() == null || uniAug.getMacAddress() == null) {
             return false;
         }
-//        UniAugmentation uniAugmentation = new UniAugmentationBuilder()
-//                                                .setIpAddress(uni.getIpAddress())
-//                                                .setMacAddress(uni.getMacAddress())
-//                                                .build();
-//        ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
-//        InstanceIdentifier<Node> path = UnimgrMapper.getUniAugmentationIidByMac(uni.getMacAddress());
-        return true;
+        return UnimgrUtils.createUniNode(dataBroker, uniAug);
     }
 
     @Override
-    public boolean removeUni(String uuid) {
+    public boolean removeUni(IpAddress ipAddress) {
         // TODO Auto-generated method stub
         return false;
     }
 
     @Override
-    public List<Uni> listUnis(boolean isConfigurationDatastore) {
+    public List<Uni> listUnis(Boolean isConfigurationData) {
         // TODO Auto-generated method stub
         return null;
     }
 
     @Override
-    public Uni getUni(String uuid) {
+    public Uni getUni(IpAddress ipAddress) {
         // TODO Auto-generated method stub
         return null;
     }
 
+
     @Override
     public boolean removeEvc(String uuid) {
         // TODO Auto-generated method stub
index f9bbfa25e1f0d4c13137edd989feed9e88f324bb..edc213b120e65ba854557e773e47161ba25c869f 100644 (file)
@@ -358,6 +358,44 @@ public class UnimgrUtils {
         return terminationPoint;
     }
 
+    /**
+     * Creates and submit an UNI Node by using the Data contained in the UniAugmentation
+     * @param dataBroker The instance of the DataBroker to create transactions
+     * @param uni The UNI's data
+     * @return The instance of the UNI Node
+     */
+    public static boolean createUniNode(DataBroker dataBroker, UniAugmentation uni) {
+        NodeId uniNodeId = new NodeId(createUniNodeId(uni.getIpAddress()));
+        boolean result = false;
+        try {
+            InstanceIdentifier<Node> uniNodeIid = UnimgrMapper.getUniNodeIid(uniNodeId);
+            NodeKey uniNodeKey = new NodeKey(uniNodeId);
+            Node nodeData = new NodeBuilder()
+                                .setNodeId(uniNodeId)
+                                .setKey(uniNodeKey)
+                                .addAugmentation(UniAugmentation.class, uni)
+                                .build();
+            WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
+            transaction.put(LogicalDatastoreType.CONFIGURATION, uniNodeIid, nodeData);
+            CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
+            future.checkedGet();
+            result = true;
+            LOG.info("Created and submitted a new Uni node {}", nodeData.getNodeId());
+        } catch (Exception e) {
+            LOG.error("Exception while creating Uni Node" + "Uni Node Id: {}", uniNodeId);
+        }
+        return result;
+    }
+
+    /**
+     * Creates an UNI node Id with an IP Address.
+     * @param ipAddress The IP address of the UNI
+     * @return A NodeId for a Specific UNI Node Id
+     */
+    public static NodeId createUniNodeId(IpAddress ipAddress) {
+        return new NodeId(UnimgrConstants.UNI_PREFIX + ipAddress.getIpv4Address().getValue().toString());
+    }
+
     /**
      * Creates and Submit a termination point Node to the configuration DateStore.
      * @param dataBroker The instance of the data broker to create transactions
index 0df544699331dbf23b9eac4a656003820cf3dc22..674e9712ef027b3117b0ce1d361c5087176890f5 100644 (file)
@@ -3,6 +3,7 @@ package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.
 import org.opendaylight.unimgr.impl.UnimgrProvider;
 
 public class UnimgrModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.impl.rev151012.AbstractUnimgrModule {
+
     public UnimgrModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
     }
diff --git a/impl/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/impl/src/main/resources/OSGI-INF/blueprint/blueprint.xml
new file mode 100644 (file)
index 0000000..6bd3c15
--- /dev/null
@@ -0,0 +1,4 @@
+<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+</blueprint>
\ No newline at end of file