VPP-378: update jvpp package names
[honeycomb.git] / nsh / impl / src / main / java / io / fd / honeycomb / vppnsh / impl / oper / NshEntryReaderCustomizer.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
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 io.fd.vpp.jvpp.VppBaseCallException;
47 import io.fd.vpp.jvpp.nsh.dto.NshEntryDetails;
48 import io.fd.vpp.jvpp.nsh.dto.NshEntryDetailsReplyDump;
49 import io.fd.vpp.jvpp.nsh.dto.NshEntryDump;
50 import io.fd.vpp.jvpp.nsh.future.FutureJVppNsh;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * Reader customizer responsible for nsh entry read.<br> to VPP.
56  */
57 public class NshEntryReaderCustomizer extends FutureJVppNshCustomizer
58         implements ListReaderCustomizer<NshEntry, NshEntryKey, NshEntryBuilder>, JvppReplyConsumer {
59
60     private static final Logger LOG = LoggerFactory.getLogger(NshEntryReaderCustomizer.class);
61     private final NamingContext nshEntryContext;
62
63     public NshEntryReaderCustomizer(@Nonnull final FutureJVppNsh futureJVppNsh,
64                                     @Nonnull final NamingContext nshEntryContext) {
65         super(futureJVppNsh);
66         this.nshEntryContext = checkNotNull(nshEntryContext, "nshEntryContext should not be null");
67     }
68
69
70     @Override
71     public void merge(@Nonnull final Builder<? extends DataObject> builder,
72                       @Nonnull final List<NshEntry> readData) {
73         ((NshEntriesBuilder) builder).setNshEntry(readData);
74     }
75
76     @Nonnull
77     @Override
78     public NshEntryBuilder getBuilder(@Nonnull final InstanceIdentifier<NshEntry> id) {
79         return new NshEntryBuilder();
80     }
81
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);
89
90         builder.addAugmentation(NshMdType1StateAugment.class, augmentBuilder.build());
91     }
92
93     @Override
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);
98         try {
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();
102
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);
106                 return;
107             }
108             request.entryIndex = nshEntryContext.getIndex(entryName, ctx.getMappingContext());
109
110             final CompletionStage<NshEntryDetailsReplyDump> nshEntryDetailsReplyDumpCompletionStage =
111                     getFutureJVppNsh().nshEntryDump(request);
112             final NshEntryDetailsReplyDump reply =
113                     getReplyForRead(nshEntryDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
114
115             if (reply == null || reply.nshEntryDetails == null || reply.nshEntryDetails.isEmpty()) {
116                 LOG.debug("Has no Nsh Entry {} in VPP. ", key.getName());
117                 return;
118             }
119
120             LOG.trace("Nsh Entry : {} attributes returned from VPP: {}", key.getName(), reply);
121
122             final NshEntryDetails nshEntryDetails = reply.nshEntryDetails.get(0);
123             builder.setName(entryName);
124             builder.setKey(key);
125             builder.setVersion((short) nshEntryDetails.verOC);
126             builder.setLength((short) nshEntryDetails.length);
127
128             switch (nshEntryDetails.nextProtocol) {
129                 case 1:
130                     builder.setNextProtocol(Ipv4.class);
131                     break;
132                 case 2:
133                     builder.setNextProtocol(Ipv6.class);
134                     break;
135                 case 3:
136                     builder.setNextProtocol(Ethernet.class);
137                     break;
138                 default:
139                     LOG.trace("Unsupported next protocol for nsh entry: {}", nshEntryDetails.nextProtocol);
140                     return;
141             }
142
143             switch (nshEntryDetails.mdType) {
144                 case 1: {
145                     builder.setMdType(MdType1.class);
146                     setNshEntryMdType1Augment(builder, nshEntryDetails);
147                     break;
148                 }
149                 case 2: {
150                     builder.setMdType(MdType1.class);
151                     break;
152                 }
153                 default:
154                     LOG.trace("Unsupported Mdtype for nsh entry: {}", nshEntryDetails.mdType);
155                     return;
156             }
157
158             builder.setNsp((long) ((nshEntryDetails.nspNsi >> 8) & 0xFFFFFF));
159             builder.setNsi((short) (nshEntryDetails.nspNsi & 0xFF));
160
161             if (LOG.isTraceEnabled()) {
162                 LOG.trace("Attributes for nsh entry {} successfully read: {}", id, builder.build());
163             }
164         } catch (VppBaseCallException e) {
165             LOG.warn("Failed to readCurrentAttributes for: {}", id);
166             throw new ReadFailedException(id, e);
167         }
168     }
169
170     @Nonnull
171     @Override
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);
175
176         final NshEntryDump request = new NshEntryDump();
177         request.entryIndex = -1; // dump call
178
179         NshEntryDetailsReplyDump reply;
180         try {
181             reply = getFutureJVppNsh().nshEntryDump(request).toCompletableFuture().get();
182         } catch (Exception e) {
183             throw new IllegalStateException("Nsh Entry dump failed", e);
184         }
185
186         if (reply == null || reply.nshEntryDetails == null) {
187             return Collections.emptyList();
188         }
189
190         final int nIdsLength = reply.nshEntryDetails.size();
191         LOG.debug("vppstate.NshEntryCustomizer.getAllIds: nIds.length={}", nIdsLength);
192         if (nIdsLength == 0) {
193             return Collections.emptyList();
194         }
195
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));
201         }
202
203         return allIds;
204     }
205 }