43f48a50e56175a2297e78e32478f80404ff839d
[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     static val LOG = LoggerFactory.getLogger(NodeChangeCommiter);
68
69     @Property
70     val FlowCapableInventoryProvider manager;
71
72     new(FlowCapableInventoryProvider manager) {
73         _manager = manager;
74     }
75
76     override onNodeConnectorRemoved(NodeConnectorRemoved connector) {
77         val ref = connector.nodeConnectorRef;
78
79         // Check path
80         val it = manager.startChange()
81
82         LOG.debug("removing node connector : " + ref.value.toString());
83
84         removeOperationalData(ref.value as InstanceIdentifier<? extends DataObject>);
85         commit()
86     }
87
88     override onNodeConnectorUpdated(NodeConnectorUpdated connector) {
89         val ref = connector.nodeConnectorRef;
90
91         val flowConnector = connector.getAugmentation(FlowCapableNodeConnectorUpdated);
92
93         val it = manager.startChange()
94         val data = new NodeConnectorBuilder(connector);
95         data.key = (new NodeConnectorKey(connector.id))
96         if (flowConnector != null) {
97             val augment = flowConnector.toInventoryAugment()
98             data.addAugmentation(FlowCapableNodeConnector, augment)
99         }
100
101         LOG.debug("updating node connector : " + ref.value.toString());
102
103         putOperationalData(ref.value as InstanceIdentifier<NodeConnector>, data.build());
104         commit()
105     }
106
107     override onNodeRemoved(NodeRemoved node) {
108         val ref = node.nodeRef;
109         val it = manager.startChange()
110
111         LOG.debug("removing node : " + ref.value.toString());
112
113         removeOperationalData(ref.value as InstanceIdentifier<? extends DataObject>);
114         commit()
115     }
116
117     override onNodeUpdated(NodeUpdated node) {
118         val ref = node.nodeRef;
119         val flowNode = node.getAugmentation(FlowCapableNodeUpdated);
120
121         val it = manager.startChange()
122         val data = new NodeBuilder(node);
123         data.key = (new NodeKey(node.id))
124         if (flowNode != null) {
125             val augment = flowNode.toInventoryAugment();
126             data.addAugmentation(FlowCapableNode, augment)
127         }
128
129         LOG.debug("updating node : " + ref.value.toString());
130
131         putOperationalData(ref.value as InstanceIdentifier<Node>, data.build())
132         commit()
133     }
134 }

©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.