75791865ebc768922d43f28db26d825450ec5fac
[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.util.read.cache.exceptions.execution.DumpExecutionFailedException;
27 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.AddressDumpExecutor;
28 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.params.AddressDumpParams;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import io.fd.honeycomb.translate.vpp.util.NamingContext;
31 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
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.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.Ipv4Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.Address;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.openvpp.jvpp.core.dto.IpAddressDetails;
45 import org.openvpp.jvpp.core.dto.IpAddressDetailsReplyDump;
46 import org.openvpp.jvpp.core.future.FutureJVppCore;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Read customizer for sub-interface Ipv4 addresses.
52  */
53 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
54         implements ListReaderCustomizer<Address, AddressKey, AddressBuilder>, Ipv4Reader {
55
56     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
57     private static final String CACHE_KEY = SubInterfaceIpv4AddressCustomizer.class.getName();
58
59     private final NamingContext interfaceContext;
60     private final DumpCacheManager<IpAddressDetailsReplyDump, AddressDumpParams> dumpManager;
61
62     public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
63                                              @Nonnull final NamingContext interfaceContext) {
64         super(futureJVppCore);
65         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
66         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, AddressDumpParams>()
67                 .withExecutor(new AddressDumpExecutor(futureJVppCore))
68                 .build();
69     }
70
71     @Override
72     @Nonnull
73     public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
74         return new AddressBuilder();
75     }
76
77     @Override
78     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
79                                       @Nonnull ReadContext ctx)
80             throws ReadFailedException {
81         LOG.debug("Reading attributes for sub-interface address: {}", id);
82
83         final String subInterfaceName = getSubInterfaceName(id);
84         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
85         final Optional<IpAddressDetailsReplyDump> dumpOptional;
86         try {
87             dumpOptional = dumpManager
88                     .getDump(CACHE_KEY, ctx.getModificationCache(), new AddressDumpParams(subInterfaceIndex, false));
89         } catch (DumpExecutionFailedException e) {
90             throw new ReadFailedException(id, e);
91         }
92
93
94         final Optional<IpAddressDetails> ipAddressDetails =
95                 findIpAddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp());
96
97         if (ipAddressDetails.isPresent()) {
98             final IpAddressDetails detail = ipAddressDetails.get();
99             builder.setIp(arrayToIpv4AddressNoZone(detail.ip));
100             builder.setSubnet(new PrefixLengthBuilder().setPrefixLength(Short.valueOf(detail.prefixLength)).build());
101
102             if (LOG.isDebugEnabled()) {
103                 LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}",
104                         subInterfaceName, subInterfaceIndex, id, builder.build());
105             }
106         }
107     }
108
109     @Override
110     public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
111             throws ReadFailedException {
112         LOG.debug("Reading list of keys for sub-interface addresses: {}", id);
113
114         final String subInterfaceName = getSubInterfaceName(id);
115         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
116         final Optional<IpAddressDetailsReplyDump> dumpOptional;
117         try {
118             dumpOptional = dumpManager
119                     .getDump(CACHE_KEY, ctx.getModificationCache(), new AddressDumpParams(subInterfaceIndex, false));
120         } catch (DumpExecutionFailedException e) {
121             throw new ReadFailedException(id, e);
122         }
123
124         return getAllIpv4AddressIds(dumpOptional, AddressKey::new);
125     }
126
127     @Override
128     public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
129         ((Ipv4Builder) builder).setAddress(readData);
130     }
131
132     private static String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
133         return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
134                 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
135     }
136 }