d9513e10e10f68a0cbf90a108b1c1fb2fbd499ec
[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 import static com.google.common.base.Preconditions.checkState;
21
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.rev160520.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;
41
42 /**
43  * Utility class allowing {@link MappingId} to {@link Eid} mapping
44  */
45 public class EidMappingContext implements EidTranslator {
46
47     private static final Logger LOG = LoggerFactory.getLogger(EidMappingContext.class);
48     private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
49
50     private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.EidMappingContext, EidMappingContextKey>
51             namingContextIid;
52
53     /**
54      * Create new naming context
55      *
56      * @param instanceName name of this context instance. Will be used as list item identifier within context data tree
57      */
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));
62     }
63
64     /**
65      * Retrieve name for mapping stored provided mappingContext instance.
66      *
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
70      */
71     @Nonnull
72     public synchronized MappingId getId(
73             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid remoteEid,
74             @Nonnull final MappingContext mappingContext) {
75
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);
78
79         return read.get().getMapping()
80                 .stream()
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();
84     }
85
86     /**
87      * Retrieve name for mapping stored provided mappingContext instance.
88      *
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
92      */
93     @Nonnull
94     public synchronized MappingId getId(
95             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid eid,
96             @Nonnull final MappingContext mappingContext) {
97
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);
101
102         return read.get().getMapping().stream()
103                 .filter(mapping -> compareEids(mapping.getEid(), eid))
104                 .collect(SINGLE_ITEM_COLLECTOR).getId();
105     }
106
107     /**
108      * Check whether mapping is present for index.
109      *
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
113      */
114     public synchronized boolean containsId(
115             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.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));
118
119         return read.isPresent() &&
120                 read.get().getMapping()
121                         .stream()
122                         .anyMatch(mapping -> compareEids(mapping.getEid(), eid));
123     }
124
125     /**
126      * Check whether mapping is present for index.
127      *
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
131      */
132     public synchronized boolean containsId(
133             @Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.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));
136
137         return read.isPresent() &&
138                 read.get().getMapping()
139                         .stream()
140                         .anyMatch(mapping -> compareEids(mapping.getEid(), eid));
141     }
142
143
144     /**
145      * Add mapping to current context
146      *
147      * @param index          index of a mapped item
148      * @param eid            eid data
149      * @param mappingContext mapping context providing context data for current transaction
150      */
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.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid eid,
154             final MappingContext mappingContext) {
155
156         final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
157         mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
158     }
159
160     /**
161      * Add mapping to current context
162      *
163      * @param index          index of a mapped item
164      * @param eid            eid data
165      * @param mappingContext mapping context providing context data for current transaction
166      */
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.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid eid,
170             final MappingContext mappingContext) {
171
172         final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(index);
173         mappingContext.put(mappingIid, new MappingBuilder().setId(index).setEid(copyEid(eid)).build());
174     }
175
176     private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final MappingId index) {
177         return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(index));
178     }
179
180     private Eid copyEid(
181             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid eid) {
182         return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
183                 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
184     }
185
186     private Eid copyEid(
187             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid eid) {
188         return new EidBuilder().setAddress(eid.getAddress()).setAddressType(eid.getAddressType())
189                 .setVirtualNetworkId(eid.getVirtualNetworkId()).build();
190     }
191
192     /**
193      * Remove mapping from current context
194      *
195      * @param index          identificator of a mapped item
196      * @param mappingContext mapping context providing context data for current transaction
197      */
198     public synchronized void removeEid(@Nonnull final MappingId index, final MappingContext mappingContext) {
199         mappingContext.delete(getMappingIid(index));
200     }
201
202     /**
203      * Returns index value associated with the given name.
204      *
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
209      */
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();
214     }
215
216     /**
217      * Check whether mapping is present for name.
218      *
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
222      */
223     public synchronized boolean containsEid(@Nonnull final MappingId index,
224                                             @Nonnull final MappingContext mappingContext) {
225         return mappingContext.read(getMappingIid(index)).isPresent();
226     }
227 }