Bug 5510 - Modifying DataTreeChangeHandler
[groupbasedpolicy.git] / renderers / iovisor / src / main / java / org / opendaylight / groupbasedpolicy / renderer / iovisor / IovisorResolvedEndpointListener.java
1 /*
2  * Copyright (c) 2015 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.iovisor;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorIidFactory;
17 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
18 import org.opendaylight.groupbasedpolicy.util.DataTreeChangeHandler;
19 import org.opendaylight.groupbasedpolicy.util.IidFactory;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorResolvedEndpointsByTenantByEndpointgroupId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.resolved.endpoints.by.tenant.by.endpointgroup.id.IovisorResolvedEndpointByTenantByEndpointgroupId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.interests.followed.tenants.followed.tenant.FollowedEndpointGroup;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.interests.followed.tenants.followed.tenant.FollowedEndpointGroupBuilder;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.annotations.VisibleForTesting;
31
32 public class IovisorResolvedEndpointListener
33         extends DataTreeChangeHandler<IovisorResolvedEndpointsByTenantByEndpointgroupId> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(IovisorResolvedEndpointListener.class);
36
37     protected IovisorResolvedEndpointListener(DataBroker dataprovider) {
38         super(dataprovider);
39         registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
40                 IovisorIidFactory.iovisorResolvedEndpointsByTenantIdByEndpointGroupIdWildCardIid()));
41     }
42
43     @Override
44     protected void onWrite(DataObjectModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootNode,
45             InstanceIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootIdentifier) {
46         onSubtreeModified(rootNode, rootIdentifier);
47     }
48
49     @Override
50     protected void onDelete(DataObjectModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootNode,
51             InstanceIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootIdentifier) {
52         throw new UnsupportedOperationException("Not implemented yet.");
53
54     }
55
56     @Override
57     protected void onSubtreeModified(DataObjectModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootNode,
58             InstanceIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootIdentifier) {
59         for (IovisorResolvedEndpointByTenantByEndpointgroupId element : rootNode.getDataAfter()
60             .getIovisorResolvedEndpointByTenantByEndpointgroupId()) {
61             endpointPolicyUpdated(element.getTenantId(), element.getEndpointgroupId(),
62                     this.dataProvider.newWriteOnlyTransaction());
63         }
64     }
65
66     @VisibleForTesting
67     void endpointPolicyUpdated(TenantId tenantId, EndpointGroupId epgId, WriteTransaction wTx) {
68         // TODO a renderer should remove followed-EPG and followed-tenant at some point
69         FollowedEndpointGroup followedEpg = new FollowedEndpointGroupBuilder().setId(epgId).build();
70         wTx.put(LogicalDatastoreType.OPERATIONAL,
71                 IidFactory.followedEndpointgroupIid(IovisorRenderer.RENDERER_NAME, tenantId, epgId), followedEpg, true);
72         if (DataStoreHelper.submitToDs(wTx)) {
73             LOG.info("IovisorRenderer following Tenant {} EndpointGroup {}", tenantId.getValue(), epgId.getValue());
74             return;
75         } else {
76             LOG.error("IovisorRenderer could not follow Tenant {} EndpointGroup {}", tenantId.getValue(),
77                     epgId.getValue());
78             return;
79         }
80     }
81
82 }