8ba1bcf3f9b8f4bb7c3167e8b798d8bf9f1c7011
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / ItrRemoteLocatorSetCustomizer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.lisp.translate.write;
18
19
20 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
22 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import java.nio.charset.StandardCharsets;
27 import java.util.concurrent.TimeoutException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocs;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34
35 public class ItrRemoteLocatorSetCustomizer extends FutureJVppCustomizer implements
36         WriterCustomizer<ItrRemoteLocatorSet>, ByteDataTranslator, JvppReplyConsumer {
37
38     public ItrRemoteLocatorSetCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
39         super(futureJVppCore);
40     }
41
42     @Override
43     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ItrRemoteLocatorSet> id,
44                                        @Nonnull final ItrRemoteLocatorSet dataAfter,
45                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
46         try {
47             addDelItrRemoteLocatorSet(true, dataAfter);
48         } catch (TimeoutException | VppBaseCallException e) {
49             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
50         }
51     }
52
53     @Override
54     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ItrRemoteLocatorSet> id,
55                                         @Nonnull final ItrRemoteLocatorSet dataBefore,
56                                         @Nonnull final ItrRemoteLocatorSet dataAfter,
57                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
58         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
59                 new UnsupportedOperationException("Operation not supported"));
60     }
61
62     @Override
63     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ItrRemoteLocatorSet> id,
64                                         @Nonnull final ItrRemoteLocatorSet dataBefore,
65                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
66         try {
67             addDelItrRemoteLocatorSet(false, dataBefore);
68         } catch (TimeoutException | VppBaseCallException e) {
69             throw new WriteFailedException.DeleteFailedException(id, e);
70         }
71     }
72
73     private void addDelItrRemoteLocatorSet(final boolean add, @Nonnull final ItrRemoteLocatorSet data)
74             throws TimeoutException, VppBaseCallException {
75
76         LispAddDelMapRequestItrRlocs request = new LispAddDelMapRequestItrRlocs();
77         request.isAdd = booleanToByte(add);
78         request.locatorSetName = data.getRemoteLocatorSetName().getBytes(StandardCharsets.UTF_8);
79
80         getReply(getFutureJVpp().lispAddDelMapRequestItrRlocs(request).toCompletableFuture());
81     }
82 }