4b9c7c8bc7102e74fe5fc89599de6b0816a9feab
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / v3po / translate / v3po / interfacesstate / VhostUserCustomizer.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.v3po.translate.v3po.interfacesstate;
18
19 import io.fd.honeycomb.v3po.translate.Context;
20 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
21 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
22 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
24 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
25 import java.math.BigInteger;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.CompletionStage;
30 import java.util.stream.Collectors;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VhostUser;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VhostUserBuilder;
38 import org.opendaylight.yangtools.concepts.Builder;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDetails;
42 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDetailsReplyDump;
43 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDump;
44 import org.openvpp.jvpp.future.FutureJVpp;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49 public class VhostUserCustomizer extends FutureJVppCustomizer
50         implements ChildReaderCustomizer<VhostUser, VhostUserBuilder> {
51
52     private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
53     public static final String DUMPED_VHOST_USERS_CONTEXT_KEY = VhostUserCustomizer.class.getName() + "dumpedVhostUsersDuringGetAllIds";
54     private NamingContext interfaceContext;
55
56     public VhostUserCustomizer(@Nonnull final FutureJVpp jvpp,
57                                final NamingContext interfaceContext) {
58         super(jvpp);
59         this.interfaceContext = interfaceContext;
60     }
61
62     @Override
63     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder,
64                       @Nonnull VhostUser readValue) {
65         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVhostUser(readValue);
66     }
67
68     @Nonnull
69     @Override
70     public VhostUserBuilder getBuilder(
71             @Nonnull InstanceIdentifier<VhostUser> id) {
72         return new VhostUserBuilder();
73     }
74
75     @Override
76     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
77                                       @Nonnull final VhostUserBuilder builder,
78                                       @Nonnull final Context ctx) throws ReadFailedException {
79         final InterfaceKey key = id.firstKeyOf(Interface.class);
80
81         @SuppressWarnings("unchecked")
82         Map<Integer, SwInterfaceVhostUserDetails> mappedVhostUsers =
83             (Map<Integer, SwInterfaceVhostUserDetails>) ctx.get(DUMPED_VHOST_USERS_CONTEXT_KEY);
84
85         if(mappedVhostUsers == null) {
86             // Full VhostUser dump has to be performed here, no filter or anything is here to help so at least we cache it
87             final SwInterfaceVhostUserDump request = new SwInterfaceVhostUserDump();
88             final CompletionStage<SwInterfaceVhostUserDetailsReplyDump> swInterfaceVhostUserDetailsReplyDumpCompletionStage =
89                 getFutureJVpp().swInterfaceVhostUserDump(request);
90             final SwInterfaceVhostUserDetailsReplyDump reply =
91                 V3poUtils.getReply(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture());
92
93             if(null == reply || null == reply.swInterfaceVhostUserDetails) {
94                 mappedVhostUsers = Collections.emptyMap();
95             } else {
96                 final List<SwInterfaceVhostUserDetails> swInterfaceVhostUserDetails = reply.swInterfaceVhostUserDetails;
97                 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
98                 mappedVhostUsers = swInterfaceVhostUserDetails.stream()
99                     .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
100             }
101
102             ctx.put(DUMPED_VHOST_USERS_CONTEXT_KEY, mappedVhostUsers);
103         }
104
105         // Relying here that parent InterfaceCustomizer was invoked first to fill in the context with initial ifc mapping
106         final int index = interfaceContext.getIndex(key.getName());
107         final SwInterfaceVhostUserDetails swInterfaceVhostUserDetails = mappedVhostUsers.get(index);
108         if(swInterfaceVhostUserDetails == null) {
109             // Not a VhostUser interface type
110             return;
111         }
112
113         builder.setRole(swInterfaceVhostUserDetails.isServer == 1 ? VhostUserRole.Server : VhostUserRole.Client);
114         builder.setFeatures(BigInteger.valueOf(swInterfaceVhostUserDetails.features));
115         builder.setNumMemoryRegions((long) swInterfaceVhostUserDetails.numRegions);
116         builder.setSocket(V3poUtils.toString(swInterfaceVhostUserDetails.sockFilename));
117         builder.setVirtioNetHdrSize((long) swInterfaceVhostUserDetails.virtioNetHdrSz);
118         builder.setConnectError(Integer.toString(swInterfaceVhostUserDetails.sockErrno));
119         // TODO add logging
120     }
121 }