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.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.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.AdjacenciesIdentificationContexts;
28 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.AdjacenciesIdentification;
29 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.AdjacenciesIdentificationKey;
30 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.adjacencies.identification.Mappings;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.adjacencies.identification.mappings.Mapping;
32 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.adjacencies.identification.mappings.MappingBuilder;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.adjacencies.identification.mappings.MappingKey;
34 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.adjacencies.identification.mappings.mapping.EidIdentificatorPair;
35 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.adjacencies.identification.context.rev160801.adjacencies.identification.context.attributes.adjacencies.identification.contexts.adjacencies.identification.mappings.mapping.EidIdentificatorPairBuilder;
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;
40 public class AdjacenciesMappingContext {
42 private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
44 private final KeyedInstanceIdentifier<AdjacenciesIdentification, AdjacenciesIdentificationKey>
48 * Create new naming context
50 * @param instanceName name of this context instance. Will be used as list item identifier within context data tree
52 public AdjacenciesMappingContext(@Nonnull final String instanceName) {
53 namingContextIid = InstanceIdentifier.create(AdjacenciesIdentificationContexts.class).child(
54 AdjacenciesIdentification.class, new AdjacenciesIdentificationKey(instanceName));
58 * Retrieve name for mapping stored provided mappingContext instance.
60 * @param localEidId {@code MappingId} for local eid
61 * @param remoteEidId {@code MappingId} for remote eid
62 * @param mappingContext mapping context providing context data for current transaction
63 * @return name mapped to provided index
66 public synchronized String getAdjacencyId(
67 @Nonnull final String localEidId,
68 @Nonnull final String remoteEidId,
69 @Nonnull final MappingContext mappingContext) {
71 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
72 checkState(read.isPresent(), "No adjacencies mappings stored");
74 return read.get().getMapping().stream()
75 .filter(mapping -> isSame(pairForCombination(localEidId, remoteEidId), mapping))
76 .collect(SINGLE_ITEM_COLLECTOR).getId();
79 private boolean isSame(final EidIdentificatorPair currentPair, final Mapping mapping) {
80 // EidIdentificatorPair is container so it needs to be compared like this
81 final EidIdentificatorPair mappingPair = mapping.getEidIdentificatorPair();
82 return currentPair.getLocalEidId().equals(mappingPair.getLocalEidId())
83 && currentPair.getRemoteEidId().equals(mappingPair.getRemoteEidId());
86 private EidIdentificatorPair pairForCombination(final @Nonnull String localEidId,
87 final @Nonnull String remoteEidId) {
88 return new EidIdentificatorPairBuilder()
89 .setLocalEidId(new MappingId(localEidId))
90 .setRemoteEidId(new MappingId(remoteEidId))
95 * Check whether mapping is present for index.
97 * @param localEidId {@code MappingId} for local eid
98 * @param remoteEidId {@code MappingId} for remote eid
99 * @param mappingContext mapping context providing context data for current transaction
100 * @return true if present, false otherwise
102 public synchronized boolean containsId(
103 @Nonnull final String localEidId,
104 @Nonnull final String remoteEidId,
105 @Nonnull final MappingContext mappingContext) {
106 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
108 return read.isPresent() &&
109 read.get().getMapping()
111 .anyMatch(mapping -> isSame(pairForCombination(localEidId, remoteEidId), mapping));
115 * Add mapping to current context
117 * @param index index of a mapped item
118 * @param localEidId {@code MappingId} for local eid
119 * @param remoteEidId {@code MappingId} for remote eid
120 * @param mappingContext mapping context providing context data for current transaction
122 public synchronized void addEidPair(
123 @Nonnull final String index,
124 @Nonnull final String localEidId,
125 @Nonnull final String remoteEidId,
126 final MappingContext mappingContext) {
128 final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
129 mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEidIdentificatorPair(
130 pairForCombination(localEidId, remoteEidId)).build());
133 private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final String index) {
134 return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(index));
139 * Remove mapping from current context
141 * @param index identificator of a mapped item
142 * @param mappingContext mapping context providing context data for current transaction
144 public synchronized void removeForIndex(@Nonnull final String index, final MappingContext mappingContext) {
145 mappingContext.delete(getMappingIid(index));
149 * Returns index value associated with the given name.
151 * @param index index whitch should value sits on
152 * @param mappingContext mapping context providing context data for current transaction
153 * @return integer index value matching supplied name
154 * @throws IllegalArgumentException if name was not found
156 public synchronized EidIdentificatorPair getEidPair(@Nonnull final String index,
157 final MappingContext mappingContext) {
158 final Optional<Mapping> read = mappingContext.read(getMappingIid(index));
159 checkArgument(read.isPresent(), "No mapping stored for index: %s", index);
160 return read.get().getEidIdentificatorPair();
164 * Check whether mapping is present for name.
166 * @param index index of a mapped item
167 * @param mappingContext mapping context providing context data for current transaction
168 * @return true if present, false otherwise
170 public synchronized boolean containsEidPairForIndex(@Nonnull final String index,
171 @Nonnull final MappingContext mappingContext) {
172 return mappingContext.read(getMappingIid(index)).isPresent();