View Javadoc

1   /*
2    * Copyright (c) 2014 Contextream, 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.implementation.util;
9   
10  import com.google.common.base.Splitter;
11  import java.net.InetAddress;
12  import java.net.UnknownHostException;
13  import java.util.ArrayList;
14  import java.util.Iterator;
15  import java.util.List;
16  import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
17  import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
18  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
19  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
20  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregisternotification.MapRegister;
21  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
22  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
23  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
24  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
25  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
26  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.SiteId;
27  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
28  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
29  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
30  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
31  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
32  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
33  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ApplicationData;
34  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedName;
35  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
36  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
37  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  public final class LispNotificationHelper {
42      protected static final Logger LOG = LoggerFactory.getLogger(LispNotificationHelper.class);
43      private static final Splitter COLON_SPLITTER = Splitter.on(':');
44  
45      // Utility class, should not be instantiated
46      private LispNotificationHelper() {
47      }
48  
49      public static TransportAddress getTransportAddressFromRloc(Rloc rloc) {
50          TransportAddressBuilder tab = new TransportAddressBuilder();
51          Address address = rloc.getAddress();
52          if (address instanceof Ipv4) {
53              tab.setIpAddress(IpAddressBuilder.getDefaultInstance(((Ipv4) address).getIpv4().getValue()));
54              tab.setPort(new PortNumber(LispMessage.PORT_NUM));
55          } else if (address instanceof Ipv6) {
56              tab.setIpAddress(IpAddressBuilder.getDefaultInstance(((Ipv6) address).getIpv6().getValue()));
57              tab.setPort(new PortNumber(LispMessage.PORT_NUM));
58          } else if (address instanceof KeyValueAddress) {
59              SimpleAddress sa = ((KeyValueAddress) address).getKeyValueAddress().getValue();
60              if (sa.getDistinguishedNameType() != null) {
61                  final Iterator<String> it = COLON_SPLITTER.split(sa.getDistinguishedNameType().getValue()).iterator();
62                  String ip = it.next();
63                  int port = Integer.valueOf(it.next());
64  
65                  tab.setIpAddress(IpAddressBuilder.getDefaultInstance(ip));
66                  tab.setPort(new PortNumber(port));
67              }
68          } else if (address instanceof DistinguishedName) {
69              DistinguishedName dname = (DistinguishedName) address;
70              final Iterator<String> it = COLON_SPLITTER.split(dname.getDistinguishedName().getValue()).iterator();
71              String ip = it.next();
72              int port = Integer.valueOf(it.next());
73  
74              tab.setIpAddress(IpAddressBuilder.getDefaultInstance(ip));
75              tab.setPort(new PortNumber(port));
76          } else if (address instanceof ApplicationData) {
77              ApplicationData appData = (ApplicationData) address;
78              tab.setIpAddress(appData.getApplicationData().getAddress().getIpAddress());
79              tab.setPort(new PortNumber(appData.getApplicationData().getLocalPortLow()));
80          }
81          return tab.build();
82      }
83  
84      public static InetAddress getAddressByName(String IPAddress) {
85          try {
86              InetAddress address = InetAddress.getByName(IPAddress);
87              return address;
88          } catch (UnknownHostException e) {
89              LOG.debug("Unknown host {}", IPAddress, e);
90              return null;
91          }
92      }
93  
94      public static List<Mapping> getMapping(AddMapping mapRegisterNotification) {
95          List<Mapping> mappings = new ArrayList<Mapping>();
96          for (int i=0; i<mapRegisterNotification.getMapRegister().getMappingRecordItem().size(); i++) {
97              MappingRecord record = mapRegisterNotification.getMapRegister().getMappingRecordItem().get(i).getMappingRecord();
98              MappingBuilder mb = new MappingBuilder();
99              mb.setEidUri(new EidUri(LispAddressStringifier.getURIString(
100                     record.getEid())));
101             mb.setOrigin(MappingOrigin.Southbound);
102             mb.setSiteId(getSiteId(mapRegisterNotification.getMapRegister()));
103             mb.setMappingRecord(record);
104             mappings.add(mb.build());
105         }
106         return mappings;
107     }
108 
109     public static List<SiteId> getSiteId(MapRegister mapRegister) {
110         if (mapRegister.isXtrSiteIdPresent()) {
111             List<SiteId> siteIds = new ArrayList<SiteId>();
112             SiteId siteId = new SiteId(mapRegister.getSiteId());
113             siteIds.add(siteId);
114             return siteIds;
115         } else {
116             return null;
117         }
118     }
119 }