f27229acda3592620a3025f13bc8eaba4dbfb8e5
[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.hc2vpp.v3po.interfacesstate.ip.v4.subinterface;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.base.Optional;
22 import com.google.common.collect.ImmutableSet;
23 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
24 import io.fd.hc2vpp.common.translate.util.NamingContext;
25 import io.fd.hc2vpp.v3po.interfacesstate.SubInterfaceCustomizer;
26 import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader;
27 import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter;
28 import io.fd.hc2vpp.v3po.util.SubInterfaceUtils;
29 import io.fd.honeycomb.translate.read.ReadContext;
30 import io.fd.honeycomb.translate.read.ReadFailedException;
31 import io.fd.honeycomb.translate.spi.read.Initialized;
32 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
33 import io.fd.honeycomb.translate.util.RWUtils;
34 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
35 import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory;
36 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
37 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
38 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
39 import java.util.List;
40 import javax.annotation.Nonnull;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
49 import org.opendaylight.yangtools.concepts.Builder;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * Read customizer for sub-interface Ipv4 addresses.
57  */
58 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
59         implements InitializingListReaderCustomizer<Address, AddressKey, AddressBuilder>, IpReader {
60
61     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
62
63     private final NamingContext interfaceContext;
64     private final DumpCacheManager<IpAddressDetailsReplyDump, IfaceDumpFilter> dumpManager;
65
66     public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
67                                              @Nonnull final NamingContext interfaceContext) {
68         super(futureJVppCore);
69         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
70         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, IfaceDumpFilter>()
71                 .withExecutor(createAddressDumpExecutor(futureJVppCore))
72                 //same as with ipv4 addresses for interfaces, these must have cache scope of their parent sub-interface
73                 .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class,
74                         ImmutableSet.of(SubInterface.class)))
75                 .build();
76     }
77
78     private static String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
79         return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
80                 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
81     }
82
83     @Override
84     @Nonnull
85     public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
86         return new AddressBuilder();
87     }
88
89     @Override
90     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
91                                       @Nonnull ReadContext ctx)
92             throws ReadFailedException {
93         LOG.debug("Reading attributes for sub-interface address: {}", id);
94
95         final String subInterfaceName = getSubInterfaceName(id);
96         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
97         final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
98                 .getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(subInterfaceIndex, false));
99
100         final Optional<IpAddressDetails> ipAddressDetails =
101                 findIpv4AddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp());
102
103         if (ipAddressDetails.isPresent()) {
104             final IpAddressDetails detail = ipAddressDetails.get();
105             builder.setIp(arrayToIpv4AddressNoZone(detail.ip));
106             builder.setSubnet(new PrefixLengthBuilder().setPrefixLength((short) detail.prefixLength).build());
107
108             if (LOG.isDebugEnabled()) {
109                 LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}",
110                         subInterfaceName, subInterfaceIndex, id, builder.build());
111             }
112         }
113     }
114
115     @Override
116     @Nonnull
117     public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
118             throws ReadFailedException {
119         LOG.debug("Reading list of keys for sub-interface addresses: {}", id);
120
121         final String subInterfaceName = getSubInterfaceName(id);
122         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
123         final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
124                 .getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(subInterfaceIndex, false));
125
126         return getAllIpv4AddressIds(dumpOptional, AddressKey::new);
127     }
128
129     @Override
130     public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
131         ((Ipv4Builder) builder).setAddress(readData);
132     }
133
134     @Override
135     public Initialized<Address> init(
136             @Nonnull final InstanceIdentifier<Address> id, @Nonnull final Address readValue,
137             @Nonnull final ReadContext ctx) {
138         return Initialized.create(getCfgId(id), readValue);
139     }
140
141     private InstanceIdentifier<Address> getCfgId(final InstanceIdentifier<Address> id) {
142         return SubInterfaceCustomizer.getCfgId(RWUtils.cutId(id, SubInterface.class))
143                 .child(Ipv4.class)
144                 .child(Address.class, new AddressKey(id.firstKeyOf(Address.class)));
145     }
146 }