2 * Copyright (c) 2017 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.hc2vpp.lisp.gpe.translate.ctx;
19 import static java.lang.String.format;
21 import io.fd.honeycomb.translate.MappingContext;
22 import io.fd.honeycomb.translate.util.RWUtils;
23 import java.util.Collections;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.GpeLocatorPairIdentificationCtxAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.GpeLocatorPairIdentificationContexts;
27 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.GpeLocatorPairIdentification;
28 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.GpeLocatorPairIdentificationKey;
29 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.Mappings;
30 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.mappings.Mapping;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.mappings.MappingBuilder;
32 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.mappings.MappingKey;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.mappings.mapping.LocatorPairMapping;
34 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.mappings.mapping.LocatorPairMappingBuilder;
35 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.gpe.locator.pair.identification.context.attributes.gpe.locator.pair.identification.contexts.gpe.locator.pair.identification.mappings.mapping.locator.pair.mapping.PairBuilder;
36 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
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;
42 public class GpeLocatorPairMappingContextImpl implements GpeLocatorPairMappingContext {
44 private static final Logger LOG = LoggerFactory.getLogger(GpeLocatorPairMappingContextImpl.class);
46 private final KeyedInstanceIdentifier<GpeLocatorPairIdentification, GpeLocatorPairIdentificationKey>
49 public GpeLocatorPairMappingContextImpl(@Nonnull final String instanceName) {
50 namingContextIid = InstanceIdentifier.create(Contexts.class)
51 .augmentation(GpeLocatorPairIdentificationCtxAugmentation.class)
52 .child(GpeLocatorPairIdentificationContexts.class)
53 .child(GpeLocatorPairIdentification.class, new GpeLocatorPairIdentificationKey(instanceName));
58 public void addMapping(@Nonnull final String entryId,
59 @Nonnull final String locatorId,
60 @Nonnull final GpeLocatorPair pair,
61 @Nonnull final MappingContext mappingContext) {
62 LOG.debug("Adding mapping for Gpe entry to locator id[entry-id={},locator-pair-id={}]", entryId, locatorId);
63 mappingContext.merge(getMappingId(entryId), getMappingData(entryId, locatorId, pair));
64 LOG.debug("Mapping for Gpe entry to locator id[entry-id={},locator-pair-id={}] successfully added", entryId,
69 public void removeMapping(@Nonnull final String entryId,
70 @Nonnull final MappingContext mappingContext) {
71 LOG.debug("Removing all mappings for Gpe entry[id={}]", entryId);
72 KeyedInstanceIdentifier<Mapping, MappingKey> mappingId = getMappingId(entryId);
73 // if no mapping present, no need to invoke delete(it would throw error because of non existing parent data)
74 if (mappingContext.read(mappingId).isPresent()) {
75 mappingContext.delete(mappingId);
77 LOG.debug("All mappings for Gpe entry[id={}] removed", entryId);
81 public LocatorPairMapping getMapping(@Nonnull final String entryId,
82 @Nonnull final GpeLocatorPair pair,
83 @Nonnull final MappingContext mappingContext) {
85 if (!contains(entryId, pair, mappingContext)) {
86 final String artificialLocatorId = artificialLocatorPairId(entryId, pair);
87 addMapping(entryId, artificialLocatorId, pair, mappingContext);
88 return getMapping(entryId, artificialLocatorId, mappingContext);
91 return mappingContext.read(getMappingId(entryId))
92 .or(new MappingBuilder().setLocatorPairMapping(Collections.emptyList()).build())
93 .getLocatorPairMapping()
95 .filter(mapping -> pair.isSame(mapping.getPair()))
96 .collect(RWUtils.singleItemCollector());
99 private boolean contains(final String entryId,
100 final GpeLocatorPair pair,
101 final MappingContext mappingContext) {
102 return mappingContext.read(getMappingId(entryId))
103 .or(new MappingBuilder().setLocatorPairMapping(Collections.emptyList()).build())
104 .getLocatorPairMapping()
106 .anyMatch(mapping -> pair.isSame(mapping.getPair()));
110 public LocatorPairMapping getMapping(@Nonnull final String entryId,
111 @Nonnull final String locatorId,
112 @Nonnull final MappingContext mappingContext) {
113 return mappingContext.read(getMappingId(entryId))
114 .or(new MappingBuilder().setLocatorPairMapping(Collections.emptyList()).build())
115 .getLocatorPairMapping()
117 .filter(mapping -> mapping.getId().equals(locatorId))
118 .findAny().orElseThrow(() -> new IllegalArgumentException(
119 format("No mapping for entry %s|locator %s", entryId, locatorId)));
122 private String artificialLocatorPairId(final String entryId, final GpeLocatorPair pair) {
123 return format("%s_%s_%s", entryId, pair.getLocalAddress(), pair.getRemoteAddress());
126 private Mapping getMappingData(final String entryId,
127 final String locatorId,
128 final GpeLocatorPair identifier) {
129 return new MappingBuilder()
131 .setLocatorPairMapping(Collections.singletonList(new LocatorPairMappingBuilder()
133 .setPair(new PairBuilder()
134 .setLocalAddress(identifier.getLocalAddress())
135 .setRemoteAddress(identifier.getRemoteAddress())
140 private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingId(final String id) {
141 return namingContextIid.child(Mappings.class)
142 .child(Mapping.class, new MappingKey(id));