9cbc86c2e218576278f92c168815e14e2218ceb4
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / LispCustomizer.java
1 /*
2  * Copyright (c) 2015 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.write;
18
19 import com.google.common.base.Preconditions;
20 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
22 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import java.util.concurrent.CompletionStage;
27 import java.util.concurrent.TimeoutException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.Lisp;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import io.fd.vpp.jvpp.core.dto.LispEnableDisable;
33 import io.fd.vpp.jvpp.core.dto.LispEnableDisableReply;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35
36
37 /**
38  * Handles updates of {@link Lisp} node. Takes care of LISP enable/disable
39  */
40 public class LispCustomizer extends FutureJVppCustomizer
41         implements WriterCustomizer<Lisp>, ByteDataTranslator, JvppReplyConsumer {
42
43     public LispCustomizer(final FutureJVppCore vppApi) {
44         super(vppApi);
45     }
46
47     @Override
48     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Lisp> id, @Nonnull final Lisp dataAfter,
49                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
50         Preconditions.checkNotNull(dataAfter, "Lisp is null");
51
52         try {
53             enableDisableLisp(dataAfter.isEnable());
54         } catch (VppBaseCallException | TimeoutException e) {
55             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
56         }
57     }
58
59     @Override
60     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Lisp> id, @Nonnull final Lisp dataBefore,
61                                         @Nonnull final Lisp dataAfter, @Nonnull final WriteContext writeContext)
62             throws WriteFailedException {
63         Preconditions.checkNotNull(dataAfter, "Lisp is null");
64
65         try {
66             enableDisableLisp(dataAfter.isEnable());
67         } catch (VppBaseCallException | TimeoutException e) {
68             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
69         }
70     }
71
72     @Override
73     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Lisp> id, @Nonnull final Lisp dataBefore,
74                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
75         Preconditions.checkNotNull(dataBefore, "Lisp is null");
76
77         try {
78             enableDisableLisp(false);
79         } catch (VppBaseCallException | TimeoutException e) {
80             throw new WriteFailedException.DeleteFailedException(id, e);
81         }
82
83     }
84
85
86     private void enableDisableLisp(final boolean enable) throws VppBaseCallException, TimeoutException {
87         final CompletionStage<LispEnableDisableReply> lispEnableDisableReplyCompletionStage =
88                 getFutureJVpp().lispEnableDisable(getRequest(enable));
89         getReply(lispEnableDisableReplyCompletionStage.toCompletableFuture());
90     }
91
92     private LispEnableDisable getRequest(final boolean enable) {
93         final LispEnableDisable lispEnableDisable = new LispEnableDisable();
94         lispEnableDisable.isEn = booleanToByte(enable);
95         return lispEnableDisable;
96     }
97 }