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.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.implementation.MappingService;
20 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
21 import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
22 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
24 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
25 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
26 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
27 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder;
36 * @author Florin Coras
39 public class MappingServiceTest extends BaseTestCase {
42 private MappingService mapService;
43 private DataStoreBackEnd dsbe;
44 private MappingSystem mapSystem;
47 private String eidIpv4String = "10.31.0.5";
51 public void before() throws Exception {
53 dao = context.mock(ILispDAO.class);
54 dsbe = context.mock(DataStoreBackEnd.class);
56 // map-caches init and table creation
57 allowing(dao).putTable(with(MappingOrigin.Northbound.toString()));will(returnValue(dao));
58 allowing(dao).putTable(with(MappingOrigin.Southbound.toString()));will(returnValue(dao));
60 mapSystem = new MappingSystem(dao, true, true, true);
62 mapService = new MappingService();
63 mapService.setDaoService(dao);
64 inject(mapService, "dsbe", dsbe);
65 inject(mapService, "mappingSystem", mapSystem);
67 eid = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/32");
71 @SuppressWarnings("unchecked")
74 public void handleAddAuthenticationKey() throws Exception {
75 MappingAuthkey authKey = new MappingAuthkeyBuilder().setKeyType(1).setKeyString("pass").build();
76 MappingEntry<MappingAuthkey> keyMappingEntry = new MappingEntry<>(SubKeys.AUTH_KEY, authKey);
77 Eid key = getDefaultKey();
78 MappingEntry<MappingAuthkey>[] authKeys =(MappingEntry<MappingAuthkey>[])
79 (Arrays.asList(keyMappingEntry).toArray());
80 addDsbeAddKeyExpectation();
81 oneOf(dao).put(weq(key), weq(authKeys));
82 mapService.addAuthenticationKey(eid, authKey);
87 public void handleGetAuthenticationKey() throws Exception {
88 Eid key = getDefaultKey();
89 oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
91 assertEquals("password", mapService.getAuthenticationKey(eid));
95 public void handleGetAuthenticationKeyNoIteration() throws Exception {
96 mapSystem.setIterateMask(false);
97 Eid key = getDefaultKey();
98 Eid passKey = getKey(30);
99 oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
100 allowing(dao).getSpecific(weq(passKey), with(SubKeys.AUTH_KEY));
102 assertEquals(null, mapService.getAuthenticationKey(eid));
107 public void handleRemoveAuthenticationKey() throws Exception {
108 Eid key = getDefaultKey();
109 addDsbeRemoveKeyExpectation();
110 oneOf(dao).removeSpecific(weq(key), with(SubKeys.AUTH_KEY));
111 mapService.removeAuthenticationKey(eid);
114 private Eid getDefaultKey() {
118 private Eid getKey(int mask) {
119 Eid key = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/" + Integer.toString(mask));
120 return MaskUtil.normalize(key, (short) mask);
123 private void addDsbeAddKeyExpectation() {
124 ValueSaverAction<AuthenticationKey> dsbeAddKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
126 public Object invoke(Invocation invocation) throws Throwable {
127 mapSystem.addAuthenticationKey(lastValue.getEid(), lastValue.getMappingAuthkey());
131 oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction);
134 private void addDsbeRemoveKeyExpectation() {
135 ValueSaverAction<AuthenticationKey> dsbeRemoveKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
137 public Object invoke(Invocation invocation) throws Throwable {
138 mapSystem.removeAuthenticationKey(lastValue.getEid());
142 oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction);