HONEYCOMB-58 - Routing Api
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / trait / MappingReader.java
1 /*
2  * Copyright (c) 2016 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.read.trait;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV4;
21 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV6;
22 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.MAC;
23
24 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams;
25 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
26 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
27 import io.fd.vpp.jvpp.core.dto.LispEidTableDetails;
28 import io.fd.vpp.jvpp.core.dto.LispEidTableDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.dto.LispEidTableDump;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.util.function.Predicate;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.LocalMapping;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMapping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.vni.table.BridgeDomainSubtable;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.vni.table.VrfSubtable;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 /**
40  * Trait providing predicates to filter mappings to respective subtables
41  */
42 public interface MappingReader extends JvppReplyConsumer {
43
44     Predicate<LispEidTableDetails> BRIDGE_DOMAIN_MAPPINGS_ONLY =
45             (LispEidTableDetails detail) -> detail.eidType == MAC.getValue();
46
47     Predicate<LispEidTableDetails> VRF_MAPPINGS_ONLY =
48             (LispEidTableDetails detail) -> detail.eidType == IPV4.getValue() || detail.eidType == IPV6.getValue();
49
50     default Predicate<LispEidTableDetails> subtableFilterForLocalMappings(
51             @Nonnull final InstanceIdentifier<LocalMapping> identifier) {
52
53         if (identifier.firstIdentifierOf(VrfSubtable.class) != null) {
54             return VRF_MAPPINGS_ONLY;
55         } else if (identifier.firstIdentifierOf(BridgeDomainSubtable.class) != null) {
56             return BRIDGE_DOMAIN_MAPPINGS_ONLY;
57         } else {
58             throw new IllegalArgumentException("Cannot determine mappings predicate for " + identifier);
59         }
60     }
61
62     default Predicate<LispEidTableDetails> subtableFilterForRemoteMappings(
63             @Nonnull final InstanceIdentifier<RemoteMapping> identifier) {
64
65         if (identifier.firstIdentifierOf(VrfSubtable.class) != null) {
66             return VRF_MAPPINGS_ONLY;
67         } else if (identifier.firstIdentifierOf(BridgeDomainSubtable.class) != null) {
68             return BRIDGE_DOMAIN_MAPPINGS_ONLY;
69         } else {
70             throw new IllegalArgumentException("Cannot determine mappings predicate for " + identifier);
71         }
72     }
73
74     default EntityDumpExecutor<LispEidTableDetailsReplyDump, MappingsDumpParams> createMappingDumpExecutor(
75             @Nonnull final FutureJVppCore vppApi) {
76         return (identifier, params) -> {
77             checkNotNull(params, "Params for dump request not present");
78
79             LispEidTableDump request = new LispEidTableDump();
80             request.eid = params.getEid();
81             request.eidSet = params.getEidSet();
82             request.eidType = params.getEidType();
83             request.prefixLength = params.getPrefixLength();
84             request.vni = params.getVni();
85             request.filter = params.getFilter();
86
87             return getReplyForRead(vppApi.lispEidTableDump(request).toCompletableFuture(), identifier);
88         };
89     }
90 }