HONEYCOMB-254 : Removed workaround for locator-set-index
authorJan Srnicek <[email protected]>
Thu, 13 Oct 2016 13:28:14 +0000 (15:28 +0200)
committerMarek Gradzki <[email protected]>
Fri, 14 Oct 2016 06:37:50 +0000 (06:37 +0000)
Used value returned from api call instead of workaround
with dumping

Change-Id: Id44404579cde6fb20bb4e9c6d5b3e91fb4dbf2b1
Signed-off-by: Jan Srnicek <[email protected]>
lisp/lisp2vpp/src/main/java/io/fd/honeycomb/lisp/translate/read/LocatorSetCustomizer.java
lisp/lisp2vpp/src/main/java/io/fd/honeycomb/lisp/translate/write/LocatorSetCustomizer.java
lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/translate/write/LocatorSetCustomizerTest.java

index bacdd55..9a55e50 100755 (executable)
@@ -49,8 +49,7 @@ public class LocatorSetCustomizer extends FutureJVppCustomizer
         implements ListReaderCustomizer<LocatorSet, LocatorSetKey, LocatorSetBuilder>, ByteDataTranslator,
         LocatorSetReader {
 
-    //TODO - temporary as public because of hack in write customizer in *.write.LocatorSetCustomizer
-    public static final String LOCATOR_SETS_CACHE_ID = LocatorSetCustomizer.class.getName();
+    private static final String LOCATOR_SETS_CACHE_ID = LocatorSetCustomizer.class.getName();
     private static final Logger LOG = LoggerFactory.getLogger(LocatorSetCustomizer.class);
 
     private final DumpCacheManager<LispLocatorSetDetailsReplyDump, Void> dumpManager;
index 6e15315..d89e120 100755 (executable)
@@ -20,26 +20,18 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
-import com.google.common.base.Optional;
 import io.fd.honeycomb.lisp.translate.read.trait.LocatorSetReader;
-import io.fd.honeycomb.translate.ModificationCache;
-import io.fd.honeycomb.translate.read.ReadFailedException;
 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
-import io.fd.honeycomb.translate.util.RWUtils;
 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
-import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
 import io.fd.honeycomb.translate.vpp.util.NamingContext;
 import io.fd.honeycomb.translate.write.WriteContext;
 import io.fd.honeycomb.translate.write.WriteFailedException;
-import io.fd.vpp.jvpp.VppBaseCallException;
 import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
-import java.io.UnsupportedEncodingException;
 import java.util.List;
-import java.util.concurrent.TimeoutException;
 import javax.annotation.Nonnull;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
@@ -72,32 +64,14 @@ public class LocatorSetCustomizer extends FutureJVppCustomizer
     public void writeCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
                                        @Nonnull LocatorSet dataAfter,
                                        @Nonnull WriteContext writeContext) throws WriteFailedException {
-
-        checkNotNull(dataAfter, "LocatorSet is null");
-
-        final String locatorSetName = dataAfter.getName();
-        checkNotNull(locatorSetName, "LocatorSet name is null");
         checkState(isNonEmptyLocatorSet(writeContext.readAfter(id).get()),
                 "Creating empty locator-sets is not allowed");
-        // TODO VPP-323 check and fill mapping when api returns index of created locator set
-        // checkState(!locatorSetContext.containsIndex(locatorSetName, writeContext.getMappingContext()),
-        //         "Locator set with name %s already defined", locatorSetName);
-
-        try {
-            addDelLocatorSetAndReply(true, dataAfter.getName());
-        } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
-            throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
-        }
-
-        //TODO - REMOVE FROM MASTER AFTER VPP-323
-        try {
-            locatorSetContext
-                    .addName(getLocatorSetIndex(id, locatorSetName, writeContext.getModificationCache()),
-                            locatorSetName, writeContext.getMappingContext());
-        } catch (ReadFailedException e) {
-            throw new WriteFailedException(id,
-                    new IllegalStateException("Unable to create mapping for locator set " + locatorSetName, e));
-        }
+        final String locatorSetName = dataAfter.getName();
+        checkState(!locatorSetContext.containsIndex(locatorSetName, writeContext.getMappingContext()),
+                "Locator set with name %s already defined", locatorSetName);
+
+        final int locatorSetIndex = addDelLocatorSetAndReply(true, dataAfter.getName(), id);
+        locatorSetContext.addName(locatorSetIndex, locatorSetName, writeContext.getMappingContext());
     }
 
     private boolean isNonEmptyLocatorSet(final LocatorSet locatorSet) {
@@ -110,61 +84,28 @@ public class LocatorSetCustomizer extends FutureJVppCustomizer
                                         @Nonnull LocatorSet dataBefore,
                                         @Nonnull LocatorSet dataAfter,
                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
-        throw new UnsupportedOperationException("Operation not supported");
+        throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
+                new UnsupportedOperationException("Operation not supported"));
     }
 
     @Override
     public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
                                         @Nonnull LocatorSet dataBefore,
                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
