Remove redundant names in paths
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / ExternalNetworkGroupInstaller.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.natservice.internal;
10
11 import com.google.common.base.Strings;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.List;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.genius.mdsalutil.ActionInfo;
21 import org.opendaylight.genius.mdsalutil.BucketInfo;
22 import org.opendaylight.genius.mdsalutil.GroupEntity;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
25 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldEthernetDestination;
26 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
27 import org.opendaylight.netvirt.elanmanager.api.IElanService;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Singleton
38 public class ExternalNetworkGroupInstaller {
39     private static final Logger LOG = LoggerFactory.getLogger(ExternalNetworkGroupInstaller.class);
40     private final DataBroker broker;
41     private final IMdsalApiManager mdsalManager;
42     private final IElanService elanService;
43     private final IdManagerService idManager;
44     private final OdlInterfaceRpcService interfaceManager;
45
46     @Inject
47     public ExternalNetworkGroupInstaller(final DataBroker broker, final IMdsalApiManager mdsalManager,
48                                      final IElanService elanService, final IdManagerService idManager,
49                                      final OdlInterfaceRpcService interfaceManager) {
50         this.broker = broker;
51         this.mdsalManager = mdsalManager;
52         this.elanService = elanService;
53         this.idManager = idManager;
54         this.interfaceManager = interfaceManager;
55     }
56
57     public void installExtNetGroupEntries(Subnetmap subnetMap) {
58         if (subnetMap == null) {
59             LOG.error("installExtNetGroupEntries : Subnetmap is null");
60             return;
61         }
62
63         if (NatUtil.isIPv6Subnet(subnetMap.getSubnetIp())) {
64             LOG.debug("installExtNetGroupEntries : Subnet id {} is not an IPv4 subnet, hence skipping.",
65                     subnetMap.getId());
66             return;
67         }
68
69         Uuid networkId = subnetMap.getNetworkId();
70         Uuid subnetId = subnetMap.getId();
71         if (networkId == null) {
72             LOG.error("installExtNetGroupEntries : No network associated subnet id {}", subnetId.getValue());
73             return;
74         }
75
76         String macAddress = NatUtil.getSubnetGwMac(broker, subnetId, networkId.getValue());
77         installExtNetGroupEntries(subnetMap, macAddress);
78     }
79
80     public void installExtNetGroupEntries(Uuid subnetId, String macAddress) {
81         Subnetmap subnetMap = NatUtil.getSubnetMap(broker, subnetId);
82         if (subnetMap == null) {
83             LOG.error("installExtNetGroupEntries : Subnetmap is null");
84             return;
85         }
86
87         if (NatUtil.isIPv6Subnet(subnetMap.getSubnetIp())) {
88             LOG.debug("installExtNetGroupEntries : Subnet-id {} is not an IPv4 subnet, hence skipping.",
89                     subnetMap.getId());
90             return;
91         }
92         installExtNetGroupEntries(subnetMap, macAddress);
93     }
94
95     public void installExtNetGroupEntries(Uuid networkId, BigInteger dpnId) {
96         if (networkId == null) {
97             return;
98         }
99
100         List<Uuid> subnetIds = NatUtil.getSubnetIdsFromNetworkId(broker, networkId);
101         if (subnetIds.isEmpty()) {
102             LOG.error("installExtNetGroupEntries : No subnet ids associated network id {}", networkId.getValue());
103             return;
104         }
105
106         for (Uuid subnetId : subnetIds) {
107             String macAddress = NatUtil.getSubnetGwMac(broker, subnetId, networkId.getValue());
108             installExtNetGroupEntry(networkId, subnetId, dpnId, macAddress);
109         }
110     }
111
112     private void installExtNetGroupEntries(Subnetmap subnetMap, String macAddress) {
113
114         String subnetName = subnetMap.getId().getValue();
115         Uuid networkId = subnetMap.getNetworkId();
116         if (networkId == null) {
117             LOG.error("installExtNetGroupEntries : No network associated subnet id {}", subnetName);
118             return;
119         }
120
121         Collection<String> extInterfaces = elanService.getExternalElanInterfaces(networkId.getValue());
122         if (extInterfaces == null || extInterfaces.isEmpty()) {
123             LOG.trace("installExtNetGroupEntries : No external ELAN interfaces attached to network:{},subnet {}",
124                     networkId, subnetName);
125             return;
126         }
127
128         long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(subnetName), idManager);
129
130         LOG.info("installExtNetGroupEntries : Installing ext-net group {} entry for subnet {} with macAddress {} "
131                 + "(extInterfaces: {})", groupId, subnetName, macAddress, Arrays.toString(extInterfaces.toArray()));
132         for (String extInterface : extInterfaces) {
133             BigInteger dpId = NatUtil.getDpnForInterface(interfaceManager, extInterface);
134             if (BigInteger.ZERO.equals(dpId)) {
135                 LOG.info("installExtNetGroupEntries: No DPN for interface {}. NAT ext-net flow will not be installed "
136                     + "for subnet {}", extInterface, subnetName);
137                 return;
138             }
139             installExtNetGroupEntry(groupId, subnetName, extInterface, macAddress, dpId);
140         }
141     }
142
143     public void installExtNetGroupEntry(Uuid networkId, Uuid subnetId, BigInteger dpnId, String macAddress) {
144         String subnetName = subnetId.getValue();
145         String extInterface = elanService.getExternalElanInterface(networkId.getValue(), dpnId);
146         if (extInterface == null) {
147             LOG.warn("installExtNetGroupEntry : No external ELAN interface attached to network {} subnet {} DPN id {}",
148                     networkId, subnetName, dpnId);
149             //return;
150         }
151
152         long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(subnetName), idManager);
153         LOG.info("installExtNetGroupEntry : Installing ext-net group {} entry for subnet {} with macAddress {} "
154                 + "(extInterface: {})", groupId, subnetName, macAddress, extInterface);
155         installExtNetGroupEntry(groupId, subnetName, extInterface, macAddress, dpnId);
156     }
157
158     private void installExtNetGroupEntry(long groupId, String subnetName, String extInterface,
159             String macAddress, BigInteger dpnId) {
160         GroupEntity groupEntity = buildExtNetGroupEntity(macAddress, subnetName, groupId, extInterface, dpnId);
161         if (groupEntity != null) {
162             mdsalManager.syncInstallGroup(groupEntity);
163         }
164     }
165
166     public void removeExtNetGroupEntries(Subnetmap subnetMap) {
167         if (subnetMap == null) {
168             return;
169         }
170
171         String subnetName = subnetMap.getId().getValue();
172         Uuid networkId = subnetMap.getNetworkId();
173         if (networkId == null) {
174             LOG.error("removeExtNetGroupEntries : No external network associated subnet id {}", subnetName);
175             return;
176         }
177
178         Collection<String> extInterfaces = elanService.getExternalElanInterfaces(networkId.getValue());
179         if (extInterfaces == null || extInterfaces.isEmpty()) {
180             LOG.debug("removeExtNetGroupEntries : No external ELAN interfaces attached to network {} subnet {}",
181                     networkId, subnetName);
182             return;
183         }
184
185         long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(subnetName), idManager);
186
187         for (String extInterface : extInterfaces) {
188             GroupEntity groupEntity = buildEmptyExtNetGroupEntity(subnetName, groupId, extInterface);
189             if (groupEntity != null) {
190                 LOG.info("removeExtNetGroupEntries : Remove ext-net Group: id {}, subnet id {}", groupId, subnetName);
191                 NatServiceCounters.remove_external_network_group.inc();
192                 mdsalManager.syncRemoveGroup(groupEntity);
193             }
194         }
195     }
196
197     private GroupEntity buildExtNetGroupEntity(String macAddress, String subnetName,
198                                                long groupId, String extInterface, BigInteger dpnId) {
199
200         List<ActionInfo> actionList = new ArrayList<>();
201         final int setFieldEthDestActionPos = 0;
202         List<ActionInfo> egressActionList = new ArrayList<>();
203         if (extInterface != null) {
204             egressActionList = NatUtil.getEgressActionsForInterface(interfaceManager, extInterface, null,
205                 setFieldEthDestActionPos + 1);
206         }
207         if (Strings.isNullOrEmpty(macAddress) || egressActionList.isEmpty()) {
208             if (Strings.isNullOrEmpty(macAddress)) {
209                 LOG.trace("buildExtNetGroupEntity : Building ext-net group {} entry with drop action since "
210                         + "GW mac has not been resolved for subnet {} extInterface {}",
211                         groupId, subnetName, extInterface);
212             } else {
213                 LOG.warn("buildExtNetGroupEntity : Building ext-net group {} entry with drop action since "
214                         + "no egress actions were found for subnet {} extInterface {}",
215                         groupId, subnetName, extInterface);
216             }
217             actionList.add(new ActionDrop());
218         } else {
219             LOG.trace("Building ext-net group {} entry for subnet {} extInterface {} macAddress {}",
220                       groupId, subnetName, extInterface, macAddress);
221             actionList.add(new ActionSetFieldEthernetDestination(setFieldEthDestActionPos, new MacAddress(macAddress)));
222             actionList.addAll(egressActionList);
223         }
224
225         List<BucketInfo> listBucketInfo = new ArrayList<>();
226         listBucketInfo.add(new BucketInfo(actionList));
227         return MDSALUtil.buildGroupEntity(dpnId, groupId, subnetName, GroupTypes.GroupAll, listBucketInfo);
228     }
229
230     private GroupEntity buildEmptyExtNetGroupEntity(String subnetName, long groupId, String extInterface) {
231         BigInteger dpId = NatUtil.getDpnForInterface(interfaceManager, extInterface);
232         if (BigInteger.ZERO.equals(dpId)) {
233             LOG.error("buildEmptyExtNetGroupEntity: No DPN for interface {}. NAT ext-net flow will not be installed "
234                     + "for subnet {}", extInterface, subnetName);
235             return null;
236         }
237
238         return MDSALUtil.buildGroupEntity(dpId, groupId, subnetName, GroupTypes.GroupAll, new ArrayList<>());
239     }
240 }