2 * Copyright (c) 2016 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.ios_xe_provider.impl.listener;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.manager.PolicyManager;
19 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyManagerImpl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.RendererName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.Renderers;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.Renderer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.RendererKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.RendererPolicy;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.Configuration;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import javax.annotation.Nonnull;
32 import java.util.Collection;
33 import java.util.Optional;
36 * Purpose: process changes of configured renderer policies
38 public class RendererConfigurationListenerImpl implements DataTreeChangeListener<RendererPolicy>, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(RendererConfigurationListenerImpl.class);
41 private final ListenerRegistration<RendererConfigurationListenerImpl> listenerRegistration;
42 private final PolicyManager policyManager;
44 public RendererConfigurationListenerImpl(final DataBroker dataBroker,
45 final PolicyManager policyManager) {
46 this.policyManager = Preconditions.checkNotNull(policyManager, "missing endpoint template cache");
47 final InstanceIdentifier<RendererPolicy> policyPath = InstanceIdentifier.create(Renderers.class)
48 .child(Renderer.class, new RendererKey(PolicyManagerImpl.IOS_XE_RENDERER))
49 .child(RendererPolicy.class);
51 final DataTreeIdentifier<RendererPolicy> treePath = new DataTreeIdentifier<>(
52 LogicalDatastoreType.CONFIGURATION,
54 listenerRegistration = dataBroker.registerDataTreeChangeListener(treePath, this);
55 LOG.info("renderer-policy listener registered");
59 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<RendererPolicy>> collection) {
60 LOG.debug("renderer policy configuration changed");
61 for (DataTreeModification<RendererPolicy> rendererPolicyDataTreeModification : collection) {
62 final DataObjectModification<RendererPolicy> rootNode = rendererPolicyDataTreeModification.getRootNode();
64 final RendererPolicy dataBefore = rootNode.getDataBefore();
65 final RendererPolicy dataAfter = rootNode.getDataAfter();
66 // Policy configuration
67 Configuration oldConfig = null;
68 Configuration newConfig = null;
70 if (dataBefore != null) {
71 oldConfig = dataBefore.getConfiguration();
73 if (dataAfter != null) {
74 newConfig = dataAfter.getConfiguration();
75 if (dataAfter.getVersion() != null) {
76 version = dataAfter.getVersion();
79 policyManager.syncPolicy(newConfig, oldConfig, version);
85 listenerRegistration.close();