928c466afe206e695902e6f7a0983e1f1b340fcb
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / MapResolverCustomizer.java
1 /*
2  * Copyright (c) 2015 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 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
23 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
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.rev160520.map.resolvers.grouping.map.resolvers.MapResolver;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.map.resolvers.grouping.map.resolvers.MapResolverKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import io.fd.vpp.jvpp.VppBaseCallException;
33 import io.fd.vpp.jvpp.core.dto.LispAddDelMapResolver;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35
36
37 /**
38  * Handles updates of {@link MapResolver} list
39  */
40 public class MapResolverCustomizer extends FutureJVppCustomizer
41         implements ListWriterCustomizer<MapResolver, MapResolverKey>, AddressTranslator,
42         JvppReplyConsumer {
43
44     public MapResolverCustomizer(final FutureJVppCore vppApi) {
45         super(vppApi);
46     }
47
48     @Override
49     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<MapResolver> id,
50                                        @Nonnull final MapResolver dataAfter, @Nonnull final WriteContext writeContext)
51             throws WriteFailedException {
52
53         checkNotNull(dataAfter, "Data is null");
54         checkNotNull(dataAfter.getIpAddress(), "Address is null");
55
56         try {
57             addDelMapResolverAndReply(true, dataAfter);
58         } catch (VppBaseCallException | TimeoutException e) {
59             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
60         }
61     }
62
63     @Override
64     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<MapResolver> id,
65                                         @Nonnull final MapResolver dataBefore, @Nonnull final MapResolver dataAfter,
66                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
67         throw new UnsupportedOperationException("Operation not supported");
68     }
69
70     @Override
71     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<MapResolver> id,
72                                         @Nonnull final MapResolver dataBefore, @Nonnull final WriteContext writeContext)
73             throws WriteFailedException {
74
75         checkNotNull(dataBefore, "Data is null");
76         checkNotNull(dataBefore.getIpAddress(), "Address is null");
77
78         try {
79             addDelMapResolverAndReply(false, dataBefore);
80         } catch (VppBaseCallException | TimeoutException e) {
81             throw new WriteFailedException.CreateFailedException(id, dataBefore, e);
82         }
83     }
84
85     private void addDelMapResolverAndReply(boolean add, MapResolver data) throws VppBaseCallException,
86             TimeoutException {
87
88         LispAddDelMapResolver request = new LispAddDelMapResolver();
89         request.isAdd = booleanToByte(add);
90
91
92         boolean ipv6 = isIpv6(data.getIpAddress());
93
94         request.isIpv6 = booleanToByte(ipv6);
95         request.ipAddress = ipAddressToArray(ipv6, data.getIpAddress());
96
97         getReply(getFutureJVpp().lispAddDelMapResolver(request).toCompletableFuture());
98     }
99 }