Fixup Augmentable and Identifiable methods changing
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronSubnetChangeListener.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.netvirt.neutronvpn;
9
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkAttributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMap;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMapBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMapKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Singleton
37 public class NeutronSubnetChangeListener extends AsyncDataTreeChangeListenerBase<Subnet, NeutronSubnetChangeListener> {
38     private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
39     private final DataBroker dataBroker;
40     private final NeutronvpnManager nvpnManager;
41     private final NeutronExternalSubnetHandler externalSubnetHandler;
42     private final NeutronvpnUtils neutronvpnUtils;
43
44     @Inject
45     public NeutronSubnetChangeListener(final DataBroker dataBroker, final NeutronvpnManager neutronvpnManager,
46             final NeutronExternalSubnetHandler externalSubnetHandler, final NeutronvpnUtils neutronvpnUtils) {
47         super(Subnet.class, NeutronSubnetChangeListener.class);
48         this.dataBroker = dataBroker;
49         this.nvpnManager = neutronvpnManager;
50         this.externalSubnetHandler = externalSubnetHandler;
51         this.neutronvpnUtils = neutronvpnUtils;
52     }
53
54     @Override
55     @PostConstruct
56     public void init() {
57         LOG.info("{} init", getClass().getSimpleName());
58         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
59     }
60
61     @Override
62     protected InstanceIdentifier<Subnet> getWildCardPath() {
63         return InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet.class);
64     }
65
66     @Override
67     protected NeutronSubnetChangeListener getDataTreeChangeListener() {
68         return NeutronSubnetChangeListener.this;
69     }
70
71
72     @Override
73     protected void add(InstanceIdentifier<Subnet> identifier, Subnet input) {
74         LOG.trace("Adding Subnet : key: {}, value={}", identifier, input);
75         Uuid networkId = input.getNetworkId();
76         Uuid subnetId = input.getUuid();
77         Network network = neutronvpnUtils.getNeutronNetwork(networkId);
78         if (network == null || !NeutronvpnUtils.isNetworkTypeSupported(network)) {
79             LOG.warn("neutron vpn received a subnet add() for a network without a provider extension augmentation "
80                             + "or with an unsupported network type for the subnet {} which is part of network {}",
81                     subnetId.getValue(), network);
82             return;
83         }
84         neutronvpnUtils.addToSubnetCache(input);
85         handleNeutronSubnetCreated(input, network);
86         externalSubnetHandler.handleExternalSubnetAdded(network, subnetId, null);
87     }
88
89     @Override
90     protected void remove(InstanceIdentifier<Subnet> identifier, Subnet input) {
91         LOG.trace("Removing subnet : key: {}, value={}", identifier, input);
92         Uuid networkId = input.getNetworkId();
93         Uuid subnetId = input.getUuid();
94         Network network = neutronvpnUtils.getNeutronNetwork(networkId);
95         if (network == null || !NeutronvpnUtils.isNetworkTypeSupported(network)) {
96             LOG.warn("neutron vpn received a subnet remove() for a network without a provider extension augmentation "
97                             + "or with an unsupported network type for the subnet {} which is part of network {}",
98                     subnetId.getValue(), network);
99             return;
100         }
101         handleNeutronSubnetDeleted(subnetId, networkId);
102         externalSubnetHandler.handleExternalSubnetRemoved(network, subnetId);
103         neutronvpnUtils.removeFromSubnetCache(input);
104     }
105
106     @Override
107     protected void update(InstanceIdentifier<Subnet> identifier, Subnet original, Subnet update) {
108         LOG.trace("Updating Subnet : key: {}, original value={}, update value={}", identifier, original, update);
109         neutronvpnUtils.addToSubnetCache(update);
110     }
111
112     private void handleNeutronSubnetCreated(Subnet subnet, Network network) {
113         Uuid networkId = network.getUuid();
114         Uuid subnetId = subnet.getUuid();
115         ProviderTypes providerType = NeutronvpnUtils.getProviderNetworkType(network);
116         String segmentationId = NeutronvpnUtils.getSegmentationIdFromNeutronNetwork(network);
117         nvpnManager.createSubnetmapNode(subnetId, String.valueOf(subnet.getCidr().getValue()),
118                 subnet.getTenantId(), networkId,
119                 providerType != null ? NetworkAttributes.NetworkType.valueOf(providerType.getName()) : null,
120                 segmentationId != null ? Long.parseLong(segmentationId) : 0L);
121         createSubnetToNetworkMapping(subnetId, networkId);
122     }
123
124     private void handleNeutronSubnetDeleted(Uuid subnetId, Uuid networkId) {
125         Uuid vpnId = neutronvpnUtils.getVpnForNetwork(networkId);
126         if (vpnId != null) {
127             nvpnManager.removeSubnetFromVpn(vpnId, subnetId, null /* internet-vpn-id */);
128         }
129         if (networkId != null) {
130             deleteSubnetToNetworkMapping(subnetId, networkId);
131         }
132         nvpnManager.deleteSubnetMapNode(subnetId);
133     }
134
135     // TODO Clean up the exception handling
136     @SuppressWarnings("checkstyle:IllegalCatch")
137     private void createSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
138         try {
139             InstanceIdentifier<NetworkMap>  networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
140             Optional<NetworkMap> optionalNetworkMap = SingleTransactionDataBroker.syncReadOptional(dataBroker,
141                     LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
142             NetworkMapBuilder nwMapBuilder = null;
143             if (optionalNetworkMap.isPresent()) {
144                 nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
145             } else {
146                 nwMapBuilder = new NetworkMapBuilder().withKey(new NetworkMapKey(networkId)).setNetworkId(networkId);
147                 LOG.debug("Adding a new network node in NetworkMaps DS for network {}", networkId.getValue());
148             }
149             List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
150             if (subnetIdList == null) {
151                 subnetIdList = new ArrayList<>();
152             }
153             subnetIdList.add(subnetId);
154             nwMapBuilder.setSubnetIdList(subnetIdList);
155             MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier, nwMapBuilder
156                     .build());
157             LOG.debug("Created subnet-network mapping for subnet {} network {}", subnetId.getValue(),
158                     networkId.getValue());
159         } catch (ReadFailedException | RuntimeException e) {
160             LOG.error("Create subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
161                     networkId.getValue());
162         }
163     }
164
165     // TODO Clean up the exception handling
166     @SuppressWarnings("checkstyle:IllegalCatch")
167     private void deleteSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
168         try {
169             InstanceIdentifier<NetworkMap>  networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
170             Optional<NetworkMap> optionalNetworkMap = SingleTransactionDataBroker.syncReadOptional(dataBroker,
171                     LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
172             if (optionalNetworkMap.isPresent()) {
173                 NetworkMapBuilder nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
174                 List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
175                 if (subnetIdList.remove(subnetId)) {
176                     if (subnetIdList.isEmpty()) {
177                         MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
178                         LOG.debug("Deleted network node in NetworkMaps DS for subnet {} network {}",
179                                 subnetId.getValue(), networkId.getValue());
180                     } else {
181                         nwMapBuilder.setSubnetIdList(subnetIdList);
182                         MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier,
183                                 nwMapBuilder.build());
184                         LOG.debug("Deleted subnet-network mapping for subnet {} network {}", subnetId.getValue(),
185                                 networkId.getValue());
186                     }
187                 } else {
188                     LOG.error("Subnet {} is not mapped to network {}", subnetId.getValue(), networkId.getValue());
189                 }
190             } else {
191                 LOG.error("network {} not present for subnet {} ", networkId, subnetId);
192             }
193         } catch (ReadFailedException | RuntimeException e) {
194             LOG.error("Delete subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
195                     networkId.getValue());
196         }
197     }
198 }
199