Add test for generated code checking list of dependencies.
[controller.git] / opendaylight / md-sal / inventory-manager / src / main / java / org / opendaylight / controller / md / inventory / manager / FlowCapableInventoryProvider.xtend
1 /*
2  * Copyright (c) 2013 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.controller.md.inventory.manager
9
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService
16 import org.opendaylight.controller.sal.binding.api.data.DataProviderService
17 import org.opendaylight.yangtools.concepts.Registration
18 import org.opendaylight.yangtools.yang.binding.NotificationListener
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
20 import org.opendaylight.yangtools.yang.binding.DataObject
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
27 import static extension org.opendaylight.controller.md.inventory.manager.InventoryMapping.*
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
32 import org.slf4j.LoggerFactory
33
34 class FlowCapableInventoryProvider implements AutoCloseable {
35
36
37     static val LOG = LoggerFactory.getLogger(FlowCapableInventoryProvider);
38
39     @Property
40     DataProviderService dataService;
41
42     @Property
43     NotificationProviderService notificationService;
44     val NodeChangeCommiter changeCommiter = new NodeChangeCommiter(this);
45
46     Registration<NotificationListener> listenerRegistration
47
48     def void start() {
49         listenerRegistration = notificationService.registerNotificationListener(changeCommiter);
50         LOG.info("Flow Capable Inventory Provider started.");
51         
52     }
53
54     protected def startChange() {
55         return dataService.beginTransaction;
56     }
57
58     override close() {
59        LOG.info("Flow Capable Inventory Provider stopped.");
60         listenerRegistration?.close();
61     }
62     
63 }
64
65 class NodeChangeCommiter implements OpendaylightInventoryListener {
66
67     @Property
68     val FlowCapableInventoryProvider manager;
69
70     new(FlowCapableInventoryProvider manager) {
71         _manager = manager;
72     }
73
74     override onNodeConnectorRemoved(NodeConnectorRemoved connector) {
75         val ref = connector.nodeConnectorRef;
76
77         // Check path
78         val it = manager.startChange()
79         removeRuntimeData(ref.value as InstanceIdentifier<? extends DataObject>);
80         commit()
81     }
82
83     override onNodeConnectorUpdated(NodeConnectorUpdated connector) {
84         val ref = connector.nodeConnectorRef;
85
86         val flowConnector = connector.getAugmentation(FlowCapableNodeConnectorUpdated);
87
88         val it = manager.startChange()
89         val data = new NodeConnectorBuilder(connector);
90         data.key = (new NodeConnectorKey(connector.id))
91         if (flowConnector != null) {
92             val augment = flowConnector.toInventoryAugment()
93             data.addAugmentation(FlowCapableNodeConnector, augment)
94         }
95
96         putRuntimeData(ref.value as InstanceIdentifier<NodeConnector>, data.build());
97         commit()
98     }
99
100     override onNodeRemoved(NodeRemoved node) {
101         val ref = node.nodeRef;
102         val it = manager.startChange()
103
104         removeRuntimeData(ref.value as InstanceIdentifier<? extends DataObject>);
105         commit()
106     }
107
108     override onNodeUpdated(NodeUpdated node) {
109         val ref = node.nodeRef;
110         val flowNode = node.getAugmentation(FlowCapableNodeUpdated);
111
112         val it = manager.startChange()
113         val data = new NodeBuilder(node);
114         data.key = (new NodeKey(node.id))
115         if (flowNode != null) {
116             val augment = flowNode.toInventoryAugment();
117             data.addAugmentation(FlowCapableNode, augment)
118         }
119
120         putRuntimeData(ref.value as InstanceIdentifier<Node>, data.build())
121         commit()
122     }
123 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.