Deprecate mappingservice.neutron
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / PortDataProcessor.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.lispflowmapping.neutron;
9
10 import java.util.Map;
11 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
12 import org.opendaylight.lispflowmapping.neutron.mappingmanager.HostInformationManager;
13 import org.opendaylight.lispflowmapping.neutron.mappingmanager.PortData;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.PortBindingExtension;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIpsKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Lisp Service implementation of NeutronPortAware API Creation of a new port
25  * results adding the mapping for the port's IP addresses to the port's host_ip
26  * in the mapping service. Currently the NetronPort object does not carry the
27  * required information to achieve the port's host_ip. Once such extension is
28  * available this class shall be updated.
29  *
30  * @author Vina Ermagan
31  */
32 @Deprecated
33 public class PortDataProcessor implements DataProcessor<Port> {
34     private static final Logger LOG = LoggerFactory.getLogger(PortDataProcessor.class);
35
36     // The implementation for each of these services is resolved by the OSGi
37     // Service Manager
38     private volatile ILispNeutronService lispNeutronService;
39
40     private final HostInformationManager hostInformationManager = HostInformationManager.getInstance();
41
42     public PortDataProcessor(ILispNeutronService lispNeutronService) {
43         this.lispNeutronService = lispNeutronService;
44     }
45
46     @Override
47     public void create(Port port) {
48         // TODO Consider adding Port MAC -> Port fixed IP in MS
49         // host_ip exists in MS
50
51         LOG.debug("Neutron Port Created : " + port.toString());
52
53         final String hostId = port.augmentation(PortBindingExtension.class).getHostId();
54         if (hostId == null) {
55             LOG.error("Adding new Neutron port to lisp mapping service failed. Port does not have a HostID. Port: {}",
56                     port.toString());
57             return;
58         }
59
60         Map<FixedIpsKey, FixedIps> fixedIPs = port.nonnullFixedIps();
61         if (fixedIPs != null && fixedIPs.size() > 0) {
62             Eid eidAddress;
63             for (FixedIps ip : fixedIPs.values()) {
64
65                 // TODO Add check/support for IPv6.
66                 // Get subnet for this port, based on v4 or v6 decide address
67                 // iana code.
68                 eidAddress = getEid(port, ip);
69                 PortData portData = new PortData(port.getUuid().getValue(), eidAddress);
70                 hostInformationManager.addHostRelatedInfo(hostId, portData);
71             }
72         }
73
74         LOG.info("Neutron Port Created: Port name: "
75                 + port.getName()
76                 + " Port Fixed IP: "
77                 + (port.getFixedIps() != null ? port.getFixedIps().values().iterator().next()
78                 : "No Fixed IP assigned"));
79     }
80
81     private Eid getEid(Port port, FixedIps ip) {
82         InstanceIdType vni = new InstanceIdType(hostInformationManager
83                                                     .getInstanceId(port.getTenantId().getValue()));
84         return LispAddressUtil.asIpv4PrefixEid(ip.getIpAddress().getIpv4Address(), vni);
85     }
86
87     @Override
88     public void update(Port port) {
89
90         final String hostId = port.augmentation(PortBindingExtension.class).getHostId();
91         if (hostId == null) {
92             LOG.error("Updating port to lisp mapping service failed. Port does not have a HostID. Port: {}",
93                     port.toString());
94             return;
95         }
96
97         Map<FixedIpsKey, FixedIps> fixedIPs = port.getFixedIps();
98         if (fixedIPs != null && fixedIPs.size() > 0) {
99             Eid eidAddress;
100             for (FixedIps ip : fixedIPs.values()) {
101
102                 eidAddress = getEid(port, ip);
103
104                 PortData portData = new PortData(port.getUuid().getValue(), eidAddress);
105                 hostInformationManager.addHostRelatedInfo(hostId, portData);
106             }
107         }
108
109         LOG.info("Neutron Port updated: Port name: "
110                 + port.getName()
111                 + " Port Fixed IP: "
112                 + (port.getFixedIps() != null ? port.getFixedIps().values().iterator().next()
113                 : "No Fixed IP assigned"));
114         LOG.debug("Neutron Port Updated : " + port.toString());
115     }
116
117     @Override
118     public void delete(Port port) {
119         // TODO if port ips existed in MapServer, delete them. Else, log error.
120
121         LOG.info("Neutron Port Deleted: Port name: "
122                 + port.getName()
123                 + " Port Fixed IP: "
124                 + (port.getFixedIps() != null ? port.getFixedIps().values().iterator().next()
125                 : "No Fixed IP assigned"));
126         LOG.debug("Neutron Port Deleted : " + port.toString());
127
128         Map<FixedIpsKey, FixedIps> fixedIPs = port.getFixedIps();
129         if (fixedIPs != null && fixedIPs.size() > 0) {
130             Eid eidAddress;
131
132             for (FixedIps ip : fixedIPs.values()) {
133
134                 // TODO Add check/support for IPv6.
135                 // Get subnet for this port, based on v4 or v6 decide address
136                 // iana code.
137
138                 eidAddress = LispAddressUtil.asIpv4PrefixEid(ip.getIpAddress().getIpv4Address().getValue() + "/32");
139                 lispNeutronService.getMappingDbService().removeMapping(LispUtil.buildRemoveMappingInput(eidAddress));
140
141                 LOG.info("Neutron Port mapping deleted from lisp: "
142                         + " Port Fixed IP: " + ip + "Port host IP: ");
143
144             }
145         }
146     }
147 }