Changes to LogicalSwitchUpdateCommand in transact as per new yang
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / TransactUtils.java
1 /*
2  * Copyright (c) 2015 China Telecom Beijing Research Institute 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.ovsdb.hwvtepsouthbound.transact;
9
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class TransactUtils {
23     private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class);
24
25     public static Node getCreated(DataObjectModification<Node> mod) {
26         if((mod.getModificationType() == ModificationType.WRITE)
27                         && (mod.getDataBefore() == null)){
28             return mod.getDataAfter();
29         }
30         return null;
31     }
32
33     public static Node getRemoved(DataObjectModification<Node> mod) {
34         if(mod.getModificationType() == ModificationType.DELETE){
35             return mod.getDataBefore();
36         }
37         return null;
38     }
39
40     public static Node getUpdated(DataObjectModification<Node> mod) {
41         Node node = null;
42         switch(mod.getModificationType()) {
43             case SUBTREE_MODIFIED:
44                 node = mod.getDataAfter();
45                 break;
46             case WRITE:
47                 if(mod.getDataBefore() !=  null) {
48                     node = mod.getDataAfter();
49                 }
50                 break;
51             default:
52                 break;
53         }
54         return node;
55     }
56
57     public static Node getOriginal(DataObjectModification<Node> mod) {
58         Node node = null;
59         switch(mod.getModificationType()) {
60             case SUBTREE_MODIFIED:
61                 node = mod.getDataBefore();
62                 break;
63             case WRITE:
64                 if(mod.getDataBefore() !=  null) {
65                     node = mod.getDataBefore();
66                 }
67                 break;
68             case DELETE:
69                 node = mod.getDataBefore();
70                 break;
71             default:
72                 break;
73         }
74         return node;
75     }
76
77     //TODO: change this function to be generic
78     public static Map<InstanceIdentifier<Node>, Node> extractCreatedOrUpdatedOrRemoved(
79             Collection<DataTreeModification<Node>> changes, Class<Node> class1) {
80         Map<InstanceIdentifier<Node>, Node> result = new HashMap<InstanceIdentifier<Node>, Node>();
81         for(DataTreeModification<Node> change : changes) {
82             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
83             final DataObjectModification<Node> mod = change.getRootNode();
84             Node created = getCreated(mod);
85             if (created != null) {
86                 result.put(key, created);
87             }
88             Node updated = getUpdated(mod);
89             if (updated != null) {
90                 result.put(key, updated);
91             }
92             Node deleted = getRemoved(mod);
93             if (deleted != null) {
94                 result.put(key, deleted);
95             }
96         }
97         return result;
98     }
99
100     /*
101     public static <T extends Augmentation<Node>> Map<InstanceIdentifier<? extends DataObject>, T> extractCreated(
102             Collection<DataTreeModification<Node>> changes, Class<T> class1) {
103         // TODO Auto-generated method stub
104         Map<InstanceIdentifier<?>, T> result =
105             new HashMap<InstanceIdentifier<?>, T>();
106         if(changes != null && !changes.isEmpty()) {
107             for(DataTreeModification<Node> change : changes) {
108                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
109                 final DataObjectModification<Node> mod = change.getRootNode();
110                 Node created = getCreated(mod);
111                 if(created != null) {
112                     T logicalSwitch = created.getAugmentation(class1);
113                     created.getKey().getNodeId().get
114                     logicalSwitch.
115                     InstanceIdentifier<?> iid = change.getRootPath().getRootIdentifier()..augmentation(class1);
116                     if(logicalSwitch != null) {
117                         result.put(iid, logicalSwitch);
118                     }
119                 }
120             }
121         }
122         return result;
123     }
124     */
125
126 }