557dc27713198f6180a2c55ec369b8855e439958
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / context / util / AdjacenciesMappingContext.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 import static com.google.common.base.Preconditions.checkState;
21
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;
39
40 public class AdjacenciesMappingContext {
41
42     private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
43
44     private final KeyedInstanceIdentifier<AdjacenciesIdentification, AdjacenciesIdentificationKey>
45             namingContextIid;
46
47     /**
48      * Create new naming context
49      *
50      * @param instanceName name of this context instance. Will be used as list item identifier within context data tree
51      */
52     public AdjacenciesMappingContext(@Nonnull final String instanceName) {
53         namingContextIid = InstanceIdentifier.create(AdjacenciesIdentificationContexts.class).child(
54                 AdjacenciesIdentification.class, new AdjacenciesIdentificationKey(instanceName));
55     }
56
57     /**
58      * Retrieve name for mapping stored provided mappingContext instance.
59      *
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
64      */
65     @Nonnull
66     public synchronized String getAdjacencyId(
67             @Nonnull final String localEidId,
68             @Nonnull final String remoteEidId,
69             @Nonnull final MappingContext mappingContext) {
70
71         final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
72         checkState(read.isPresent(), "No adjacencies mappings stored");
73
74         return read.get().getMapping().stream()
75                 .filter(mapping -> isSame(pairForCombination(localEidId, remoteEidId), mapping))
76                 .collect(SINGLE_ITEM_COLLECTOR).getId();
77     }
78
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());
84     }
85
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))
91                 .build();
92     }
93
94     /**
95      * Check whether mapping is present for index.
96      *
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
101      */
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));
107
108         return read.isPresent() &&
109                 read.get().getMapping()
110                         .stream()
111                         .anyMatch(mapping -> isSame(pairForCombination(localEidId, remoteEidId), mapping));
112     }
113
114     /**
115      * Add mapping to current context
116      *
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
121      */
122     public synchronized void addEidPair(
123             @Nonnull final String index,
124             @Nonnull final String localEidId,
125             @Nonnull final String remoteEidId,
126             final MappingContext mappingContext) {
127
128         final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
129         mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEidIdentificatorPair(
130                 pairForCombination(localEidId, remoteEidId)).build());
131     }
132
133     private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final String index) {
134         return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(index));
135     }
136
137
138     /**
139      * Remove mapping from current context
140      *
141      * @param index          identificator of a mapped item
142      * @param mappingContext mapping context providing context data for current transaction
143      */
144     public synchronized void removeForIndex(@Nonnull final String index, final MappingContext mappingContext) {
145         mappingContext.delete(getMappingIid(index));
146     }
147
148     /**
149      * Returns index value associated with the given name.
150      *
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
155      */
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();
161     }
162
163     /**
164      * Check whether mapping is present for name.
165      *
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
169      */
170     public synchronized boolean containsEidPairForIndex(@Nonnull final String index,
171                                                         @Nonnull final MappingContext mappingContext) {
172         return mappingContext.read(getMappingIid(index)).isPresent();
173     }
174 }