Bump upstreams
[openflowplugin.git] / applications / device-ownership-service / src / main / java / org / opendaylight / openflowplugin / applications / deviceownershipservice / impl / DeviceOwnershipServiceImpl.java
1 /*
2  * Copyright (c) 2017 Lumina Networks, Inc.  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.openflowplugin.applications.deviceownershipservice.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Optional;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.regex.Pattern;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
20 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
21 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
22 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange;
23 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.osgi.service.component.annotations.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Singleton
34 @Component(service = DeviceOwnershipService.class)
35 public final class DeviceOwnershipServiceImpl
36         implements DeviceOwnershipService, EntityOwnershipListener, AutoCloseable {
37     private static final Logger LOG = LoggerFactory.getLogger(DeviceOwnershipServiceImpl.class);
38     private static final String SERVICE_ENTITY_TYPE = "org.opendaylight.mdsal.ServiceEntityType";
39     private static final Pattern NODE_ID_PATTERN = Pattern.compile("^openflow:\\d+");
40
41     private final ConcurrentMap<String, EntityOwnershipState> ownershipStateCache = new ConcurrentHashMap<>();
42     private final EntityOwnershipService entityOwnershipService;
43     private final Registration registration;
44
45     @Inject
46     @Activate
47     public DeviceOwnershipServiceImpl(@Reference final EntityOwnershipService entityOwnershipService) {
48         this.entityOwnershipService = requireNonNull(entityOwnershipService);
49         registration = entityOwnershipService.registerListener(SERVICE_ENTITY_TYPE, this);
50         LOG.info("DeviceOwnershipService started");
51     }
52
53     @PreDestroy
54     @Deactivate
55     @Override
56     public void close() {
57         registration.close();
58         LOG.info("DeviceOwnershipService closed");
59     }
60
61     @Override
62     public boolean isEntityOwned(final String nodeId) {
63         EntityOwnershipState state = ownershipStateCache.get(nodeId);
64         if (state == null) {
65             LOG.debug("The ownership state for node {} is not cached. Retrieving from the EOS Datastore", nodeId);
66             Optional<EntityOwnershipState> status = getCurrentOwnershipStatus(nodeId);
67             if (status.isPresent()) {
68                 state = status.orElseThrow();
69                 ownershipStateCache.put(nodeId, state);
70             } else {
71                 LOG.warn("Fetching ownership status failed for node {}", nodeId);
72             }
73         }
74         return state != null && state.equals(EntityOwnershipState.IS_OWNER);
75     }
76
77     @Override
78     public void ownershipChanged(final org.opendaylight.mdsal.eos.binding.api.Entity entity,
79             final EntityOwnershipStateChange change, final boolean inJeopardy) {
80         final String entityName = entity.getIdentifier().firstKeyOf(Entity.class).getName();
81         if (entityName != null && isOpenFlowEntity(entityName)) {
82             LOG.info("Entity ownership change received for node : {} : {}", entityName, change);
83             if (!change.isOwner() && !change.hasOwner() && !inJeopardy) {
84                 LOG.debug("Entity for node {} is unregistered.", entityName);
85                 ownershipStateCache.remove(entityName);
86             } else if (!change.isOwner() && change.hasOwner()) {
87                 ownershipStateCache.put(entityName, EntityOwnershipState.OWNED_BY_OTHER);
88             } else if (change.isOwner()) {
89                 LOG.trace("Entity for node : {} is registered", entityName);
90                 ownershipStateCache.put(entityName, EntityOwnershipState.IS_OWNER);
91             }
92         }
93     }
94
95     private Optional<EntityOwnershipState> getCurrentOwnershipStatus(final String nodeId) {
96         org.opendaylight.mdsal.eos.binding.api.Entity entity = createNodeEntity(nodeId);
97         final var ownershipStatus = entityOwnershipService.getOwnershipState(entity);
98         ownershipStatus.ifPresent(status -> {
99             LOG.trace("Fetched ownership status for node {} is {}", nodeId, status);
100         });
101         return ownershipStatus;
102     }
103
104     private static org.opendaylight.mdsal.eos.binding.api.Entity createNodeEntity(final String nodeId) {
105         return new org.opendaylight.mdsal.eos.binding.api.Entity(SERVICE_ENTITY_TYPE, nodeId);
106     }
107
108     private static boolean isOpenFlowEntity(final String entity) {
109         return NODE_ID_PATTERN.matcher(entity).matches();
110     }
111 }