86ab994b306cba7602939b4aa059ef57a2f50a49
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vppstate / VersionCustomizer.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.vppstate;
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.vpp.util.ByteDataTranslator;
23 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import java.util.concurrent.CompletionStage;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppStateBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.Version;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.VersionBuilder;
30 import org.opendaylight.yangtools.concepts.Builder;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import io.fd.vpp.jvpp.VppBaseCallException;
34 import io.fd.vpp.jvpp.core.dto.ShowVersion;
35 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
36 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
37
38 public final class VersionCustomizer
39         extends FutureJVppCustomizer
40         implements ReaderCustomizer<Version, VersionBuilder>, ByteDataTranslator, JvppReplyConsumer {
41
42     public VersionCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
43         super(futureJVppCore);
44     }
45
46     @Override
47     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Version readValue) {
48         ((VppStateBuilder) parentBuilder).setVersion(readValue);
49     }
50
51     @Nonnull
52     @Override
53     public VersionBuilder getBuilder(@Nonnull InstanceIdentifier<Version> id) {
54         return new VersionBuilder();
55     }
56
57     @Override
58     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Version> id, @Nonnull final VersionBuilder builder,
59                                       @Nonnull final ReadContext context) throws ReadFailedException {
60         try {
61             // Execute with timeout
62             final CompletionStage<ShowVersionReply> showVersionFuture = getFutureJVpp().showVersion(new ShowVersion());
63             final ShowVersionReply reply = getReplyForRead(showVersionFuture.toCompletableFuture(), id);
64
65             builder.setBranch(toString(reply.version));
66             builder.setName(toString(reply.program));
67             builder.setBuildDate(toString(reply.buildDate));
68             builder.setBuildDirectory(toString(reply.buildDirectory));
69         } catch (VppBaseCallException e) {
70             throw new ReadFailedException(id, e);
71         }
72     }
73
74 }