Remove leftover data after updating EVC
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcUpdateCommand.java
1 /*
2  * Copyright (c) 2015 CableLabs 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.unimgr.command;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12 import java.util.concurrent.ExecutionException;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.unimgr.impl.UnimgrConstants;
19 import org.opendaylight.unimgr.impl.UnimgrMapper;
20 import org.opendaylight.unimgr.impl.UnimgrUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Optional;
32 import com.google.common.util.concurrent.CheckedFuture;
33
34 public class EvcUpdateCommand extends AbstractUpdateCommand {
35
36     private static final Logger LOG = LoggerFactory.getLogger(EvcUpdateCommand.class);
37
38     public EvcUpdateCommand(final DataBroker dataBroker,
39             final Map<InstanceIdentifier<?>, DataObject> changes) {
40         super.dataBroker = dataBroker;
41         super.changes = changes;
42     }
43
44     @Override
45     public void execute() {
46         for (final Entry<InstanceIdentifier<?>, DataObject> updated : changes.entrySet()) {
47             if (updated.getValue() != null && updated.getValue() instanceof EvcAugmentation) {
48                 final EvcAugmentation evc = (EvcAugmentation) updated.getValue();
49                 final InstanceIdentifier<?> evcKey = updated.getKey();
50
51                 // FIXME: For now, we assume that there is 1 uni per
52                 // source/destination
53                 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
54                     LOG.error("Destination UNI cannot be null.");
55                     break;
56                 }
57                 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
58                     LOG.error("Source UNI cannot be null.");
59                     break;
60                 }
61
62                 final Ipv4Address laterUni1Ip = evc.getUniSource().iterator().next().getIpAddress().getIpv4Address();
63                 final Ipv4Address laterUni2Ip = evc.getUniDest().iterator().next().getIpAddress().getIpv4Address();
64                 LOG.trace("New EVC created, source IP: {} destination IP {}.", laterUni1Ip, laterUni2Ip);
65
66
67                 final ReadTransaction readTransac = dataBroker.newReadOnlyTransaction();
68                 final CheckedFuture<?, ReadFailedException> retFormerEvc = readTransac.read(LogicalDatastoreType.OPERATIONAL, evcKey);
69                 EvcAugmentation formerEvc;
70                 try {
71                     formerEvc = (EvcAugmentation) ((Optional<EvcAugmentation>) retFormerEvc.checkedGet()).get();
72                     final Ipv4Address formerUni1ip = formerEvc.getUniDest().iterator().next().getIpAddress().getIpv4Address();
73                     final Ipv4Address formerUni2ip = formerEvc.getUniDest().iterator().next().getIpAddress().getIpv4Address();
74
75                     if (formerUni1ip.equals(laterUni1Ip)) {
76                         // do nothing
77                     } else if (formerUni1ip.equals(laterUni2Ip)) {
78                         // do nothing
79                     } else {
80                         LOG.info("{} is not part of the EVC, removing configuration", formerUni1ip);
81                         final InstanceIdentifier<?> formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni1ip), LogicalDatastoreType.OPERATIONAL);
82                         final Optional<Node> formerUni = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID);
83                         UnimgrUtils.deleteEvcData(dataBroker, formerUni);
84                     }
85                     if (formerUni2ip.equals(laterUni1Ip)) {
86                         // do nothing
87                     } else if (formerUni2ip.equals(laterUni2Ip)) {
88                         // do nothing
89                     } else {
90                         LOG.info("{} is not part of the EVC, removing configuration", formerUni1ip);
91                         final InstanceIdentifier<?> formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni1ip), LogicalDatastoreType.OPERATIONAL);
92                         final Optional<Node> formerUni = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID);
93                         UnimgrUtils.deleteEvcData(dataBroker, formerUni);
94                         }
95                 } catch (ReadFailedException e) {
96                     LOG.error("Failed to retrieve former EVC {}", evcKey, e);
97                 }
98
99                 InstanceIdentifier<Node> sourceUniIid;
100                 InstanceIdentifier<Node> destinationUniIid;
101
102                 final InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
103                 if (iidSource != null) {
104                     sourceUniIid = iidSource.firstIdentifierOf(Node.class);
105                 } else {
106                     sourceUniIid = UnimgrMapper.getUniIid(dataBroker,
107                             evc.getUniSource().iterator().next().getIpAddress(),
108                             LogicalDatastoreType.OPERATIONAL);
109                 }
110                 final InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
111                 if (iidDest != null) {
112                     destinationUniIid = iidDest.firstIdentifierOf(Node.class);
113                 } else {
114                     destinationUniIid = UnimgrMapper.getUniIid(dataBroker,
115                             evc.getUniDest().iterator().next().getIpAddress(),
116                             LogicalDatastoreType.OPERATIONAL);
117                 }
118
119                 // Retrieve the source and destination UNIs
120                 final Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
121                         LogicalDatastoreType.OPERATIONAL,
122                         sourceUniIid);
123                 final Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
124                         LogicalDatastoreType.OPERATIONAL,
125                         destinationUniIid);
126
127                 Node uniSource = null;
128                 Node uniDestination = null;
129
130                 if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) {
131                     uniSource = optionalUniSource.get();
132                     uniDestination = optionalUniDestination.get();
133
134                     // Retrieve the source and/or destination OVSDB node
135                     final UniAugmentation sourceUniAugmentation =
136                             uniSource.getAugmentation(UniAugmentation.class);
137                     final UniAugmentation destinationUniAugmentation =
138                             uniDestination.getAugmentation(UniAugmentation.class);
139                     final Optional<Node> optionalSourceOvsdbNode =
140                             UnimgrUtils.readNode(dataBroker,
141                                     LogicalDatastoreType.OPERATIONAL,
142                                     sourceUniAugmentation
143                                     .getOvsdbNodeRef()
144                                     .getValue());
145                     final Optional<Node> optionalDestinationOvsdbNode =
146                             UnimgrUtils.readNode(dataBroker,
147                                     LogicalDatastoreType.OPERATIONAL,
148                                     destinationUniAugmentation
149                                     .getOvsdbNodeRef()
150                                     .getValue());
151                     if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
152                         // Retrieve the source and/or destination bridge
153                         final InstanceIdentifier<Node> sourceBridgeIid =
154                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
155                         final Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
156                                 LogicalDatastoreType.OPERATIONAL,
157                                 sourceBridgeIid);
158                         final InstanceIdentifier<Node> destinationBridgeIid =
159                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
160                         final Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
161                                 LogicalDatastoreType.OPERATIONAL,
162                                 destinationBridgeIid);
163                         Node sourceBr = null;
164                         Node destinationBr = null;
165                         if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
166                             sourceBr = optionalSourceBr.get();
167                             destinationBr = optionalDestinationBr.get();
168
169                             // Creating termination points (OVSDB CONFIG
170                             // datastore)
171                             UnimgrUtils.createTerminationPointNode(dataBroker,
172                                     uniSource.getAugmentation(UniAugmentation.class),
173                                     sourceBr,
174                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
175                                     UnimgrConstants.DEFAULT_TUNNEL_IFACE);
176
177                             // Create GRE tunnel (OVSDB CONFIG datastore)
178                             UnimgrUtils.createGreTunnel(dataBroker,
179                                     uniSource.getAugmentation(UniAugmentation.class),
180                                     uniDestination.getAugmentation(UniAugmentation.class),
181                                     sourceBr,
182                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
183                                     UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
184
185                             // Create termination points (CONFIG datastore)
186                             UnimgrUtils.createTerminationPointNode(dataBroker,
187                                     uniSource.getAugmentation(UniAugmentation.class),
188                                     destinationBr,
189                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
190                                     UnimgrConstants.DEFAULT_TUNNEL_IFACE);
191
192                             // Create GRE tunnel (OVSDB CONFIG datastore)
193                             UnimgrUtils.createGreTunnel(dataBroker,
194                                     uniDestination.getAugmentation(UniAugmentation.class),
195                                     uniSource.getAugmentation(UniAugmentation.class), destinationBr,
196                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
197                                     UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
198
199                             // Update EVC
200                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
201                                     evcKey,
202                                     evc,
203                                     sourceUniIid,
204                                     destinationUniIid,
205                                     dataBroker);
206                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
207                                     evcKey,
208                                     evc,
209                                     sourceUniIid,
210                                     destinationUniIid,
211                                     dataBroker);
212                         } else {
213                             LOG.info("Unable to retrieve the source and/or destination bridge.");
214                         }
215                     } else {
216                         LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
217                     }
218                 } else {
219                     LOG.info("Unable to retrieve the source and/or destination Uni.");
220                 }
221             }
222         }
223     }
224 }