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