2 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.lispflowmapping.implementation.lisp;
11 import static org.junit.Assert.assertEquals;
13 import java.util.Arrays;
15 import org.jmock.api.Invocation;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.implementation.MappingService;
19 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
20 import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
21 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
23 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
24 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
25 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
26 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder;
35 * @author Florin Coras
38 public class MappingServiceTest extends BaseTestCase {
41 private MappingService mapService;
42 private DataStoreBackEnd dsbe;
43 private MappingSystem mapSystem;
46 private String eidIpv4String = "10.31.0.5";
50 public void before() throws Exception {
52 dao = context.mock(ILispDAO.class);
53 dsbe = context.mock(DataStoreBackEnd.class);
55 // map-caches init and table creation
56 allowing(dao).putTable(with(MappingOrigin.Northbound.toString()));will(returnValue(dao));
57 allowing(dao).putTable(with(MappingOrigin.Southbound.toString()));will(returnValue(dao));
59 mapSystem = new MappingSystem(dao, true, true, true);
61 mapService = new MappingService();
62 mapService.setDaoService(dao);
63 inject(mapService, "dsbe", dsbe);
64 inject(mapService, "mappingSystem", mapSystem);
66 eid = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/32");
70 @SuppressWarnings("unchecked")
72 public void handleAddAuthenticationKey() throws Exception {
73 MappingAuthkey authKey = new MappingAuthkeyBuilder().setKeyType(1).setKeyString("pass").build();
74 MappingEntry<MappingAuthkey> keyMappingEntry = new MappingEntry<>(SubKeys.AUTH_KEY, authKey);
75 Eid key = getDefaultKey();
76 MappingEntry<MappingAuthkey>[] authKeys =(MappingEntry<MappingAuthkey>[]) (Arrays.asList(keyMappingEntry).toArray());
77 addDsbeAddKeyExpectation();
78 oneOf(dao).put(weq(key), weq(authKeys));
79 mapService.addAuthenticationKey(eid, authKey);
83 public void handleGetAuthenticationKey() throws Exception {
84 Eid key = getDefaultKey();
85 oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
87 assertEquals("password", mapService.getAuthenticationKey(eid));
91 public void handleGetAuthenticationKeyNoIteration() throws Exception {
92 mapSystem.setIterateMask(false);
93 Eid key = getDefaultKey();
94 Eid passKey = getKey(30);
95 oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
96 allowing(dao).getSpecific(weq(passKey), with(SubKeys.AUTH_KEY));
98 assertEquals(null, mapService.getAuthenticationKey(eid));
102 public void handleRemoveAuthenticationKey() throws Exception {
103 Eid key = getDefaultKey();
104 addDsbeRemoveKeyExpectation();
105 oneOf(dao).removeSpecific(weq(key), with(SubKeys.AUTH_KEY));
106 mapService.removeAuthenticationKey(eid);
109 private Eid getDefaultKey() {
113 private Eid getKey(int mask) {
114 Eid key = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/" + Integer.toString(mask));
115 return MaskUtil.normalize(key, (short) mask);
118 private void addDsbeAddKeyExpectation() {
119 ValueSaverAction<AuthenticationKey> dsbeAddKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
121 public Object invoke(Invocation invocation) throws Throwable {
122 mapSystem.addAuthenticationKey(lastValue.getEid(), lastValue.getMappingAuthkey());
126 oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction);
129 private void addDsbeRemoveKeyExpectation() {
130 ValueSaverAction<AuthenticationKey> dsbeRemoveKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
132 public Object invoke(Invocation invocation) throws Throwable {
133 mapSystem.removeAuthenticationKey(lastValue.getEid());
137 oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction);