Migrate implementation/neutron/southbound to IETF YANG model
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / LispNeutronSubnetHandler.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.lispflowmapping.neutron;
10
11 import java.net.HttpURLConnection;
12
13 import org.apache.commons.lang3.exception.ExceptionUtils;
14 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
15 import org.opendaylight.neutron.spi.INeutronSubnetAware;
16 import org.opendaylight.neutron.spi.NeutronSubnet;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
18
19 /**
20  * Lisp Service implementation of NeutronSubnetAware API Creation of a new
21  * Subnet results in defining the subnet as an EID prefix in the LISP Mapping
22  * System with subnet's network UUID as the key to use for registering mappings
23  * for the subnet.
24  *
25  * @author Vina Ermagan
26  *
27  */
28 public class LispNeutronSubnetHandler extends LispNeutronService implements
29                 INeutronSubnetAware {
30         private static final Integer SIX = Integer.valueOf(6);
31
32         // The implementation for each of these services is resolved by the OSGi
33         // Service Manager
34         private volatile ILispNeutronService lispNeutronService;
35
36         @Override
37         public int canCreateSubnet(NeutronSubnet subnet) {
38                 LOG.info("Neutron canCreateSubnet : Subnet name: " + subnet.getName()
39                                 + " Subnet Cidr: " + subnet.getCidr());
40                 LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
41                 if (SIX.equals(subnet.getIpVersion())) {
42                         return HttpURLConnection.HTTP_PARTIAL;
43                 }
44                 return HttpURLConnection.HTTP_OK;
45         }
46
47         /**
48          * Method adds the newly created subnet as an EID prefix to the
49          * MappingService. The subnet's network UUID is used as the key for this EID
50          * prefix.
51          *
52          */
53         @Override
54         public void neutronSubnetCreated(NeutronSubnet subnet) {
55                 // TODO update for multi-tenancy
56
57                 LOG.info("Neutron Subnet Created request : Subnet name: "
58                                 + subnet.getName() + " Subnet Cidr: " + subnet.getCidr());
59                 LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
60
61                 // Determine the IANA code for the subnet IP version
62                 // Default is set to IPv4 for neutron subnets
63
64                 Eid eid = LispAddressUtil.asIpv4PrefixEid(subnet.getCidr());
65
66                 try {
67
68                     lispNeutronService.getMappingDbService().addKey(LispUtil.buildAddKeyInput(eid, subnet.getNetworkUUID()));
69
70                         LOG.debug("Neutron Subnet Added to MapServer : Subnet name: "
71                                         + subnet.getName() + " EID Prefix: "
72                                         + subnet.getCidr() + " Key: "
73                                         + subnet.getNetworkUUID());
74                 } catch (Exception e) {
75                         LOG.error("Adding new subnet to lisp service mapping service failed. Subnet : "
76                                         + subnet.toString() + "Error: " + ExceptionUtils.getStackTrace(e));
77                 }
78         LOG.info("Neutron Subnet Created request : Subnet name: "
79                 + subnet.getName() + " Subnet Cidr: " + subnet.getCidr());
80
81         }
82
83         /**
84          * Method to check whether new Subnet can be created by LISP implementation
85          * of Neutron service API. Since we store the Cidr part of the subnet as the
86          * main key to the Lisp mapping service, we do not support updates to
87          * subnets that change it's cidr.
88          */
89         @Override
90         public int canUpdateSubnet(NeutronSubnet delta, NeutronSubnet original) {
91                 if (delta == null || original == null) {
92                         LOG.error("Neutron canUpdateSubnet rejected: subnet objects were null");
93                         return HttpURLConnection.HTTP_BAD_REQUEST;
94                 }
95                 LOG.info("Neutron canUpdateSubnet : Subnet name: " + original.getName()
96                                 + " Subnet Cidr: " + original.getCidr());
97                 LOG.debug("Lisp Neutron Subnet update: original : "
98                                 + original.toString() + " delta : " + delta.toString());
99
100                 // We do not accept a Subnet update that changes the cidr. If cider or
101                 // network UUID is changed, return error.
102                 if (!(original.getCidr().equals(delta.getCidr()))) {
103                         LOG.error("Neutron canUpdateSubnet rejected: Subnet name: "
104                                         + original.getName() + " Subnet Cidr: "
105                                         + original.getCidr());
106                         return HttpURLConnection.HTTP_CONFLICT;
107                 }
108                 return HttpURLConnection.HTTP_OK;
109         }
110
111         @Override
112         public void neutronSubnetUpdated(NeutronSubnet subnet) {
113                 // Nothing to do here.
114         }
115
116         /**
117          * Method to check if subnet can be deleted. Returns error only if subnet
118          * does not exist in the lisp mapping service.
119          */
120         @Override
121         public int canDeleteSubnet(NeutronSubnet subnet) {
122                 LOG.info("Neutron canDeleteSubnet : Subnet name: " + subnet.getName()
123                                 + " Subnet Cidr: " + subnet.getCidr() + "Key: "
124                                 + subnet.getNetworkUUID());
125                 LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
126
127
128                         return HttpURLConnection.HTTP_OK;
129         }
130
131         /**
132          * Method removes the EID prefix and key associated with the deleted subnet
133          * from Lisp mapping service.
134          */
135         @Override
136         public void neutronSubnetDeleted(NeutronSubnet subnet) {
137                 LOG.info("Neutron Subnet Deleted Request : Subnet name: "
138                                 + subnet.getName() + " Subnet Cidr: " + subnet.getCidr()
139                                 + "Key: " + subnet.getNetworkUUID());
140                 LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
141
142                 // Determine the IANA code for the subnet IP version
143                 // Default is set to IPv4 for neutron subnets
144
145                 Eid eid = LispAddressUtil.asIpv4PrefixEid(subnet.getCidr());
146
147         try {
148
149             lispNeutronService.getMappingDbService().removeKey(LispUtil.buildRemoveKeyInput(eid));
150
151                         LOG.debug("Neutron Subnet Deleted from MapServer : Subnet name: "
152                                         + subnet.getName() + " Eid Prefix: "
153                                         + subnet.getCidr() + " Key: "
154                                         + subnet.getNetworkUUID());
155                 } catch (Exception e) {
156                         LOG.error("Deleting subnet's EID prefix from mapping service failed + Subnet: "
157                                         + subnet.toString() + "Error: " + ExceptionUtils.getStackTrace(e));
158                 }
159         }
160
161 }