2 * Copyright © 2015, 2017 China Telecom Beijing Research Institute and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Lists;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
30 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
31 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
32 import org.opendaylight.ovsdb.lib.notation.UUID;
33 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
34 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
35 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
36 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocatorSet;
37 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalRouters;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Acls;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class TransactUtils {
54 private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class);
56 private TransactUtils(){
59 public static Node getCreated(DataObjectModification<Node> mod) {
60 if((mod.getModificationType() == ModificationType.WRITE)
61 && (mod.getDataBefore() == null)){
62 return mod.getDataAfter();
67 public static Node getRemoved(DataObjectModification<Node> mod) {
68 if(mod.getModificationType() == ModificationType.DELETE){
69 return mod.getDataBefore();
74 public static Node getUpdated(DataObjectModification<Node> mod) {
76 switch(mod.getModificationType()) {
77 case SUBTREE_MODIFIED:
78 node = mod.getDataAfter();
81 if(mod.getDataBefore() != null) {
82 node = mod.getDataAfter();
91 public static Node getOriginal(DataObjectModification<Node> mod) {
93 switch(mod.getModificationType()) {
94 case SUBTREE_MODIFIED:
95 node = mod.getDataBefore();
98 if(mod.getDataBefore() != null) {
99 node = mod.getDataBefore();
103 node = mod.getDataBefore();
111 //TODO: change this function to be generic
112 public static Map<InstanceIdentifier<Node>, Node> extractCreatedOrUpdatedOrRemoved(
113 Collection<DataTreeModification<Node>> changes, Class<Node> class1) {
114 Map<InstanceIdentifier<Node>, Node> result = new HashMap<>();
115 for(DataTreeModification<Node> change : changes) {
116 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
117 final DataObjectModification<Node> mod = change.getRootNode();
118 Node created = getCreated(mod);
119 if (created != null) {
120 result.put(key, created);
122 Node updated = getUpdated(mod);
123 if (updated != null) {
124 result.put(key, updated);
126 Node deleted = getRemoved(mod);
127 if (deleted != null) {
128 result.put(key, deleted);
134 public static UUID createPhysicalLocatorSet(HwvtepOperationalState hwvtepOperationalState, TransactionBuilder transaction, List<LocatorSet> locatorList) {
135 Set<UUID> locators = new HashSet<UUID>();
136 for (LocatorSet locator: locatorList) {
137 @SuppressWarnings("unchecked")
138 InstanceIdentifier<TerminationPoint> iid =(InstanceIdentifier<TerminationPoint>) locator.getLocatorRef().getValue();
139 UUID locatorUuid = createPhysicalLocator(transaction, hwvtepOperationalState, iid);
140 if (locatorUuid != null) {
141 locators.add(locatorUuid);
144 PhysicalLocatorSet physicalLocatorSet = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocatorSet.class);
145 physicalLocatorSet.setLocators(locators);
146 String locatorSetUuid = "PhysicalLocatorSet_" + HwvtepSouthboundMapper.getRandomUUID();
147 transaction.add(op.insert(physicalLocatorSet).withId(locatorSetUuid));
148 return new UUID(locatorSetUuid);
151 public static UUID createPhysicalLocator(TransactionBuilder transaction, HwvtepOperationalState operationalState,
152 InstanceIdentifier<TerminationPoint> iid) {
153 UUID locatorUuid = null;
154 HwvtepDeviceInfo.DeviceData deviceData = operationalState.getDeviceInfo().getDeviceOperData(
155 TerminationPoint.class, iid);
156 if (deviceData != null && deviceData.getUuid() != null) {
157 locatorUuid = deviceData.getUuid();
160 locatorUuid = operationalState.getUUIDFromCurrentTx(TerminationPoint.class, iid);
161 if (locatorUuid != null) {
164 HwvtepPhysicalLocatorAugmentationBuilder builder = new HwvtepPhysicalLocatorAugmentationBuilder();
165 HwvtepPhysicalLocatorAugmentation locatorAugmentation = null;
166 builder.setEncapsulationType(EncapsulationTypeVxlanOverIpv4.class);
167 String tepKey = iid.firstKeyOf(TerminationPoint.class).getTpId().getValue();
168 String ip = tepKey.substring(tepKey.indexOf(":")+1);
169 builder.setDstIp(new IpAddress(ip.toCharArray()));
170 locatorAugmentation = builder.build();
171 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
172 operationalState.updateCurrentTxData(TerminationPoint.class, iid, locatorUuid);
173 operationalState.getDeviceInfo().markKeyAsInTransit(TerminationPoint.class, iid);
177 public static UUID createPhysicalLocator(TransactionBuilder transaction, HwvtepPhysicalLocatorAugmentation inputLocator) {
178 LOG.debug("Creating a physical locator: {}", inputLocator.getDstIp());
179 PhysicalLocator physicalLocator = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocator.class);
180 setEncapsulationType(physicalLocator, inputLocator);
181 setDstIp(physicalLocator, inputLocator);
182 String locatorUuid = "PhysicalLocator_" + HwvtepSouthboundMapper.getRandomUUID();
183 transaction.add(op.insert(physicalLocator).withId(locatorUuid));
184 return new UUID(locatorUuid);
187 private static final void setEncapsulationType(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
188 if (inputLocator.getEncapsulationType() != null) {
189 String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(HwvtepSouthboundMapper.createEncapsulationType(""));
190 physicalLocator.setEncapsulationType(encapType);
194 private static final void setDstIp(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
195 if (inputLocator.getDstIp() != null) {
196 physicalLocator.setDstIp(inputLocator.getDstIp().getIpv4Address().getValue());
200 static String sanitizeUUID(HwvtepNodeName hwvtepNodeName) {
201 return sanitizeUUID(hwvtepNodeName.getValue());
204 static String sanitizeUUID(String nodeName) {
205 //ovs is not accepting '-' in the named uuids
206 return nodeName.replaceAll("-", "_");
209 public static String getLogicalSwitchId(LogicalSwitches lswitch){
210 return HwvtepSouthboundConstants.LOGICALSWITCH_UUID_PREFIX + sanitizeUUID(lswitch.getHwvtepNodeName());
213 public static UUID getLogicalSwitchUUID(InstanceIdentifier<LogicalSwitches> lswitchIid){
214 return new UUID(HwvtepSouthboundConstants.LOGICALSWITCH_UUID_PREFIX +
215 sanitizeUUID(lswitchIid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName()));
218 public static UUID getLogicalSwitchUUID(final TransactionBuilder transaction,
219 final HwvtepOperationalState operationalState,
220 final InstanceIdentifier<LogicalSwitches> lswitchIid) {
221 HwvtepDeviceInfo hwvtepDeviceInfo = operationalState.getDeviceInfo();
222 HwvtepDeviceInfo.DeviceData lsData = hwvtepDeviceInfo.getDeviceOperData(LogicalSwitches.class, lswitchIid);
223 if (lsData != null) {
224 if (lsData.getUuid() != null) {
225 return lsData.getUuid();
227 if (lsData.isInTransitState()) {
228 return getLogicalSwitchUUID(lswitchIid);
232 LogicalSwitchUpdateCommand cmd = new LogicalSwitchUpdateCommand(operationalState, Collections.EMPTY_LIST);
233 MdsalUtils mdsalUtils = new MdsalUtils(operationalState.getDataBroker());
234 LogicalSwitches ls = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, lswitchIid);
236 cmd.updateLogicalSwitch(transaction, lswitchIid.firstIdentifierOf(Node.class), Lists.newArrayList(ls));
238 LOG.error("Could not find logical switch in config ds {}", lswitchIid);
241 return getLogicalSwitchUUID(lswitchIid);
244 public static String getLogicalRouterId(final LogicalRouters lrouter){
245 return HwvtepSouthboundConstants.LOGICALROUTER_UUID_PREFIX + sanitizeUUID(lrouter.getHwvtepNodeName());
248 public static UUID getAclUUID(final InstanceIdentifier<Acls> aclIid){
249 return new UUID(HwvtepSouthboundConstants.ACL_UUID_PREFIX +
250 sanitizeUUID(aclIid.firstKeyOf(Acls.class).getAclName()));