Reenable Neutron using YANG models
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / SubnetDataProcessor.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.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
12 import org.apache.commons.lang3.exception.ExceptionUtils;
13 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Lisp Service implementation of creation and deletion of a Subnet results
25  * in defining the subnet as an EID prefix in the LISP Mapping System with
26  * subnet's network UUID as the key to use for registering mappings for the subnet.
27  *
28  * @author Vina Ermagan
29  *
30  */
31 public class SubnetDataProcessor implements DataProcessor<Subnet> {
32     private static final Logger LOG = LoggerFactory.getLogger(SubnetDataProcessor.class);
33 //    private static final Integer SIX = Integer.valueOf(6);
34
35     // The implementation for each of these services is resolved by the OSGi
36     // Service Manager
37     private volatile ILispNeutronService lispNeutronService;
38
39     public SubnetDataProcessor(ILispNeutronService lispNeutronService) {
40         this.lispNeutronService = lispNeutronService;
41     }
42
43     /**
44      * Method adds the newly created subnet as an EID prefix to the
45      * MappingService. The subnet's network UUID is used as the key for this EID
46      * prefix.
47      */
48     @Override
49     public void create(Subnet subnet) {
50         // TODO update for multi-tenancy
51         LOG.info("Neutron Subnet Created request : Subnet name: "
52                 + subnet.getName() + " Subnet Cidr: " + subnet.getCidr());
53         LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
54
55         // Determine the IANA code for the subnet IP version
56         // Default is set to IPv4 for neutron subnets
57         final Eid eid = LispAddressUtil.asIpv4PrefixEid(String.valueOf(subnet.getCidr().getValue()));
58
59         try {
60             final OdlMappingserviceService lfmdb = lispNeutronService.getMappingDbService();
61             if (lfmdb == null) {
62                 LOG.debug("lfmdb is null!!!");
63                 return;
64             }
65             final AddKeyInput addKeyInput = LispUtil.buildAddKeyInput(eid, subnet.getUuid().getValue());
66             final Future<RpcResult<Void>> result = lfmdb.addKey(addKeyInput);
67             final Boolean isSuccessful = result.get().isSuccessful();
68
69             if (isSuccessful) {
70                 LOG.debug("Neutron Subnet Added to MapServer : Subnet name: "
71                         + subnet.getName() + " EID Prefix: "
72                         + subnet.getCidr() + " Key: "
73                         + subnet.getUuid());
74             }
75             LOG.info("Neutron Subnet Created request : Subnet name: "
76                     + subnet.getName() + " Subnet Cidr: " + subnet.getCidr());
77         } catch (InterruptedException | ExecutionException e) {
78             LOG.error("Adding new subnet to lisp service mapping service failed. Subnet : "
79                     + subnet.toString() + "Error: " + ExceptionUtils.getStackTrace(e));
80         }
81     }
82
83     @Override
84     public void update(Subnet subnet) {
85         // Nothing to do here.
86     }
87
88     /**
89      * Method removes the EID prefix and key associated with the deleted subnet
90      * from Lisp mapping service.
91      */
92     @Override
93     public void delete(Subnet subnet) {
94         LOG.info("Neutron Subnet Deleted Request : Subnet name: "
95                 + subnet.getName() + " Subnet Cidr: " + subnet.getCidr()
96                 + "Key: " + subnet.getUuid());
97         LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
98
99         // Determine the IANA code for the subnet IP version
100         // Default is set to IPv4 for neutron subnets
101         final Eid eid = LispAddressUtil.asIpv4PrefixEid(String.valueOf(subnet.getCidr().getValue()));
102
103         try {
104             final OdlMappingserviceService lfmdb = lispNeutronService.getMappingDbService();
105             if (lfmdb == null) {
106                 LOG.debug("lfmdb is null!!!");
107                 return;
108             }
109             final RemoveKeyInput removeKeyInput = LispUtil.buildRemoveKeyInput(eid);
110             final Future<RpcResult<Void>> result = lfmdb.removeKey(removeKeyInput);
111             final Boolean isSuccessful = result.get().isSuccessful();
112
113             if (isSuccessful) {
114                 LOG.debug("Neutron Subnet Deleted from MapServer : Subnet name: "
115                         + subnet.getName() + " Eid Prefix: "
116                         + subnet.getCidr() + " Key: "
117                         + subnet.getUuid());
118             }
119         } catch (InterruptedException | ExecutionException e) {
120             LOG.error("Deleting subnet's EID prefix from mapping service failed + Subnet: "
121                     + subnet.toString() + "Error: " + ExceptionUtils.getStackTrace(e));
122         }
123     }
124 }