2 * Copyright (c) 2014 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.neutron;
11 import java.net.HttpURLConnection;
12 import java.util.List;
13 import java.util.concurrent.Future;
15 import org.apache.commons.lang3.exception.ExceptionUtils;
16 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
17 import org.opendaylight.neutron.spi.INeutronPortAware;
18 import org.opendaylight.neutron.spi.NeutronPort;
19 import org.opendaylight.neutron.spi.Neutron_IPs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.locatorrecords.LocatorRecord;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
30 * Lisp Service implementation of NeutronPortAware API Creation of a new port
31 * results adding the mapping for the port's IP addresses to the port's host_ip
32 * in the mapping service. Currently the NetronPort object does not carry the
33 * required information to achieve the port's host_ip. Once such extension is
34 * available this class shall be updated.
36 * @author Vina Ermagan
40 public class LispNeutronPortHandler extends LispNeutronService implements
43 // The implementation for each of these services is resolved by the OSGi
45 private volatile ILispNeutronService lispNeutronService;
48 public int canCreatePort(NeutronPort port) {
49 LOG.info("Neutron canCreatePort : Port name: " + port.getName());
51 return HttpURLConnection.HTTP_OK;
55 public void neutronPortCreated(NeutronPort port) {
57 // TODO Consider adding Port MAC -> Port fixed IP in MS
58 // TODO Add Port fixed ip -> host ip , if Port.host_id mapping to
59 // host_ip exists in MS
61 LOG.debug("Neutron Port Created : " + port.toString());
63 // Check if port.hostID is in map-server, if it is, get host eidtoloc
65 if (port.getBindinghostID() == null) {
66 LOG.error("Adding new Neutron port to lisp service mapping service failed. Port does not have Host_ID. Port : "
70 LispAFIAddress hostAddress = LispAFIConvertor.asDistinguishedNameAddress(port.getBindinghostID());
72 EidToLocatorRecord eidRecord;
73 List<LocatorRecord> hostLocRecords;
74 GetMappingInput input = LispUtil.buildGetMappingInput(LispAFIConvertor.toContainer(hostAddress), (short) 0);
76 LfmMappingDatabaseService lfmdb = lispNeutronService.getMappingDbService();
78 LOG.debug("lfmdb is null!!!");
81 Future<RpcResult<GetMappingOutput>> result = lfmdb.getMapping(input);
82 GetMappingOutput output = result.get().getResult();
84 // TODO for now only selecting the first EidToLocatorRecord from the
87 eidRecord = output.getEidToLocatorRecord().get(0);
88 hostLocRecords = eidRecord.getLocatorRecord();
89 LOG.debug("hostLocRecords is : {}",hostLocRecords);
92 } catch (Exception e) {
93 LOG.warn("Failed to GET mapping for EID {}: , mappingInput: {} , Exception: {}", hostAddress, input,
94 ExceptionUtils.getStackTrace(e));
98 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
99 if (fixedIPs != null && fixedIPs.size() > 0) {
100 LispAddressContainer eidAddress;
101 for (Neutron_IPs ip : fixedIPs) {
103 // TODO Add check/support for IPv6.
104 // Get subnet for this port, based on v4 or v6 decide address
107 eidAddress = LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ip.getIpAddress()));
108 lispNeutronService.getMappingDbService().addMapping(LispUtil.buildAddMappingInput(eidAddress, hostLocRecords, 32));
112 LOG.info("Neutron Port Created: Port name: "
115 + (port.getFixedIPs() != null ? port.getFixedIPs().get(0)
116 : "No Fixed IP assigned"));
121 public int canUpdatePort(NeutronPort delta, NeutronPort original) {
122 // TODO Change of Fixed IPs are not allowed as we are storing ports by
125 if (original.getFixedIPs().equals(original.getFixedIPs())) {
126 LOG.info("Neutron canUpdatePort : Port name: "
129 + (original.getFixedIPs() != null ? original.getFixedIPs()
130 .get(0) : "No Fixed IP assigned")
131 + "New Port Fixed IP: "
132 + (delta.getFixedIPs() != null ? delta.getFixedIPs().get(0)
133 : "No Fixed IP assigned"));
134 LOG.debug("Neutron canUpdatePort : original" + original.toString()
135 + " delta : " + delta.toString());
137 return HttpURLConnection.HTTP_OK;
139 return HttpURLConnection.HTTP_NOT_IMPLEMENTED;
143 public void neutronPortUpdated(NeutronPort port) {
144 // TODO Port IP and port's host ip is stored by Lisp Neutron Service. If
145 // there is change to these fields, the update needs to be processed.
147 LOG.info("Neutron Port updated: Port name: "
150 + (port.getFixedIPs() != null ? port.getFixedIPs().get(0)
151 : "No Fixed IP assigned"));
152 LOG.debug("Neutron Port Updated : " + port.toString());
157 public int canDeletePort(NeutronPort port) {
158 // TODO Check if Port IPs are stored by Lisp Neutron Service. if not
159 // return error code.
161 LOG.info("Neutron canDeletePort : Port name: "
164 + (port.getFixedIPs() != null ? port.getFixedIPs().get(0)
165 : "No Fixed IP assigned"));
166 LOG.debug("Neutron canDeltePort: " + port.toString());
168 return HttpURLConnection.HTTP_OK;
172 public void neutronPortDeleted(NeutronPort port) {
173 // TODO if port ips existed in MapServer, delete them. Else, log error.
175 LOG.info("Neutron Port Deleted: Port name: "
178 + (port.getFixedIPs() != null ? port.getFixedIPs().get(0)
179 : "No Fixed IP assigned"));
180 LOG.debug("Neutron Port Deleted : " + port.toString());
182 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
183 if (fixedIPs != null && fixedIPs.size() > 0) {
184 LispAddressContainer eidAddress;
186 for (Neutron_IPs ip : fixedIPs) {
188 // TODO Add check/support for IPv6.
189 // Get subnet for this port, based on v4 or v6 decide address
192 eidAddress = LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ip.getIpAddress()));
193 lispNeutronService.getMappingDbService().removeMapping(LispUtil.buildRemoveMappingInput(eidAddress,32));
195 LOG.info("Neutron Port mapping deleted from lisp: "
196 + " Port Fixed IP: " + ip + "Port host IP: ");