Migrate implementation/neutron/southbound to IETF YANG model
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MappingServiceTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.implementation.lisp;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.util.Arrays;
14
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;
32
33 /**
34  *
35  * @author Florin Coras
36  *
37  */
38 public class MappingServiceTest extends BaseTestCase {
39
40     private ILispDAO dao;
41     private MappingService mapService;
42     private DataStoreBackEnd dsbe;
43     private MappingSystem mapSystem;
44
45     private Eid eid;
46     private String eidIpv4String = "10.31.0.5";
47
48     @Override
49     @Before
50     public void before() throws Exception {
51         super.before();
52         dao = context.mock(ILispDAO.class);
53         dsbe = context.mock(DataStoreBackEnd.class);
54
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));
58
59         mapSystem = new MappingSystem(dao, true, true, true);
60
61         mapService = new MappingService();
62         mapService.setDaoService(dao);
63         inject(mapService, "dsbe", dsbe);
64         inject(mapService, "mappingSystem", mapSystem);
65
66         eid = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/32");
67
68     }
69
70     @SuppressWarnings("unchecked")
71     @Test
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);
80     }
81
82     @Test
83     public void handleGetAuthenticationKey() throws Exception {
84         Eid key = getDefaultKey();
85         oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY));
86         ret("password");
87         assertEquals("password", mapService.getAuthenticationKey(eid));
88     }
89
90     @Test
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));
97         ret("password");
98         assertEquals(null, mapService.getAuthenticationKey(eid));
99     }
100
101     @Test
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);
107     }
108
109     private Eid getDefaultKey() {
110         return getKey(32);
111     }
112
113     private Eid getKey(int mask) {
114         Eid key = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/" + Integer.toString(mask));
115         return MaskUtil.normalize(key, (short) mask);
116     }
117
118     private void addDsbeAddKeyExpectation() {
119         ValueSaverAction<AuthenticationKey> dsbeAddKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
120             @Override
121             public Object invoke(Invocation invocation) throws Throwable {
122                 mapSystem.addAuthenticationKey(lastValue.getEid(), lastValue.getMappingAuthkey());
123                 return null;
124             }
125         };
126         oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction);
127     }
128
129     private void addDsbeRemoveKeyExpectation() {
130         ValueSaverAction<AuthenticationKey> dsbeRemoveKeySaverAction = new ValueSaverAction<AuthenticationKey>() {
131             @Override
132             public Object invoke(Invocation invocation) throws Throwable {
133                 mapSystem.removeAuthenticationKey(lastValue.getEid());
134                 return null;
135             }
136         };
137         oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction);
138     }
139
140 }