View Javadoc

1   /*
2    * Copyright (c) 2014 Cisco Systems, Inc. and others.  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   package org.opendaylight.lispflowmapping.lisp.util;
9   
10  import com.google.common.net.InetAddresses;
11  import java.net.InetAddress;
12  import java.util.ArrayList;
13  import java.util.Random;
14  
15  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
16  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
17  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
18  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
19  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
20  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
21  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
22  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
23  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
24  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  public final class MapRequestUtil {
29      protected static final Logger LOG = LoggerFactory.getLogger(MapRequestUtil.class);
30  
31      // Utility class, should not be instantiated
32      private MapRequestUtil() {
33      }
34  
35      public static InetAddress selectItrRloc(MapRequest request) {
36          if (request.getItrRloc() == null) {
37              return null;
38          }
39          InetAddress selectedItrRloc = null;
40          for (ItrRloc itr : request.getItrRloc()) {
41              Address addr = itr.getRloc().getAddress();
42              if (addr instanceof Ipv4) {
43                  selectedItrRloc = InetAddresses.forString(((Ipv4) addr).getIpv4().getValue());
44                  break;
45              }
46              if (addr instanceof Ipv6) {
47                  selectedItrRloc = InetAddresses.forString(((Ipv6) addr).getIpv6().getValue());
48                  break;
49              }
50          }
51          return selectedItrRloc;
52      }
53  
54      public static MapRequestBuilder prepareSMR(Eid srcEid, Rloc itrRloc) {
55          MapRequestBuilder builder = new MapRequestBuilder();
56          builder.setAuthoritative(false);
57          builder.setMapDataPresent(false);
58          builder.setPitr(false);
59          builder.setProbe(false);
60          builder.setSmr(true);
61          builder.setSmrInvoked(false);
62  
63          builder.setSourceEid(new SourceEidBuilder().setEid(srcEid).build());
64          builder.setItrRloc(new ArrayList<ItrRloc>());
65          builder.getItrRloc().add(new ItrRlocBuilder().setRloc(itrRloc).build());
66          builder.setMapReply(null);
67          builder.setNonce(new Random().nextLong());
68  
69          return builder;
70      }
71  
72  }