7b40f86ea104f7562b5ee61015c24ec98c916f92
[groupbasedpolicy.git] / renderers / iovisor / src / main / java / org / opendaylight / groupbasedpolicy / renderer / iovisor / ResolvedPolicyListener.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 static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.groupbasedpolicy.renderer.iovisor.module.IovisorModuleManager;
23 import org.opendaylight.groupbasedpolicy.renderer.iovisor.restclient.RestClient;
24 import org.opendaylight.groupbasedpolicy.util.DataTreeChangeHandler;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.modules.by.tenant.by.endpointgroup.id.iovisor.module.by.tenant.by.endpointgroup.id.IovisorModuleInstanceId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.annotations.VisibleForTesting;
34
35 public class ResolvedPolicyListener extends DataTreeChangeHandler<ResolvedPolicy> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(ResolvedPolicyListener.class);
38
39     private IovisorModuleManager iovisorModuleManager;
40
41     public ResolvedPolicyListener(DataBroker dataBroker, IovisorModuleManager iovisorModuleManager) {
42         super(dataBroker, new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
43                 InstanceIdentifier.builder(ResolvedPolicies.class).child(ResolvedPolicy.class).build()));
44         this.iovisorModuleManager = iovisorModuleManager;
45     }
46
47     @Override
48     protected void onWrite(DataObjectModification<ResolvedPolicy> rootNode,
49             InstanceIdentifier<ResolvedPolicy> rootIdentifier) {
50         processResolvedPolicyNotification(rootNode.getDataAfter());
51         LOG.trace("Called processResolvedPolicyNotification with ResolvedPolicyKey {} ",
52                 rootNode.getDataAfter().getKey());
53     }
54
55     @Override
56     protected void onDelete(DataObjectModification<ResolvedPolicy> rootNode,
57             InstanceIdentifier<ResolvedPolicy> rootIdentifier) {
58         throw new UnsupportedOperationException("Not implemented yet.");
59     }
60
61     @Override
62     protected void onSubtreeModified(DataObjectModification<ResolvedPolicy> rootNode,
63             InstanceIdentifier<ResolvedPolicy> rootIdentifier) {
64         throw new UnsupportedOperationException("Not implemented yet.");
65     }
66
67     @VisibleForTesting
68     void processResolvedPolicyNotification(ResolvedPolicy resolvedPolicy) {
69         checkNotNull(resolvedPolicy);
70         Set<IovisorModuleInstanceId> ioms = new HashSet<>();
71         List<IovisorModuleInstanceId> tempIoms = new ArrayList<>();
72
73         tempIoms = iovisorModuleManager.getIovisorModulesByTenantByEpg(resolvedPolicy.getProviderTenantId(),
74                 resolvedPolicy.getProviderEpgId());
75         if (tempIoms == null || tempIoms.isEmpty()) {
76             // TODO In Multi Renderer environment ResolvedPolicies will have to only resolve
77             // policies between EPGs where an EP is present. Not just one.
78             LOG.info("No IovisorModule found for Tenant: {} EndpointGroup: {}. Therefore no endpoints to process.",
79                     resolvedPolicy.getProviderTenantId().getValue(), resolvedPolicy.getProviderEpgId().getValue());
80             return;
81         }
82         ioms.addAll(tempIoms);
83
84         tempIoms = iovisorModuleManager.getIovisorModulesByTenantByEpg(resolvedPolicy.getConsumerTenantId(),
85                 resolvedPolicy.getConsumerEpgId());
86         if (tempIoms == null || tempIoms.isEmpty()) {
87             // TODO In Multi Renderer environment ResolvedPolicies will have to only resolve
88             // policies between EPGs where an EP is present. Not just one.
89             LOG.info("No IovisorModule found for Tenant: {} EndpointGroup: {}. Therefore no endpoints to process.",
90                     resolvedPolicy.getConsumerTenantId().getValue(), resolvedPolicy.getConsumerEpgId().getValue());
91             return;
92         }
93         ioms.addAll(tempIoms);
94
95         for (IovisorModuleInstanceId iom : ioms) {
96             IovisorModuleInstance iomInstance = iovisorModuleManager.getActiveIovisorModule(iom.getId());
97             RestClient restClient = new RestClient("http://" + iomInstance.getUri().getValue());
98             restClient.post(IovisorRenderer.IOVISOR_MODULE_LISTENER_BASE_URL, buildPolicyUris(resolvedPolicy));
99         }
100     }
101
102     @VisibleForTesting
103     String buildPolicyUris(ResolvedPolicy resolvedPolicy) {
104         // TODO Move String definition of URIs to common place, perhaps something like IidFactory ?
105         StringBuilder base =
106                 new StringBuilder("/restconf/operational/resolved-policy:resolved-policies/resolved-policy/");
107         base.append(resolvedPolicy.getConsumerTenantId().getValue());
108         base.append("/");
109         base.append(resolvedPolicy.getConsumerEpgId().getValue());
110         base.append("/");
111         base.append(resolvedPolicy.getProviderTenantId().getValue());
112         base.append("/");
113         base.append(resolvedPolicy.getProviderEpgId().getValue());
114         base.append("/");
115         return base.toString();
116     }
117
118 }