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;
20 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.byteToBoolean;
22 import com.google.common.base.Preconditions;
23 import io.fd.honeycomb.translate.read.ReadContext;
24 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
25 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.translate.v3po.util.SubInterfaceUtils;
29 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.stream.Collectors;
35 import javax.annotation.Nonnull;
36 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.CVlan;
38 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qTagVlanType;
39 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.Dot1qVlanId;
40 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.SVlan;
41 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTag;
42 import org.opendaylight.yang.gen.v1.urn.ieee.params.xml.ns.yang.dot1q.types.rev150626.dot1q.tag.or.any.Dot1qTagBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.SubInterfaceStatus;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.SubInterfacesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.DefaultBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.UntaggedBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.match.attributes.match.type.vlan.tagged.VlanTaggedBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Match;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.MatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Tags;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.TagsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.Tag;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.tags.TagKey;
61 import org.opendaylight.yangtools.concepts.Builder;
62 import org.opendaylight.yangtools.yang.binding.DataObject;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.openvpp.jvpp.VppBaseCallException;
65 import org.openvpp.jvpp.dto.SwInterfaceDetails;
66 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
67 import org.openvpp.jvpp.dto.SwInterfaceDump;
68 import org.openvpp.jvpp.future.FutureJVpp;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
73 * Customizer for reading sub interfaces form the VPP.
75 public class SubInterfaceCustomizer extends FutureJVppCustomizer
76 implements ListReaderCustomizer<SubInterface, SubInterfaceKey, SubInterfaceBuilder> {
78 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceCustomizer.class);
79 private NamingContext interfaceContext;
80 private static final Dot1qTag.VlanId ANY_VLAN_ID = new Dot1qTag.VlanId(Dot1qTag.VlanId.Enumeration.Any);
82 public SubInterfaceCustomizer(@Nonnull final FutureJVpp jvpp,
83 @Nonnull final NamingContext interfaceContext) {
85 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
90 public List<SubInterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<SubInterface> id,
91 @Nonnull final ReadContext context) throws ReadFailedException {
93 // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
94 // to fill in the context with initial ifc mapping
95 final InterfaceKey key = id.firstKeyOf(Interface.class);
96 final String ifaceName = key.getName();
97 final int ifaceId = interfaceContext.getIndex(ifaceName, context.getMappingContext());
99 // TODO if we know that full dump was already performed we could use cache
100 // (checking if getCachedInterfaceDump() returns non empty map is not enough, because
101 // we could be part of particular iface state read
102 final SwInterfaceDump request = new SwInterfaceDump();
103 request.nameFilter = "".getBytes();
104 request.nameFilterValid = 0;
106 final CompletableFuture<SwInterfaceDetailsReplyDump> swInterfaceDetailsReplyDumpCompletableFuture =
107 getFutureJVpp().swInterfaceDump(request).toCompletableFuture();
108 final SwInterfaceDetailsReplyDump ifaces =
109 TranslateUtils.getReplyForRead(swInterfaceDetailsReplyDumpCompletableFuture, id);
111 if (null == ifaces || null == ifaces.swInterfaceDetails) {
112 LOG.warn("Looking for sub-interfaces, but no interfaces found in VPP");
113 return Collections.emptyList();
116 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
117 context.getModificationCache().put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, ifaces.swInterfaceDetails.stream()
118 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails)));
120 final List<SubInterfaceKey> interfacesKeys = ifaces.swInterfaceDetails.stream()
121 .filter(elt -> elt != null)
122 // accept only sub-interfaces for current iface:
123 .filter(elt -> elt.subId != 0 && elt.supSwIfIndex == ifaceId)
124 .map(details -> new SubInterfaceKey(new Long(details.subId)))
125 .collect(Collectors.toList());
127 LOG.debug("Sub-interfaces of {} found in VPP: {}", ifaceName, interfacesKeys);
128 return interfacesKeys;
129 } catch (VppBaseCallException e) {
130 throw new ReadFailedException(id,e);
135 public void merge(@Nonnull final Builder<? extends DataObject> builder,
136 @Nonnull final List<SubInterface> readData) {
137 ((SubInterfacesBuilder) builder).setSubInterface(readData);
142 public SubInterfaceBuilder getBuilder(@Nonnull final InstanceIdentifier<SubInterface> id) {
143 return new SubInterfaceBuilder();
147 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<SubInterface> id,
148 @Nonnull final SubInterfaceBuilder builder, @Nonnull final ReadContext ctx)
149 throws ReadFailedException {
150 final String subInterfaceName = getSubInterfaceName(id);
151 LOG.debug("Reading attributes for sub interface: {}", subInterfaceName);
153 final SwInterfaceDetails iface = InterfaceUtils.getVppInterfaceDetails(getFutureJVpp(), id, subInterfaceName,
154 interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()), ctx.getModificationCache());
155 LOG.debug("VPP sub-interface details: {}", ReflectionToStringBuilder.toString(iface));
157 checkState(iface.subId != 0, "Interface returned by the VPP is not a sub-interface");
159 builder.setIdentifier(Long.valueOf(iface.subId));
160 builder.setKey(new SubInterfaceKey(builder.getIdentifier()));
162 // sub-interface-base-attributes:
163 builder.setTags(readTags(iface));
164 builder.setMatch(readMatch(iface));
166 // sub-interface-operational-attributes:
167 builder.setAdminStatus(1 == iface.adminUpDown
168 ? SubInterfaceStatus.Up
169 : SubInterfaceStatus.Down);
170 builder.setOperStatus(1 == iface.linkUpDown
171 ? SubInterfaceStatus.Up
172 : SubInterfaceStatus.Down);
173 builder.setIfIndex(InterfaceUtils.vppIfIndexToYang(iface.swIfIndex));
174 if (iface.l2AddressLength == 6) {
175 builder.setPhysAddress(new PhysAddress(InterfaceUtils.vppPhysAddrToYang(iface.l2Address)));
177 if (0 != iface.linkSpeed) {
178 builder.setSpeed(InterfaceUtils.vppInterfaceSpeedToYang(iface.linkSpeed));
182 private static String getSubInterfaceName(final InstanceIdentifier<SubInterface> id) {
183 return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
184 Math.toIntExact(id.firstKeyOf(id.getTargetType()).getIdentifier()));
187 private Tags readTags(final SwInterfaceDetails iface) {
188 final TagsBuilder tags = new TagsBuilder();
189 final List<Tag> list = new ArrayList<>();
190 if (iface.subNumberOfTags > 0) {
191 if (iface.subOuterVlanIdAny == 1) {
192 list.add(buildTag((short) 0, SVlan.class, ANY_VLAN_ID));
194 list.add(buildTag((short) 0, SVlan.class, buildVlanId(iface.subOuterVlanId)));
196 // inner tag (customer tag):
197 if (iface.subNumberOfTags == 2) {
198 if (iface.subInnerVlanIdAny == 1) {
199 list.add(buildTag((short) 1, CVlan.class, ANY_VLAN_ID));
201 list.add(buildTag((short) 1, CVlan.class, buildVlanId(iface.subInnerVlanId)));
209 private static Tag buildTag(final short index, final Class<? extends Dot1qTagVlanType> tagType,
210 final Dot1qTag.VlanId vlanId) {
211 TagBuilder tag = new TagBuilder();
213 tag.setKey(new TagKey(index));
214 final Dot1qTagBuilder dtag = new Dot1qTagBuilder();
215 dtag.setTagType(tagType);
216 dtag.setVlanId(vlanId);
217 tag.setDot1qTag(dtag.build());
221 private static Dot1qTag.VlanId buildVlanId(final short vlanId) {
222 // treat vlanId as unsigned value:
223 return new Dot1qTag.VlanId(new Dot1qVlanId(0xffff & vlanId));
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();