2 * Copyright (c) 2017 Cisco Systems. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.lisp;
11 import java.util.concurrent.ExecutionException;
13 import javax.annotation.Nonnull;
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.lisp.AbstractLispCommand;
16 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.lisp.LispCommandWrapper;
17 import org.opendaylight.groupbasedpolicy.renderer.vpp.config.ConfigUtil;
18 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.exception.LispConfigCommandFailedException;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.exception.LispNotFoundException;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container.EndpointHost;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container.HostRelatedInfoContainer;
22 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container.states.LispState;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.mappers.NeutronTenantToVniMapper;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.util.ConfigManagerHelper;
25 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.util.Constants;
26 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
27 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.NativeForwardPathsTables;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170518.gpe.feature.data.grouping.GpeFeatureData;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.Lisp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.local.mappings.LocalMapping;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.local.mappings.local.mapping.Eid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.eid.table.grouping.eid.table.VniTable;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.lisp.feature.data.grouping.LispFeatureData;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.locator.sets.grouping.locator.sets.LocatorSet;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.map.register.grouping.MapRegister;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.map.resolvers.grouping.map.resolvers.MapResolver;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.map.servers.grouping.map.servers.MapServer;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.base.Preconditions;
48 * Created by Shakib Ahmed on 3/29/17.
50 public class LispStateManager {
51 private static final Logger LOG = LoggerFactory.getLogger(LispStateManager.class);
53 private HostRelatedInfoContainer hostRelatedInfoContainer;
54 private MountedDataBrokerProvider mountedDataBrokerProvider;
55 private ConfigManagerHelper lispStateHelper;
57 private NeutronTenantToVniMapper neutronTenantToVniMapper;
59 private static final short DEFAULT_PRIORITY = 1;
60 private static final short DEFAULT_WEIGHT = 1;
61 public static final String DEFAULT_XTR_KEY = "admin";
62 public static final String DEFAULT_LOCATOR_SET_NAME_PREFIX = "LS";
63 public static final String DEFAULT_MAPPINGRECORD_NAME_PREFIX = "MR_";
65 public LispStateManager(@Nonnull MountedDataBrokerProvider mountedDataBrokerProvider) {
66 Preconditions.checkNotNull(mountedDataBrokerProvider,
67 "MountedDataBrokerProvider found to be null!");
68 hostRelatedInfoContainer = HostRelatedInfoContainer.getInstance();
69 this.mountedDataBrokerProvider= mountedDataBrokerProvider;
70 this.lispStateHelper = new ConfigManagerHelper(this.mountedDataBrokerProvider);
71 neutronTenantToVniMapper = NeutronTenantToVniMapper.getInstance();
74 public synchronized void processCreateEndPoint(AddressEndpointWithLocation addressEp) {
76 if (lispStateHelper.isMetadataPort(addressEp)) {
79 EndpointHost endpointHost = lispStateHelper.getEndpointHostInformation(addressEp);
80 LispState lispStateOfHost = configureHostIfNeeded(endpointHost);
82 long vni = getVni(addressEp.getTenant().getValue());
85 Eid eid = lispStateHelper.getEid(addressEp, vni);
86 String eidMappingName = lispStateHelper.constructEidMappingName(addressEp);
88 addVniToVrfMappingIfNeeded(endpointHost, lispStateOfHost, vni, vrf);
90 if (lispStateHelper.getInterfaceIp(addressEp).getValue().equals(Constants.METADATA_IP)) {
94 addEidOnHostIfNeeded(endpointHost, lispStateOfHost, eid, eidMappingName);
95 } catch (LispConfigCommandFailedException e) {
96 LOG.warn("Lisp endpoint configuration failed for address endpoint: {}", addressEp);
100 private void addEidOnHostIfNeeded(EndpointHost endpointHost, LispState lispStateOfNode, Eid eid,
101 String eidMappingName)
102 throws LispConfigCommandFailedException {
103 if(!lispStateOfNode.eidSetContains(eid)) {
104 addEidInEidTable(endpointHost, lispStateOfNode, eid, eidMappingName);
108 private synchronized LispState configureHostIfNeeded(EndpointHost endpointHost)
109 throws LispConfigCommandFailedException {
110 LispState lispStateOfHost = hostRelatedInfoContainer.getLispStateOfHost(endpointHost.getHostName());
112 if (lispStateOfHost == null) {
113 LOG.debug("Configuring host {} for LISP", endpointHost.getHostName());
114 lispStateOfHost = new LispState();
116 enableLispOnHost(endpointHost, lispStateOfHost);
117 enableGpeOnHostIfNeeded(endpointHost, lispStateOfHost);
118 addLocatorSetOnHost(endpointHost, lispStateOfHost);
119 addMapResolverOnHost(endpointHost, lispStateOfHost);
120 enableMapRegistrationOnHostIfNeeded(endpointHost, lispStateOfHost);
122 hostRelatedInfoContainer.setLispStateOfHost(endpointHost.getHostName(), lispStateOfHost);
123 } catch (LispNotFoundException e) {
124 LOG.warn("Lisp host configuration failed: ", e.getMessage());
125 throw new LispConfigCommandFailedException("Failed LISP configuration!");
128 return lispStateOfHost;
131 private void enableGpeOnHostIfNeeded(EndpointHost endpointHost, LispState lispStateOfHost)
132 throws LispConfigCommandFailedException {
133 if (ConfigUtil.getInstance().isL3FlatEnabled()) {
134 enableGpeForHost(endpointHost, lispStateOfHost);
138 private void enableMapRegistrationOnHostIfNeeded(EndpointHost endpointHost, LispState lispStateOfHost)
139 throws LispConfigCommandFailedException {
140 if (ConfigUtil.getInstance().isLispMapRegisterEnabled()) {
141 enableMapRegister(endpointHost);
142 addMapServer(endpointHost, lispStateOfHost);
146 private void enableLispOnHost(EndpointHost endpointHost, LispState lispState)
147 throws LispConfigCommandFailedException {
148 AbstractLispCommand<Lisp> lispEnableCommand = LispCommandWrapper.enableLisp();
149 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), lispEnableCommand)) {
150 lispState.setLispEnabled(true);
152 throw new LispConfigCommandFailedException("Lisp Enable Command failed execution!");
156 private void enableGpeForHost(EndpointHost endpointHost, LispState lispState)
157 throws LispConfigCommandFailedException {
158 AbstractLispCommand<GpeFeatureData> gpeEnableCommand = LispCommandWrapper.enableGpe();
159 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), gpeEnableCommand)) {
160 lispState.setGpeEnabled(true);
162 throw new LispConfigCommandFailedException("GPE Enable Command failed execution!");
166 private void addLocatorSetOnHost(EndpointHost endpointHost, LispState lispState)
167 throws LispNotFoundException, LispConfigCommandFailedException {
169 String locatorSetName = lispStateHelper.constructLocatorSetName(lispState.getLocatorCount());
170 String lispDataInterfaceName = lispStateHelper
171 .getLispDataRlocInterfaceName(endpointHost.getHostName()).get();
172 AbstractLispCommand<LocatorSet> addLocatorSetCommand = LispCommandWrapper.addLocatorSet(locatorSetName,
173 lispDataInterfaceName, DEFAULT_PRIORITY, DEFAULT_WEIGHT);
174 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addLocatorSetCommand)) {
175 lispState.setLocIntfToLocSetNameMapping(lispDataInterfaceName, locatorSetName);
177 throw new LispConfigCommandFailedException("Lisp add locator set failed for host "
178 + endpointHost.getHostName() + " and locator interface " + lispDataInterfaceName);
181 addExtraItrRlocLocatorSetIfNeeded(endpointHost, lispDataInterfaceName);
182 } catch (InterruptedException | ExecutionException e) {
183 throw new LispNotFoundException("No interface with Ip Address found!");
187 private void addExtraItrRlocLocatorSetIfNeeded(EndpointHost endpointHost, String lispDataInterfaceName)
188 throws LispNotFoundException, LispConfigCommandFailedException {
189 String lispCpRlocInterfaceName = lispStateHelper.getLispCpRlocInterfaceName(endpointHost);
190 if (lispCpRlocInterfaceName == null
191 || lispCpRlocInterfaceName.isEmpty()
192 || lispCpRlocInterfaceName.equals(lispDataInterfaceName)) {
196 addItrLocatorSet(endpointHost, lispCpRlocInterfaceName);
199 private void addItrLocatorSet(EndpointHost endpointHost, String lispCpInterfaceName)
200 throws LispNotFoundException, LispConfigCommandFailedException {
201 String locatorSetName = lispStateHelper.constructLocatorSetNameForItrRloc();
202 AbstractLispCommand<LocatorSet> addLocatorSetCommand = LispCommandWrapper.addLocatorSet(locatorSetName,
203 lispCpInterfaceName, DEFAULT_PRIORITY, DEFAULT_WEIGHT);
204 if (!LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addLocatorSetCommand)) {
205 throw new LispConfigCommandFailedException("Lisp add locator set failed for host "
206 + endpointHost.getHostName() + " and locator interface " + lispCpInterfaceName);
209 AbstractLispCommand<ItrRemoteLocatorSet> addItrRlocCommand = LispCommandWrapper.addItrRloc(locatorSetName);
210 if (!LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addItrRlocCommand)) {
211 throw new LispConfigCommandFailedException("Lisp add Itr Rloc command failed for host "
212 + endpointHost.getHostName() + " and locator set " + locatorSetName);
216 private void addMapResolverOnHost(EndpointHost endpointHost, LispState lispState)
217 throws LispConfigCommandFailedException {
218 IpAddress mapResolverIpAddress = ConfigUtil.getInstance().getOdlIp();
219 Preconditions.checkNotNull(mapResolverIpAddress, "Map Resolver ip not properly configured!");
221 AbstractLispCommand<MapResolver> addMapResolverCommand = LispCommandWrapper.
222 addMapResolver(mapResolverIpAddress);
223 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addMapResolverCommand)) {
224 lispState.addInMapResolverSet(mapResolverIpAddress);
226 throw new LispConfigCommandFailedException("Lisp add map resolver for host " + endpointHost.getHostName()
227 + " failed for ODL ip " + mapResolverIpAddress);
231 private void enableMapRegister(EndpointHost endpointHost)
232 throws LispConfigCommandFailedException {
233 AbstractLispCommand<MapRegister> enableMapRegisterCommand = LispCommandWrapper.enableMapRegister();
235 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), enableMapRegisterCommand)) {
237 throw new LispConfigCommandFailedException("Lisp enable mapregistration for host "
238 + endpointHost.getHostName() + " failed!");
243 private void addMapServer(EndpointHost endpointHost, LispState lispState) throws LispConfigCommandFailedException {
244 IpAddress mapServerIpAddress = ConfigUtil.getInstance().getOdlIp();
245 Preconditions.checkNotNull(mapServerIpAddress, "Mapserver ip not properly configured!");
246 AbstractLispCommand<MapServer> addMapServerCommand = LispCommandWrapper.addMapServer(mapServerIpAddress);
248 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addMapServerCommand)) {
249 lispState.addInMapServerSet(mapServerIpAddress);
251 throw new LispConfigCommandFailedException("Lisp add map server for host " + endpointHost.getHostName()
252 + " failed for ODL ip " + mapServerIpAddress);
256 private void addVniToVrfMappingIfNeeded(EndpointHost endpointHost,
258 long vni, long vrf) throws LispConfigCommandFailedException {
259 if (!lispState.isVniConfigured(vni)) {
260 AbstractLispCommand<VniTable> addVniToVrfMapping = LispCommandWrapper.mapVniToVrf(vni, vrf);
261 addVniToVrfMapping.setOptions(General.Operations.PUT);
262 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addVniToVrfMapping)) {
263 lispState.addInVniSet(vni);
265 throw new LispConfigCommandFailedException("Lisp add vrf " + vrf +" for vni " +vni
266 + " command failed!");
271 private void addEidInEidTable(EndpointHost endpointHost,
274 String eidMappingName) throws LispConfigCommandFailedException {
275 AbstractLispCommand<LocalMapping> addLocalMappingInEidTableCommand = LispCommandWrapper
276 .addLocalMappingInEidTable(eidMappingName,
278 lispStateHelper.getFirstLocatorSetName(lispState),
279 lispStateHelper.getDefaultHmacKey());
280 if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addLocalMappingInEidTableCommand)) {
281 lispState.addEidInEidSet(eid);
283 throw new LispConfigCommandFailedException("Lisp add local mapping for eid " + eid + "failed!");
287 public synchronized void processDeleteEndpoint(AddressEndpointWithLocation addressEp) {
290 if (lispStateHelper.isMetadataPort(addressEp)) {
294 EndpointHost endpointHost = lispStateHelper.getEndpointHostInformation(addressEp);
296 LispState lispState = hostRelatedInfoContainer.getLispStateOfHost(endpointHost.getHostName());
298 if (lispState == null) {
299 LOG.debug("Endpoint not configured for LISP. EndPoint: {}", addressEp);
301 long vni = getVni(addressEp.getTenant().getValue());
302 Eid eid = lispStateHelper.getEid(addressEp, vni);
303 String eidMappingName = lispStateHelper.constructEidMappingName(addressEp);
305 deleteEidFromLocalEidTableOfHostIfNeeded(endpointHost, lispState, eid, eidMappingName);
307 if (lispState.eidCount() == 0) {
308 deleteLispStatesFromHost(endpointHost);
309 deleteNativeForwardPathsTables(endpointHost);
312 } catch (LispConfigCommandFailedException e) {
313 LOG.warn("Lisp command execution failed: {}", e.getMessage());
317 private void deleteEidFromLocalEidTableOfHostIfNeeded(EndpointHost endpointHost, LispState lispState, Eid eid, String eidMappingName) throws LispConfigCommandFailedException {
318 if (lispState.eidSetContains(eid)) {
319 deleteEidFromLocalEidTableOfHost(endpointHost, lispState, eid, eidMappingName);
323 private void deleteEidFromLocalEidTableOfHost(EndpointHost endpointHost,
326 String eidMappingName) throws LispConfigCommandFailedException {
327 long value = eid.getVirtualNetworkId().getValue();
329 AbstractLispCommand<LocalMapping> deleteLocalMappingCommand = LispCommandWrapper
330 .deleteLocalMappingFromEidTable(eidMappingName, value);
332 if (LispStateCommandExecutor
333 .executeDeleteCommand(endpointHost.getHostName(), deleteLocalMappingCommand)) {
334 LOG.debug("Successfully deleted eid {} from host {}", eid, endpointHost.getHostName());
335 lispState.deleteEid(eid);
337 throw new LispConfigCommandFailedException("Lisp delete local mapping command failed!");
342 private void deleteLispStatesFromHost(EndpointHost endpointHost) throws LispConfigCommandFailedException {
343 AbstractLispCommand<LispFeatureData> deleteLispFeatureData = LispCommandWrapper.deleteLispFeatureData();
345 if (LispStateCommandExecutor.executeDeleteCommand(endpointHost.getHostName(), deleteLispFeatureData)) {
346 hostRelatedInfoContainer.deleteLispStateOfHost(endpointHost.getHostName());
347 LOG.debug("Deleted all lisp data for host {}", endpointHost.getHostName());
349 throw new LispConfigCommandFailedException("Lisp delete feature data command failed!");
353 private void deleteNativeForwardPathsTables(EndpointHost endpointHost)
354 throws LispConfigCommandFailedException {
355 AbstractLispCommand<NativeForwardPathsTables> deleteNativeForwardPathsTables = LispCommandWrapper
356 .deleteNativeForwardPathsTables();
358 if (!LispStateCommandExecutor.executeDeleteCommand(endpointHost.getHostName(),
359 deleteNativeForwardPathsTables)) {
360 throw new LispConfigCommandFailedException("Delete Native Forward Paths Tables command failed!");
364 private long getVni(String tenantUuid) {
365 return neutronTenantToVniMapper.getVni(tenantUuid);