f5b79a7b46a7ec3c2043181f42e670491d528a59
[netvirt.git] / sfc / translator / src / main / java / org / opendaylight / netvirt / sfc / translator / portchain / NeutronPortPairListener.java
1 /*
2  * Copyright (c) 2016, 2017 Brocade Communications 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.netvirt.sfc.translator.portchain;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.netvirt.sfc.translator.DelegatingDataTreeListener;
15 import org.opendaylight.netvirt.sfc.translator.SfcMdsalHelper;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName;
17 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunctionKey;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarderKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.PortPairs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pairs.PortPair;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * OpenDaylight Neutron Port Pair yang models data change listener.
29  */
30 public class NeutronPortPairListener extends DelegatingDataTreeListener<PortPair> {
31     private static final Logger LOG = LoggerFactory.getLogger(NeutronPortPairListener.class);
32
33     private static final InstanceIdentifier<PortPair> PORT_PAIR_IID =
34             InstanceIdentifier.create(Neutron.class).child(PortPairs.class).child(PortPair.class);
35
36     private final SfcMdsalHelper sfcMdsalHelper;
37
38     public NeutronPortPairListener(DataBroker db) {
39         super(db,new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, PORT_PAIR_IID));
40         sfcMdsalHelper = new SfcMdsalHelper(db);
41     }
42
43     /**
44      * Method removes PortPair which is identified by InstanceIdentifier.
45      *
46      * @param deletedPortPair        - PortPair for removing
47      */
48     @Override
49     public void remove(PortPair deletedPortPair) {
50         LOG.info("Received remove port pair event {}", deletedPortPair);
51
52         ServiceFunctionKey sfKey = PortPairTranslator.getSFKey(deletedPortPair);
53         LOG.info("Removing service function {}", sfKey);
54         sfcMdsalHelper.removeServiceFunction(sfKey);
55
56         ServiceFunctionForwarder sff;
57         ServiceFunctionForwarder updatedSff;
58         SffName sffName = new SffName(SfcMdsalHelper.NETVIRT_LOGICAL_SFF_NAME);
59         sff = sfcMdsalHelper.readServiceFunctionForwarder(new ServiceFunctionForwarderKey(sffName));
60         updatedSff = PortPairGroupTranslator.removePortPairFromServiceFunctionForwarder(sff, deletedPortPair);
61         LOG.info("Updating service function forwarder as {}", updatedSff);
62         sfcMdsalHelper.addServiceFunctionForwarder(updatedSff);
63     }
64
65     /**
66      * Method updates the original PortPair to the update PortPair.
67      * Both are identified by same InstanceIdentifier.
68      *
69      * @param origPortPair       - original PortPair
70      * @param updatePortPair     - changed PortPair (contain updates)
71      */
72     @Override
73     public void update(PortPair origPortPair, PortPair updatePortPair) {
74         //NO-OP
75     }
76
77     /**
78      * Method adds the PortPair which is identified by InstanceIdentifier
79      * to device.
80      *
81      * @param newPortPair        - new PortPair
82      */
83     @Override
84     public void add(PortPair newPortPair) {
85         //NO-OP
86         // Port Pair data written in neutron data store will be used
87         // When user will create port chain.
88     }
89 }