ed1d2d7d69ef210d7146619baacd7a6598298307
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.lisp.gpe.translate.ctx;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.verifyNoMoreInteractions;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import com.google.common.base.Optional;
28 import io.fd.honeycomb.translate.MappingContext;
29 import java.util.Collections;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.gpe.locator.pair.identification.context.rev170517.GpeLocatorPairIdentificationCtxAugmentation;
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.GpeLocatorPairIdentificationContexts;
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.GpeLocatorPairIdentification;
36 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;
37 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;
38 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;
39 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;
40 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;
41 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;
42 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;
43 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.Pair;
44 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;
45 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50
51 public class GpeLocatorPairMappingContextImplTest {
52
53     private static final String INSTANCE = "instance";
54
55     @Mock
56     private MappingContext mappingContext;
57
58     private GpeLocatorPairMappingContextImpl mapping;
59     private IpAddress localAddress;
60     private IpAddress remoteAddress;
61     private String entryId;
62     private String locatorId;
63
64     @Before
65     public void setUp() throws Exception {
66         initMocks(this);
67         mapping = new GpeLocatorPairMappingContextImpl(INSTANCE);
68         localAddress = new IpAddress(new Ipv4Address("192.168.2.1"));
69         remoteAddress = new IpAddress(new Ipv4Address("192.168.2.1"));
70         entryId = "entry";
71         locatorId = "locator";
72     }
73
74     @Test
75     public void addMapping() throws Exception {
76         mapping.addMapping(entryId, locatorId, gpeLocatorPair(localAddress, remoteAddress), mappingContext);
77         verify(mappingContext, times(1)).merge(mappingId(entryId),
78                 mapingData(entryId, locatorId, localAddress, remoteAddress));
79         verifyNoMoreInteractions(mappingContext);
80     }
81
82     @Test
83     public void removeMappingPresent() throws Exception {
84         KeyedInstanceIdentifier<Mapping, MappingKey> instanceIdentifier = mappingId(entryId);
85         when(mappingContext.read(instanceIdentifier)).thenReturn(Optional.of(new MappingBuilder().build()));
86         mapping.removeMapping(entryId, mappingContext);
87         verify(mappingContext, times(1)).read(instanceIdentifier);
88         verify(mappingContext, times(1)).delete(instanceIdentifier);
89         verifyNoMoreInteractions(mappingContext);
90     }
91
92     @Test
93     public void removeMappingNotPresent() throws Exception {
94         when(mappingContext.read(mappingId(entryId))).thenReturn(Optional.absent());
95         mapping.removeMapping(entryId, mappingContext);
96         verify(mappingContext, times(1)).read(mappingId(entryId));
97         verifyNoMoreInteractions(mappingContext);
98     }
99
100
101     @Test
102     public void getMappingTextLocatorId() throws Exception {
103         when(mappingContext.read(mappingId(entryId)))
104                 .thenReturn(Optional.of(mapingData(entryId, locatorId, localAddress, remoteAddress)));
105         final LocatorPairMapping mapping = this.mapping.getMapping(entryId, locatorId, mappingContext);
106         assertNotNull(mapping);
107         assertEquals(locatorId, mapping.getId());
108         final Pair pair = mapping.getPair();
109         assertEquals(localAddress, pair.getLocalAddress());
110         assertEquals(remoteAddress, pair.getRemoteAddress());
111     }
112
113     @Test
114     public void getMappingObjectLocatorId() throws Exception {
115         when(mappingContext.read(mappingId(entryId)))
116                 .thenReturn(Optional.of(mapingData(entryId, locatorId, localAddress, remoteAddress)));
117         final LocatorPairMapping mapping =
118                 this.mapping.getMapping(entryId, gpeLocatorPair(localAddress, remoteAddress), mappingContext);
119         assertNotNull(mapping);
120         assertEquals(locatorId, mapping.getId());
121         final Pair pair = mapping.getPair();
122         assertEquals(localAddress, pair.getLocalAddress());
123         assertEquals(remoteAddress, pair.getRemoteAddress());
124     }
125
126     private static GpeLocatorPair gpeLocatorPair(final IpAddress localAddress, final IpAddress remoteAddress) {
127         return new GpeLocatorPair.GpeLocatorPairBuilder().setLocalAddress(localAddress).setRemoteAddress(remoteAddress)
128                 .createGpeLocatorPairIdentifier();
129     }
130
131     private static KeyedInstanceIdentifier<Mapping, MappingKey> mappingId(String entryId) {
132         return InstanceIdentifier.create(Contexts.class)
133                 .augmentation(GpeLocatorPairIdentificationCtxAugmentation.class)
134                 .child(GpeLocatorPairIdentificationContexts.class)
135                 .child(GpeLocatorPairIdentification.class, new GpeLocatorPairIdentificationKey(INSTANCE))
136                 .child(Mappings.class)
137                 .child(Mapping.class, new MappingKey(entryId));
138     }
139
140     private static Mapping mapingData(String entryId, String locatorId, IpAddress localAddress,
141                                       IpAddress remoteAddress) {
142         return new MappingBuilder()
143                 .setId(entryId)
144                 .setLocatorPairMapping(Collections.singletonList(new LocatorPairMappingBuilder()
145                         .setId(locatorId)
146                         .setPair(new PairBuilder()
147                                 .setLocalAddress(localAddress)
148                                 .setRemoteAddress(remoteAddress)
149                                 .build())
150                         .build())).build();
151     }
152 }