HONEYCOMB-75 - Lisp implemetation
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / context / util / EidMappingContext.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.context.util;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.lisp.translate.util.EidConverter;
23 import io.fd.honeycomb.translate.MappingContext;
24 import io.fd.honeycomb.translate.util.RWUtils;
25 import java.util.stream.Collector;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.Contexts;
28 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContextKey;
29 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.Mappings;
30 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.Mapping;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.MappingBuilder;
32 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.MappingKey;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid;
34 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.EidBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MappingId;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38
39 /**
40  * Utility class allowing {@link MappingId} to {@link Eid} mapping
41  */
42 public class EidMappingContext {
43
44     private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
45
46     private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContext, EidMappingContextKey>
47             namingContextIid;
48
49     /**
50      * Create new naming context
51      *
52      * @param instanceName name of this context instance. Will be used as list item identifier within context data tree
53      */
54     public EidMappingContext(@Nonnull final String instanceName) {
55         namingContextIid = InstanceIdentifier.create(Contexts.class).child(
56                 org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContext.class,
57                 new EidMappingContextKey(instanceName));
58     }
59
60     /**
61      * Retrieve name for mapping stored provided mappingContext instance.
62      *
63      * @param eid            eid of a mapped item
64      * @param mappingContext mapping context providing context data for current transaction
65      * @return name mapped to provided index
66      */
67     @Nonnull
68     public synchronized MappingId getId(
69             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid eid,
70             @Nonnull final MappingContext mappingContext) {
71
72         final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
73
74         //dont create artificial name as naming context, to not create refference to some artificial(in vpp non-existing)eid
75         checkArgument(read.isPresent(), "No mapping stored for eid: %s", eid);
76
77         return read.get().getMapping().stream()
78                 .filter(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
79                 .collect(SINGLE_ITEM_COLLECTOR).getId();
80     }
81
82     /**
83      * Retrieve name for mapping stored provided mappingContext instance.
84      *
85      * @param eid            eid of a mapped item
86      * @param mappingContext mapping context providing context data for current transaction
87      * @return name mapped to provided index
88      */
89     @Nonnull
90     public synchronized MappingId getId(
91             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid eid,
92             @Nonnull final MappingContext mappingContext) {
93
94         final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
95
96         //dont create artificial name as naming context, to not create refference to some artificial(in vpp non-existing)eid
97         checkArgument(read.isPresent(), "No mapping stored for eid: %s", eid);
98
99         //this kind of comparing is needed ,because yang generated containers does not override equals,unless they are defined as types
100         //in this hierarchy the first that define proper equals is Ipv4Address/Ipv6Address/MacAddress
101         //
102         // From official javadoc
103         //  The equals method for class Object implements the most discriminating possible equivalence relation on objects;
104         //  that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object
105         //  (x == y has the value true).
106         return read.get().getMapping().stream()
107                 .filter(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
108                 .collect(SINGLE_ITEM_COLLECTOR).getId();
109     }
110
111     /**
112      * Check whether mapping is present for index.
113      *
114      * @param eid            eid of a mapped item
115      * @param mappingContext mapping context providing context data for current transaction
116      * @return true if present, false otherwise
117      */
118     public synchronized boolean containsId(
119             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid eid,
120             @Nonnull final MappingContext mappingContext) {
121         final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
122
123         //dont create artificial name as naming context, to not create refference to some artificial(in vpp non-existing)eid
124         checkArgument(read.isPresent(), "No mapping stored for eid: %s", eid);
125
126         return read.isPresent()
127                 ? read.get().getMapping().stream().anyMatch(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
128                 : false;
129     }
130
131     /**
132      * Check whether mapping is present for index.
133      *
134      * @param eid            eid of a mapped item
135      * @param mappingContext mapping context providing context data for current transaction
136      * @return true if present, false otherwise
137      */
138     public synchronized boolean containsId(
139             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid eid,
140             @Nonnull final MappingContext mappingContext) {
141         final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
142
143         checkArgument(read.isPresent(), "No mapping stored for eid: %s", eid);
144
145         return read.isPresent()
146                 ? read.get().getMapping().stream().anyMatch(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
147                 : false;
148     }
149
150
151     /**
152      * Add mapping to current context
153      *
154      * @param index          index of a mapped item
155      * @param eid            eid data
156      * @param mappingContext mapping context providing context data for current transaction
157      */
158     public synchronized void addEid(
159             @Nonnull final MappingId index,
160             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid eid,
161             final MappingContext mappingContext) {
162
163         final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
164         //this copy is needed (type of eid in mapping is different that in local mapping,they only have same ancestor)
165         mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
166     }
167
168     /**
169      * Add mapping to current context
170      *
171      * @param index          index of a mapped item
172      * @param eid            eid data
173      * @param mappingContext mapping context providing context data for current transaction
174      */
175     public synchronized void addEid(
176             @Nonnull final MappingId index,
177             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid eid,
178             final MappingContext mappingContext) {
179
180         final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
181         mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
182     }
183
184     private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final MappingId index) {
185         return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(index));
186     }
187
188     private Eid copyEid(
189             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid eid) {
190         return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
191                 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
192     }
193
194     private Eid copyEid(
195             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid eid) {
196         return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
197                 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
198     }
199
200     /**
201      * Remove mapping from current context
202      *
203      * @param index          identificator of a mapped item
204      * @param mappingContext mapping context providing context data for current transaction
205      */
206     public synchronized void removeEid(@Nonnull final MappingId index, final MappingContext mappingContext) {
207         mappingContext.delete(getMappingIid(index));
208     }
209
210     /**
211      * Returns index value associated with the given name.
212      *
213      * @param index          index whitch should value sits on
214      * @param mappingContext mapping context providing context data for current transaction
215      * @return integer index value matching supplied name
216      * @throws IllegalArgumentException if name was not found
217      */
218     public synchronized Eid getEid(@Nonnull final MappingId index, final MappingContext mappingContext) {
219         final Optional<Mapping> read = mappingContext.read(getMappingIid(index));
220         checkArgument(read.isPresent(), "No mapping stored for index: %s", index);
221         return mappingContext.read(getMappingIid(index)).get().getEid();
222     }
223
224     /**
225      * Check whether mapping is present for name.
226      *
227      * @param index          index of a mapped item
228      * @param mappingContext mapping context providing context data for current transaction
229      * @return true if present, false otherwise
230      */
231     public synchronized boolean containsEid(@Nonnull final MappingId index,
232                                             @Nonnull final MappingContext mappingContext) {
233         return mappingContext.read(getMappingIid(index)).isPresent();
234     }
235 }