2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.nat.read;
19 import io.fd.honeycomb.translate.read.ReadContext;
20 import io.fd.honeycomb.translate.read.ReadFailedException;
21 import io.fd.honeycomb.translate.spi.read.Initialized;
22 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
23 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
24 import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetailsReplyDump;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatConfig;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.NatInstancesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstance;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstanceBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstanceKey;
33 import org.opendaylight.yangtools.concepts.Builder;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Nat instance ID is mapped to VRF-ID in VPP.
42 final class NatInstanceCustomizer implements InitializingListReaderCustomizer<NatInstance, NatInstanceKey, NatInstanceBuilder> {
44 private static final Logger LOG = LoggerFactory.getLogger(NatInstanceCustomizer.class);
45 static final NatInstanceKey DEFAULT_VRF_ID = new NatInstanceKey(0L);
47 private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager;
49 NatInstanceCustomizer(
50 final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager) {
51 this.dumpCacheManager = dumpCacheManager;
56 public NatInstanceBuilder getBuilder(@Nonnull final InstanceIdentifier<NatInstance> id) {
57 return new NatInstanceBuilder();
61 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<NatInstance> id,
62 @Nonnull final NatInstanceBuilder builder, @Nonnull final ReadContext ctx)
63 throws ReadFailedException {
64 LOG.trace("Reading current attributes for nat-instance: {}", id);
65 builder.setId(id.firstKeyOf(NatInstance.class).getId());
70 public List<NatInstanceKey> getAllIds(@Nonnull final InstanceIdentifier<NatInstance> id,
71 @Nonnull final ReadContext context) throws ReadFailedException {
72 LOG.trace("Listing IDs for all nat-instances");
74 // Find the nat instance IDs (vrf-ids) by listing all static mappings and their VRF assignment
75 final List<NatInstanceKey> vrfIds =
76 dumpCacheManager.getDump(id, context.getModificationCache(), null)
77 .or(new SnatStaticMappingDetailsReplyDump()).snatStaticMappingDetails.stream()
78 .map(detail -> detail.vrfId)
79 .map(vrfId -> new NatInstanceKey((long) vrfId))
80 .collect(Collectors.toList());
82 // Add default vrf id if not present
83 if (!vrfIds.contains(DEFAULT_VRF_ID)) {
84 vrfIds.add(0, DEFAULT_VRF_ID);
87 LOG.debug("List of nat-instance keys (vrf-ids): {}", vrfIds);
92 public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<NatInstance> readData) {
93 ((NatInstancesBuilder) builder).setNatInstance(readData);
97 public Initialized<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstance> init(@Nonnull final InstanceIdentifier<NatInstance> id,
98 @Nonnull final NatInstance readValue,
99 @Nonnull final ReadContext ctx) {
100 return Initialized.create(getCfgId(id),
101 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstanceBuilder()
102 .setId(readValue.getId())
106 static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstance> getCfgId(
107 @Nonnull final InstanceIdentifier<NatInstance> id) {
108 return InstanceIdentifier.create(NatConfig.class)
109 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.NatInstances.class)
110 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstance.class,
111 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstanceKey(
112 id.firstKeyOf(NatInstance.class).getId()));