HONEYCOMB-145 - Utility Class Refactoring
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / 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.translate.v3po.interfacesstate;
18
19 import io.fd.honeycomb.translate.read.ReadContext;
20 import io.fd.honeycomb.translate.read.ReadFailedException;
21 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
22 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.v3po.util.NamingContext;
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.VppBaseCallException;
42 import org.openvpp.jvpp.core.dto.SwInterfaceVhostUserDetails;
43 import org.openvpp.jvpp.core.dto.SwInterfaceVhostUserDetailsReplyDump;
44 import org.openvpp.jvpp.core.dto.SwInterfaceVhostUserDump;
45 import org.openvpp.jvpp.core.future.FutureJVppCore;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50 public class VhostUserCustomizer extends FutureJVppCustomizer
51         implements ReaderCustomizer<VhostUser, VhostUserBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
52
53     public static final String DUMPED_VHOST_USERS_CONTEXT_KEY =
54             VhostUserCustomizer.class.getName() + "dumpedVhostUsersDuringGetAllIds";
55     private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
56     private NamingContext interfaceContext;
57
58     public VhostUserCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
59         super(jvpp);
60         this.interfaceContext = interfaceContext;
61     }
62
63     @Override
64     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull VhostUser readValue) {
65         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVhostUser(readValue);
66     }
67
68     @Nonnull
69     @Override
70     public VhostUserBuilder getBuilder(@Nonnull InstanceIdentifier<VhostUser> id) {
71         return new VhostUserBuilder();
72     }
73
74     @Override
75     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
76                                       @Nonnull final VhostUserBuilder builder,
77                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
78         try {
79             final InterfaceKey key = id.firstKeyOf(Interface.class);
80             final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
81             if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index,
82                     org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class,
83                     LOG)) {
84                 return;
85             }
86
87             LOG.debug("Reading attributes for vhpost user interface: {}", key.getName());
88
89             @SuppressWarnings("unchecked")
90             Map<Integer, SwInterfaceVhostUserDetails> mappedVhostUsers =
91                     (Map<Integer, SwInterfaceVhostUserDetails>) ctx.getModificationCache()
92                             .get(DUMPED_VHOST_USERS_CONTEXT_KEY);
93
94             if (mappedVhostUsers == null) {
95                 // Full VhostUser dump has to be performed here, no filter or anything is here to help so at least we cache it
96                 final SwInterfaceVhostUserDump request = new SwInterfaceVhostUserDump();
97                 final CompletionStage<SwInterfaceVhostUserDetailsReplyDump>
98                         swInterfaceVhostUserDetailsReplyDumpCompletionStage =
99                         getFutureJVpp().swInterfaceVhostUserDump(request);
100                 final SwInterfaceVhostUserDetailsReplyDump reply =
101                         getReplyForRead(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
102
103                 if (null == reply || null == reply.swInterfaceVhostUserDetails) {
104                     mappedVhostUsers = Collections.emptyMap();
105                 } else {
106                     final List<SwInterfaceVhostUserDetails> swInterfaceVhostUserDetails =
107                             reply.swInterfaceVhostUserDetails;
108                     // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
109                     mappedVhostUsers = swInterfaceVhostUserDetails.stream()
110                             .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
111                 }
112
113                 ctx.getModificationCache().put(DUMPED_VHOST_USERS_CONTEXT_KEY, mappedVhostUsers);
114             }
115
116             // Relying here that parent InterfaceCustomizer was invoked first to fill in the context with initial ifc mapping
117             final SwInterfaceVhostUserDetails swInterfaceVhostUserDetails = mappedVhostUsers.get(index);
118             LOG.trace("Vhost user interface: {} attributes returned from VPP: {}", key.getName(),
119                     swInterfaceVhostUserDetails);
120
121             builder.setRole(swInterfaceVhostUserDetails.isServer == 1
122                     ? VhostUserRole.Server
123                     : VhostUserRole.Client);
124             builder.setFeatures(BigInteger.valueOf(swInterfaceVhostUserDetails.features));
125             builder.setNumMemoryRegions((long) swInterfaceVhostUserDetails.numRegions);
126             builder.setSocket(toString(swInterfaceVhostUserDetails.sockFilename));
127             builder.setVirtioNetHdrSize((long) swInterfaceVhostUserDetails.virtioNetHdrSz);
128             builder.setConnectError(Integer.toString(swInterfaceVhostUserDetails.sockErrno));
129
130             LOG.debug("Vhost user interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);
131         } catch (VppBaseCallException e) {
132             LOG.warn("Failed to readCurrentAttributes for: {}", id, e);
133             throw new ReadFailedException(id, e);
134         }
135     }
136 }