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.translate.vpp.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.naming.context.rev160513.Contexts;
28 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContextKey;
29 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
30 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder;
32 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 * Utility adapter on top of {@link MappingContext} storing integer to string mappings according to naming-context yang
40 public final class NamingContext implements AutoCloseable {
42 private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector();
43 private final String artificialNamePrefix;
44 private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext, NamingContextKey>
48 * Create new naming context
50 * @param artificialNamePrefix artificial name to be used for items without a name in VPP (or not provided)
51 * @param instanceName name of this context instance. Will be used as list item identifier within context
54 public NamingContext(@Nonnull final String artificialNamePrefix, @Nonnull final String instanceName) {
55 this.artificialNamePrefix = artificialNamePrefix;
56 namingContextIid = InstanceIdentifier.create(Contexts.class).child(
57 org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext.class,
58 new NamingContextKey(instanceName));
62 * Retrieve name for mapping stored provided mappingContext instance. If not present, artificial name will be
65 * @param index index of a mapped item
66 * @param mappingContext mapping context providing context data for current transaction
67 * @return name mapped to provided index
70 public synchronized String getName(final int index, @Nonnull final MappingContext mappingContext) {
71 if (!containsName(index, mappingContext)) {
72 final String artificialName = getArtificialName(index);
73 addName(index, artificialName, mappingContext);
76 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
77 checkState(read.isPresent(), "Mapping for index: %s is not present. But should be", index);
79 return read.get().getMapping().stream()
80 .filter(mapping -> mapping.getIndex().equals(index))
81 .collect(SINGLE_ITEM_COLLECTOR).getName();
85 * Retrieve name for mapping stored provided mappingContext instance. if present
87 * @param index index of a mapped item
88 * @param mappingContext mapping context providing context data for current transaction
89 * @return name mapped to provided index
92 public synchronized Optional<String> getNameIfPresent(final int index,
93 @Nonnull final MappingContext mappingContext) {
94 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
96 return read.isPresent()
97 ? Optional.of(read.get().getMapping().stream()
98 .filter(mapping -> mapping.getIndex().equals(index))
99 .collect(SINGLE_ITEM_COLLECTOR)
105 * Check whether mapping is present for index.
107 * @param index index of a mapped item
108 * @param mappingContext mapping context providing context data for current transaction
109 * @return true if present, false otherwise
111 public synchronized boolean containsName(final int index, @Nonnull final MappingContext mappingContext) {
112 final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
113 return read.isPresent()
114 ? read.get().getMapping().stream().anyMatch(mapping -> mapping.getIndex().equals(index))
120 * Add mapping to current context
122 * @param index index of a mapped item
123 * @param name name of a mapped item
124 * @param mappingContext mapping context providing context data for current transaction
126 public synchronized void addName(final int index, final String name, final MappingContext mappingContext) {
127 final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(name);
128 mappingContext.put(mappingIid, new MappingBuilder().setIndex(index).setName(name).build());
131 private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final String name) {
132 return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(name));
136 * Remove mapping from current context
138 * @param name name of a mapped item
139 * @param mappingContext mapping context providing context data for current transaction
141 public synchronized void removeName(final String name, final MappingContext mappingContext) {
142 mappingContext.delete(getMappingIid(name));
146 * Returns index value associated with the given name.
148 * @param name the name whose associated index value is to be returned
149 * @param mappingContext mapping context providing context data for current transaction
150 * @return integer index value matching supplied name
151 * @throws IllegalArgumentException if name was not found
153 public synchronized int getIndex(final String name, final MappingContext mappingContext) {
154 final Optional<Mapping> read = mappingContext.read(getMappingIid(name));
155 checkArgument(read.isPresent(), "No mapping stored for name: %s", name);
156 return read.get().getIndex();
161 * Check whether mapping is present for name.
163 * @param name name of a mapped item
164 * @param mappingContext mapping context providing context data for current transaction
165 * @return true if present, false otherwise
167 public synchronized boolean containsIndex(final String name, final MappingContext mappingContext) {
168 return mappingContext.read(getMappingIid(name)).isPresent();
171 private String getArtificialName(final int index) {
172 return artificialNamePrefix + index;
176 public void close() throws Exception {
177 /// Not removing the mapping from backing storage