2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.frm.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.frm.ForwardingRulesCommiter;
13 import org.opendaylight.controller.frm.ForwardingRulesManager;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import java.util.Collections;
24 * AbstractChangeListner implemented basic {@link AsyncDataChangeEvent} processing for
25 * flow node subDataObject (flows, groups and meters).
27 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
30 public abstract class AbstractListeningCommiter <T extends DataObject> implements ForwardingRulesCommiter<T> {
32 protected ForwardingRulesManager provider;
34 protected final Class<T> clazz;
36 public AbstractListeningCommiter (ForwardingRulesManager provider, Class<T> clazz) {
37 this.provider = Preconditions.checkNotNull(provider, "ForwardingRulesManager can not be null!");
38 this.clazz = Preconditions.checkNotNull(clazz, "Class can not be null!");
42 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changeEvent) {
43 Preconditions.checkNotNull(changeEvent,"Async ChangeEvent can not be null!");
45 /* All DataObjects for create */
46 final Map<InstanceIdentifier<?>, DataObject> createdData = changeEvent.getCreatedData() != null
47 ? changeEvent.getCreatedData() : Collections.<InstanceIdentifier<?>, DataObject> emptyMap();
48 /* All DataObjects for remove */
49 final Set<InstanceIdentifier<?>> removeData = changeEvent.getRemovedPaths() != null
50 ? changeEvent.getRemovedPaths() : Collections.<InstanceIdentifier<?>> emptySet();
51 /* All DataObjects for updates */
52 final Map<InstanceIdentifier<?>, DataObject> updateData = changeEvent.getUpdatedData() != null
53 ? changeEvent.getUpdatedData() : Collections.<InstanceIdentifier<?>, DataObject> emptyMap();
54 /* All Original DataObjects */
55 final Map<InstanceIdentifier<?>, DataObject> originalData = changeEvent.getOriginalData() != null
56 ? changeEvent.getOriginalData() : Collections.<InstanceIdentifier<?>, DataObject> emptyMap();
58 this.createData(createdData);
59 this.updateData(updateData, originalData);
60 this.removeData(removeData, originalData);
64 * Method return wildCardPath for Listener registration
65 * and for identify the correct KeyInstanceIdentifier from data;
67 protected abstract InstanceIdentifier<T> getWildCardPath();
71 @SuppressWarnings("unchecked")
72 private void createData(final Map<InstanceIdentifier<?>, DataObject> createdData) {
73 final Set<InstanceIdentifier<?>> keys = createdData.keySet() != null
74 ? createdData.keySet() : Collections.<InstanceIdentifier<?>> emptySet();
75 for (InstanceIdentifier<?> key : keys) {
76 if (clazz.equals(key.getTargetType())) {
77 final InstanceIdentifier<FlowCapableNode> nodeIdent =
78 key.firstIdentifierOf(FlowCapableNode.class);
79 if (preConfigurationCheck(nodeIdent)) {
80 InstanceIdentifier<T> createKeyIdent = key.firstIdentifierOf(clazz);
81 final Optional<DataObject> value = Optional.of(createdData.get(key));
82 if (value.isPresent()) {
83 this.add(createKeyIdent, (T)value.get(), nodeIdent);
90 @SuppressWarnings("unchecked")
91 private void updateData(final Map<InstanceIdentifier<?>, DataObject> updateData,
92 final Map<InstanceIdentifier<?>, DataObject> originalData) {
94 final Set<InstanceIdentifier<?>> keys = updateData.keySet() != null
95 ? updateData.keySet() : Collections.<InstanceIdentifier<?>> emptySet();
96 for (InstanceIdentifier<?> key : keys) {
97 if (clazz.equals(key.getTargetType())) {
98 final InstanceIdentifier<FlowCapableNode> nodeIdent =
99 key.firstIdentifierOf(FlowCapableNode.class);
100 if (preConfigurationCheck(nodeIdent)) {
101 InstanceIdentifier<T> updateKeyIdent = key.firstIdentifierOf(clazz);
102 final Optional<DataObject> value = Optional.of(updateData.get(key));
103 final Optional<DataObject> original = Optional.of(originalData.get(key));
104 if (value.isPresent() && original.isPresent()) {
105 this.update(updateKeyIdent, (T)original.get(), (T)value.get(), nodeIdent);
112 @SuppressWarnings("unchecked")
113 private void removeData(final Set<InstanceIdentifier<?>> removeData,
114 final Map<InstanceIdentifier<?>, DataObject> originalData) {
116 for (InstanceIdentifier<?> key : removeData) {
117 if (clazz.equals(key.getTargetType())) {
118 final InstanceIdentifier<FlowCapableNode> nodeIdent =
119 key.firstIdentifierOf(FlowCapableNode.class);
120 if (preConfigurationCheck(nodeIdent)) {
121 final InstanceIdentifier<T> ident = key.firstIdentifierOf(clazz);
122 final DataObject removeValue = originalData.get(key);
123 this.remove(ident, (T)removeValue, nodeIdent);
129 private boolean preConfigurationCheck(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
130 Preconditions.checkNotNull(nodeIdent, "FlowCapableNode ident can not be null!");
131 return provider.isNodeActive(nodeIdent);