-
-        checkNotNull(dataBefore, "LocatorSet is null");
-
         final String locatorSetName = dataBefore.getName();
-        checkNotNull(locatorSetName, "LocatorSet name is null");
-
-        try {
-            addDelLocatorSetAndReply(false, dataBefore.getName());
-        } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
-            throw new WriteFailedException.DeleteFailedException(id, e);
-        }
-
+        addDelLocatorSetAndReply(false, dataBefore.getName(), id);
         //removes mapping after successful delete
         locatorSetContext.removeName(locatorSetName, writeContext.getMappingContext());
     }
 
-    private void addDelLocatorSetAndReply(boolean add, String name)
-            throws VppBaseCallException, TimeoutException, UnsupportedEncodingException {
+    private int addDelLocatorSetAndReply(final boolean add, final String name, final InstanceIdentifier<LocatorSet> id)
+            throws WriteFailedException {
 
         LispAddDelLocatorSet addDelSet = new LispAddDelLocatorSet();
 
         addDelSet.isAdd = booleanToByte(add);
         addDelSet.locatorSetName = name.getBytes(UTF_8);
 
-
-        getReply(getFutureJVpp().lispAddDelLocatorSet(addDelSet).toCompletableFuture());
+        return getReplyForWrite(getFutureJVpp().lispAddDelLocatorSet(addDelSet).toCompletableFuture(), id).lsIndex;
     }
-
-    //TODO - REMOVE FROM MASTER AFTER VPP-323
-    // total hack
-    public int getLocatorSetIndex(final InstanceIdentifier<LocatorSet> identifier, final String name,
-                                  final ModificationCache cache)
-            throws ReadFailedException {
-
-        Optional<LispLocatorSetDetailsReplyDump> reply = dumpManager
-                .getDump(identifier, io.fd.honeycomb.lisp.translate.read.LocatorSetCustomizer.LOCATOR_SETS_CACHE_ID,
-                        cache,
-                        EntityDumpExecutor.NO_PARAMS);
-
-        if (reply.isPresent()) {
-            return reply.get().lispLocatorSetDetails.stream()
-                    .filter(a -> name.equals(toString(a.lsName)))
-                    .collect(RWUtils.singleItemCollector())
-                    .lsIndex;
-        } else {
-            throw new IllegalStateException("Unable to find index of locator set " + name);
-        }
-    }
-
-
 }
index 9307af6..43b5ee8 100755 (executable)
@@ -18,6 +18,8 @@ package io.fd.honeycomb.lisp.translate.write;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
@@ -29,6 +31,10 @@ import com.google.common.collect.ImmutableList;
 import io.fd.honeycomb.translate.vpp.util.NamingContext;
 import io.fd.honeycomb.translate.write.WriteFailedException;
 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
