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.ListReaderCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
26 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.stream.Collectors;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
36 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
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.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubInterfaceStatus;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.SubInterfacesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.DefaultBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.UntaggedBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Tags;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
59 import org.opendaylight.yangtools.concepts.Builder;
60 import org.opendaylight.yangtools.yang.binding.DataObject;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.openvpp.jvpp.VppBaseCallException;
63 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
64 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
65 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
66 import org.openvpp.jvpp.core.future.FutureJVppCore;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
71 * Customizer for reading sub interfaces form the VPP.
73 public class SubInterfaceCustomizer extends FutureJVppCustomizer
74 implements ListReaderCustomizer<SubInterface, SubInterfaceKey, SubInterfaceBuilder>, ByteDataTranslator,
75 InterfaceDataTranslator {
77 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
78 private static final Dot1qTag.VlanId ANY_VLAN_ID = new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any);
79 private NamingContext interfaceContext;
81 public SubInterfaceCustomizer(@Nonnull final FutureJVppCore jvpp,
82 @Nonnull final NamingContext interfaceContext) {
84 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
87 private static String getSubInterfaceName(final InstanceIdentifier<SubInterface> id) {
88 return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
89 Math.toIntExact(id.firstKeyOf(id.getTargetType()).getIdentifier()));
92 private static Tag buildTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
93 final Dot1qTag.VlanId vlanId) {
94 TagBuilder tag = new TagBuilder();
96 tag.setKey(new TagKey(index));
97 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
98 dtag.setTagType(tagType);
99 dtag.setVlanId(vlanId);
100 tag.setDot1qTag(dtag.build());
104 private static Dot1qTag.VlanId buildVlanId(final short vlanId) {
105 // treat vlanId as unsigned value:
106 return new Dot1qTag.VlanId(new Dot1qVlanId(0xffff & vlanId));
111 public List<SubInterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<SubInterface> id,
112 @Nonnull final ReadContext context) throws ReadFailedException {
114 // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
115 // to fill in the context with initial ifc mapping
116 final InterfaceKey key = id.firstKeyOf(Interface.class);
117 final String ifaceName = key.getName();
118 final int ifaceId = interfaceContext.getIndex(ifaceName, context.getMappingContext());
120 // TODO HONEYCOMB-189 if we know that full dump was already performed we could use cache
121 // (checking if getCachedInterfaceDump() returns non empty map is not enough, because
122 // we could be part of particular iface state read
123 final SwInterfaceDump request = new SwInterfaceDump();
124 request.nameFilter = "".getBytes();
125 request.nameFilterValid = 0;
127 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
128 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
129 final SwInterfaceDetailsReplyDump ifaces =
130 getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
132 if (null == ifaces || null == ifaces.swInterfaceDetails) {
133 LOG.warn("Looking for sub-interfaces, but no interfaces found in VPP");
134 return Collections.emptyList();
137 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
138 context.getModificationCache()
139 .put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
140 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
142 final List<SubInterfaceKey> interfacesKeys = ifaces.swInterfaceDetails.stream()
143 .filter(elt -> elt != null)
144 // accept only sub-interfaces for current iface:
145 .filter(elt -> elt.subId != 0 && elt.supSwIfIndex == ifaceId)
146 .map(details -> new SubInterfaceKey(new Long(details.subId)))
147 .collect(Collectors.toList());
149 LOG.debug("Sub-interfaces of {} found in VPP: {}", ifaceName, interfacesKeys);
150 return interfacesKeys;
151 } catch (VppBaseCallException e) {
152 throw new ReadFailedException(id, e);
157 public void merge(@Nonnull final Builder<? extends DataObject> builder,
158 @Nonnull final List<SubInterface> readData) {
159 ((SubInterfacesBuilder) builder).setSubInterface(readData);
164 public SubInterfaceBuilder getBuilder(@Nonnull final InstanceIdentifier<SubInterface> id) {
165 return new SubInterfaceBuilder();
169 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
170 @Nonnull final SubInterfaceBuilder builder, @Nonnull final ReadContext ctx)
171 throws ReadFailedException {
172 final String subInterfaceName = getSubInterfaceName(id);
173 LOG.debug("Reading attributes for sub interface: {}", subInterfaceName);
175 final SwInterfaceDetails iface = getVppInterfaceDetails(getFutureJVpp(), id, subInterfaceName,
176 interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()), ctx.getModificationCache(), LOG);
177 LOG.debug("VPP sub-interface details: {}", iface);
179 checkState(iface.subId != 0, "Interface returned by the VPP is not a sub-interface");
181 builder.setIdentifier(Long.valueOf(iface.subId));
182 builder.setKey(new SubInterfaceKey(builder.getIdentifier()));
184 // sub-interface-base-attributes:
185 builder.setTags(readTags(iface));
186 builder.setMatch(readMatch(iface));
188 // sub-interface-operational-attributes:
189 builder.setAdminStatus(1 == iface.adminUpDown
190 ? SubInterfaceStatus.Up
191 : SubInterfaceStatus.Down);
192 builder.setOperStatus(1 == iface.linkUpDown
193 ? SubInterfaceStatus.Up
194 : SubInterfaceStatus.Down);
195 builder.setIfIndex(vppIfIndexToYang(iface.swIfIndex));
196 if (iface.l2AddressLength == 6) {
197 builder.setPhysAddress(new PhysAddress(vppPhysAddrToYang(iface.l2Address)));
199 if (0 != iface.linkSpeed) {
200 builder.setSpeed(vppInterfaceSpeedToYang(iface.linkSpeed));
204 private Tags readTags(final SwInterfaceDetails iface) {
205 final TagsBuilder tags = new TagsBuilder();
206 final List<Tag> list = new ArrayList<>();
207 if (iface.subNumberOfTags > 0) {
208 if (iface.subOuterVlanIdAny == 1) {
209 list.add(buildTag((short) 0, SVlan.class, ANY_VLAN_ID));
211 list.add(buildTag((short) 0, SVlan.class, buildVlanId(iface.subOuterVlanId)));
213 // inner tag (customer tag):
214 if (iface.subNumberOfTags == 2) {
215 if (iface.subInnerVlanIdAny == 1) {
216 list.add(buildTag((short) 1, CVlan.class, ANY_VLAN_ID));
218 list.add(buildTag((short) 1, CVlan.class, buildVlanId(iface.subInnerVlanId)));
226 private Match readMatch(final SwInterfaceDetails iface) {
227 final MatchBuilder match = new MatchBuilder();
228 if (iface.subDefault == 1) {
229 match.setMatchType(new DefaultBuilder().build());
230 } else if (iface.subNumberOfTags == 0) {
231 match.setMatchType(new UntaggedBuilder().build());
233 final VlanTaggedBuilder tagged = new VlanTaggedBuilder();
234 tagged.setMatchExactTags(byteToBoolean(iface.subExactMatch));
236 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.VlanTaggedBuilder()
237 .setVlanTagged(tagged.build()).build());
239 return match.build();