8b1fb86e9882c17d77115e688617a10332dfd5a2
[hc2vpp.git] /
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.translate.v3po.interfacesstate.ip;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
25 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
26 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.params.AddressDumpParams;
27 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
30 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.List;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLengthBuilder;
40 import org.opendaylight.yangtools.concepts.Builder;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Read customizer for interface Ipv4 addresses.
48  */
49 public class Ipv4AddressCustomizer extends FutureJVppCustomizer
50         implements ListReaderCustomizer<Address, AddressKey, AddressBuilder>, Ipv4Reader {
51
52     private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
53     private static final String CACHE_KEY = Ipv4AddressCustomizer.class.getName();
54
55     private final NamingContext interfaceContext;
56     private final DumpCacheManager<IpAddressDetailsReplyDump, AddressDumpParams> dumpManager;
57
58     public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
59                                  @Nonnull final NamingContext interfaceContext) {
60         super(futureJVppCore);
61         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
62         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, AddressDumpParams>()
63                 .withExecutor(createExecutor(futureJVppCore))
64                 .build();
65     }
66
67     @Override
68     @Nonnull
69     public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
70         return new AddressBuilder();
71     }
72
73     @Override
74     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
75                                       @Nonnull ReadContext ctx)
76             throws ReadFailedException {
77         LOG.debug("Reading attributes for interface address: {}", id);
78
79         final String interfaceName = id.firstKeyOf(Interface.class).getName();
80         final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext());
81         // Key needs to contain interface ID to distinguish dumps between interfaces
82         final String cacheKey = CACHE_KEY + interfaceName;
83         final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
84                 .getDump(id, cacheKey, ctx.getModificationCache(), new AddressDumpParams(interfaceIndex, false));
85
86         if (!dumpOptional.isPresent() || dumpOptional.get().ipAddressDetails.isEmpty()) {
87             return;
88         }
89         final Optional<IpAddressDetails> ipAddressDetails =
90                 findIpAddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp());
91
92         if (ipAddressDetails.isPresent()) {
93             final IpAddressDetails detail = ipAddressDetails.get();
94             builder.setIp(arrayToIpv4AddressNoZone(detail.ip))
95                     .setSubnet(
96                             new PrefixLengthBuilder().setPrefixLength(Short.valueOf(detail.prefixLength)).build());
97
98             if (LOG.isDebugEnabled()) {
99                 LOG.debug("Attributes for {} interface (id={}) address {} successfully read: {}",
100                         interfaceName, interfaceIndex, id, builder.build());
101             }
102         }
103     }
104
105     @Override
106     public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
107             throws ReadFailedException {
108         LOG.debug("Reading list of keys for interface addresses: {}", id);
109
110         final String interfaceName = id.firstKeyOf(Interface.class).getName();
111         final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext());
112         // Key needs to contain interface ID to distinguish dumps between interfaces
113         final String cacheKey = CACHE_KEY + interfaceName;
114         final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
115                 .getDump(id, cacheKey, ctx.getModificationCache(), new AddressDumpParams(interfaceIndex, false));
116
117         return getAllIpv4AddressIds(dumpOptional, AddressKey::new);
118     }
119
120     @Override
121     public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
122         ((Ipv4Builder) builder).setAddress(readData);
123     }
124
125 }