MappingServiceManager: pass VPN info to intents 08/31308/16
authorIcaro Camelo <icamelo@inocybe.com>
Mon, 14 Dec 2015 22:22:58 +0000 (17:22 -0500)
committerIcaro Camelo <icamelo@inocybe.com>
Fri, 22 Jan 2016 14:51:32 +0000 (09:51 -0500)
Change-Id: I53d9a72d6cb6b7cbe0d78ec0faf1d5fb1d87006b
Signed-off-by: Icaro Camelo <icamelo@inocybe.com>
vpnintent/impl/pom.xml
vpnintent/impl/src/main/java/org/opendaylight/vpnservice/impl/MappingServiceManager.java [new file with mode: 0644]
vpnintent/impl/src/test/java/org/opendaylight/vpnservice/impl/MappingServiceManagerTests.java [new file with mode: 0644]

index 51cb833d3ae55beb0fd88e18c3eb3eceafef0a95..546b1e130a873b67bcc2474ecf6b0c2feeeeced2 100644 (file)
@@ -18,7 +18,17 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
   <artifactId>vpnintent-impl</artifactId>
   <version>${vpnservices.version}</version>
   <packaging>bundle</packaging>
+
+  <properties>
+    <nic.version>1.2.0-SNAPSHOT</nic.version>
+  </properties>
+
   <dependencies>
+    <dependency>
+      <groupId>org.opendaylight.vpnservice</groupId>
+      <artifactId>mdsalutil-api</artifactId>
+      <version>${project.version}</version>
+    </dependency>
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>vpnintent-api</artifactId>
@@ -36,6 +46,12 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
       <artifactId>mockito-all</artifactId>
       <scope>test</scope>
     </dependency>
+
+    <dependency>
+      <groupId>org.opendaylight.nic</groupId>
+      <artifactId>intent-mapping-interface</artifactId>
+      <version>${nic.version}</version>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/vpnintent/impl/src/main/java/org/opendaylight/vpnservice/impl/MappingServiceManager.java b/vpnintent/impl/src/main/java/org/opendaylight/vpnservice/impl/MappingServiceManager.java
new file mode 100644 (file)
index 0000000..e025b91
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2016 Inocybe Technologies 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.vpnservice.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.opendaylight.nic.mapping.api.IntentMappingService;
+
+import com.google.common.base.Preconditions;
+
+public class MappingServiceManager {
+
+    private IntentMappingService intentMappingService;
+    private String IP_PREFIX_PROPERTY = "ip_prefix";
+    private String SWITCH_PORT_ID_PROPERTY = "switch_port";
+    private String MPLS_LABEL_PROPERTY = "mpls_label";
+    private String NEXT_HOP_PROPERTY = "next_hop";
+
+    public MappingServiceManager(IntentMappingService intentMappingService) {
+        Preconditions.checkNotNull(intentMappingService);
+        this.intentMappingService = intentMappingService;
+    }
+
+    /**
+     * @param siteName
+     *            Name of the member
+     * @param ipPrefix
+     *            Ip prefix of the member
+     * @param switchPortId
+     *            Switch ID and port ID (i.e. openflow:1:2)
+     * @param mplsLabel
+     *            MPLS label, if needed
+     * @param nextHop
+     *            Next hop in the route
+     */
+    public void add(final String siteName, final String ipPrefix, final String switchPortId, final Long mplsLabel,
+            final String nextHop) {
+        Preconditions.checkNotNull(siteName);
+        Preconditions.checkNotNull(ipPrefix);
+        Preconditions.checkNotNull(switchPortId);
+
+        Map<String, String> objs = new HashMap<>();
+        objs.put(IP_PREFIX_PROPERTY, ipPrefix);
+        objs.put(SWITCH_PORT_ID_PROPERTY, switchPortId);
+
+        if (mplsLabel != null)
+            objs.put(MPLS_LABEL_PROPERTY, String.valueOf(mplsLabel));
+        if (nextHop != null)
+            objs.put(NEXT_HOP_PROPERTY, nextHop);
+
+        intentMappingService.add(siteName, objs);
+    }
+
+    /**
+     * @param siteName
+     *            Name of the member
+     * @return Map of parameters related to the member
+     */
+    public Map<String, String> get(String siteName) {
+        return intentMappingService.get(siteName);
+    }
+
+    /**
+     * @param siteName
+     *            Name of the member
+     * @return Return true if transaction succeed, otherwise false
+     */
+    public boolean delete(String siteName) {
+        try {
+            // TODO: Implement delete() in mapping service
+            // By now, it's going to overwrite data for this key
+            intentMappingService.add(siteName, null);
+            return true;
+        } catch (Exception e) {
+            throw e;
+        }
+    }
+}
diff --git a/vpnintent/impl/src/test/java/org/opendaylight/vpnservice/impl/MappingServiceManagerTests.java b/vpnintent/impl/src/test/java/org/opendaylight/vpnservice/impl/MappingServiceManagerTests.java
new file mode 100644 (file)
index 0000000..2fa5cb3
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2016 Inocybe Technologies 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.vpnservice.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.opendaylight.nic.mapping.api.IntentMappingService;
+
+public class MappingServiceManagerTests {
+
+    @Test
+    public void addInfo() {
+        // Arrange
+        String siteName = "UoR";
+        String ipPrefix = "16.101.233.2/8";
+        String switchPortId = "openflow:1:3";
+        long mplsLabel = 10L;
+        String nextHop = "16.101.233.1/8";
+
+        Map<String, String> map = new HashMap<>();
+        map.put("ip_prefix", ipPrefix);
+        map.put("switch_port", switchPortId);
+        map.put("mpls_label", String.valueOf(mplsLabel));
+        map.put("next_hop", nextHop);
+
+        IntentMappingService mapSvc = mock(IntentMappingService.class);
+        when(mapSvc.get(any(String.class))).thenReturn(map);
+
+        MappingServiceManager manager = new MappingServiceManager(mapSvc);
+
+        // Act
+        manager.add(siteName, ipPrefix, switchPortId, mplsLabel, nextHop);
+
+        Map<String, String> returnedObjs = manager.get(siteName);
+
+        // Assert
+        assertEquals(ipPrefix, returnedObjs.get("ip_prefix"));
+        assertEquals(switchPortId, returnedObjs.get("switch_port"));
+        assertEquals(mplsLabel, Long.parseLong(returnedObjs.get("mpls_label")));
+        assertEquals(nextHop, returnedObjs.get("next_hop"));
+    }
+
+    @Test
+    public void removeInfo() {
+        // Arrange
+        String siteName = "UoR";
+        String ipPrefix = "16.101.233.2/8";
+        String switchPortId = "openflow:1:3";
+
+        Map<String, String> map = new HashMap<>();
+        map.put("ip_prefix", ipPrefix);
+        map.put("switch_port", switchPortId);
+
+        IntentMappingService mapSvc = mock(IntentMappingService.class);
+
+        MappingServiceManager manager = new MappingServiceManager(mapSvc);
+
+        // Add first to delete next
+        manager.add(siteName, ipPrefix, switchPortId, null, null);
+
+        // Act
+        boolean result = manager.delete(siteName);
+
+        // Assert
+        assertTrue(result);
+    }
+}