HONEYCOMB-58 - Routing Api
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / LispStateCustomizer.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.lisp.translate.read;
18
19
20 import io.fd.honeycomb.translate.read.ReadContext;
21 import io.fd.honeycomb.translate.read.ReadFailedException;
22 import io.fd.honeycomb.translate.spi.read.Initialized;
23 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
25 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
27 import io.fd.vpp.jvpp.VppBaseCallException;
28 import io.fd.vpp.jvpp.core.dto.ShowLispStatus;
29 import io.fd.vpp.jvpp.core.dto.ShowLispStatusReply;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.util.concurrent.TimeoutException;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.Lisp;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.LispBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.LispState;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.LispStateBuilder;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 /**
45  * Customizer that handles reads of {@code LispState}
46  */
47 public class LispStateCustomizer extends FutureJVppCustomizer
48         implements InitializingReaderCustomizer<LispState, LispStateBuilder>, JvppReplyConsumer, ByteDataTranslator {
49
50     private static final Logger LOG = LoggerFactory.getLogger(LispStateCustomizer.class);
51
52     public LispStateCustomizer(FutureJVppCore futureJvpp) {
53         super(futureJvpp);
54     }
55
56     @Override
57     public LispStateBuilder getBuilder(InstanceIdentifier<LispState> id) {
58         return new LispStateBuilder();
59     }
60
61     @Override
62     public void readCurrentAttributes(InstanceIdentifier<LispState> id, LispStateBuilder builder, ReadContext ctx)
63             throws ReadFailedException {
64
65         ShowLispStatusReply reply;
66         try {
67             reply = getReply(getFutureJVpp().showLispStatus(new ShowLispStatus()).toCompletableFuture());
68         } catch (TimeoutException | VppBaseCallException e) {
69             throw new ReadFailedException(id, e);
70         }
71
72         builder.setEnable(byteToBoolean(reply.featureStatus));
73     }
74
75     @Override
76     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final LispState readValue) {
77         LOG.warn("Merge is unsupported for data roots");
78     }
79
80     @Override
81     public Initialized<Lisp> init(
82             @Nonnull final InstanceIdentifier<LispState> id, @Nonnull final LispState readValue,
83             @Nonnull final ReadContext ctx) {
84         return Initialized.create(InstanceIdentifier.create(Lisp.class),
85
86                 // set everything from LispState to LispBuilder
87                 // this is necessary in cases, when HC connects to a running VPP with some LISP configuration. HC needs to
88                 // reconstruct configuration based on what's present in VPP in order to support subsequent configuration changes
89                 // without any issues
90
91                 // the other reason this should work is HC persistence, so that HC after restart only performs diff (only push
92                 // configuration that is not currently in VPP, but is persisted. If they are equal skip any VPP calls)
93                 // updates to VPP. If this is not fully implemented (depending on VPP implementation, restoration of persisted
94                 // configuration can fail)
95                 new LispBuilder()
96                         .setEnable(readValue.isEnable())
97                         .setLispFeatureData(readValue.getLispFeatureData())
98                         .build());
99     }
100 }