467ac5161a45e8171e2f78759d43edb84506f332
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / vpp / util / NamingContext.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.translate.vpp.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.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;
35
36 /**
37  * Utility adapter on top of {@link MappingContext} storing integer to string mappings according to naming-context yang
38  * model.
39  */
40 public final class NamingContext implements AutoCloseable {
41
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>
45             namingContextIid;
46
47     /**
48      * Create new naming context
49      *
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
52      *                             data tree
53      */
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));
59     }
60
61     /**
62      * Retrieve name for mapping stored provided mappingContext instance. If not present, artificial name will be
63      * generated.
64      *
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
68      */
69     @Nonnull
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);
74         }
75
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);
78
79         return read.get().getMapping().stream()
80                 .filter(mapping -> mapping.getIndex().equals(index))
81                 .collect(SINGLE_ITEM_COLLECTOR).getName();
82     }
83
84     /**
85      * Retrieve name for mapping stored provided mappingContext instance. if present
86      *
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
90      */
91     @Nonnull
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));
95
96         return read.isPresent()
97                 ? Optional.of(read.get().getMapping().stream()
98                 .filter(mapping -> mapping.getIndex().equals(index))
99                 .collect(SINGLE_ITEM_COLLECTOR)
100                 .getName())
101                 : Optional.absent();
102     }
103
104     /**
105      * Check whether mapping is present for index.
106      *
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
110      */
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))
115                 : false;
116     }
117
118
119     /**
120      * Add mapping to current context
121      *
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
125      */
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());
129     }
130
131     private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final String name) {
132         return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(name));
133     }
134
135     /**
136      * Remove mapping from current context
137      *
138      * @param name           name of a mapped item
139      * @param mappingContext mapping context providing context data for current transaction
140      */
141     public synchronized void removeName(final String name, final MappingContext mappingContext) {
142         mappingContext.delete(getMappingIid(name));
143     }
144
145     /**
146      * Returns index value associated with the given name.
147      *
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
152      */
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();
157
158     }
159
160     /**
161      * Check whether mapping is present for name.
162      *
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
166      */
167     public synchronized boolean containsIndex(final String name, final MappingContext mappingContext) {
168         return mappingContext.read(getMappingIid(name)).isPresent();
169     }
170
171     private String getArtificialName(final int index) {
172         return artificialNamePrefix + index;
173     }
174
175     @Override
176     public void close() throws Exception {
177         /// Not removing the mapping from backing storage
178     }
179 }