ee68090a15d0451815b88e36aaad4b65fbbfe1c5
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / recovery / AclInterfaceRecoveryHandler.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.netvirt.aclservice.recovery;
9
10 import java.util.Optional;
11
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.netvirt.aclservice.listeners.AclInterfaceListener;
17 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
18 import org.opendaylight.serviceutils.srm.ServiceRecoveryInterface;
19 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.NetvirtAclInterface;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @Singleton
27 public class AclInterfaceRecoveryHandler implements ServiceRecoveryInterface {
28
29     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceRecoveryHandler.class);
30
31     private final DataBroker dataBroker;
32     private final AclInterfaceListener aclInterfaceListener;
33
34     @Inject
35     public AclInterfaceRecoveryHandler(ServiceRecoveryRegistry serviceRecoveryRegistry, DataBroker dataBroker,
36             AclInterfaceListener aclInterfaceListener) {
37         serviceRecoveryRegistry.registerServiceRecoveryRegistry(buildServiceRegistryKey(), this);
38         this.dataBroker = dataBroker;
39         this.aclInterfaceListener = aclInterfaceListener;
40     }
41
42     @Override
43     public void recoverService(String entityId) {
44         LOG.info("Recover ACL interface {}", entityId);
45         Optional<Interface> interfaceOp = AclServiceUtils.getInterface(dataBroker, entityId);
46         if (interfaceOp.isPresent()) {
47             Interface aclInterface = interfaceOp.get();
48             InstanceIdentifier<Interface> interfaceIdentifier = AclServiceUtils.getInterfaceIdentifier(entityId);
49             aclInterfaceListener.add(interfaceIdentifier, aclInterface);
50         } else {
51             LOG.warn("{} is not valid ACL interface", entityId);
52         }
53     }
54
55     private String buildServiceRegistryKey() {
56         return NetvirtAclInterface.class.toString();
57     }
58 }