+ mapRegister.setNonce((long) 8);
+ EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
+ etlr.setLispAddressContainer(LispAFIConvertor.toContainer(address));
+ etlr.setMaskLength((short) mask);
+ etlr.setRecordTtl(254);
+ LocatorRecordBuilder record = new LocatorRecordBuilder();
+ record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
+ etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
+ etlr.getLocatorRecord().add(record.build());
+ mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
+ mapRegister.getEidToLocatorRecord().add(etlr.build());
+
+ mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
+ mapRegister.setAuthenticationData(zeros);
+
+ sendMapRegister(mapRegister.build());
+ assertNoPacketReceived(3000);
+
+ mapRegister.setAuthenticationData(expectedSha);
+
+ sendMapRegister(mapRegister.build());
+
+ // this will fail if no MapNotify arrives for 6 seconds
+ MapNotify notify = receiveMapNotify();
+ }
+
+ @Test
+ public void testPasswordMaskMatch() throws Exception {
+ LispIpv4Address addressInRange = LispAFIConvertor.asIPAfiAddress("10.20.30.40");
+ LispIpv4Address addressOutOfRange = LispAFIConvertor.asIPAfiAddress("20.40.30.40");
+ LispIpv4Address range = LispAFIConvertor.asIPAfiAddress("10.20.30.0");
+
+ int mask = 32;
+ String pass = "pass";
+
+ URL url = createPutURL("key");
+ String jsonAuthData = createAuthKeyJSON(pass, range, 8);
+
+ callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
+ // build a MapRegister
+ MapRegisterBuilder mapRegister = new MapRegisterBuilder();
+
+ mapRegister.setWantMapNotify(true);
+ mapRegister.setNonce((long) 8);
+ EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
+ etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressInRange));
+ etlr.setMaskLength((short) mask);
+ etlr.setRecordTtl(254);
+ LocatorRecordBuilder record = new LocatorRecordBuilder();
+ record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
+ record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
+ etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
+ etlr.getLocatorRecord().add(record.build());
+ mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
+ mapRegister.getEidToLocatorRecord().add(etlr.build());
+
+ mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
+ mapRegister
+ .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
+
+ sendMapRegister(mapRegister.build());
+
+ // this will fail if no MapNotify arrives for 6 seconds
+ MapNotify notify = receiveMapNotify();
+
+ etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
+ mapRegister
+ .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
+
+ sendMapRegister(mapRegister.build());
+ assertNoPacketReceived(3000);
+ }
+
+ private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
+ return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
+ + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
+ }
+
+ @Test
+ public void northboundAddMapping() throws Exception {
+
+ String pass = "asdf";
+ LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
+ int mask = 32;
+ LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
+
+ // NB add mapping always checks the key
+ lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
+
+ URL url = createPutURL("mapping");
+ String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
+ callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
+
+ // Retrieve the RLOC from the database
+ MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
+ mapRequestBuilder.setPitr(false);
+ mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
+ mapRequestBuilder.getEidRecord().add(
+ new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
+ MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
+
+ LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
+ .getAddress();
+
+ assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
+
+ }
+
+ private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
+ String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ " + "\"proxyMapReply\" : false, "
+ + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : " + "{ " + "\"ipAddress\" : \""
+ + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "}," + "\"mapVersion\" : 0,"
+ + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ " + "\"multicastPriority\" : 1,"
+ + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\"," + "\"afi\" : "
+ + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50," + "\"rlocProbed\" : false, "
+ + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], " + "\"recordTtl\" : 100" + "} " + "], "
+ + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
+
+ return jsonString;
+ }
+
+ @Test
+ public void northboundRetrieveMapping() throws Exception {
+ LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
+ int mask = 32;
+ LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
+
+ // Insert mapping in the database
+ MapRegisterBuilder mapRegister = new MapRegisterBuilder();
+ EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
+ etlr.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
+ etlr.setMaskLength((short) mask);
+ etlr.setRecordTtl(254);
+ etlr.setAuthoritative(false);
+ etlr.setAction(Action.NoAction);
+ LocatorRecordBuilder record = new LocatorRecordBuilder();
+ record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
+ record.setRouted(true);
+ record.setRlocProbed(false);
+ record.setLocalLocator(false);
+ record.setPriority((short) 1);
+ record.setWeight((short) 50);
+ record.setMulticastPriority((short) 1);
+ record.setMulticastWeight((short) 1);
+ etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
+ etlr.getLocatorRecord().add(record.build());
+ mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
+ mapRegister.getEidToLocatorRecord().add(etlr.build());
+ lms.handleMapRegister(mapRegister.build());
+
+ // Get mapping using NB interface. No IID used
+ URL url = createGetMappingIPv4URL(0, eid, mask);
+ String reply = callURL("GET", null, "application/json", null, url);
+ JSONTokener jt = new JSONTokener(reply);
+ JSONObject json = new JSONObject(jt);
+
+ // With just one locator, locators is not a JSONArray
+ String rlocRetrieved = json.getJSONObject("locators").getJSONObject("locatorGeneric").getString("ipAddress");
+
+ assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
+
+ }
+
+ @Test
+ public void northboundRetrieveSourceDestMapping() throws Exception {
+
+ org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
+ .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
+ org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
+ .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
+ int mask1 = 32;
+ int mask2 = 32;
+ LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
+ .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
+ .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
+ .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
+ LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
+
+ // Insert mapping in the database
+ MapRegisterBuilder mapRegister = new MapRegisterBuilder();
+ EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
+ etlr.setLispAddressContainer(LispAFIConvertor.toContainer(sourceDestAddress));
+ etlr.setMaskLength((short) mask1);
+ etlr.setRecordTtl(254);
+ etlr.setAuthoritative(false);
+ etlr.setAction(Action.NoAction);
+ LocatorRecordBuilder record = new LocatorRecordBuilder();
+ record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
+ record.setRouted(true);
+ record.setRlocProbed(false);
+ record.setLocalLocator(false);
+ record.setPriority((short) 1);
+ record.setWeight((short) 50);
+ record.setMulticastPriority((short) 1);
+ record.setMulticastWeight((short) 1);
+ etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
+ etlr.getLocatorRecord().add(record.build());
+ mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
+ mapRegister.getEidToLocatorRecord().add(etlr.build());
+ lms.handleMapRegister(mapRegister.build());
+
+ // Get mapping using NB interface. No IID used
+ URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
+ mask2);
+ String reply = callURL("GET", null, "application/json", null, url);
+ JSONTokener jt = new JSONTokener(reply);
+ JSONObject json = new JSONObject(jt);
+
+ // With just one locator, locators is not a JSONArray
+ String rlocRetrieved = json.getJSONObject("locators").getJSONObject("locatorGeneric").getString("ipAddress");
+
+ assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
+
+ }
+
+ private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
+ String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key", address.getAfi().shortValue(),
+ address.getIpv4Address().getValue(), mask);
+ URL url = new URL(restUrl);
+ return url;
+ }
+
+ private URL createGetKeySourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
+ String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "key", afi, srcAddress, srcMask,
+ dstAddress, dstMask);
+ URL url = new URL(restUrl);
+ return url;
+ }
+
+ private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
+ String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "mapping", afi, srcAddress,
+ srcMask, dstAddress, dstMask);
+ URL url = new URL(restUrl);
+ return url;
+ }
+
+ private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
+ String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping", iid, address.getAfi()
+ .shortValue(), address.getIpv4Address().getValue(), mask);
+ URL url = new URL(restUrl);
+ return url;
+ }
+
+ private URL createPutURL(String resource) throws MalformedURLException {
+
+ String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
+
+ URL url = new URL(restUrl);
+ return url;
+ }
+
+ private String createAuthenticationString() {
+ String authString = "admin:admin";
+ byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
+ String authStringEnc = new String(authEncBytes);
+ return authStringEnc;
+ }
+
+ private String callURL(String method, String content, String accept, String body, URL url) throws IOException, JSONException {
+ String authStringEnc = createAuthenticationString();
+ connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestMethod(method);
+ connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
+ if (content != null) {
+ connection.setRequestProperty("Content-Type", content);
+ }
+ if (accept != null) {
+ connection.setRequestProperty("Accept", accept);
+ }
+ if (body != null) {
+ // now add the request body
+ connection.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
+ wr.write(body);
+ wr.flush();
+ }
+ connection.connect();
+
+ // getting the result, first check response code
+ Integer httpResponseCode = connection.getResponseCode();
+
+ if (httpResponseCode > 299) {
+ logger.trace("HTTP Address: " + url);
+ logger.trace("HTTP Response Code: " + httpResponseCode);
+ fail();
+ }
+
+ InputStream is = connection.getInputStream();
+ BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
+ StringBuilder sb = new StringBuilder();
+ int cp;
+ while ((cp = rd.read()) != -1) {
+ sb.append((char) cp);
+ }
+ is.close();
+ connection.disconnect();
+ return (sb.toString());
+ }
+
+ private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
+ return registerAddressAndQuery(eid, -1);
+ }
+
+ private IConfigLispPlugin configLispPlugin;
+
+ // takes an address, packs it in a MapRegister, sends it, returns the
+ // MapReply
+ private MapReply registerAddressAndQuery(LispAFIAddress eid, int maskLength) throws SocketTimeoutException {
+ MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
+ mapRegisterBuilder.setWantMapNotify(true);
+ mapRegisterBuilder.setKeyId((short) 0);
+ mapRegisterBuilder.setAuthenticationData(new byte[0]);
+ mapRegisterBuilder.setNonce((long) 8);
+ mapRegisterBuilder.setProxyMapReply(false);
+ EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
+ etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));