Policy exclusions & parallel netconf transactions
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / lisp / LispStateManager.java
1 /*
2  * Copyright (c) 2017 Cisco Systems. 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.groupbasedpolicy.renderer.vpp.lisp;
10
11 import java.util.concurrent.ExecutionException;
12
13 import javax.annotation.Nonnull;
14
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;
44
45 import com.google.common.base.Preconditions;
46
47 /**
48  * Created by Shakib Ahmed on 3/29/17.
49  */
50 public class LispStateManager {
51     private static final Logger LOG = LoggerFactory.getLogger(LispStateManager.class);
52
53     private HostRelatedInfoContainer hostRelatedInfoContainer;
54     private MountedDataBrokerProvider mountedDataBrokerProvider;
55     private ConfigManagerHelper lispStateHelper;
56
57     private NeutronTenantToVniMapper neutronTenantToVniMapper;
58
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_";
64
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();
72     }
73
74     public synchronized void processCreateEndPoint(AddressEndpointWithLocation addressEp) {
75         try {
76             if (lispStateHelper.isMetadataPort(addressEp)) {
77                 return;
78             }
79             EndpointHost endpointHost = lispStateHelper.getEndpointHostInformation(addressEp);
80             LispState lispStateOfHost = configureHostIfNeeded(endpointHost);
81
82             long vni = getVni(addressEp.getTenant().getValue());
83             long vrf = vni;
84
85             Eid eid = lispStateHelper.getEid(addressEp, vni);
86             String eidMappingName = lispStateHelper.constructEidMappingName(addressEp);
87
88             addVniToVrfMappingIfNeeded(endpointHost, lispStateOfHost, vni, vrf);
89
90             if (lispStateHelper.getInterfaceIp(addressEp).getValue().equals(Constants.METADATA_IP)) {
91                 return;
92             }
93
94             addEidOnHostIfNeeded(endpointHost, lispStateOfHost, eid, eidMappingName);
95         } catch (LispConfigCommandFailedException e) {
96             LOG.warn("Lisp endpoint configuration failed for address endpoint: {}", addressEp);
97         }
98     }
99
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);
105         }
106     }
107
108     private synchronized LispState configureHostIfNeeded(EndpointHost endpointHost)
109             throws LispConfigCommandFailedException {
110         LispState lispStateOfHost = hostRelatedInfoContainer.getLispStateOfHost(endpointHost.getHostName());
111
112         if (lispStateOfHost == null) {
113             LOG.debug("Configuring host {} for LISP", endpointHost.getHostName());
114             lispStateOfHost = new LispState();
115             try {
116                 enableLispOnHost(endpointHost, lispStateOfHost);
117                 enableGpeOnHostIfNeeded(endpointHost, lispStateOfHost);
118                 addLocatorSetOnHost(endpointHost, lispStateOfHost);
119                 addMapResolverOnHost(endpointHost, lispStateOfHost);
120                 enableMapRegistrationOnHostIfNeeded(endpointHost, lispStateOfHost);
121
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!");
126             }
127         }
128         return lispStateOfHost;
129     }
130
131     private void enableGpeOnHostIfNeeded(EndpointHost endpointHost, LispState lispStateOfHost)
132             throws LispConfigCommandFailedException {
133         if (ConfigUtil.getInstance().isL3FlatEnabled()) {
134             enableGpeForHost(endpointHost, lispStateOfHost);
135         }
136     }
137
138     private void enableMapRegistrationOnHostIfNeeded(EndpointHost endpointHost, LispState lispStateOfHost)
139             throws LispConfigCommandFailedException {
140         if (ConfigUtil.getInstance().isLispMapRegisterEnabled()) {
141             enableMapRegister(endpointHost);
142             addMapServer(endpointHost, lispStateOfHost);
143         }
144     }
145
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);
151         } else {
152             throw new LispConfigCommandFailedException("Lisp Enable Command failed execution!");
153         }
154     }
155
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);
161         } else {
162             throw new LispConfigCommandFailedException("GPE Enable Command failed execution!");
163         }
164     }
165
166     private void addLocatorSetOnHost(EndpointHost endpointHost, LispState lispState)
167             throws LispNotFoundException, LispConfigCommandFailedException {
168         try {
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);
176             } else {
177                 throw new LispConfigCommandFailedException("Lisp add locator set failed for host "
178                         + endpointHost.getHostName() + " and locator interface " + lispDataInterfaceName);
179             }
180
181             addExtraItrRlocLocatorSetIfNeeded(endpointHost, lispDataInterfaceName);
182         } catch (InterruptedException | ExecutionException e) {
183             throw new LispNotFoundException("No interface with Ip Address found!");
184         }
185     }
186
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)) {
193             return;
194         }
195
196         addItrLocatorSet(endpointHost, lispCpRlocInterfaceName);
197     }
198
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);
207         }
208
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);
213         }
214     }
215
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!");
220
221         AbstractLispCommand<MapResolver> addMapResolverCommand = LispCommandWrapper.
222                 addMapResolver(mapResolverIpAddress);
223         if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addMapResolverCommand)) {
224             lispState.addInMapResolverSet(mapResolverIpAddress);
225         } else {
226             throw new LispConfigCommandFailedException("Lisp add map resolver for host " + endpointHost.getHostName()
227                     + " failed for ODL ip " + mapResolverIpAddress);
228         }
229     }
230
231     private void enableMapRegister(EndpointHost endpointHost)
232             throws LispConfigCommandFailedException {
233         AbstractLispCommand<MapRegister> enableMapRegisterCommand = LispCommandWrapper.enableMapRegister();
234
235         if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), enableMapRegisterCommand)) {
236         } else {
237             throw new LispConfigCommandFailedException("Lisp enable mapregistration for host "
238                     + endpointHost.getHostName() + " failed!");
239         }
240
241     }
242
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);
247
248         if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addMapServerCommand)) {
249             lispState.addInMapServerSet(mapServerIpAddress);
250         } else {
251             throw new LispConfigCommandFailedException("Lisp add map server for host " + endpointHost.getHostName()
252                     + " failed for ODL ip " + mapServerIpAddress);
253         }
254     }
255
256     private void addVniToVrfMappingIfNeeded(EndpointHost endpointHost,
257                                             LispState lispState,
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);
264             } else {
265                 throw new LispConfigCommandFailedException("Lisp add vrf " + vrf +" for vni " +vni
266                         + " command failed!");
267             }
268         }
269     }
270
271     private void addEidInEidTable(EndpointHost endpointHost,
272                                   LispState lispState,
273                                   Eid eid,
274                                   String eidMappingName) throws LispConfigCommandFailedException {
275         AbstractLispCommand<LocalMapping> addLocalMappingInEidTableCommand = LispCommandWrapper
276                 .addLocalMappingInEidTable(eidMappingName,
277                         eid,
278                         lispStateHelper.getFirstLocatorSetName(lispState),
279                         lispStateHelper.getDefaultHmacKey());
280         if (LispStateCommandExecutor.executePutCommand(endpointHost.getHostName(), addLocalMappingInEidTableCommand)) {
281             lispState.addEidInEidSet(eid);
282         } else {
283             throw new LispConfigCommandFailedException("Lisp add local mapping for eid " + eid + "failed!");
284         }
285     }
286
287     public synchronized void processDeleteEndpoint(AddressEndpointWithLocation addressEp) {
288         try {
289
290             if (lispStateHelper.isMetadataPort(addressEp)) {
291                 return;
292             }
293
294             EndpointHost endpointHost = lispStateHelper.getEndpointHostInformation(addressEp);
295
296             LispState lispState = hostRelatedInfoContainer.getLispStateOfHost(endpointHost.getHostName());
297
298             if (lispState == null) {
299                 LOG.debug("Endpoint not configured for LISP. EndPoint: {}", addressEp);
300             } else {
301                 long vni = getVni(addressEp.getTenant().getValue());
302                 Eid eid = lispStateHelper.getEid(addressEp, vni);
303                 String eidMappingName = lispStateHelper.constructEidMappingName(addressEp);
304
305                 deleteEidFromLocalEidTableOfHostIfNeeded(endpointHost, lispState, eid, eidMappingName);
306
307                 if (lispState.eidCount() == 0) {
308                     deleteLispStatesFromHost(endpointHost);
309                     deleteNativeForwardPathsTables(endpointHost);
310                 }
311             }
312         } catch (LispConfigCommandFailedException e) {
313             LOG.warn("Lisp command execution failed: {}", e.getMessage());
314         }
315     }
316
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);
320         }
321     }
322
323     private void deleteEidFromLocalEidTableOfHost(EndpointHost endpointHost,
324                                        LispState lispState,
325                                        Eid eid,
326                                        String eidMappingName) throws LispConfigCommandFailedException {
327         long value = eid.getVirtualNetworkId().getValue();
328
329         AbstractLispCommand<LocalMapping> deleteLocalMappingCommand = LispCommandWrapper
330                 .deleteLocalMappingFromEidTable(eidMappingName, value);
331
332         if (LispStateCommandExecutor
333                 .executeDeleteCommand(endpointHost.getHostName(), deleteLocalMappingCommand)) {
334             LOG.debug("Successfully deleted eid {} from host {}", eid, endpointHost.getHostName());
335             lispState.deleteEid(eid);
336         } else {
337             throw new LispConfigCommandFailedException("Lisp delete local mapping command failed!");
338         }
339     }
340
341
342     private void deleteLispStatesFromHost(EndpointHost endpointHost) throws LispConfigCommandFailedException {
343         AbstractLispCommand<LispFeatureData> deleteLispFeatureData = LispCommandWrapper.deleteLispFeatureData();
344
345         if (LispStateCommandExecutor.executeDeleteCommand(endpointHost.getHostName(), deleteLispFeatureData)) {
346             hostRelatedInfoContainer.deleteLispStateOfHost(endpointHost.getHostName());
347             LOG.debug("Deleted all lisp data for host {}", endpointHost.getHostName());
348         } else {
349             throw new LispConfigCommandFailedException("Lisp delete feature data command failed!");
350         }
351     }
352
353     private void deleteNativeForwardPathsTables(EndpointHost endpointHost)
354             throws LispConfigCommandFailedException {
355         AbstractLispCommand<NativeForwardPathsTables> deleteNativeForwardPathsTables = LispCommandWrapper
356                 .deleteNativeForwardPathsTables();
357
358         if (!LispStateCommandExecutor.executeDeleteCommand(endpointHost.getHostName(),
359                 deleteNativeForwardPathsTables)) {
360             throw new LispConfigCommandFailedException("Delete Native Forward Paths Tables command failed!");
361         }
362     }
363
364     private long getVni(String tenantUuid) {
365         return neutronTenantToVniMapper.getVni(tenantUuid);
366     }
367 }