Change logical-switch-ref to iid in hwvtep.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 static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
26 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
27 import org.opendaylight.ovsdb.lib.notation.UUID;
28 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
29 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
30 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
31 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocatorSet;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.base.Optional;
41
42 public class TransactUtils {
43     private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class);
44
45     public static Node getCreated(DataObjectModification<Node> mod) {
46         if((mod.getModificationType() == ModificationType.WRITE)
47                         && (mod.getDataBefore() == null)){
48             return mod.getDataAfter();
49         }
50         return null;
51     }
52
53     public static Node getRemoved(DataObjectModification<Node> mod) {
54         if(mod.getModificationType() == ModificationType.DELETE){
55             return mod.getDataBefore();
56         }
57         return null;
58     }
59
60     public static Node getUpdated(DataObjectModification<Node> mod) {
61         Node node = null;
62         switch(mod.getModificationType()) {
63             case SUBTREE_MODIFIED:
64                 node = mod.getDataAfter();
65                 break;
66             case WRITE:
67                 if(mod.getDataBefore() !=  null) {
68                     node = mod.getDataAfter();
69                 }
70                 break;
71             default:
72                 break;
73         }
74         return node;
75     }
76
77     public static Node getOriginal(DataObjectModification<Node> mod) {
78         Node node = null;
79         switch(mod.getModificationType()) {
80             case SUBTREE_MODIFIED:
81                 node = mod.getDataBefore();
82                 break;
83             case WRITE:
84                 if(mod.getDataBefore() !=  null) {
85                     node = mod.getDataBefore();
86                 }
87                 break;
88             case DELETE:
89                 node = mod.getDataBefore();
90                 break;
91             default:
92                 break;
93         }
94         return node;
95     }
96
97     //TODO: change this function to be generic
98     public static Map<InstanceIdentifier<Node>, Node> extractCreatedOrUpdatedOrRemoved(
99             Collection<DataTreeModification<Node>> changes, Class<Node> class1) {
100         Map<InstanceIdentifier<Node>, Node> result = new HashMap<InstanceIdentifier<Node>, Node>();
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                 result.put(key, created);
107             }
108             Node updated = getUpdated(mod);
109             if (updated != null) {
110                 result.put(key, updated);
111             }
112             Node deleted = getRemoved(mod);
113             if (deleted != null) {
114                 result.put(key, deleted);
115             }
116         }
117         return result;
118     }
119
120     /*
121     public static <T extends Augmentation<Node>> Map<InstanceIdentifier<? extends DataObject>, T> extractCreated(
122             Collection<DataTreeModification<Node>> changes, Class<T> class1) {
123         // TODO Auto-generated method stub
124         Map<InstanceIdentifier<?>, T> result =
125             new HashMap<InstanceIdentifier<?>, T>();
126         if(changes != null && !changes.isEmpty()) {
127             for(DataTreeModification<Node> change : changes) {
128                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
129                 final DataObjectModification<Node> mod = change.getRootNode();
130                 Node created = getCreated(mod);
131                 if(created != null) {
132                     T logicalSwitch = created.getAugmentation(class1);
133                     created.getKey().getNodeId().get
134                     logicalSwitch.
135                     InstanceIdentifier<?> iid = change.getRootPath().getRootIdentifier()..augmentation(class1);
136                     if(logicalSwitch != null) {
137                         result.put(iid, logicalSwitch);
138                     }
139                 }
140             }
141         }
142         return result;
143     }
144     */
145
146     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNodeFromConfig(
147             ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
148         Optional<D> node = Optional.absent();
149         try {
150             node = transaction.read(LogicalDatastoreType.CONFIGURATION, connectionIid).checkedGet();
151         } catch (final ReadFailedException e) {
152             LOG.warn("Read Configration/DS for Node failed! {}", connectionIid, e);
153         }
154         return node;
155     }
156
157     public static UUID createPhysicalLocatorSet(HwvtepOperationalState hwvtepOperationalState, TransactionBuilder transaction, List<LocatorSet> locatorList) {
158         Set<UUID> locators = new HashSet<UUID>();
159         for (LocatorSet locator: locatorList) {
160             UUID locatorUuid = null;
161             @SuppressWarnings("unchecked")
162             InstanceIdentifier<TerminationPoint> iid =(InstanceIdentifier<TerminationPoint>) locator.getLocatorRef().getValue();
163             //try to find locator in operational DS
164             Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
165                     hwvtepOperationalState.getPhysicalLocatorAugmentation(iid);
166             if (operationalLocatorOptional.isPresent()) {
167                 //if exist, get uuid
168                 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
169                 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
170             } else {
171                 //if no, get it from config DS and create id
172                 Optional<TerminationPoint> configLocatorOptional =
173                         readNodeFromConfig(hwvtepOperationalState.getReadWriteTransaction(), iid);
174                 if (configLocatorOptional.isPresent()) {
175                     HwvtepPhysicalLocatorAugmentation locatorAugmentation =
176                             configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
177                     locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
178                 } else {
179                     LOG.warn("Create or update localMcastMac: No physical locator found in operational datastore!"
180                             + "Its indentifier is {}", locator.getLocatorRef().getValue());
181                 }
182             }
183             if (locatorUuid != null) {
184                 locators.add(locatorUuid);
185             }
186         }
187         PhysicalLocatorSet physicalLocatorSet = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocatorSet.class);
188         physicalLocatorSet.setLocators(locators);
189         String locatorSetUuid = "PhysicalLocatorSet_" + HwvtepSouthboundMapper.getRandomUUID();
190         transaction.add(op.insert(physicalLocatorSet).withId(locatorSetUuid));
191         return new UUID(locatorSetUuid);
192     }
193
194     public static UUID createPhysicalLocator(TransactionBuilder transaction, HwvtepPhysicalLocatorAugmentation inputLocator) {
195         LOG.debug("Creating a physical locator: {}", inputLocator.getDstIp());
196         PhysicalLocator physicalLocator = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocator.class);
197         setEncapsulationType(physicalLocator, inputLocator);
198         setDstIp(physicalLocator, inputLocator);
199         String locatorUuid = "PhysicalLocator_" + HwvtepSouthboundMapper.getRandomUUID();
200         transaction.add(op.insert(physicalLocator).withId(locatorUuid));
201         return new UUID(locatorUuid);
202     }
203
204     private static final void setEncapsulationType(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
205         if (inputLocator.getEncapsulationType() != null) {
206             String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(HwvtepSouthboundMapper.createEncapsulationType(""));
207             physicalLocator.setEncapsulationType(encapType);
208         }
209     }
210
211     private static final void setDstIp(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
212         if (inputLocator.getDstIp() != null) {
213             physicalLocator.setDstIp(inputLocator.getDstIp().getIpv4Address().getValue());
214         }
215     }
216 }