701141ebc43d54b2dead8aef4eed2fd763dd3012
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / LocatorSetCustomizer.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.write;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.base.Preconditions.checkState;
21 import static java.nio.charset.StandardCharsets.UTF_8;
22
23 import io.fd.honeycomb.lisp.translate.read.trait.LocatorSetReader;
24 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
26 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
27 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
32 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.List;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.Interface;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41
42 /**
43  * Customizer for {@link LocatorSet} entity.
44  *
45  * @see LocatorSet
46  */
47 public class LocatorSetCustomizer extends FutureJVppCustomizer
48         implements ListWriterCustomizer<LocatorSet, LocatorSetKey>, ByteDataTranslator,
49         LocatorSetReader {
50
51     private final NamingContext locatorSetContext;
52     private final DumpCacheManager<LispLocatorSetDetailsReplyDump, Void> dumpManager;
53
54     public LocatorSetCustomizer(@Nonnull final FutureJVppCore futureJvpp,
55                                 @Nonnull final NamingContext locatorSetContext) {
56         super(futureJvpp);
57         this.locatorSetContext = checkNotNull(locatorSetContext, "Locator set context cannot be null");
58         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorSetDetailsReplyDump, Void>()
59                 .withExecutor(createExecutor(futureJvpp))
60                 .build();
61     }
62
63     @Override
64     public void writeCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
65                                        @Nonnull LocatorSet dataAfter,
66                                        @Nonnull WriteContext writeContext) throws WriteFailedException {
67         checkState(isNonEmptyLocatorSet(writeContext.readAfter(id).get()),
68                 "Creating empty locator-sets is not allowed");
69         final String locatorSetName = dataAfter.getName();
70         checkState(!locatorSetContext.containsIndex(locatorSetName, writeContext.getMappingContext()),
71                 "Locator set with name %s already defined", locatorSetName);
72
73         final int locatorSetIndex = addDelLocatorSetAndReply(true, dataAfter.getName(), id);
74         locatorSetContext.addName(locatorSetIndex, locatorSetName, writeContext.getMappingContext());
75     }
76
77     private boolean isNonEmptyLocatorSet(final LocatorSet locatorSet) {
78         final List<Interface> locators = locatorSet.getInterface();
79         return locators != null && !locators.isEmpty();
80     }
81
82     @Override
83     public void updateCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
84                                         @Nonnull LocatorSet dataBefore,
85                                         @Nonnull LocatorSet dataAfter,
86                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
87         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
88                 new UnsupportedOperationException("Operation not supported"));
89     }
90
91     @Override
92     public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
93                                         @Nonnull LocatorSet dataBefore,
94                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
95         final String locatorSetName = dataBefore.getName();
96         addDelLocatorSetAndReply(false, dataBefore.getName(), id);
97         //removes mapping after successful delete
98         locatorSetContext.removeName(locatorSetName, writeContext.getMappingContext());
99     }
100
101     private int addDelLocatorSetAndReply(final boolean add, final String name, final InstanceIdentifier<LocatorSet> id)
102             throws WriteFailedException {
103
104         LispAddDelLocatorSet addDelSet = new LispAddDelLocatorSet();
105
106         addDelSet.isAdd = booleanToByte(add);
107         addDelSet.locatorSetName = name.getBytes(UTF_8);
108
109         return getReplyForWrite(getFutureJVpp().lispAddDelLocatorSet(addDelSet).toCompletableFuture(), id).lsIndex;
110     }
111 }