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