Added Update and Remove commands code
[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     public static Map<InstanceIdentifier<Node>, Node> extractCreatedOrUpdatedOrRemoved(
78             Collection<DataTreeModification<Node>> changes, Class<Node> class1) {
79         // TODO Auto-generated method stub
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             result.put(key, created);
86             Node updated = getUpdated(mod);
87             result.put(key, updated);
88             Node deleted = getRemoved(mod);
89             result.put(key, deleted);
90         }
91         return null;
92     }
93
94     /*
95     public static <T extends Augmentation<Node>> Map<InstanceIdentifier<? extends DataObject>, T> extractCreated(
96             Collection<DataTreeModification<Node>> changes, Class<T> class1) {
97         // TODO Auto-generated method stub
98         Map<InstanceIdentifier<?>, T> result =
99             new HashMap<InstanceIdentifier<?>, T>();
100         if(changes != null && !changes.isEmpty()) {
101             for(DataTreeModification<Node> change : changes) {
102                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
103                 final DataObjectModification<Node> mod = change.getRootNode();
104                 Node created = getCreated(mod);
105                 if(created != null) {
106                     T logicalSwitch = created.getAugmentation(class1);
107                     created.getKey().getNodeId().get
108                     logicalSwitch.
109                     InstanceIdentifier<?> iid = change.getRootPath().getRootIdentifier()..augmentation(class1);
110                     if(logicalSwitch != null) {
111                         result.put(iid, logicalSwitch);
112                     }
113                 }
114             }
115         }
116         return result;
117     }
118     */
119
120 }