2 * Copyright (c) 2016 Intel 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.vppnsh.impl.oper;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
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.JvppReplyConsumer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.vppnsh.impl.util.FutureJVppNshCustomizer;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.concurrent.CompletionStage;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Ethernet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Ipv4;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Ipv6;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.MdType1;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.NshMdType1StateAugment;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.NshMdType1StateAugmentBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.NshEntriesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.entries.NshEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.entries.NshEntryBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.entries.NshEntryKey;
43 import org.opendaylight.yangtools.concepts.Builder;
44 import org.opendaylight.yangtools.yang.binding.DataObject;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.openvpp.jvpp.VppBaseCallException;
47 import org.openvpp.jvpp.nsh.dto.NshEntryDetails;
48 import org.openvpp.jvpp.nsh.dto.NshEntryDetailsReplyDump;
49 import org.openvpp.jvpp.nsh.dto.NshEntryDump;
50 import org.openvpp.jvpp.nsh.future.FutureJVppNsh;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Reader customizer responsible for nsh entry read.<br> to VPP.
57 public class NshEntryReaderCustomizer extends FutureJVppNshCustomizer
58 implements ListReaderCustomizer<NshEntry, NshEntryKey, NshEntryBuilder>, JvppReplyConsumer {
60 private static final Logger LOG = LoggerFactory.getLogger(NshEntryReaderCustomizer.class);
61 private final NamingContext nshEntryContext;
63 public NshEntryReaderCustomizer(@Nonnull final FutureJVppNsh futureJVppNsh,
64 @Nonnull final NamingContext nshEntryContext) {
66 this.nshEntryContext = checkNotNull(nshEntryContext, "nshEntryContext should not be null");
71 public void merge(@Nonnull final Builder<? extends DataObject> builder,
72 @Nonnull final List<NshEntry> readData) {
73 ((NshEntriesBuilder) builder).setNshEntry(readData);
78 public NshEntryBuilder getBuilder(@Nonnull final InstanceIdentifier<NshEntry> id) {
79 return new NshEntryBuilder();
82 private void setNshEntryMdType1Augment(@Nonnull final NshEntryBuilder builder,
83 @Nonnull NshEntryDetails nshEntryDetails) {
84 final NshMdType1StateAugmentBuilder augmentBuilder = new NshMdType1StateAugmentBuilder();
85 augmentBuilder.setC1((long) nshEntryDetails.c1);
86 augmentBuilder.setC2((long) nshEntryDetails.c2);
87 augmentBuilder.setC3((long) nshEntryDetails.c3);
88 augmentBuilder.setC4((long) nshEntryDetails.c4);
90 builder.addAugmentation(NshMdType1StateAugment.class, augmentBuilder.build());
94 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<NshEntry> id,
95 @Nonnull final NshEntryBuilder builder, @Nonnull final ReadContext ctx)
96 throws ReadFailedException {
97 LOG.debug("Reading attributes for nsh entry: {}", id);
99 final NshEntryKey key = id.firstKeyOf(NshEntry.class);
100 checkArgument(key != null, "could not find NshEntry key in {}", id);
101 final NshEntryDump request = new NshEntryDump();
103 final String entryName = key.getName();
104 if (!nshEntryContext.containsIndex(entryName, ctx.getMappingContext())) {
105 LOG.debug("Could not find nsh entry {} in the naming context", entryName);
108 request.entryIndex = nshEntryContext.getIndex(entryName, ctx.getMappingContext());
110 final CompletionStage<NshEntryDetailsReplyDump> nshEntryDetailsReplyDumpCompletionStage =
111 getFutureJVppNsh().nshEntryDump(request);
112 final NshEntryDetailsReplyDump reply =
113 getReplyForRead(nshEntryDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
115 if (reply == null || reply.nshEntryDetails == null || reply.nshEntryDetails.isEmpty()) {
116 LOG.debug("Has no Nsh Entry {} in VPP. ", key.getName());
120 LOG.trace("Nsh Entry : {} attributes returned from VPP: {}", key.getName(), reply);
122 final NshEntryDetails nshEntryDetails = reply.nshEntryDetails.get(0);
123 builder.setName(entryName);
125 builder.setVersion((short) nshEntryDetails.verOC);
126 builder.setLength((short) nshEntryDetails.length);
128 switch (nshEntryDetails.nextProtocol) {
130 builder.setNextProtocol(Ipv4.class);
133 builder.setNextProtocol(Ipv6.class);
136 builder.setNextProtocol(Ethernet.class);
139 LOG.trace("Unsupported next protocol for nsh entry: {}", nshEntryDetails.nextProtocol);
143 switch (nshEntryDetails.mdType) {
145 builder.setMdType(MdType1.class);
146 setNshEntryMdType1Augment(builder, nshEntryDetails);
150 builder.setMdType(MdType1.class);
154 LOG.trace("Unsupported Mdtype for nsh entry: {}", nshEntryDetails.mdType);
158 builder.setNsp((long) ((nshEntryDetails.nspNsi >> 8) & 0xFFFFFF));
159 builder.setNsi((short) (nshEntryDetails.nspNsi & 0xFF));
161 if (LOG.isTraceEnabled()) {
162 LOG.trace("Attributes for nsh entry {} successfully read: {}", id, builder.build());
164 } catch (VppBaseCallException e) {
165 LOG.warn("Failed to readCurrentAttributes for: {}", id);
166 throw new ReadFailedException(id, e);
172 public List<NshEntryKey> getAllIds(@Nonnull final InstanceIdentifier<NshEntry> id,
173 @Nonnull final ReadContext context) throws ReadFailedException {
174 LOG.debug("Reading list of keys for nsh entry: {}", id);
176 final NshEntryDump request = new NshEntryDump();
177 request.entryIndex = -1; // dump call
179 NshEntryDetailsReplyDump reply;
181 reply = getFutureJVppNsh().nshEntryDump(request).toCompletableFuture().get();
182 } catch (Exception e) {
183 throw new IllegalStateException("Nsh Entry dump failed", e);
186 if (reply == null || reply.nshEntryDetails == null) {
187 return Collections.emptyList();
190 final int nIdsLength = reply.nshEntryDetails.size();
191 LOG.debug("vppstate.NshEntryCustomizer.getAllIds: nIds.length={}", nIdsLength);
192 if (nIdsLength == 0) {
193 return Collections.emptyList();
196 final List<NshEntryKey> allIds = new ArrayList<>(nIdsLength);
197 for (NshEntryDetails detail : reply.nshEntryDetails) {
198 final String nshName = nshEntryContext.getName(detail.entryIndex, context.getMappingContext());
199 LOG.debug("vppstate.NshEntryCustomizer.getAllIds: nName={}", nshName);
200 allIds.add(new NshEntryKey(nshName));