+import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
+import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSetReply;
+import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetails;
+import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.concurrent.ExecutionException;
@@ -40,10 +46,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
-import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSetReply;
-import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetails;
-import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
 
 public class LocatorSetCustomizerTest extends WriterCustomizerTest {
 
@@ -51,21 +53,22 @@ public class LocatorSetCustomizerTest extends WriterCustomizerTest {
 
     @Override
     public void setUp() {
-        customizer = new LocatorSetCustomizer(api, new NamingContext("locator-set", "instance"));
+        customizer = new LocatorSetCustomizer(api, new NamingContext("locator-set", "locator-set-context"));
     }
 
     @Test(expected = NullPointerException.class)
     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
-        customizer.writeCurrentAttributes(null, null, null);
+        customizer.writeCurrentAttributes(null, null, writeContext);
     }
 
     @Test(expected = NullPointerException.class)
     public void testWriteCurrentAttributesBadData() throws WriteFailedException {
-        customizer.writeCurrentAttributes(null, mock(LocatorSet.class), null);
+        customizer.writeCurrentAttributes(null, mock(LocatorSet.class), writeContext);
     }
 
     @Test
     public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
+        noMappingDefined(mappingContext, "Locator", "locator-set-context");
         LocatorSet locatorSet = new LocatorSetBuilder()
                 .setName("Locator")
                 .setInterface(Arrays.asList(new InterfaceBuilder().build()))
@@ -77,7 +80,8 @@ public class LocatorSetCustomizerTest extends WriterCustomizerTest {
 
         ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
 
-        when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class))).thenReturn(future(new LispAddDelLocatorSetReply()));
+        when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class)))
+                .thenReturn(future(new LispAddDelLocatorSetReply()));
         when(writeContext.readAfter(validId)).thenReturn(Optional.of(locatorSet));
 
         final LispLocatorSetDetailsReplyDump reply = new LispLocatorSetDetailsReplyDump();
@@ -85,8 +89,6 @@ public class LocatorSetCustomizerTest extends WriterCustomizerTest {
         details.lsName = "Locator".getBytes(StandardCharsets.UTF_8);
         reply.lispLocatorSetDetails = ImmutableList.of(details);
 
-        cache.put(io.fd.honeycomb.lisp.translate.read.LocatorSetCustomizer.LOCATOR_SETS_CACHE_ID, reply);
-
         customizer.writeCurrentAttributes(validId, locatorSet, writeContext);
 
         verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
@@ -98,19 +100,28 @@ public class LocatorSetCustomizerTest extends WriterCustomizerTest {
         assertEquals(1, request.isAdd);
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testUpdateCurrentAttributes() throws WriteFailedException {
-        customizer.updateCurrentAttributes(null, null, null, null);
+        final InstanceIdentifier<LocatorSet> identifier = InstanceIdentifier.create(LocatorSet.class);
+        try {
+            customizer
+                    .updateCurrentAttributes(identifier, mock(LocatorSet.class), mock(LocatorSet.class), writeContext);
+        } catch (WriteFailedException e) {
+            assertTrue(e.getCause() instanceof UnsupportedOperationException);
+            assertEquals(identifier, e.getFailedId());
+            return;
+        }
+        fail("Test should have failed");
     }
 
     @Test(expected = NullPointerException.class)
     public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
-        customizer.deleteCurrentAttributes(null, null, null);
+        customizer.deleteCurrentAttributes(null, null, writeContext);
     }
 
     @Test(expected = NullPointerException.class)
     public void testDeleteCurrentAttributesBadData() throws WriteFailedException {
-        customizer.deleteCurrentAttributes(null, mock(LocatorSet.class), null);
+        customizer.deleteCurrentAttributes(null, mock(LocatorSet.class), writeContext);
     }
 
     @Test
@@ -121,7 +132,8 @@ public class LocatorSetCustomizerTest extends WriterCustomizerTest {
 
         ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
 
-        when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class))).thenReturn(future(new LispAddDelLocatorSetReply()));
+        when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class)))
+                .thenReturn(future(new LispAddDelLocatorSetReply()));
 
         customizer.deleteCurrentAttributes(null, locatorSet, writeContext);