d7f46ad9b68cb1b8c19005161ec1436369073888
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / trait / SubtableWriter.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.trait;
18
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import java.util.concurrent.TimeoutException;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.BridgeDomainSubtable;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.VrfSubtable;
29 import org.opendaylight.yangtools.yang.binding.ChildOf;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import io.fd.vpp.jvpp.core.dto.LispEidTableAddDelMap;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import org.slf4j.Logger;
35
36 /**
37  * Trait providing logic for writing subtables
38  */
39 public interface SubtableWriter extends ByteDataTranslator, JvppReplyConsumer {
40     int DEFAULT_VNI = 0;
41
42     /**
43      * Writes mapping from {@link VniTable}
44      * to {@link VrfSubtable} or
45      * {@link BridgeDomainSubtable}
46      *
47      * @param addDel  true if add,delete otherwise
48      * @param vni     {@link VniTable} ID
49      * @param tableId if <b>isL2</b> is true, than bridge domain subtable id,else vrf subtable id
50      * @param isL2    indicates whether (false) writing to L3 vrfSubtrable of (true) L2 bridgeDomainSubtrable
51      */
52     default void addDelSubtableMapping(@Nonnull final FutureJVppCore vppApi, final boolean addDel, final int vni,
53                                        final int tableId,
54                                        final boolean isL2,
55                                        final Logger logger) throws TimeoutException, VppBaseCallException {
56
57         if (vni == DEFAULT_VNI) {
58             // attempt to write subtable with default vni mapping(it does'nt make sense and it should'nt be possible)
59             // also allows to enable lisp without defining default mapping in request
60             logger.info("An attempt to write subtable[id = {}] with default vni {} was detected, ignoring write",
61                     tableId, DEFAULT_VNI);
62             return;
63         }
64
65         checkNotNull(vppApi, "VPP Api refference cannot be null");
66
67         LispEidTableAddDelMap request = new LispEidTableAddDelMap();
68
69         request.isAdd = booleanToByte(addDel);
70         request.vni = vni;
71         request.dpTable = tableId;
72         request.isL2 = booleanToByte(isL2);
73
74         getReply(vppApi.lispEidTableAddDelMap(request).toCompletableFuture());
75     }
76
77     default int extractVni(@Nonnull final InstanceIdentifier<? extends ChildOf<VniTable>> id) {
78         return checkNotNull(
79                 checkNotNull(id, "Identifier cannot be null").firstKeyOf(VniTable.class),
80                 "Parent VNI id not defined").getVirtualNetworkIdentifier().intValue();
81     }
82 }