6d2faf2971c6c4cd7896c0954f2668d9246e95c4
[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
57     @Override
58     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
59                                         @Nonnull final NativeForwardPathsTable dataBefore,
60                                         @Nonnull final NativeForwardPathsTable dataAfter,
61                                         @Nonnull final WriteContext writeContext)
62             throws WriteFailedException {
63         // not sure if update makes sense, but just in case
64         deleteFibTable(id);
65         createFibTable(id, dataAfter);
66     }
67
68     @Override
69     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
70                                         @Nonnull final NativeForwardPathsTable dataBefore,
71                                         @Nonnull final WriteContext writeContext)
72             throws WriteFailedException {
73         deleteFibTable(id);
74     }
75
76     private void createFibTable(final InstanceIdentifier<NativeForwardPathsTable> id,
77                                 final NativeForwardPathsTable data) throws WriteFailedException {
78         getReplyForCreate(getFutureJVpp().gpeAddDelIface(getRequest(true, id)).toCompletableFuture(), id, data);
79     }
80
81     private void deleteFibTable(final InstanceIdentifier<NativeForwardPathsTable> id) throws WriteFailedException {
82         getReplyForDelete(getFutureJVpp().gpeAddDelIface(getRequest(false, id)).toCompletableFuture(), id);
83     }
84
85     /**
86      * Maps dpTable and vni to tableId,this also allows to dump lisp specific tables by dumping vni's
87      */
88     private GpeAddDelIface getRequest(final boolean add, final InstanceIdentifier<NativeForwardPathsTable> id) {
89         GpeAddDelIface request = new GpeAddDelIface();
90         request.isL2 = 0;
91         // expects reversed order
92         request.dpTable = tableId(id);
93         request.vni = request.dpTable; // vni must be unique for every table
94         request.isAdd = booleanToByte(add);
95         return request;
96     }
97
98     static int tableId(final InstanceIdentifier<?> id) {
99         return id.firstKeyOf(NativeForwardPathsTable.class).getTableId().intValue();
100     }
101 }