summaryrefslogtreecommitdiffstats
path: root/lisp/lisp2vpp/src/main/java/io/fd/honeycomb/lisp/translate/write/RemoteMappingCustomizer.java
blob: c382246ff71239aa859ad1c1cd6abf5b9f0a16d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.lisp.translate.write;


import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.fd.honeycomb.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.NEGATIVE;
import static io.fd.honeycomb.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.POSITIVE;

import com.google.common.base.Preconditions;
import io.fd.honeycomb.lisp.context.util.EidMappingContext;
import io.fd.honeycomb.lisp.translate.read.trait.MappingProducer;
import io.fd.honeycomb.lisp.translate.util.EidTranslator;
import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MapReplyAction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMapping;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMappingKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.LocatorList;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.NegativeMapping;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.PositiveMapping;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.Rlocs;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.Locator;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import io.fd.vpp.jvpp.VppBaseCallException;
import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMapping;
import io.fd.vpp.jvpp.core.future.FutureJVppCore;


/**
 * Customizer for {@link RemoteMapping}
 */
public class RemoteMappingCustomizer extends FutureJVppCustomizer
        implements ListWriterCustomizer<RemoteMapping, RemoteMappingKey>, EidTranslator,
        AddressTranslator, JvppReplyConsumer, MappingProducer {

    private final EidMappingContext remoteMappingContext;

    public RemoteMappingCustomizer(@Nonnull final FutureJVppCore futureJvpp,
                                   @Nonnull final EidMappingContext remoteMappingContext) {
        super(futureJvpp);
        this.remoteMappingContext = remoteMappingContext;
    }

    @Override
    public void writeCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataAfter,
                                       WriteContext writeContext) throws WriteFailedException {
        checkNotNull(dataAfter, "Mapping is null");
        checkNotNull(dataAfter.getEid(), "Eid is null");
        checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
        checkAllowedCombination(id, dataAfter);

        //checks whether mapping not already contains such key
        MappingId mappingId = id.firstKeyOf(RemoteMapping.class).getId();
        checkState(!remoteMappingContext.containsEid(mappingId, writeContext.getMappingContext()),
                "Mapping for id %s already defined", mappingId);

        try {
            addDelRemoteMappingAndReply(true, dataAfter,
                    id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
        } catch (VppBaseCallException | TimeoutException | IOException e) {
            throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
        }

        //after successfull adition adds mapping
        remoteMappingContext.addEid(mappingId, dataAfter.getEid(), writeContext.getMappingContext());
    }

    @Override
    public void updateCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataBefore,
                                        RemoteMapping dataAfter, WriteContext writeContext)
            throws WriteFailedException {
        throw new UnsupportedOperationException("Operation not supported");
    }

    @Override
    public void deleteCurrentAttributes(InstanceIdentifier<RemoteMapping> id, RemoteMapping dataBefore,
                                        WriteContext writeContext) throws WriteFailedException {
        checkNotNull(dataBefore, "Mapping is null");
        checkNotNull(dataBefore.getEid(), "Eid is null");

        //checks whether mapping already contains such key
        MappingId mappingId = id.firstKeyOf(RemoteMapping.class).getId();
        checkState(remoteMappingContext.containsEid(mappingId, writeContext.getMappingContext()),
                "Mapping for id %s is not existing,nothing to remove", mappingId);

        try {
            addDelRemoteMappingAndReply(false, dataBefore,
                    id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
        } catch (VppBaseCallException | TimeoutException | IOException e) {
            throw new WriteFailedException.DeleteFailedException(id, e);
        }

        //remote mapping after successfull remove of data
        remoteMappingContext.removeEid(mappingId, writeContext.getMappingContext());
    }

    private void addDelRemoteMappingAndReply(boolean add, RemoteMapping data, int vni)
            throws VppBaseCallException, TimeoutException, IOException {

        LispAddDelRemoteMapping request = new LispAddDelRemoteMapping();

        request.isAdd = booleanToByte(add);
        request.vni = vni;
        request.eidType = (byte) getEidType(data.getEid()).getValue();
        request.eid = getEidAsByteArray(data.getEid());

        //this is not length of eid array,but prefix length(bad naming by vpp)
        request.eidLen = getPrefixLength(data.getEid());

        if (LocatorListType.NEGATIVE
                .equals(resolveType(data.getLocatorList()))) {
            request.action = (byte) extractAction(data.getLocatorList()).getIntValue();
        } else {
            Rlocs rlocs = extractRemoteLocators(data.getLocatorList());

            checkArgument(rlocs != null, "No remote locators set for Positive mapping");

            request.rlocs = locatorsToBinaryData(rlocs.getLocator());
            request.rlocNum = Integer.valueOf(rlocs.getLocator().size()).byteValue();
        }

        getReply(getFutureJVpp().lispAddDelRemoteMapping(request).toCompletableFuture());
    }

    private static LocatorListType resolveType(LocatorList locatorList) {
        checkNotNull(locatorList, "Locator List cannot be null");

        if (locatorList instanceof PositiveMapping) {
            return POSITIVE;
        } else {
            return NEGATIVE;
        }
    }

    private static MapReplyAction extractAction(LocatorList locatorList) {
        checkNotNull(locatorList, "Locator List cannot be null");
        Preconditions.checkArgument(NEGATIVE.equals(resolveType(locatorList)),
                "Action can be extracted only from Negative Mapping");

        return ((NegativeMapping) locatorList).getMapReply().getMapReplyAction();
    }

    private static Rlocs extractRemoteLocators(LocatorList locatorList) {
        checkNotNull(locatorList, "Locator List cannot be null");
        Preconditions.checkArgument(POSITIVE.equals(resolveType(locatorList)),
                "RLocs can be extracted only from Positive Mapping");

        return ((PositiveMapping) locatorList).getRlocs();
    }

    //cant be static because of use of default methods from traits
    private byte[] locatorsToBinaryData(List<Locator> locators) throws IOException {
        checkNotNull(locators, "Cannot convert null list");

        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

        DataOutputStream out = new DataOutputStream(byteArrayOut);


        for (Locator locator : locators) {
            boolean isIpv4;
            byte[] address;

            //first byte says that its v4/v6
            isIpv4 = !isIpv6(locator.getAddress());
            out.writeByte(booleanToByte(isIpv4));

            //then writes priority
            out.write(locator.getPriority());

            //and weight
            out.write(locator.getWeight());

            if (isIpv4) {
                //vpp in this case needs address as 16 byte array,regardless if it is ivp4 or ipv6
                address = Arrays.copyOf(

                        ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(locator.getAddress().getIpv4Address())),
                        16);

                out.write(address);
            } else {
                out.write(
                        ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(locator.getAddress().getIpv6Address())));
            }
        }

        return byteArrayOut.toByteArray();
    }

    public enum LocatorListType {

        /**
         * Represents locator list as negative mapping
         */
        NEGATIVE,

        /**
         * Represents locator list as positive mapping
         */
        POSITIVE
    }

}