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