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