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 com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.lisp.AbstractLispCommand;
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.lisp.LispCommandWrapper;
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.config.ConfigUtil;
16 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.exception.LispConfigCommandFailedException;
17 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.exception.LispNotFoundException;
18 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.mappers.NeutronTenantToVniMapper;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.util.ConfigManagerHelper;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.Lisp;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.local.mappings.LocalMapping;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.local.mappings.local.mapping.Eid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.eid.table.grouping.eid.table.VniTable;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.lisp.feature.data.grouping.LispFeatureData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.locator.sets.grouping.locator.sets.LocatorSet;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.map.register.grouping.MapRegister;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.map.resolvers.grouping.map.resolvers.MapResolver;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.map.servers.grouping.map.servers.MapServer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import javax.annotation.Nonnull;
37 import java.util.HashMap;
38 import java.util.concurrent.ExecutionException;
41 * Created by Shakib Ahmed on 3/29/17.
43 public class LispStateManager {
44 private static final Logger LOG = LoggerFactory.getLogger(LispStateManager.class);
46 private HashMap<String, LispState> lispStateMapper;
47 private MountedDataBrokerProvider mountedDataBrokerProvider;
48 private ConfigManagerHelper lispStateHelper;
50 private NeutronTenantToVniMapper neutronTenantToVniMapper;
52 private static final short DEFAULT_PRIORITY = 1;
53 private static final short DEFAULT_WEIGHT = 1;
54 public static final String DEFAULT_XTR_KEY = "admin";
55 public static final String DEFAULT_LOCATOR_SET_NAME_PREFIX = "LS";
56 public static final String DEFAULT_MAPPINGRECORD_NAME_PREFIX = "MR";
58 public LispStateManager(@Nonnull MountedDataBrokerProvider mountedDataBrokerProvider) {
59 Preconditions.checkNotNull(mountedDataBrokerProvider,
60 "MountedDataBrokerProvider found to be null!");
61 lispStateMapper = new HashMap<>();
62 this.mountedDataBrokerProvider= mountedDataBrokerProvider;
63 this.lispStateHelper = new ConfigManagerHelper(this.mountedDataBrokerProvider);
64 neutronTenantToVniMapper = NeutronTenantToVniMapper.getInstance();
67 public synchronized void configureEndPoint(AddressEndpointWithLocation addressEp) {
69 DataBroker dataBroker = lispStateHelper.getPotentialExternalDataBroker(addressEp).get();
70 String hostName = lispStateHelper.getHostName(addressEp).get();
71 LispState lispStateOfNode = configureHostIfNeeded(hostName, dataBroker);
73 long vni = getVni(addressEp.getTenant().getValue());
75 addVniToVrfMappingIfNeeded(dataBroker, lispStateOfNode, vni, vrf);
77 Eid eid = lispStateHelper.getEid(addressEp, vni);
79 if(!lispStateOfNode.eidSetContains(eid)) {
80 addEidInEidTable(dataBroker, lispStateOfNode, eid);
83 } catch (LispConfigCommandFailedException e) {
84 LOG.warn("Lisp endpoint configuration failed for address endpoint: {}", addressEp);
88 public synchronized LispState configureHostIfNeeded(String hostName, DataBroker vppDataBroker) throws LispConfigCommandFailedException {
89 LispState lispStateOfNode = lispStateMapper.get(hostName);
91 if (lispStateOfNode == null) {
92 lispStateOfNode = new LispState(hostName);
94 enableLispForNode(vppDataBroker, lispStateOfNode);
95 addLocatorSet(vppDataBroker, lispStateOfNode);
96 addMapResolver(vppDataBroker, lispStateOfNode);
97 if (ConfigUtil.getInstance().isLispMapRegisterEnabled()) {
98 enableMapRegister(vppDataBroker, lispStateOfNode);
99 addMapServer(vppDataBroker, lispStateOfNode);
101 lispStateMapper.put(hostName, lispStateOfNode);
102 } catch (LispNotFoundException e) {
103 LOG.warn("Lisp host configuration failed: ", e.getMessage());
104 throw new LispConfigCommandFailedException("Failed LISP configuration!");
107 return lispStateOfNode;
110 public synchronized void deleteLispConfigurationForEndpoint(AddressEndpointWithLocation addressEp) {
112 DataBroker vppDataBroker = lispStateHelper.getPotentialExternalDataBroker(addressEp).get();
113 String hostName = lispStateHelper.getHostName(addressEp).get();
115 LispState lispState = lispStateMapper.get(hostName);
117 if (lispState == null) {
118 LOG.debug("Endpoint not configured for LISP. EndPoint: {}", addressEp);
120 long vni = getVni(addressEp.getTenant().getValue());
121 Eid eid = lispStateHelper.getEid(addressEp, vni);
123 if (lispState.eidSetContains(eid)) {
124 deleteEidFromEidTable(vppDataBroker, lispState, eid);
127 if (lispState.eidCount() == 0) {
128 deleteLispStatesInEndPoints(vppDataBroker, lispState);
131 } catch (LispConfigCommandFailedException e) {
132 LOG.warn("Lisp command execution failed: {}", e.getMessage());
136 private void enableLispForNode(DataBroker vppDataBroker, LispState lispState) throws LispConfigCommandFailedException {
137 AbstractLispCommand<Lisp>
138 lispEnableCommand = LispCommandWrapper.enableLisp();
139 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, lispEnableCommand)) {
140 lispState.setLispEnabled(true);
142 throw new LispConfigCommandFailedException("Lisp Enable Command failed execution!");
146 private void addLocatorSet(DataBroker vppDataBroker, LispState lispState) throws LispNotFoundException, LispConfigCommandFailedException {
148 String locatorSetName = lispStateHelper.constructLocatorSetName(lispState.getLocatorCount());
149 String interfaceName = lispStateHelper.readRlocInterface(lispState.getHostName(), vppDataBroker).get();
150 AbstractLispCommand<LocatorSet> addLocatorSetCommand = LispCommandWrapper.addLocatorSet(locatorSetName,
151 interfaceName, DEFAULT_PRIORITY, DEFAULT_WEIGHT);
152 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, addLocatorSetCommand)) {
153 lispState.setLocIntfToLocSetNameMapping(interfaceName, locatorSetName);
155 throw new LispConfigCommandFailedException("Lisp add locator set failed for host "
156 + lispState.getHostName() + " and locator interface " + interfaceName);
158 } catch (InterruptedException | ExecutionException e) {
159 throw new LispNotFoundException("No interface with Ip Address found!");
164 private void addMapResolver(DataBroker vppDataBroker, LispState lispState) throws LispConfigCommandFailedException {
165 IpAddress mapResolverIpAddress = ConfigUtil.getInstance().getOdlTenantIp();
166 Preconditions.checkNotNull(mapResolverIpAddress, "Map Resolver ip not properly configured!");
168 AbstractLispCommand<MapResolver> addMapResolverCommand = LispCommandWrapper.
169 addMapResolver(mapResolverIpAddress);
170 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, addMapResolverCommand)) {
171 lispState.addInMapResolverSet(mapResolverIpAddress);
173 throw new LispConfigCommandFailedException("Lisp add map resolver for host " + lispState.getHostName()
174 + " failed for ODL ip " + mapResolverIpAddress);
178 private void addMapServer(DataBroker vppDataBroker, LispState lispState) throws LispConfigCommandFailedException {
179 IpAddress mapServerIpAddress = ConfigUtil.getInstance().getOdlTenantIp();
180 Preconditions.checkNotNull(mapServerIpAddress, "Mapserver ip not properly configured!");
181 AbstractLispCommand<MapServer> addMapServerCommand = LispCommandWrapper.addMapServer(mapServerIpAddress);
183 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, addMapServerCommand)) {
184 lispState.addInMapServerSet(mapServerIpAddress);
186 throw new LispConfigCommandFailedException("Lisp add map server for host " + lispState.getHostName()
187 + " failed for ODL ip " + mapServerIpAddress);
191 private void enableMapRegister(DataBroker vppDataBroker, LispState lispState) throws LispConfigCommandFailedException {
192 AbstractLispCommand<MapRegister> enableMapRegisterCommand = LispCommandWrapper.enableMapRegister();
194 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, enableMapRegisterCommand)) {
195 lispState.setMapRegisteredEnabled(true);
197 throw new LispConfigCommandFailedException("Lisp enable mapregistration for host "
198 + lispState.getHostName() + " failed!");
203 private void addVniToVrfMappingIfNeeded(DataBroker vppDataBroker,
205 long vni, long vrf) throws LispConfigCommandFailedException {
206 if (!lispState.vniSetContains(vni)) {
207 AbstractLispCommand<VniTable> addVniToVrfMapping = LispCommandWrapper.mapVniToVrf(vni, vrf);
208 addVniToVrfMapping.setOptions(General.Operations.PUT);
209 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, addVniToVrfMapping)) {
210 lispState.addInVniSet(vni);
212 throw new LispConfigCommandFailedException("Lisp add vrf " + vrf +" for vni " +vni
213 + " command failed!");
218 private void addEidInEidTable(DataBroker vppDataBroker,
220 Eid eid) throws LispConfigCommandFailedException {
221 String mappingId = lispStateHelper.constructMappingName(lispState.getInterfaceId());
222 AbstractLispCommand<LocalMapping> addLocalMappingInEidTableCommand = LispCommandWrapper
223 .addLocalMappingInEidTable(mappingId,
225 lispStateHelper.getFirstLocatorSetName(lispState),
226 lispStateHelper.getDefaultHmacKey());
227 if (LispStateCommandExecutor.executePutCommand(vppDataBroker, addLocalMappingInEidTableCommand)) {
228 lispState.addInEidSet(eid, mappingId);
230 throw new LispConfigCommandFailedException("Lisp add local mapping for eid " + eid + "failed!");
234 private void deleteLispStatesInEndPoints(DataBroker vppDataBroker,
235 LispState lispState) throws LispConfigCommandFailedException {
236 AbstractLispCommand<LispFeatureData> deleteLispFeatureData = LispCommandWrapper.deleteLispFeatureData();
238 if (LispStateCommandExecutor.executeDeleteCommand(vppDataBroker, deleteLispFeatureData)) {
239 String computeNode = lispState.getHostName();
240 lispStateMapper.remove(computeNode);
242 throw new LispConfigCommandFailedException("Lisp delete feature data command failed!");
246 private void deleteEidFromEidTable(DataBroker vppDataBroker,
248 Eid eid) throws LispConfigCommandFailedException {
249 String mappingId = lispState.getEidMapping(eid);
250 long value = eid.getVirtualNetworkId().getValue();
252 AbstractLispCommand<LocalMapping> deleteLocalMappingCommand = LispCommandWrapper
253 .deleteLocalMappingFromEidTable(mappingId, value);
255 if (LispStateCommandExecutor.executeDeleteCommand(vppDataBroker, deleteLocalMappingCommand)) {
256 lispState.deleteEid(eid);
258 throw new LispConfigCommandFailedException("Lisp delete local mapping command failed!");
262 private long getVni(String tenantUuid) {
263 return neutronTenantToVniMapper.getVni(tenantUuid);