HONEYCOMB-58 - Routing Api
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / LocalMappingCustomizer.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 io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV4;
22 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV6;
23 import static java.nio.charset.StandardCharsets.UTF_8;
24
25 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
26 import io.fd.honeycomb.lisp.translate.read.trait.MappingProducer;
27 import io.fd.honeycomb.lisp.translate.util.EidTranslator;
28 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
30 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
31 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
32 import io.fd.honeycomb.translate.write.WriteContext;
33 import io.fd.honeycomb.translate.write.WriteFailedException;
34 import java.io.UnsupportedEncodingException;
35 import java.util.concurrent.TimeoutException;
36 import javax.annotation.Nonnull;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.LocalMapping;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.LocalMappingKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import io.fd.vpp.jvpp.VppBaseCallException;
43 import io.fd.vpp.jvpp.core.dto.LispAddDelLocalEid;
44 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
45
46
47 /**
48  * Customizer that writes changes for {@link LocalMapping}
49  */
50 public class LocalMappingCustomizer extends FutureJVppCustomizer
51         implements ListWriterCustomizer<LocalMapping, LocalMappingKey>, ByteDataTranslator, EidTranslator,
52         JvppReplyConsumer, MappingProducer {
53
54     private final EidMappingContext localMappingsContext;
55
56     public LocalMappingCustomizer(@Nonnull FutureJVppCore futureJvpp, @Nonnull EidMappingContext localMappingsContext) {
57         super(futureJvpp);
58         this.localMappingsContext = checkNotNull(localMappingsContext, "No local mappings context defined");
59     }
60
61     @Override
62     public void writeCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataAfter,
63                                        WriteContext writeContext) throws WriteFailedException {
64         checkNotNull(dataAfter, "Mapping is null");
65         checkNotNull(dataAfter.getEid(), "Eid is null");
66         checkNotNull(dataAfter.getLocatorSet(), "Locator set is null");
67         checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
68         checkAllowedCombination(id, dataAfter);
69
70         //checks whether value with specified mapping-id does not exist in mapping allready
71         final MappingId mappingId = id.firstKeyOf(LocalMapping.class).getId();
72         checkState(!localMappingsContext
73                         .containsEid(mappingId, writeContext.getMappingContext()),
74                 "Local mapping with id %s already defined", id);
75
76
77         try {
78             addDelMappingAndReply(true, dataAfter,
79                     id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
80         } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
81             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
82         }
83
84         //adds mapping for id and eid
85         localMappingsContext.addEid(mappingId, dataAfter.getEid(), writeContext.getMappingContext());
86     }
87
88     @Override
89     public void updateCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataBefore,
90                                         LocalMapping dataAfter, WriteContext writeContext)
91             throws WriteFailedException {
92         throw new UnsupportedOperationException("Operation not supported");
93     }
94
95     @Override
96     public void deleteCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataBefore,
97                                         WriteContext writeContext) throws WriteFailedException {
98         checkNotNull(dataBefore, "Mapping is null");
99         checkNotNull(dataBefore.getEid(), "Eid is null");
100         checkNotNull(dataBefore.getLocatorSet(), "LocatorSet is null");
101         checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
102
103         //checks whether value with specified mapping-id does exist in mapping,so there is something to delete
104         MappingId mappingId = id.firstKeyOf(LocalMapping.class).getId();
105         checkState(localMappingsContext
106                         .containsEid(mappingId, writeContext.getMappingContext()),
107                 "Local mapping with id %s not present in mapping", id);
108
109         try {
110             addDelMappingAndReply(false, dataBefore,
111                     id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
112         } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
113             throw new WriteFailedException.DeleteFailedException(id, e);
114         }
115
116         //removes value also from mapping
117         localMappingsContext.removeEid(mappingId, writeContext.getMappingContext());
118     }
119
120     private void addDelMappingAndReply(boolean add, LocalMapping data, int vni) throws VppBaseCallException,
121             TimeoutException, UnsupportedEncodingException {
122
123         LispAddDelLocalEid request = new LispAddDelLocalEid();
124
125         request.isAdd = booleanToByte(add);
126         request.eid = getEidAsByteArray(data.getEid());
127         request.eidType = (byte) getEidType(data.getEid()).getValue();
128         request.locatorSetName = data.getLocatorSet().getBytes(UTF_8);
129         request.vni = vni;
130
131         //default prefixes
132         if (request.eidType == IPV4.getValue()) {
133             request.prefixLen = 32;
134         } else if (request.eidType == IPV6.getValue()) {
135             request.prefixLen = (byte) 128;
136         }
137
138         getReply(getFutureJVpp().lispAddDelLocalEid(request).toCompletableFuture());
139     }
140
141 }