c2fbb6b39bc592eae0bd360124358c56e59c59bf
[sfc.git] /
1 /*
2  * Copyright (c) 2014, 2017 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 package org.opendaylight.sfc.sbrest.provider.listener;
9
10 import static org.opendaylight.sfc.provider.SfcProviderDebug.printTraceStart;
11 import static org.opendaylight.sfc.provider.SfcProviderDebug.printTraceStop;
12
13 import java.util.Collection;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.sfc.provider.api.SfcInstanceIdentifiers;
20 import org.opendaylight.sfc.sbrest.provider.task.RestOperation;
21 import org.opendaylight.sfc.sbrest.provider.task.SbRestSffTask;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class SbRestSffEntryDataListener extends SbRestAbstractDataListener<ServiceFunctionForwarder> {
27     private static final Logger LOG = LoggerFactory.getLogger(SbRestSffEntryDataListener.class);
28     private final ExecutorService executor = Executors.newFixedThreadPool(5);
29
30     public SbRestSffEntryDataListener() {
31         setInstanceIdentifier(SfcInstanceIdentifiers.SFF_ENTRY_IID);
32     }
33
34     public void setDataProvider(DataBroker dataBroker) {
35         setDataBroker(dataBroker);
36         registerAsDataChangeListener();
37     }
38
39     @Override
40     public void onDataTreeChanged(Collection<DataTreeModification<ServiceFunctionForwarder>> changes) {
41         printTraceStart(LOG);
42         for (DataTreeModification<ServiceFunctionForwarder> change: changes) {
43             DataObjectModification<ServiceFunctionForwarder> rootNode = change.getRootNode();
44             switch (rootNode.getModificationType()) {
45                 case SUBTREE_MODIFIED:
46                 case WRITE:
47                     ServiceFunctionForwarder updatedServiceFunctionForwarder = rootNode.getDataAfter();
48                     LOG.debug("\nUpdated Service Function Forwarder Name: {}",
49                             updatedServiceFunctionForwarder.getName());
50
51                     RestOperation restOp = rootNode.getDataBefore() == null ? RestOperation.POST
52                             : RestOperation.PUT;
53                     executor.execute(new SbRestSffTask(restOp, updatedServiceFunctionForwarder, executor));
54                     break;
55                 case DELETE:
56                     ServiceFunctionForwarder originalServiceFunctionForwarder = rootNode.getDataBefore();
57                     LOG.debug("\nDeleted Service Function Forwarder Name: {}",
58                             originalServiceFunctionForwarder.getName());
59
60                     executor.execute(new SbRestSffTask(RestOperation.DELETE, originalServiceFunctionForwarder,
61                             executor));
62                     break;
63                 default:
64                     break;
65             }
66         }
67
68         printTraceStop(LOG);
69     }
70 }