2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
19 import static com.google.common.base.Preconditions.checkState;
21 import com.google.common.base.Preconditions;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.Initialized;
25 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
26 import io.fd.honeycomb.translate.util.RWUtils;
27 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
28 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
31 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
32 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.stream.Collectors;
40 import javax.annotation.Nonnull;
41 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
42 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
43 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
44 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
45 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
46 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubInterfaceStatus;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceAugmentation;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.SubInterfaces;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfacesBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.DefaultBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.UntaggedBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.Match;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.MatchBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.Tags;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.TagsBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.Tag;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.TagBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.tags.TagKey;
67 import org.opendaylight.yangtools.concepts.Builder;
68 import org.opendaylight.yangtools.yang.binding.DataObject;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 * Customizer for reading sub interfaces form the VPP.
76 public class SubInterfaceCustomizer extends FutureJVppCustomizer
77 implements InitializingListReaderCustomizer<SubInterface, SubInterfaceKey, SubInterfaceBuilder>, ByteDataTranslator,
78 InterfaceDataTranslator {
80 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
81 private static final Dot1qTag.VlanId ANY_VLAN_ID = new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any);
82 private NamingContext interfaceContext;
84 public SubInterfaceCustomizer(@Nonnull final FutureJVppCore jvpp,
85 @Nonnull final NamingContext interfaceContext) {
87 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
90 private static String getSubInterfaceName(final InstanceIdentifier<SubInterface> id) {
91 return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
92 Math.toIntExact(id.firstKeyOf(id.getTargetType()).getIdentifier()));
95 private static Tag buildTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
96 final Dot1qTag.VlanId vlanId) {
97 TagBuilder tag = new TagBuilder();
99 tag.setKey(new TagKey(index));
100 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
101 dtag.setTagType(tagType);
102 dtag.setVlanId(vlanId);
103 tag.setDot1qTag(dtag.build());
107 private static Dot1qTag.VlanId buildVlanId(final short vlanId) {
108 // treat vlanId as unsigned value:
109 return new Dot1qTag.VlanId(new Dot1qVlanId(0xffff & vlanId));
114 public List<SubInterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<SubInterface> id,
115 @Nonnull final ReadContext context) throws ReadFailedException {
116 // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
117 // to fill in the context with initial ifc mapping
118 final InterfaceKey key = id.firstKeyOf(Interface.class);
119 final String ifaceName = key.getName();
120 final int ifaceId = interfaceContext.getIndex(ifaceName, context.getMappingContext());
122 // TODO HONEYCOMB-189 if we know that full dump was already performed we could use cache
123 // (checking if getCachedInterfaceDump() returns non empty map is not enough, because
124 // we could be part of particular iface state read
125 final SwInterfaceDump request = new SwInterfaceDump();
126 request.nameFilter = "".getBytes();
127 request.nameFilterValid = 0;
129 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
130 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
131 final SwInterfaceDetailsReplyDump ifaces =
132 getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
134 if (null == ifaces || null == ifaces.swInterfaceDetails) {
135 LOG.warn("Looking for sub-interfaces, but no interfaces found in VPP");
136 return Collections.emptyList();
139 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
140 context.getModificationCache()
141 .put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
142 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
144 final List<SubInterfaceKey> interfacesKeys = ifaces.swInterfaceDetails.stream()
145 .filter(elt -> elt != null)
146 // accept only sub-interfaces for current iface:
147 .filter(elt -> elt.subId != 0 && elt.supSwIfIndex == ifaceId)
148 .map(details -> new SubInterfaceKey(new Long(details.subId)))
149 .collect(Collectors.toList());
151 LOG.debug("Sub-interfaces of {} found in VPP: {}", ifaceName, interfacesKeys);
152 return interfacesKeys;
156 public void merge(@Nonnull final Builder<? extends DataObject> builder,
157 @Nonnull final List<SubInterface> readData) {
158 ((SubInterfacesBuilder) builder).setSubInterface(readData);
163 public SubInterfaceBuilder getBuilder(@Nonnull final InstanceIdentifier<SubInterface> id) {
164 return new SubInterfaceBuilder();
168 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
169 @Nonnull final SubInterfaceBuilder builder, @Nonnull final ReadContext ctx)
170 throws ReadFailedException {
171 final String subInterfaceName = getSubInterfaceName(id);
172 LOG.debug("Reading attributes for sub interface: {}", subInterfaceName);
174 final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, subInterfaceName,
175 interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()), ctx.getModificationCache(), LOG);
176 LOG.debug("VPP sub-interface details: {}", iface);
178 checkState(iface.subId != 0, "Interface returned by the VPP is not a sub-interface");
180 builder.setIdentifier(Long.valueOf(iface.subId));
181 builder.setKey(new SubInterfaceKey(builder.getIdentifier()));
183 // sub-interface-base-attributes:
184 builder.setTags(readTags(iface));
185 builder.setMatch(readMatch(iface));
187 // sub-interface-operational-attributes:
188 builder.setAdminStatus(1 == iface.adminUpDown
189 ? SubInterfaceStatus.Up
190 : SubInterfaceStatus.Down);
191 builder.setOperStatus(1 == iface.linkUpDown
192 ? SubInterfaceStatus.Up
193 : SubInterfaceStatus.Down);
194 builder.setIfIndex(vppIfIndexToYang(iface.swIfIndex));
195 if (iface.l2AddressLength == 6) {
196 builder.setPhysAddress(new PhysAddress(vppPhysAddrToYang(iface.l2Address)));
198 if (0 != iface.linkSpeed) {
199 builder.setSpeed(vppInterfaceSpeedToYang(iface.linkSpeed));
203 private Tags readTags(final SwInterfaceDetails iface) {
204 final TagsBuilder tags = new TagsBuilder();
205 final List<Tag> list = new ArrayList<>();
206 if (iface.subNumberOfTags > 0) {
207 if (iface.subOuterVlanIdAny == 1) {
208 list.add(buildTag((short) 0, SVlan.class, ANY_VLAN_ID));
210 list.add(buildTag((short) 0, SVlan.class, buildVlanId(iface.subOuterVlanId)));
212 // inner tag (customer tag):
213 if (iface.subNumberOfTags == 2) {
214 if (iface.subInnerVlanIdAny == 1) {
215 list.add(buildTag((short) 1, CVlan.class, ANY_VLAN_ID));
217 list.add(buildTag((short) 1, CVlan.class, buildVlanId(iface.subInnerVlanId)));
225 private Match readMatch(final SwInterfaceDetails iface) {
226 final MatchBuilder match = new MatchBuilder();
227 if (iface.subDefault == 1) {
228 match.setMatchType(new DefaultBuilder().build());
229 } else if (iface.subNumberOfTags == 0) {
230 match.setMatchType(new UntaggedBuilder().build());
232 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
233 tagged.setMatchExactTags(byteToBoolean(iface.subExactMatch));
235 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.match.attributes.match.type.VlanTaggedBuilder()
236 .setVlanTagged(tagged.build()).build());
238 return match.build();
242 public Initialized<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface> init(
243 @Nonnull final InstanceIdentifier<SubInterface> id, @Nonnull final SubInterface readValue,
244 @Nonnull final ReadContext ctx) {
245 return Initialized.create(getCfgId(id),
246 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceBuilder()
247 .setEnabled(SubInterfaceStatus.Up.equals(readValue.getAdminStatus()))
248 .setIdentifier(readValue.getIdentifier())
249 .setMatch(readValue.getMatch())
250 .setTags(readValue.getTags())
251 .setVlanType(readValue.getVlanType())
255 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface> getCfgId(
256 final InstanceIdentifier<SubInterface> id) {
257 return InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
258 .augmentation(SubinterfaceAugmentation.class)
259 .child(SubInterfaces.class)
260 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface.class,
261 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceKey(
262 id.firstKeyOf(SubInterface.class).getIdentifier()));