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.vpp.test.util;
19 import static org.mockito.Mockito.doReturn;
21 import com.google.common.base.Optional;
22 import com.google.common.collect.Lists;
23 import io.fd.honeycomb.translate.MappingContext;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
27 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext;
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.MappingsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
32 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38 * Utility that helps stubbing {@link io.fd.honeycomb.translate.vpp.util.NamingContext} methods.
40 // TODO(HONEYCOMB-226): the class needs to be refactored or even removed after extracting interface from NamingContext
41 public interface NamingContextHelper {
44 * Creates {@link Mapping} for given data.
46 * @param name to be mapped
47 * @param index to be mapped
48 * @return name to index mapping
50 default Optional<Mapping> mapping(@Nonnull final String name, final int index) {
51 return Optional.of(new MappingBuilder().setName(name).setIndex(index).build());
55 * Creates {@link KeyedInstanceIdentifier} for {@link Mapping} in {@link NamingContext}.
57 * @param name name of the mapping
58 * @param namingContextName name of the naming context
59 * @return identifier for the mapping
61 default KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid(@Nonnull final String name,
62 @Nonnull final String namingContextName) {
63 return InstanceIdentifier.create(Contexts.class).child(NamingContext.class,
64 new NamingContextKey(namingContextName)).child(Mappings.class).child(Mapping.class, new MappingKey(name));
68 * Stubs {@link MappingContext#read} to include given mapping in {@link NamingContext}.
70 * @param mappingContext mock instance of {@link MappingContext}
71 * @param name name of the mapping
72 * @param index index to be mapped
73 * @param namingContextName name of the naming context
75 default void defineMapping(@Nonnull final MappingContext mappingContext, @Nonnull final String name,
76 final int index, @Nonnull final String namingContextName) {
77 final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = mappingIid(name, namingContextName);
78 final InstanceIdentifier<Mappings> mappingsIid = mappingIid.firstIdentifierOf(Mappings.class);
80 final Optional<Mapping> singleMapping = mapping(name, index);
81 final List<Mapping> list = Common.getMappingList(mappingContext, mappingsIid);
82 list.add(singleMapping.get());
84 doReturn(Optional.of(new MappingsBuilder().setMapping(list).build())).when(mappingContext).read(mappingsIid);
85 doReturn(singleMapping).when(mappingContext).read(mappingIid);
89 * Stubs {@link MappingContext#read} for given {@link NamingContext} to return {@link Optional#absent} for provided
92 * @param mappingContext mock instance of {@link MappingContext}
93 * @param name name of the mapping
94 * @param namingContextName name of the naming context
96 default void noMappingDefined(@Nonnull final MappingContext mappingContext, @Nonnull final String name,
97 @Nonnull final String namingContextName) {
98 final InstanceIdentifier<Mappings> iid = mappingIid(name, namingContextName).firstIdentifierOf(Mappings.class);
99 final List<Mapping> list = Common.getMappingList(mappingContext, iid);
101 doReturn(Optional.of(new MappingsBuilder().setMapping(list).build())).when(mappingContext).read(iid);
102 doReturn(Optional.absent()).when(mappingContext).read(mappingIid(name, namingContextName));
106 private static List<Mapping> getMappingList(@Nonnull final MappingContext mappingContext,
107 @Nonnull final InstanceIdentifier<Mappings> mappingsIid) {
108 final Optional<Mappings> previousMappings = mappingContext.read(mappingsIid);
109 final MappingsBuilder mappingsBuilder;
110 if (previousMappings != null && previousMappings.isPresent()) {
111 mappingsBuilder = new MappingsBuilder(previousMappings.get());
113 mappingsBuilder = new MappingsBuilder();
114 mappingsBuilder.setMapping(Lists.newArrayList());
116 return mappingsBuilder.getMapping();