1e4b62c87f36f02fab27231ac7000d8d61666c57
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.v3po.interfacesstate.ip.v6.subinterface;
18
19
20 import com.google.common.base.Optional;
21 import io.fd.hc2vpp.common.translate.util.NamingContext;
22 import io.fd.hc2vpp.v3po.interfacesstate.SubInterfaceCustomizer;
23 import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter;
24 import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpAddressReader;
25 import io.fd.hc2vpp.v3po.util.SubInterfaceUtils;
26 import io.fd.honeycomb.translate.read.ReadContext;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.Initialized;
29 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
30 import io.fd.honeycomb.translate.util.RWUtils;
31 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
32 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
33 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.Address;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.AddressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.AddressKey;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import javax.annotation.Nonnull;
49 import java.util.List;
50
51 public class SubInterfaceIpv6AddressCustomizer extends IpAddressReader
52         implements InitializingListReaderCustomizer<Address, AddressKey, AddressBuilder> {
53
54     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv6AddressCustomizer.class);
55
56     public SubInterfaceIpv6AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
57                                              @Nonnull final NamingContext interfaceContext) {
58         super(interfaceContext, true, new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, IfaceDumpFilter>()
59                 .withExecutor(createAddressDumpExecutor(futureJVppCore))
60                 .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpAddressDetailsReplyDump.class))
61                 .build());
62     }
63
64     private static String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
65         return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
66                 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
67     }
68
69     @Override
70     @Nonnull
71     public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
72         return new AddressBuilder();
73     }
74
75     @Override
76     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
77                                       @Nonnull ReadContext ctx)
78             throws ReadFailedException {
79         LOG.debug("Reading attributes for sub-interface address: {}", id);
80         final Optional<IpAddressDetails> ipAddressDetails =
81                 findIpv6AddressDetailsByIp(subInterfaceAddressDumpSupplier(id, ctx), id.firstKeyOf(Address.class).getIp());
82
83         if (ipAddressDetails.isPresent()) {
84             final IpAddressDetails detail = ipAddressDetails.get();
85             builder.setIp(arrayToIpv6AddressNoZone(detail.ip));
86             builder.setPrefixLength((short) Byte.toUnsignedInt(detail.prefixLength));
87
88             if (LOG.isDebugEnabled()) {
89                 final String subInterfaceName = getSubInterfaceName(id);
90                 final int subInterfaceIndex = getInterfaceContext().getIndex(subInterfaceName, ctx.getMappingContext());
91                 LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}",
92                         subInterfaceName, subInterfaceIndex, id, builder.build());
93             }
94         }
95     }
96
97     @Override
98     @Nonnull
99     public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
100             throws ReadFailedException {
101         LOG.debug("Reading list of keys for sub-interface addresses: {}", id);
102         return getAllIpv6AddressIds(subInterfaceAddressDumpSupplier(id, ctx), AddressKey::new);
103     }
104
105     @Override
106     public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
107         ((Ipv6Builder) builder).setAddress(readData);
108     }
109
110     @Override
111     @Nonnull
112     public Initialized<Address> init(
113             @Nonnull final InstanceIdentifier<Address> id, @Nonnull final Address readValue,
114             @Nonnull final ReadContext ctx) {
115         return Initialized.create(getCfgId(id), readValue);
116     }
117
118     private InstanceIdentifier<Address> getCfgId(final InstanceIdentifier<Address> id) {
119         return SubInterfaceCustomizer.getCfgId(RWUtils.cutId(id, SubInterface.class))
120                 .child(Ipv6.class)
121                 .child(Address.class, new AddressKey(id.firstKeyOf(Address.class)));
122     }
123 }