Merge "Bug 8084 - FilterContentValidator.getKeyValues creates invalid YII key values"
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / controller / config / yang / netconf / mdsal / notification / CapabilityChangeNotificationProducer.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.controller.config.yang.netconf.mdsal.notification;
10
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Sets;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Set;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
22 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.changed.by.parms.ChangedByBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.changed.by.parms.changed.by.server.or.user.ServerBuilder;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * Listens on capabilities changes in data store and publishes them to base
37  * netconf notification stream listener.
38  */
39 public final class CapabilityChangeNotificationProducer extends OperationalDatastoreListener<Capabilities> {
40
41     private static final InstanceIdentifier<Capabilities> CAPABILITIES_INSTANCE_IDENTIFIER =
42             InstanceIdentifier.create(NetconfState.class).child(Capabilities.class);
43     private static final Logger LOG = LoggerFactory.getLogger(CapabilityChangeNotificationProducer.class);
44
45     private final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration;
46     private final ListenerRegistration capabilityChangeListenerRegistration;
47
48     public CapabilityChangeNotificationProducer(final NetconfNotificationCollector netconfNotificationCollector,
49                                                 final DataBroker dataBroker) {
50         super(CAPABILITIES_INSTANCE_IDENTIFIER);
51         this.baseNotificationPublisherRegistration = netconfNotificationCollector.registerBaseNotificationPublisher();
52         this.capabilityChangeListenerRegistration = registerOnChanges(dataBroker);
53     }
54
55     @Override
56     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Capabilities>> changes) {
57         for (DataTreeModification<Capabilities> change : changes) {
58             final DataObjectModification<Capabilities> rootNode = change.getRootNode();
59             final DataObjectModification.ModificationType modificationType = rootNode.getModificationType();
60             switch (modificationType) {
61                 case WRITE: {
62                     final Capabilities dataAfter = rootNode.getDataAfter();
63                     final Capabilities dataBefore = rootNode.getDataBefore();
64                     final Set<Uri> before = dataBefore != null ? ImmutableSet.copyOf(dataBefore.getCapability()) :
65                             Collections.emptySet();
66                     final Set<Uri> after = dataAfter != null ? ImmutableSet.copyOf(dataAfter.getCapability()) :
67                             Collections.emptySet();
68                     final Set<Uri> added = Sets.difference(after, before);
69                     final Set<Uri> removed = Sets.difference(before, after);
70                     publishNotification(added, removed);
71                     break;
72                 }
73                 case DELETE: {
74                     final Capabilities dataBeforeDelete = rootNode.getDataBefore();
75                     if (dataBeforeDelete != null) {
76                         final Set<Uri> removed = ImmutableSet.copyOf(dataBeforeDelete.getCapability());
77                         publishNotification(Collections.emptySet(), removed);
78                     }
79                     break;
80                 }
81                 default:
82                     LOG.debug("Received intentionally unhandled type: {}.", modificationType);
83             }
84         }
85
86     }
87
88     private void publishNotification(Set<Uri> added, Set<Uri> removed) {
89         final NetconfCapabilityChangeBuilder netconfCapabilityChangeBuilder = new NetconfCapabilityChangeBuilder();
90         netconfCapabilityChangeBuilder.setChangedBy(new ChangedByBuilder().setServerOrUser(new ServerBuilder()
91                 .setServer(true).build()).build());
92         netconfCapabilityChangeBuilder.setAddedCapability(ImmutableList.copyOf(added));
93         netconfCapabilityChangeBuilder.setDeletedCapability(ImmutableList.copyOf(removed));
94         // TODO modified should be computed ... but why ?
95         netconfCapabilityChangeBuilder.setModifiedCapability(Collections.<Uri>emptyList());
96         baseNotificationPublisherRegistration.onCapabilityChanged(netconfCapabilityChangeBuilder.build());
97     }
98
99     /**
100      * Invoked by blueprint.
101      */
102     public void close() {
103         if (baseNotificationPublisherRegistration != null) {
104             baseNotificationPublisherRegistration.close();
105         }
106         if (capabilityChangeListenerRegistration != null) {
107             capabilityChangeListenerRegistration.close();
108         }
109     }
110 }