dfa0f9632448a6abca0bda65315f4a4482975082
[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 com.google.common.primitives.UnsignedInts;
20 import io.fd.honeycomb.translate.read.ReadContext;
21 import io.fd.honeycomb.translate.read.ReadFailedException;
22 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
24 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
26 import io.fd.vpp.jvpp.core.dto.ShowVersion;
27 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import io.fd.vpp.jvpp.dto.ControlPing;
30 import io.fd.vpp.jvpp.dto.ControlPingReply;
31 import io.fd.vpp.jvpp.dto.JVppReply;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppStateBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.state.Version;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.state.VersionBuilder;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 public final class VersionCustomizer
42         extends FutureJVppCustomizer
43         implements ReaderCustomizer<Version, VersionBuilder>, ByteDataTranslator, JvppReplyConsumer {
44
45     public VersionCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
46         super(futureJVppCore);
47     }
48
49     @Override
50     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Version readValue) {
51         ((VppStateBuilder) parentBuilder).setVersion(readValue);
52     }
53
54     @Nonnull
55     @Override
56     public VersionBuilder getBuilder(@Nonnull InstanceIdentifier<Version> id) {
57         return new VersionBuilder();
58     }
59
60     @Override
61     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Version> id, @Nonnull final VersionBuilder builder,
62                                       @Nonnull final ReadContext context) throws ReadFailedException {
63
64         // Execute with timeout
65         final CompletionStage<ShowVersionReply> showVersionFuture = getFutureJVpp().showVersion(new ShowVersion());
66         final ShowVersionReply reply = getReplyForRead(showVersionFuture.toCompletableFuture(), id);
67
68         builder.setBranch(toString(reply.version));
69         builder.setName(toString(reply.program));
70         builder.setBuildDate(toString(reply.buildDate));
71         builder.setBuildDirectory(toString(reply.buildDirectory));
72         builder.setPid(getPid(id));
73     }
74
75     private Long getPid(@Nonnull final InstanceIdentifier<Version> id) throws ReadFailedException {
76         final CompletionStage<JVppReply<ControlPing>> request = getFutureJVpp().send(new ControlPing());
77         final ControlPingReply reply = (ControlPingReply)getReplyForRead(request.toCompletableFuture(), id);
78         return UnsignedInts.toLong(reply.vpePid);
79     }
80
81
82 }