2afa852d47eb9db1b1ec6a7e1f0a5ee6cc81f4f6
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.lisp.gpe.translate.write;
18
19 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
20 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
21 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
22 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
23 import io.fd.honeycomb.translate.write.WriteContext;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import io.fd.vpp.jvpp.core.dto.GpeAddDelIface;
26 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 /**
33  * Uses api to create gpe interfaces, which creates fib table as by-product.
34  * There is currently no other lisp-specific way to define fib table, as native paths expects existing table id
35  */
36 public class NativeForwardPathsTableCustomizer extends FutureJVppCustomizer
37         implements ListWriterCustomizer<NativeForwardPathsTable, NativeForwardPathsTableKey>, ByteDataTranslator,
38         JvppReplyConsumer {
39
40     public NativeForwardPathsTableCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
41         super(futureJVppCore);
42     }
43
44     @Override
45     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
46                                        @Nonnull final NativeForwardPathsTable dataAfter,
47                                        @Nonnull final WriteContext writeContext)
48             throws WriteFailedException {
49         createFibTable(id, dataAfter);
50     }
51
52     @Override
53     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
54                                         @Nonnull final NativeForwardPathsTable dataBefore,
55                                         @Nonnull final WriteContext writeContext)
56             throws WriteFailedException {
57         deleteFibTable(id);
58     }
59
60     private void createFibTable(final InstanceIdentifier<NativeForwardPathsTable> id,
61                                 final NativeForwardPathsTable data) throws WriteFailedException {
62         getReplyForCreate(getFutureJVpp().gpeAddDelIface(getRequest(true, id)).toCompletableFuture(), id, data);
63     }
64
65     private void deleteFibTable(final InstanceIdentifier<NativeForwardPathsTable> id) throws WriteFailedException {
66         getReplyForDelete(getFutureJVpp().gpeAddDelIface(getRequest(false, id)).toCompletableFuture(), id);
67     }
68
69     /**
70      * Maps dpTable and vni to tableId,this also allows to dump lisp specific tables by dumping vni's
71      */
72     private GpeAddDelIface getRequest(final boolean add, final InstanceIdentifier<NativeForwardPathsTable> id) {
73         GpeAddDelIface request = new GpeAddDelIface();
74         request.isL2 = 0;
75         // expects reversed order
76         request.dpTable = tableId(id);
77         request.vni = request.dpTable; // vni must be unique for every table
78         request.isAdd = booleanToByte(add);
79         return request;
80     }
81
82     static int tableId(final InstanceIdentifier<?> id) {
83         return id.firstKeyOf(NativeForwardPathsTable.class).getTableId().intValue();
84     }
85 }