Merge "L3: Add eth to br-ex"
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / AbstractDataTreeListener.java
1 /*
2  * Copyright (c) 2013, 2015 Dell, 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.ovsdb.openstack.netvirt.sfc;
10
11 import java.util.Collection;
12
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * AbstractChangeListner implemented basic {@link AsyncDataChangeEvent} processing for
24  * netvirt-sfc data change listener.
25  */
26 public abstract class AbstractDataTreeListener<T extends DataObject> implements INetvirtSfcDataProcessor<T> {
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractDataTreeListener.class);
28     protected INetvirtSfcOF13Provider provider;
29     protected final Class<T> clazz;
30
31     public AbstractDataTreeListener(INetvirtSfcOF13Provider provider, Class<T> clazz) {
32         this.provider = Preconditions.checkNotNull(provider, "provider can not be null!");
33         this.clazz = Preconditions.checkNotNull(clazz, "Class can not be null!");
34     }
35
36     @Override
37     public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
38         Preconditions.checkNotNull(changes, "Changes may not be null!");
39
40         LOG.info("Received Data Tree Changed ...", changes);
41         for (DataTreeModification<T> change : changes) {
42             final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
43             final DataObjectModification<T> mod = change.getRootNode();
44             LOG.info("Received Data Tree Changed Update of Type={} for Key={}", mod.getModificationType(), key);
45             switch (mod.getModificationType()) {
46                 case DELETE:
47                     remove(key, mod.getDataBefore());
48                     break;
49                 case SUBTREE_MODIFIED:
50                     update(key, mod.getDataBefore(), mod.getDataAfter());
51                     break;
52                 case WRITE:
53                     if (mod.getDataBefore() == null) {
54                         add(key, mod.getDataAfter());
55                     } else {
56                         update(key, mod.getDataBefore(), mod.getDataAfter());
57                     }
58                     break;
59                 default:
60                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
61             }
62         }
63     }
64 }