Adding option to disable BDs for L3 only case
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / policy / VppRendererPolicyManager.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.groupbasedpolicy.renderer.vpp.policy;
10
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.Set;
16 import java.util.stream.Collectors;
17
18 import javax.annotation.Nonnull;
19
20 import org.opendaylight.controller.config.yang.config.vpp_provider.impl.VppRenderer;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.groupbasedpolicy.renderer.util.AddressEndpointUtils;
25 import org.opendaylight.groupbasedpolicy.renderer.vpp.config.ConfigUtil;
26 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent;
27 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.RendererPolicyConfEvent;
28 import org.opendaylight.groupbasedpolicy.renderer.vpp.iface.AclManager;
29 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
30 import org.opendaylight.groupbasedpolicy.util.IidFactory;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.rule.group.with.renderer.endpoint.participation.RuleGroupWithRendererEndpointParticipation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.RendererPolicy;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.RendererPolicyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpointKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpoint;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.rule.groups.RuleGroupKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.common.base.Preconditions;
47 import com.google.common.collect.HashMultimap;
48 import com.google.common.collect.ImmutableSet;
49 import com.google.common.collect.MapDifference;
50 import com.google.common.collect.MapDifference.ValueDifference;
51 import com.google.common.collect.Maps;
52 import com.google.common.collect.SetMultimap;
53 import com.google.common.collect.Sets;
54 import com.google.common.collect.Sets.SetView;
55 import com.google.common.eventbus.Subscribe;
56 import com.google.common.util.concurrent.FutureCallback;
57 import com.google.common.util.concurrent.Futures;
58
59
60 public class VppRendererPolicyManager {
61
62     private static final Logger LOG = LoggerFactory.getLogger(VppRendererPolicyManager.class);
63     private final DataBroker dataProvider;
64     private ForwardingManager fwManager;
65     private final AclManager aclManager;
66
67     public VppRendererPolicyManager(@Nonnull ForwardingManager fwManager, @Nonnull AclManager aclManager,
68             @Nonnull DataBroker dataProvider) {
69         this.fwManager = Preconditions.checkNotNull(fwManager);
70         this.dataProvider = Preconditions.checkNotNull(dataProvider);
71         this.aclManager = Preconditions.checkNotNull(aclManager);
72     }
73
74     @Subscribe
75     public void rendererPolicyChanged(RendererPolicyConfEvent event) {
76         RendererPolicyBuilder responseBuilder = new RendererPolicyBuilder();
77         switch (event.getDtoModificationType()) {
78             case CREATED:
79                 LOG.debug("CREATED : {}", event.getIid());
80                 responseBuilder.setVersion(event.getAfter().get().getVersion());
81                 rendererPolicyCreated(event.getAfter().get());
82                 break;
83             case UPDATED:
84                 LOG.debug("UPDATED: {}", event.getIid());
85                 RendererPolicy rPolicyBefore = event.getBefore().get();
86                 RendererPolicy rPolicyAfter = event.getAfter().get();
87                 responseBuilder.setVersion(rPolicyAfter.getVersion());
88                 if (rPolicyBefore.getConfiguration() == null && rPolicyAfter.getConfiguration() == null) {
89                     LOG.debug("Configuration is not changed only updating config version from {} to {}",
90                             rPolicyBefore.getVersion(), rPolicyAfter.getVersion());
91                 } else {
92                     // TODO collect unconfigured rules and put them to responseBuilder
93                     rendererPolicyUpdated(rPolicyBefore, rPolicyAfter);
94                 }
95                 break;
96             case DELETED:
97                 LOG.debug("DELETED: {}", event.getIid());
98                 responseBuilder.setVersion(event.getBefore().get().getVersion());
99                 rendererPolicyDeleted(event.getBefore().get());
100                 break;
101         }
102         WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
103         RendererPolicy response = responseBuilder.build();
104         wTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.rendererIid(VppRenderer.NAME).child(RendererPolicy.class),
105                 response, true);
106         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
107
108             @Override
109             public void onSuccess(Void result) {
110                 LOG.info("Renderer updated renderer policy to version {}", response.getVersion());
111             }
112
113             @Override
114             public void onFailure(Throwable t) {
115                 LOG.warn("Renderer failed to update renderer-policy to version {}", response.getVersion());
116             }
117         });
118     }
119
120     private void rendererPolicyUpdated(RendererPolicy rPolicyBefore, RendererPolicy rPolicyAfter) {
121         LOG.trace("VPP renderer policy updated");
122         PolicyContext policyCtxBefore = new PolicyContext(rPolicyBefore);
123         PolicyContext policyCtxAfter = new PolicyContext(rPolicyAfter);
124         MapDifference<String, Collection<NodeId>> vppNodesByL2FlDiff =
125                 createDiffForVppNodesByL2Fd(policyCtxBefore, policyCtxAfter);
126         SetMultimap<String, NodeId> removedVppNodesByL2Fd = HashMultimap.create();
127         SetMultimap<String, NodeId> createdVppNodesByL2Fd = HashMultimap.create();
128         for (Entry<String, ValueDifference<Collection<NodeId>>> entry : vppNodesByL2FlDiff.entriesDiffering()
129             .entrySet()) {
130             String bridgeDomain = entry.getKey();
131             Collection<NodeId> beforeNodes = entry.getValue().leftValue();
132             Collection<NodeId> afterNodes = entry.getValue().rightValue();
133             if (beforeNodes != null && afterNodes != null) {
134                 SetView<NodeId> removedNodes = Sets.difference(new HashSet<>(beforeNodes), new HashSet<>(afterNodes));
135                 removedVppNodesByL2Fd.putAll(bridgeDomain, removedNodes);
136                 SetView<NodeId> createdNodes = Sets.difference(new HashSet<>(afterNodes), new HashSet<>(beforeNodes));
137                 createdVppNodesByL2Fd.putAll(bridgeDomain, createdNodes);
138             } else if (beforeNodes != null) {
139                 removedVppNodesByL2Fd.putAll(bridgeDomain, beforeNodes);
140             } else if (afterNodes != null) {
141                 createdVppNodesByL2Fd.putAll(bridgeDomain, afterNodes);
142             }
143         }
144         Map<String, Collection<NodeId>> removedL2Fds = vppNodesByL2FlDiff.entriesOnlyOnLeft();
145         for (Entry<String, Collection<NodeId>> entry : removedL2Fds.entrySet()) {
146             String bridgeDomain = entry.getKey();
147             Collection<NodeId> removedNodes = entry.getValue();
148             if (removedNodes != null) {
149                 removedVppNodesByL2Fd.putAll(bridgeDomain, removedNodes);
150             }
151         }
152         Map<String, Collection<NodeId>> createdL2Fds = vppNodesByL2FlDiff.entriesOnlyOnRight();
153         for (Entry<String, Collection<NodeId>> entry : createdL2Fds.entrySet()) {
154             String bridgeDomain = entry.getKey();
155             Collection<NodeId> createdNodes = entry.getValue();
156             if (createdNodes != null) {
157                 createdVppNodesByL2Fd.putAll(bridgeDomain, createdNodes);
158             }
159         }
160
161         ImmutableSet<RendererEndpointKey> rendEpsBefore = policyCtxBefore.getPolicyTable().rowKeySet();
162         ImmutableSet<RendererEndpointKey> rendEpsAfter = policyCtxAfter.getPolicyTable().rowKeySet();
163
164         SetView<RendererEndpointKey> removedRendEps = Sets.difference(rendEpsBefore, rendEpsAfter);
165         LOG.debug("Removed renderer endpoints {}", removedRendEps);
166         removedRendEps.forEach(rEpKey -> fwManager.removeForwardingForEndpoint(rEpKey, policyCtxBefore));
167
168         if (!ConfigUtil.getInstance().isL3FlatEnabled()) {
169             LOG.debug("Removing bridge domains on nodes {}", removedVppNodesByL2Fd);
170             fwManager.removeBridgeDomainOnNodes(removedVppNodesByL2Fd);
171             LOG.debug("Creating bridge domains on nodes {}", createdVppNodesByL2Fd);
172             fwManager.createBridgeDomainOnNodes(createdVppNodesByL2Fd);
173         }
174
175         fwManager.syncNatEntries(policyCtxAfter);
176
177         fwManager.deleteRouting(policyCtxBefore);
178         fwManager.syncRouting(policyCtxAfter);
179
180         SetView<RendererEndpointKey> createdRendEps = Sets.difference(rendEpsAfter, rendEpsBefore);
181         LOG.debug("Created renderer endpoints {}", createdRendEps);
182         createdRendEps.forEach(rEpKey -> fwManager.createForwardingForEndpoint(rEpKey, policyCtxAfter));
183
184         SetView<RendererEndpointKey> updatedRendEps = Sets.intersection(rendEpsBefore, rendEpsAfter);
185         LOG.debug("Updated renderer endpoints {}", updatedRendEps);
186         // update forwarding for endpoint
187         updatedRendEps.forEach(rEpKey -> {
188             AddressEndpointWithLocation addrEpWithLocBefore =
189                     policyCtxBefore.getAddrEpByKey().get(KeyFactory.addressEndpointKey(rEpKey));
190             AddressEndpointWithLocation addrEpWithLocAfter =
191                     policyCtxAfter.getAddrEpByKey().get(KeyFactory.addressEndpointKey(rEpKey));
192             if (isLocationChanged(addrEpWithLocBefore, addrEpWithLocAfter)) {
193                 LOG.debug("Location is changed in endpoint {}", rEpKey);
194                 LOG.debug("\nLocation before: {}\nLocation after: {}", addrEpWithLocBefore.getAbsoluteLocation(),
195                         addrEpWithLocAfter.getAbsoluteLocation());
196                 fwManager.removeForwardingForEndpoint(rEpKey, policyCtxBefore);
197                 fwManager.createForwardingForEndpoint(rEpKey, policyCtxAfter);
198             }
199         });
200         updatePolicy(policyCtxBefore, policyCtxAfter);
201     }
202
203     /**
204      * Looks for changed rule groups in {@code policyCtxBefore} and {@code policyCtxAfter}.
205      * Access lists are updated for endpoints in {@code policyCtxAfter} affected by changed rule
206      * groups.
207      *
208      * @param policyCtxBefore policy before
209      * @param policyCtxAfter policy after
210      */
211     private void updatePolicy(PolicyContext policyCtxBefore, PolicyContext policyCtxAfter) {
212         LOG.info("Updating policy by rule groups.");
213         Set<RuleGroupKey> diffRuleGroups = new HashSet<>();
214         diffRuleGroups.addAll(Sets.difference(policyCtxBefore.getRuleGroupByKey().keySet(),
215                 policyCtxAfter.getRuleGroupByKey().keySet()));
216         diffRuleGroups.addAll(Sets.difference(policyCtxAfter.getRuleGroupByKey().keySet(), policyCtxBefore.getRuleGroupByKey().keySet()));
217         LOG.trace("Rule groups changed: {} ", diffRuleGroups.size());
218         Set<RendererEndpointKey> updates = new HashSet<>();
219         for (PolicyContext policy : new PolicyContext[] {policyCtxBefore, policyCtxAfter}) {
220             if (policy.getPolicy().getConfiguration() == null
221                     || policy.getPolicy().getConfiguration().getRendererEndpoints() == null
222                     || policy.getPolicy().getConfiguration().getRendererEndpoints().getRendererEndpoint() == null) {
223                 continue;
224             }
225             policy.getPolicy()
226                 .getConfiguration()
227                 .getRendererEndpoints()
228                 .getRendererEndpoint()
229                 .stream()
230                 .filter(rEp -> !updates.contains(rEp.getKey()))
231                 .forEach(rEp -> {
232                     for (PeerEndpoint pEp : rEp.getPeerEndpoint()) {
233                         for (RuleGroupWithRendererEndpointParticipation rg : pEp
234                             .getRuleGroupWithRendererEndpointParticipation()) {
235                             if (!diffRuleGroups.contains(
236                                     new RuleGroupKey(rg.getContractId(), rg.getSubjectName(), rg.getTenantId()))) {
237                                 continue;
238                             }
239                             LOG.debug("Updated resolved rule group: {}. Affected endpoints {} and {}.", rg.getKey(), rEp.getKey(), pEp.getKey());
240                             updates.add(rEp.getKey());
241                             AddressEndpointKey k1 = AddressEndpointUtils.fromPeerEpKey(pEp.getKey());
242                             updates.add(AddressEndpointUtils.toRendererEpKey(k1));
243                         }
244                     }
245                 });
246         }
247         for (RendererEndpointKey rEpKey : updates) {
248             aclManager.updateAclsForRendEp(rEpKey, policyCtxAfter);
249         }
250     }
251
252     private static boolean isLocationChanged(AddressEndpointWithLocation before, AddressEndpointWithLocation after) {
253         ExternalLocationCase locationBefore = ForwardingManager.resolveAndValidateLocation(before);
254         ExternalLocationCase locationAfter = ForwardingManager.resolveAndValidateLocation(after);
255         return !locationBefore.equals(locationAfter);
256     }
257
258     private static MapDifference<String, Collection<NodeId>> createDiffForVppNodesByL2Fd(PolicyContext policyCtxBefore,
259             PolicyContext policyCtxAfter) {
260         ImmutableSet<RendererEndpointKey> rendEpsBefore = policyCtxBefore.getPolicyTable().rowKeySet();
261         ImmutableSet<RendererEndpointKey> rendEpsAfter = policyCtxAfter.getPolicyTable().rowKeySet();
262         SetMultimap<String, NodeId> vppNodesByL2FdBefore = resolveVppNodesByL2Fd(rendEpsBefore, policyCtxBefore);
263         SetMultimap<String, NodeId> vppNodesByL2FdAfter = resolveVppNodesByL2Fd(rendEpsAfter, policyCtxAfter);
264         return Maps.difference(vppNodesByL2FdBefore.asMap(), vppNodesByL2FdAfter.asMap());
265     }
266
267     private void rendererPolicyCreated(RendererPolicy rPolicy) {
268         LOG.trace("VPP renderer policy version {} created", rPolicy.getVersion());
269         PolicyContext policyCtx = new PolicyContext(rPolicy);
270         ImmutableSet<RendererEndpointKey> rEpKeys = policyCtx.getPolicyTable().rowKeySet();
271         if (!ConfigUtil.getInstance().isL3FlatEnabled()) {
272             SetMultimap<String, NodeId> vppNodesByL2Fd = resolveVppNodesByL2Fd(rEpKeys, policyCtx);
273             fwManager.createBridgeDomainOnNodes(vppNodesByL2Fd);
274         }
275         fwManager.syncNatEntries(policyCtx);
276         fwManager.syncRouting(policyCtx);
277         rEpKeys.forEach(rEpKey -> fwManager.createForwardingForEndpoint(rEpKey, policyCtx));
278     }
279
280     private void rendererPolicyDeleted(RendererPolicy rendererPolicy) {
281         LOG.trace("VPP renderer policy version {} deleted", rendererPolicy.getVersion());
282         PolicyContext policyCtx = new PolicyContext(rendererPolicy);
283         ImmutableSet<RendererEndpointKey> rEpKeys = policyCtx.getPolicyTable().rowKeySet();
284
285         rEpKeys.forEach(rEpKey -> fwManager.removeForwardingForEndpoint(rEpKey, policyCtx));
286         if (!ConfigUtil.getInstance().isL3FlatEnabled()) {
287             SetMultimap<String, NodeId> vppNodesByL2Fd = resolveVppNodesByL2Fd(rEpKeys, policyCtx);
288             fwManager.removeBridgeDomainOnNodes(vppNodesByL2Fd);
289         }
290         fwManager.deleteNatEntries(policyCtx);
291         fwManager.deleteRouting(policyCtx);
292     }
293
294     private static SetMultimap<String, NodeId> resolveVppNodesByL2Fd(Set<RendererEndpointKey> rEpKeys,
295             PolicyContext policyCtx) {
296         SetMultimap<String, NodeId> vppNodesByL2Fd = HashMultimap.create();
297         rEpKeys.stream()
298             .map(rEpKey -> KeyFactory.addressEndpointKey(rEpKey))
299             .map(addrEpKey -> policyCtx.getAddrEpByKey().get(addrEpKey))
300             .collect(Collectors.toSet())
301             .forEach(addrEpWithLoc -> {
302                 java.util.Optional<String> optL2Fd = ForwardingManager.resolveL2FloodDomain(addrEpWithLoc, policyCtx);
303                 if (optL2Fd.isPresent()) {
304                     ExternalLocationCase rEpLoc = ForwardingManager.resolveAndValidateLocation(addrEpWithLoc);
305                     InstanceIdentifier<?> externalNodeMountPoint = rEpLoc.getExternalNodeMountPoint();
306                     NodeId vppNode = externalNodeMountPoint.firstKeyOf(Node.class).getNodeId();
307                     vppNodesByL2Fd.put(optL2Fd.get(), vppNode);
308                 }
309             });
310         return vppNodesByL2Fd;
311     }
312
313     @Subscribe
314     public void vppNodeChanged(NodeOperEvent event) {
315         switch (event.getDtoModificationType()) {
316             case CREATED:
317                 if (event.isAfterConnected()) {
318                     // TODO
319                 }
320                 break;
321             case UPDATED:
322                 if (!event.isBeforeConnected() && event.isAfterConnected()) {
323                     // TODO
324                 }
325                 break;
326             case DELETED:
327                 if (event.isBeforeConnected()) {
328                     // TODO
329                 }
330                 break;
331         }
332     }
333 }