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;
20 import static com.google.common.base.Preconditions.checkState;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.lisp.translate.util.EidTranslator;
24 import io.fd.honeycomb.translate.MappingContext;
25 import io.fd.honeycomb.translate.util.RWUtils;
26 import java.util.stream.Collector;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.Contexts;
29 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContextKey;
30 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.Mappings;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.Mapping;
32 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.MappingBuilder;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.MappingKey;
34 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid;
35 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.EidBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Utility class allowing {@link MappingId} to {@link Eid} mapping
45 public class EidMappingContext implements EidTranslator {
47 private static final Logger LOG = LoggerFactory.getLogger(EidMappingContext.class);
48 private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
50 private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContext, EidMappingContextKey>
54 * Create new naming context
56 * @param instanceName name of this context instance. Will be used as list item identifier within context data tree
58 public EidMappingContext(@Nonnull final String instanceName) {
59 namingContextIid = InstanceIdentifier.create(Contexts.class).child(
60 org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContext.class,
61 new EidMappingContextKey(instanceName));
65 * Retrieve name for mapping stored provided mappingContext instance.
67 * @param remoteEid eid of a mapped item
68 * @param mappingContext mapping context providing context data for current transaction
69 * @return name mapped to provided index
72 public synchronized MappingId getId(
73 @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid remoteEid,
74 @Nonnull final MappingContext mappingContext) {
76 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
77 checkState(read.isPresent(), "Mapping for eid: %s is not present. But should be", remoteEid);
79 return read.get().getMapping()
81 //cannot split to map + filtering,because its collecting mappings,not eid's
82 .filter(mapping -> compareEids(mapping.getEid(), remoteEid))
83 .collect(SINGLE_ITEM_COLLECTOR).getId();
87 * Retrieve name for mapping stored provided mappingContext instance.
89 * @param eid eid of a mapped item
90 * @param mappingContext mapping context providing context data for current transaction
91 * @return name mapped to provided index
94 public synchronized MappingId getId(
95 @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid eid,
96 @Nonnull final MappingContext mappingContext) {
98 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
99 //don't create artificial name as naming context, to not create reference to some artificial(in vpp non-existing)eid
100 checkState(read.isPresent(), "Mapping for eid: %s is not present. But should be", eid);
102 return read.get().getMapping().stream()
103 .filter(mapping -> compareEids(mapping.getEid(), eid))
104 .collect(SINGLE_ITEM_COLLECTOR).getId();
108 * Check whether mapping is present for index.
110 * @param eid eid of a mapped item
111 * @param mappingContext mapping context providing context data for current transaction
112 * @return true if present, false otherwise
114 public synchronized boolean containsId(
115 @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid eid,
116 @Nonnull final MappingContext mappingContext) {
117 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
119 return read.isPresent() &&
120 read.get().getMapping()
122 .anyMatch(mapping -> compareEids(mapping.getEid(), eid));
126 * Check whether mapping is present for index.
128 * @param eid eid of a mapped item
129 * @param mappingContext mapping context providing context data for current transaction
130 * @return true if present, false otherwise
132 public synchronized boolean containsId(
133 @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid eid,
134 @Nonnull final MappingContext mappingContext) {
135 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
137 return read.isPresent() &&
138 read.get().getMapping()
140 .anyMatch(mapping -> compareEids(mapping.getEid(), eid));
145 * Add mapping to current context
147 * @param index index of a mapped item
148 * @param eid eid data
149 * @param mappingContext mapping context providing context data for current transaction
151 public synchronized void addEid(
152 @Nonnull final MappingId index,
153 @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid eid,
154 final MappingContext mappingContext) {
156 final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
157 mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
161 * Add mapping to current context
163 * @param index index of a mapped item
164 * @param eid eid data
165 * @param mappingContext mapping context providing context data for current transaction
167 public synchronized void addEid(
168 @Nonnull final MappingId index,
169 @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid eid,
170 final MappingContext mappingContext) {
172 final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
173 mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
176 private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final MappingId index) {
177 return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(index));
181 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid eid) {
182 return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
183 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
187 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid eid) {
188 return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
189 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
193 * Remove mapping from current context
195 * @param index identificator of a mapped item
196 * @param mappingContext mapping context providing context data for current transaction
198 public synchronized void removeEid(@Nonnull final MappingId index, final MappingContext mappingContext) {
199 mappingContext.delete(getMappingIid(index));
203 * Returns index value associated with the given name.
205 * @param index index whitch should value sits on
206 * @param mappingContext mapping context providing context data for current transaction
207 * @return integer index value matching supplied name
208 * @throws IllegalArgumentException if name was not found
210 public synchronized Eid getEid(@Nonnull final MappingId index, final MappingContext mappingContext) {
211 final Optional<Mapping> read = mappingContext.read(getMappingIid(index));
212 checkArgument(read.isPresent(), "No mapping stored for index: %s", index);
213 return read.get().getEid();
217 * Check whether mapping is present for name.
219 * @param index index of a mapped item
220 * @param mappingContext mapping context providing context data for current transaction
221 * @return true if present, false otherwise
223 public synchronized boolean containsEid(@Nonnull final MappingId index,
224 @Nonnull final MappingContext mappingContext) {
225 return mappingContext.read(getMappingIid(index)).isPresent();