f04a13f5ca6d25cf97200b56a1156f01669f2683
[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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Uses api to create gpe interfaces, which creates fib table as by-product.
36  * There is currently no other lisp-specific way to define fib table, as native paths expects existing table id
37  */
38 public class NativeForwardPathsTableCustomizer extends FutureJVppCustomizer
39         implements ListWriterCustomizer<NativeForwardPathsTable, NativeForwardPathsTableKey>, ByteDataTranslator,
40         JvppReplyConsumer {
41
42     private static final Logger LOG = LoggerFactory.getLogger(NativeForwardPathsTableCustomizer.class);
43
44     public NativeForwardPathsTableCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
45         super(futureJVppCore);
46     }
47
48     @Override
49     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
50                                        @Nonnull final NativeForwardPathsTable dataAfter,
51                                        @Nonnull final WriteContext writeContext)
52             throws WriteFailedException {
53         createFibTable(id, dataAfter);
54     }
55
56     @Override
57     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
58                                         @Nonnull final NativeForwardPathsTable dataBefore,
59                                         @Nonnull final WriteContext writeContext)
60             throws WriteFailedException {
61         deleteFibTable(id);
62     }
63
64     private void createFibTable(final InstanceIdentifier<NativeForwardPathsTable> id,
65                                 final NativeForwardPathsTable data) throws WriteFailedException {
66         getReplyForCreate(getFutureJVpp().gpeAddDelIface(getRequest(true, id)).toCompletableFuture(), id, data);
67     }
68
69     private void deleteFibTable(final InstanceIdentifier<NativeForwardPathsTable> id) throws WriteFailedException {
70         getReplyForDelete(getFutureJVpp().gpeAddDelIface(getRequest(false, id)).toCompletableFuture(), id);
71     }
72
73     /**
74      * Maps dpTable and vni to tableId,this also allows to dump lisp specific tables by dumping vni's
75      */
76     private GpeAddDelIface getRequest(final boolean add, final InstanceIdentifier<NativeForwardPathsTable> id) {
77         GpeAddDelIface request = new GpeAddDelIface();
78         request.isL2 = 0;
79         // expects reversed order
80         request.dpTable = tableId(id);
81         request.vni = request.dpTable; // vni must be unique for every table
82         request.isAdd = booleanToByte(add);
83         return request;
84     }
85
86     static int tableId(final InstanceIdentifier<?> id) {
87         return id.firstKeyOf(NativeForwardPathsTable.class).getTableId().intValue();
88     }
89 }