VPP-378: update jvpp package names
[honeycomb.git] / nsh / impl / src / main / java / io / fd / honeycomb / vppnsh / impl / oper / NshMapReaderCustomizer.java
1 /*
2  * Copyright (c) 2016 Intel 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.vppnsh.impl.oper;
18
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;
22
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.vpp.util.JvppReplyConsumer;
27 import io.fd.honeycomb.translate.vpp.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 io.fd.vpp.jvpp.VppBaseCallException;
43 import io.fd.vpp.jvpp.nsh.dto.NshMapDetails;
44 import io.fd.vpp.jvpp.nsh.dto.NshMapDetailsReplyDump;
45 import io.fd.vpp.jvpp.nsh.dto.NshMapDump;
46 import io.fd.vpp.jvpp.nsh.future.FutureJVppNsh;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Reader customizer responsible for nsh map read.<br> to VPP.
52  */
53 public class NshMapReaderCustomizer extends FutureJVppNshCustomizer
54         implements ListReaderCustomizer<NshMap, NshMapKey, NshMapBuilder>, JvppReplyConsumer {
55
56     private static final Logger LOG = LoggerFactory.getLogger(NshMapReaderCustomizer.class);
57     private final NamingContext nshMapContext;
58     private final NamingContext interfaceContext;
59
60     public NshMapReaderCustomizer(@Nonnull final FutureJVppNsh futureJVppNsh,
61                                   @Nonnull final NamingContext nshMapContext,
62                                   @Nonnull final NamingContext interfaceContext) {
63         super(futureJVppNsh);
64         this.nshMapContext = checkNotNull(nshMapContext, "nshMapContext should not be null");
65         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
66     }
67
68
69     @Override
70     public void merge(@Nonnull final Builder<? extends DataObject> builder,
71                       @Nonnull final List<NshMap> readData) {
72         ((NshMapsBuilder) builder).setNshMap(readData);
73     }
74
75     @Nonnull
76     @Override
77     public NshMapBuilder getBuilder(@Nonnull final InstanceIdentifier<NshMap> id) {
78         return new NshMapBuilder();
79     }
80
81     @Override
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);
86         try {
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();
90
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);
94                 return;
95             }
96             request.mapIndex = nshMapContext.getIndex(mapName, ctx.getMappingContext());
97
98             final CompletionStage<NshMapDetailsReplyDump> nshMapDetailsReplyDumpCompletionStage =
99                     getFutureJVppNsh().nshMapDump(request);
100             final NshMapDetailsReplyDump reply =
101                     getReplyForRead(nshMapDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
102
103             if (reply == null || reply.nshMapDetails == null || reply.nshMapDetails.isEmpty()) {
104                 LOG.debug("Has no Nsh Map {} in VPP. ", key.getName());
105                 return;
106             }
107
108             LOG.trace("Nsh Map : {} attributes returned from VPP: {}", key.getName(), reply);
109
110             final NshMapDetails nshMapDetails = reply.nshMapDetails.get(0);
111             builder.setName(mapName);
112             builder.setKey(key);
113
114             builder.setNsp((long) ((nshMapDetails.nspNsi >> 8) & 0xFFFFFF));
115             builder.setNsi((short) (nshMapDetails.nspNsi & 0xFF));
116
117             builder.setMappedNsp((long) ((nshMapDetails.mappedNspNsi >> 8) & 0xFFFFFF));
118             builder.setMappedNsi((short) (nshMapDetails.mappedNspNsi & 0xFF));
119
120             switch (nshMapDetails.nextNode) {
121                 case 2:
122                     builder.setEncapType(VxlanGpe.class);
123                     break;
124                 default:
125                     LOG.trace("Unsupported encap type for nsh map: {}", nshMapDetails.nextNode);
126                     return;
127             }
128
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);
133
134             if (LOG.isTraceEnabled()) {
135                 LOG.trace("Attributes for nsh map {} successfully read: {}", id, builder.build());
136             }
137         } catch (VppBaseCallException e) {
138             LOG.warn("Failed to readCurrentAttributes for: {}", id);
139             throw new ReadFailedException(id, e);
140         }
141     }
142
143     @Nonnull
144     @Override
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);
148
149         final NshMapDump request = new NshMapDump();
150         request.mapIndex = -1; // dump call
151
152         NshMapDetailsReplyDump reply;
153         try {
154             reply = getFutureJVppNsh().nshMapDump(request).toCompletableFuture().get();
155         } catch (Exception e) {
156             throw new IllegalStateException("Nsh Map dump failed", e);
157         }
158
159         if (reply == null || reply.nshMapDetails == null) {
160             return Collections.emptyList();
161         }
162
163         final int nIdsLength = reply.nshMapDetails.size();
164         LOG.debug("vppstate.NshMapCustomizer.getAllIds: nIds.length={}", nIdsLength);
165         if (nIdsLength == 0) {
166             return Collections.emptyList();
167         }
168
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));
174         }
175
176         return allIds;
177     }
178 }