2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.lisp.context.util;
19 import static com.google.common.base.Preconditions.checkArgument;
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;
40 * Utility class allowing {@link MappingId} to {@link Eid} mapping
42 public class EidMappingContext {
44 private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
46 private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContext, EidMappingContextKey>
50 * Create new naming context
52 * @param instanceName name of this context instance. Will be used as list item identifier within context data tree
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));
61 * Retrieve name for mapping stored provided mappingContext instance.
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
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) {
72 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
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);
77 return read.get().getMapping().stream()
78 .filter(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
79 .collect(SINGLE_ITEM_COLLECTOR).getId();
83 * Retrieve name for mapping stored provided mappingContext instance.
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
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) {
94 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
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);
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
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();
112 * Check whether mapping is present for index.
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
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));
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);
126 return read.isPresent()
127 ? read.get().getMapping().stream().anyMatch(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
132 * Check whether mapping is present for index.
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
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));
143 checkArgument(read.isPresent(), "No mapping stored for eid: %s", eid);
145 return read.isPresent()
146 ? read.get().getMapping().stream().anyMatch(mapping -> EidConverter.compareEids(mapping.getEid(), eid))
152 * Add mapping to current context
154 * @param index index of a mapped item
155 * @param eid eid data
156 * @param mappingContext mapping context providing context data for current transaction
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) {
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());
169 * Add mapping to current context
171 * @param index index of a mapped item
172 * @param eid eid data
173 * @param mappingContext mapping context providing context data for current transaction
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) {
180 final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
181 mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
184 private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final MappingId index) {
185 return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(index));
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();
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();
201 * Remove mapping from current context
203 * @param index identificator of a mapped item
204 * @param mappingContext mapping context providing context data for current transaction
206 public synchronized void removeEid(@Nonnull final MappingId index, final MappingContext mappingContext) {
207 mappingContext.delete(getMappingIid(index));
211 * Returns index value associated with the given name.
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
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();
225 * Check whether mapping is present for name.
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
231 public synchronized boolean containsEid(@Nonnull final MappingId index,
232 @Nonnull final MappingContext mappingContext) {
233 return mappingContext.read(getMappingIid(index)).isPresent();