2 * Copyright (c) 2015 Cisco Systems, Inc. and others. 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.iovisor;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
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;
33 import com.google.common.annotations.VisibleForTesting;
35 public class ResolvedPolicyListener extends DataTreeChangeHandler<ResolvedPolicy> {
37 private static final Logger LOG = LoggerFactory.getLogger(ResolvedPolicyListener.class);
39 private IovisorModuleManager iovisorModuleManager;
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;
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());
56 protected void onDelete(DataObjectModification<ResolvedPolicy> rootNode,
57 InstanceIdentifier<ResolvedPolicy> rootIdentifier) {
58 throw new UnsupportedOperationException("Not implemented yet.");
62 protected void onSubtreeModified(DataObjectModification<ResolvedPolicy> rootNode,
63 InstanceIdentifier<ResolvedPolicy> rootIdentifier) {
64 throw new UnsupportedOperationException("Not implemented yet.");
68 void processResolvedPolicyNotification(ResolvedPolicy resolvedPolicy) {
69 checkNotNull(resolvedPolicy);
70 Set<IovisorModuleInstanceId> ioms = new HashSet<>();
71 List<IovisorModuleInstanceId> tempIoms = new ArrayList<>();
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());
82 ioms.addAll(tempIoms);
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());
93 ioms.addAll(tempIoms);
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));
103 String buildPolicyUris(ResolvedPolicy resolvedPolicy) {
104 // TODO Move String definition of URIs to common place, perhaps something like IidFactory ?
106 new StringBuilder("/restconf/operational/resolved-policy:resolved-policies/resolved-policy/");
107 base.append(resolvedPolicy.getConsumerTenantId().getValue());
109 base.append(resolvedPolicy.getConsumerEpgId().getValue());
111 base.append(resolvedPolicy.getProviderTenantId().getValue());
113 base.append(resolvedPolicy.getProviderEpgId().getValue());
115 return base.toString();