Merge "Added support to update flows for induvidual security rule add/remove , after...
[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.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchAugmentation;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
19 import org.opendaylight.yangtools.yang.binding.Augmentation;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class TransactUtils {
26     private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class);
27
28     public static Node getCreated(DataObjectModification<Node> mod) {
29         if((mod.getModificationType() == ModificationType.WRITE)
30                         && (mod.getDataBefore() == null)){
31             return mod.getDataAfter();
32         }
33         return null;
34     }
35
36     public static Node getRemoved(DataObjectModification<Node> mod) {
37         if(mod.getModificationType() == ModificationType.DELETE){
38             return mod.getDataBefore();
39         }
40         return null;
41     }
42
43     public static Node getUpdated(DataObjectModification<Node> mod) {
44         Node node = null;
45         switch(mod.getModificationType()) {
46             case SUBTREE_MODIFIED:
47                 node = mod.getDataAfter();
48                 break;
49             case WRITE:
50                 if(mod.getDataBefore() !=  null) {
51                     node = mod.getDataAfter();
52                 }
53                 break;
54             default:
55                 break;
56         }
57         return node;
58     }
59
60     public static Node getOriginal(DataObjectModification<Node> mod) {
61         Node node = null;
62         switch(mod.getModificationType()) {
63             case SUBTREE_MODIFIED:
64                 node = mod.getDataBefore();
65                 break;
66             case WRITE:
67                 if(mod.getDataBefore() !=  null) {
68                     node = mod.getDataBefore();
69                 }
70                 break;
71             case DELETE:
72                 node = mod.getDataBefore();
73                 break;
74             default:
75                 break;
76         }
77         return node;
78     }
79
80     public static Map<InstanceIdentifier<Node>, Node> extractCreatedOrUpdatedOrRemoved(
81             Collection<DataTreeModification<Node>> changes, Class<Node> class1) {
82         // TODO Auto-generated method stub
83         Map<InstanceIdentifier<Node>, Node> result = new HashMap<InstanceIdentifier<Node>, Node>();
84         for(DataTreeModification<Node> change : changes) {
85             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
86             final DataObjectModification<Node> mod = change.getRootNode();
87             Node created = getCreated(mod);
88             result.put(key, created);
89             Node updated = getUpdated(mod);
90             result.put(key, updated);
91             Node deleted = getRemoved(mod);
92             result.put(key, deleted);
93         }
94         return null;
95     }
96
97     /*
98     public static <T extends Augmentation<Node>> Map<InstanceIdentifier<? extends DataObject>, T> extractCreated(
99             Collection<DataTreeModification<Node>> changes, Class<T> class1) {
100         // TODO Auto-generated method stub
101         Map<InstanceIdentifier<?>, T> result =
102             new HashMap<InstanceIdentifier<?>, T>();
103         if(changes != null && !changes.isEmpty()) {
104             for(DataTreeModification<Node> change : changes) {
105                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
106                 final DataObjectModification<Node> mod = change.getRootNode();
107                 Node created = getCreated(mod);
108                 if(created != null) {
109                     T logicalSwitch = created.getAugmentation(class1);
110                     created.getKey().getNodeId().get
111                     logicalSwitch.
112                     InstanceIdentifier<?> iid = change.getRootPath().getRootIdentifier()..augmentation(class1);
113                     if(logicalSwitch != null) {
114                         result.put(iid, logicalSwitch);
115                     }
116                 }
117             }
118         }
119         return result;
120     }
121     */
122
123 }