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;
21 import static com.google.common.base.Preconditions.checkState;
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.JvppReplyConsumer;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.vppnsh.impl.util.FutureJVppNshCustomizer;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.VxlanGpe;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.NshMapsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.maps.NshMap;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.maps.NshMapBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.maps.NshMapKey;
39 import org.opendaylight.yangtools.concepts.Builder;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.openvpp.jvpp.VppBaseCallException;
43 import org.openvpp.jvpp.nsh.dto.NshMapDetails;
44 import org.openvpp.jvpp.nsh.dto.NshMapDetailsReplyDump;
45 import org.openvpp.jvpp.nsh.dto.NshMapDump;
46 import org.openvpp.jvpp.nsh.future.FutureJVppNsh;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Reader customizer responsible for nsh map read.<br> to VPP.
53 public class NshMapReaderCustomizer extends FutureJVppNshCustomizer
54 implements ListReaderCustomizer<NshMap, NshMapKey, NshMapBuilder>, JvppReplyConsumer {
56 private static final Logger LOG = LoggerFactory.getLogger(NshMapReaderCustomizer.class);
57 private final NamingContext nshMapContext;
58 private final NamingContext interfaceContext;
60 public NshMapReaderCustomizer(@Nonnull final FutureJVppNsh futureJVppNsh,
61 @Nonnull final NamingContext nshMapContext,
62 @Nonnull final NamingContext interfaceContext) {
64 this.nshMapContext = checkNotNull(nshMapContext, "nshMapContext should not be null");
65 this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
70 public void merge(@Nonnull final Builder<? extends DataObject> builder,
71 @Nonnull final List<NshMap> readData) {
72 ((NshMapsBuilder) builder).setNshMap(readData);
77 public NshMapBuilder getBuilder(@Nonnull final InstanceIdentifier<NshMap> id) {
78 return new NshMapBuilder();
82 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
83 @Nonnull final NshMapBuilder builder, @Nonnull final ReadContext ctx)
84 throws ReadFailedException {
85 LOG.debug("Reading attributes for nsh map: {}", id);
87 final NshMapKey key = id.firstKeyOf(NshMap.class);
88 checkArgument(key != null, "could not find NshMap key in {}", id);
89 final NshMapDump request = new NshMapDump();
91 final String mapName = key.getName();
92 if (!nshMapContext.containsIndex(mapName, ctx.getMappingContext())) {
93 LOG.debug("Could not find nsh map {} in the naming context", mapName);
96 request.mapIndex = nshMapContext.getIndex(mapName, ctx.getMappingContext());
98 final CompletionStage<NshMapDetailsReplyDump> nshMapDetailsReplyDumpCompletionStage =
99 getFutureJVppNsh().nshMapDump(request);
100 final NshMapDetailsReplyDump reply =
101 getReplyForRead(nshMapDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
103 if (reply == null || reply.nshMapDetails == null || reply.nshMapDetails.isEmpty()) {
104 LOG.debug("Has no Nsh Map {} in VPP. ", key.getName());
108 LOG.trace("Nsh Map : {} attributes returned from VPP: {}", key.getName(), reply);
110 final NshMapDetails nshMapDetails = reply.nshMapDetails.get(0);
111 builder.setName(mapName);
114 builder.setNsp((long) ((nshMapDetails.nspNsi >> 8) & 0xFFFFFF));
115 builder.setNsi((short) (nshMapDetails.nspNsi & 0xFF));
117 builder.setMappedNsp((long) ((nshMapDetails.mappedNspNsi >> 8) & 0xFFFFFF));
118 builder.setMappedNsi((short) (nshMapDetails.mappedNspNsi & 0xFF));
120 switch (nshMapDetails.nextNode) {
122 builder.setEncapType(VxlanGpe.class);
125 LOG.trace("Unsupported encap type for nsh map: {}", nshMapDetails.nextNode);
129 checkState(interfaceContext.containsName(nshMapDetails.swIfIndex, ctx.getMappingContext()),
130 "Mapping does not contains mapping for provider interface Index ");
131 final String interfaceName = interfaceContext.getName(nshMapDetails.swIfIndex, ctx.getMappingContext());
132 builder.setEncapIfName(interfaceName);
134 if (LOG.isTraceEnabled()) {
135 LOG.trace("Attributes for nsh map {} successfully read: {}", id, builder.build());
137 } catch (VppBaseCallException e) {
138 LOG.warn("Failed to readCurrentAttributes for: {}", id);
139 throw new ReadFailedException(id, e);
145 public List<NshMapKey> getAllIds(@Nonnull final InstanceIdentifier<NshMap> id,
146 @Nonnull final ReadContext context) throws ReadFailedException {
147 LOG.debug("Reading list of keys for nsh map: {}", id);
149 final NshMapDump request = new NshMapDump();
150 request.mapIndex = -1; // dump call
152 NshMapDetailsReplyDump reply;
154 reply = getFutureJVppNsh().nshMapDump(request).toCompletableFuture().get();
155 } catch (Exception e) {
156 throw new IllegalStateException("Nsh Map dump failed", e);
159 if (reply == null || reply.nshMapDetails == null) {
160 return Collections.emptyList();
163 final int nIdsLength = reply.nshMapDetails.size();
164 LOG.debug("vppstate.NshMapCustomizer.getAllIds: nIds.length={}", nIdsLength);
165 if (nIdsLength == 0) {
166 return Collections.emptyList();
169 final List<NshMapKey> allIds = new ArrayList<>(nIdsLength);
170 for (NshMapDetails detail : reply.nshMapDetails) {
171 final String nshName = nshMapContext.getName(detail.mapIndex, context.getMappingContext());
172 LOG.debug("vppstate.NshMapCustomizer.getAllIds: nName={}", nshName);
173 allIds.add(new NshMapKey(nshName));