c4ab0e3b2c967efe66fc20235146167798d10b53
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / listener / ItemLifecycleListenerImpl.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.openflowplugin.impl.rpc.listener;
10
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
13 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.Identifiable;
16 import org.opendaylight.yangtools.yang.binding.Identifier;
17 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * General implementation of {@link ItemLifecycleListener} - keeping of DS/operational reflection up-to-date
23  */
24 public class ItemLifecycleListenerImpl implements ItemLifecycleListener {
25
26     private static final Logger LOG = LoggerFactory.getLogger(ItemLifecycleListenerImpl.class);
27     public static final String NOT_ABLE_TO_WRITE_TO_TRANSACTION = "Not able to write to transaction: {}";
28
29     private final DeviceContext deviceContext;
30
31     public ItemLifecycleListenerImpl(DeviceContext deviceContext) {
32         this.deviceContext = deviceContext;
33     }
34
35     @Override
36     public <I extends Identifiable<K> & DataObject, K extends Identifier<I>> void onAdded(KeyedInstanceIdentifier<I, K> itemPath, I itemBody) {
37         try {
38             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, itemPath, itemBody);
39             deviceContext.submitTransaction();
40         } catch (Exception e) {
41             LOG.warn(NOT_ABLE_TO_WRITE_TO_TRANSACTION, e);
42         }
43     }
44
45     @Override
46     public <I extends Identifiable<K> & DataObject, K extends Identifier<I>> void onRemoved(KeyedInstanceIdentifier<I, K> itemPath) {
47         try {
48             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, itemPath);
49             deviceContext.submitTransaction();
50         } catch (Exception e) {
51             LOG.warn(NOT_ABLE_TO_WRITE_TO_TRANSACTION, e);
52         }
53     }
54
55     @Override
56     public <I extends Identifiable<K> & DataObject, K extends Identifier<I>> void onUpdated(KeyedInstanceIdentifier<I, K> itemPath, I itemBody) {
57         try {
58             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, itemPath);
59             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, itemPath, itemBody);
60             deviceContext.submitTransaction();
61         } catch (Exception e) {
62             LOG.warn(NOT_ABLE_TO_WRITE_TO_TRANSACTION, e);
63         }
64     }
65 }