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 <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNodeFromConfig(
135 ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
136 Optional<D> node = Optional.absent();
138 node = transaction.read(LogicalDatastoreType.CONFIGURATION, connectionIid).checkedGet();
139 } catch (final ReadFailedException e) {
140 LOG.warn("Read Configration/DS for Node failed! {}", connectionIid, e);
145 public static UUID createPhysicalLocatorSet(HwvtepOperationalState hwvtepOperationalState, TransactionBuilder transaction, List<LocatorSet> locatorList) {
146 Set<UUID> locators = new HashSet<UUID>();
147 for (LocatorSet locator: locatorList) {
148 @SuppressWarnings("unchecked")
149 InstanceIdentifier<TerminationPoint> iid =(InstanceIdentifier<TerminationPoint>) locator.getLocatorRef().getValue();
150 UUID locatorUuid = createPhysicalLocator(transaction, hwvtepOperationalState, iid);
151 if (locatorUuid != null) {
152 locators.add(locatorUuid);
155 PhysicalLocatorSet physicalLocatorSet = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocatorSet.class);
156 physicalLocatorSet.setLocators(locators);
157 String locatorSetUuid = "PhysicalLocatorSet_" + HwvtepSouthboundMapper.getRandomUUID();
158 transaction.add(op.insert(physicalLocatorSet).withId(locatorSetUuid));
159 return new UUID(locatorSetUuid);
162 public static UUID createPhysicalLocator(TransactionBuilder transaction, HwvtepOperationalState operationalState,
163 InstanceIdentifier<TerminationPoint> iid) {
164 UUID locatorUuid = null;
165 HwvtepDeviceInfo.DeviceData deviceData = operationalState.getDeviceInfo().getDeviceOperData(
166 TerminationPoint.class, iid);
167 if (deviceData != null && deviceData.getUuid() != null) {
168 locatorUuid = deviceData.getUuid();
171 locatorUuid = operationalState.getUUIDFromCurrentTx(TerminationPoint.class, iid);
172 if (locatorUuid != null) {
175 HwvtepPhysicalLocatorAugmentationBuilder builder = new HwvtepPhysicalLocatorAugmentationBuilder();
176 HwvtepPhysicalLocatorAugmentation locatorAugmentation = null;
177 builder.setEncapsulationType(EncapsulationTypeVxlanOverIpv4.class);
178 String tepKey = iid.firstKeyOf(TerminationPoint.class).getTpId().getValue();
179 String ip = tepKey.substring(tepKey.indexOf(":")+1);
180 builder.setDstIp(new IpAddress(ip.toCharArray()));
181 locatorAugmentation = builder.build();
182 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
183 operationalState.updateCurrentTxData(TerminationPoint.class, iid, locatorUuid);
184 operationalState.getDeviceInfo().markKeyAsInTransit(TerminationPoint.class, iid);
188 public static UUID createPhysicalLocator(TransactionBuilder transaction, HwvtepPhysicalLocatorAugmentation inputLocator) {
189 LOG.debug("Creating a physical locator: {}", inputLocator.getDstIp());
190 PhysicalLocator physicalLocator = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocator.class);
191 setEncapsulationType(physicalLocator, inputLocator);
192 setDstIp(physicalLocator, inputLocator);
193 String locatorUuid = "PhysicalLocator_" + HwvtepSouthboundMapper.getRandomUUID();
194 transaction.add(op.insert(physicalLocator).withId(locatorUuid));
195 return new UUID(locatorUuid);
198 private static final void setEncapsulationType(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
199 if (inputLocator.getEncapsulationType() != null) {
200 String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(HwvtepSouthboundMapper.createEncapsulationType(""));
201 physicalLocator.setEncapsulationType(encapType);
205 private static final void setDstIp(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
206 if (inputLocator.getDstIp() != null) {
207 physicalLocator.setDstIp(inputLocator.getDstIp().getIpv4Address().getValue());
211 static String sanitizeUUID(HwvtepNodeName hwvtepNodeName) {
212 return sanitizeUUID(hwvtepNodeName.getValue());
215 static String sanitizeUUID(String nodeName) {
216 //ovs is not accepting '-' in the named uuids
217 return nodeName.replaceAll("-", "_");
220 public static String getLogicalSwitchId(LogicalSwitches lswitch){
221 return HwvtepSouthboundConstants.LOGICALSWITCH_UUID_PREFIX + sanitizeUUID(lswitch.getHwvtepNodeName());
224 public static UUID getLogicalSwitchUUID(InstanceIdentifier<LogicalSwitches> lswitchIid){
225 return new UUID(HwvtepSouthboundConstants.LOGICALSWITCH_UUID_PREFIX +
226 sanitizeUUID(lswitchIid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName()));
229 public static UUID getLogicalSwitchUUID(final TransactionBuilder transaction,
230 final HwvtepOperationalState operationalState,
231 final InstanceIdentifier<LogicalSwitches> lswitchIid) {
232 HwvtepDeviceInfo hwvtepDeviceInfo = operationalState.getDeviceInfo();
233 HwvtepDeviceInfo.DeviceData lsData = hwvtepDeviceInfo.getDeviceOperData(LogicalSwitches.class, lswitchIid);
234 if (lsData != null) {
235 if (lsData.getUuid() != null) {
236 return lsData.getUuid();
238 if (lsData.isInTransitState()) {
239 return getLogicalSwitchUUID(lswitchIid);
243 LogicalSwitchUpdateCommand cmd = new LogicalSwitchUpdateCommand(operationalState, Collections.EMPTY_LIST);
244 MdsalUtils mdsalUtils = new MdsalUtils(operationalState.getDataBroker());
245 LogicalSwitches ls = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, lswitchIid);
247 cmd.updateLogicalSwitch(transaction, lswitchIid.firstIdentifierOf(Node.class), Lists.newArrayList(ls));
249 LOG.error("Could not find logical switch in config ds {}", lswitchIid);
252 return getLogicalSwitchUUID(lswitchIid);
255 public static String getLogicalRouterId(final LogicalRouters lrouter){
256 return HwvtepSouthboundConstants.LOGICALROUTER_UUID_PREFIX + sanitizeUUID(lrouter.getHwvtepNodeName());
259 public static UUID getAclUUID(final InstanceIdentifier<Acls> aclIid){
260 return new UUID(HwvtepSouthboundConstants.ACL_UUID_PREFIX +
261 sanitizeUUID(aclIid.firstKeyOf(Acls.class).getAclName()));