Fixing sonar bug
[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     private TransactUtils(){
46     }
47
48     public static Node getCreated(DataObjectModification<Node> mod) {
49         if((mod.getModificationType() == ModificationType.WRITE)
50                         && (mod.getDataBefore() == null)){
51             return mod.getDataAfter();
52         }
53         return null;
54     }
55
56     public static Node getRemoved(DataObjectModification<Node> mod) {
57         if(mod.getModificationType() == ModificationType.DELETE){
58             return mod.getDataBefore();
59         }
60         return null;
61     }
62
63     public static Node getUpdated(DataObjectModification<Node> mod) {
64         Node node = null;
65         switch(mod.getModificationType()) {
66             case SUBTREE_MODIFIED:
67                 node = mod.getDataAfter();
68                 break;
69             case WRITE:
70                 if(mod.getDataBefore() !=  null) {
71                     node = mod.getDataAfter();
72                 }
73                 break;
74             default:
75                 break;
76         }
77         return node;
78     }
79
80     public static Node getOriginal(DataObjectModification<Node> mod) {
81         Node node = null;
82         switch(mod.getModificationType()) {
83             case SUBTREE_MODIFIED:
84                 node = mod.getDataBefore();
85                 break;
86             case WRITE:
87                 if(mod.getDataBefore() !=  null) {
88                     node = mod.getDataBefore();
89                 }
90                 break;
91             case DELETE:
92                 node = mod.getDataBefore();
93                 break;
94             default:
95                 break;
96         }
97         return node;
98     }
99
100     //TODO: change this function to be generic
101     public static Map<InstanceIdentifier<Node>, Node> extractCreatedOrUpdatedOrRemoved(
102             Collection<DataTreeModification<Node>> changes, Class<Node> class1) {
103         Map<InstanceIdentifier<Node>, Node> result = new HashMap<InstanceIdentifier<Node>, Node>();
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                 result.put(key, created);
110             }
111             Node updated = getUpdated(mod);
112             if (updated != null) {
113                 result.put(key, updated);
114             }
115             Node deleted = getRemoved(mod);
116             if (deleted != null) {
117                 result.put(key, deleted);
118             }
119         }
120         return result;
121     }
122
123     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNodeFromConfig(
124             ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
125         Optional<D> node = Optional.absent();
126         try {
127             node = transaction.read(LogicalDatastoreType.CONFIGURATION, connectionIid).checkedGet();
128         } catch (final ReadFailedException e) {
129             LOG.warn("Read Configration/DS for Node failed! {}", connectionIid, e);
130         }
131         return node;
132     }
133
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             UUID locatorUuid = null;
138             @SuppressWarnings("unchecked")
139             InstanceIdentifier<TerminationPoint> iid =(InstanceIdentifier<TerminationPoint>) locator.getLocatorRef().getValue();
140             //try to find locator in operational DS
141             Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
142                     hwvtepOperationalState.getPhysicalLocatorAugmentation(iid);
143             if (operationalLocatorOptional.isPresent()) {
144                 //if exist, get uuid
145                 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
146                 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
147             } else {
148                 //if no, get it from config DS and create id
149                 Optional<TerminationPoint> configLocatorOptional =
150                         readNodeFromConfig(hwvtepOperationalState.getReadWriteTransaction(), iid);
151                 if (configLocatorOptional.isPresent()) {
152                     HwvtepPhysicalLocatorAugmentation locatorAugmentation =
153                             configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
154                     locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
155                 } else {
156                     LOG.warn("Create or update localMcastMac: No physical locator found in operational datastore!"
157                             + "Its indentifier is {}", locator.getLocatorRef().getValue());
158                 }
159             }
160             if (locatorUuid != null) {
161                 locators.add(locatorUuid);
162             }
163         }
164         PhysicalLocatorSet physicalLocatorSet = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocatorSet.class);
165         physicalLocatorSet.setLocators(locators);
166         String locatorSetUuid = "PhysicalLocatorSet_" + HwvtepSouthboundMapper.getRandomUUID();
167         transaction.add(op.insert(physicalLocatorSet).withId(locatorSetUuid));
168         return new UUID(locatorSetUuid);
169     }
170
171     public static UUID createPhysicalLocator(TransactionBuilder transaction, HwvtepPhysicalLocatorAugmentation inputLocator) {
172         LOG.debug("Creating a physical locator: {}", inputLocator.getDstIp());
173         PhysicalLocator physicalLocator = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalLocator.class);
174         setEncapsulationType(physicalLocator, inputLocator);
175         setDstIp(physicalLocator, inputLocator);
176         String locatorUuid = "PhysicalLocator_" + HwvtepSouthboundMapper.getRandomUUID();
177         transaction.add(op.insert(physicalLocator).withId(locatorUuid));
178         return new UUID(locatorUuid);
179     }
180
181     private static final void setEncapsulationType(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
182         if (inputLocator.getEncapsulationType() != null) {
183             String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(HwvtepSouthboundMapper.createEncapsulationType(""));
184             physicalLocator.setEncapsulationType(encapType);
185         }
186     }
187
188     private static final void setDstIp(PhysicalLocator physicalLocator, HwvtepPhysicalLocatorAugmentation inputLocator) {
189         if (inputLocator.getDstIp() != null) {
190             physicalLocator.setDstIp(inputLocator.getDstIp().getIpv4Address().getValue());
191         }
192     }
193 }