Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcCreateCommand.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
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.unimgr.impl.UnimgrConstants;
16 import org.opendaylight.unimgr.impl.UnimgrMapper;
17 import org.opendaylight.unimgr.utils.EvcUtils;
18 import org.opendaylight.unimgr.utils.MdsalUtils;
19 import org.opendaylight.unimgr.utils.OvsdbUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.common.base.Optional;
29
30 public class EvcCreateCommand extends AbstractCreateCommand {
31
32     private static final Logger LOG = LoggerFactory.getLogger(EvcCreateCommand.class);
33
34     public EvcCreateCommand(DataBroker dataBroker,
35                             Map<InstanceIdentifier<?>, DataObject> changes) {
36         super.dataBroker = dataBroker;
37         super.changes = changes;
38     }
39
40     @Override
41     public void execute() {
42         for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
43             if ((created.getValue() != null) && (created.getValue() instanceof EvcAugmentation)) {
44                 EvcAugmentation evc = (EvcAugmentation) created.getValue();
45                 InstanceIdentifier<?> evcKey = created.getKey();
46                 // For now, we assume that there is 1 uni per source/destination
47                 if ((evc.getUniDest() == null) || evc.getUniDest().isEmpty()) {
48                     LOG.error("Destination UNI cannot be null.");
49                     break;
50                 }
51                 if ((evc.getUniSource() == null) || evc.getUniSource().isEmpty()) {
52                     LOG.error("Source UNI cannot be null.");
53                     break;
54                 }
55                 LOG.info("New EVC created, source IP: {} destination IP {}.",
56                         evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
57                         evc.getUniDest().iterator().next().getIpAddress().getIpv4Address());
58                 InstanceIdentifier<Node> sourceUniIid;
59                 InstanceIdentifier<Node> destinationUniIid;
60                 //FIXME we are assuming that there is only 1 UNI source and destination
61                 // per evc
62                 InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
63                 if (iidSource != null) {
64                     sourceUniIid = iidSource.firstIdentifierOf(Node.class);
65                 } else {
66                     sourceUniIid = UnimgrMapper.getUniIid(dataBroker,
67                                                           evc.getUniSource().iterator().next().getIpAddress(),
68                                                           LogicalDatastoreType.OPERATIONAL);
69                 }
70                 InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
71                 if (iidDest != null) {
72                     destinationUniIid = iidDest.firstIdentifierOf(Node.class);
73                 } else {
74                     destinationUniIid = UnimgrMapper.getUniIid(dataBroker,
75                                                                evc.getUniDest().iterator().next().getIpAddress(),
76                                                                LogicalDatastoreType.OPERATIONAL);
77                 }
78                 Optional<Node> optionalUniSource = MdsalUtils.readNode(dataBroker,
79                                                                         LogicalDatastoreType.OPERATIONAL,
80                                                                         sourceUniIid);
81                 Optional<Node> optionalUniDestination = MdsalUtils.readNode(dataBroker,
82                                                                              LogicalDatastoreType.OPERATIONAL,
83                                                                              destinationUniIid);
84                 Node uniSource;
85                 Node uniDestination;
86                 // Retrieve the source and destination Unis
87                 if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) {
88                     uniSource = optionalUniSource.get();
89                     uniDestination = optionalUniDestination.get();
90                     // Set source and destination
91                     UniAugmentation sourceUniAugmentation =
92                                         uniSource.getAugmentation(UniAugmentation.class);
93                     UniAugmentation destinationUniAugmentation =
94                                         uniDestination.getAugmentation(UniAugmentation.class);
95                     Optional<Node> optionalSourceOvsdbNode =
96                             MdsalUtils.readNode(dataBroker,
97                                                             LogicalDatastoreType.OPERATIONAL,
98                                                             sourceUniAugmentation
99                                                                 .getOvsdbNodeRef()
100                                                                 .getValue());
101                     Optional<Node> optionalDestinationOvsdbNode =
102                             MdsalUtils.readNode(dataBroker,
103                                                             LogicalDatastoreType.OPERATIONAL,
104                                                             destinationUniAugmentation
105                                                                 .getOvsdbNodeRef()
106                                                                 .getValue());
107                     if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
108                         InstanceIdentifier<Node> sourceBridgeIid =
109                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
110                         Optional<Node> optionalSourceBr = MdsalUtils.readNode(dataBroker,
111                                                                                LogicalDatastoreType.OPERATIONAL,
112                                                                                sourceBridgeIid);
113                         InstanceIdentifier<Node> destinationBridgeIid =
114                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
115                         Optional<Node> optionalDestinationBr = MdsalUtils.readNode(dataBroker,
116                                                                                     LogicalDatastoreType.OPERATIONAL,
117                                                                                     destinationBridgeIid);
118                         //update ovsdb qos-entry and queues with max-rate to match evc ingress BW
119                         OvsdbUtils.updateMaxRate(dataBroker, sourceUniAugmentation, destinationUniAugmentation, evc);
120                         Node sourceBr;
121                         Node destinationBr;
122                         if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
123                             sourceBr = optionalSourceBr.get();
124                             destinationBr = optionalDestinationBr.get();
125                             OvsdbUtils.createTerminationPointNode(dataBroker,
126                                                                    uniSource.getAugmentation(UniAugmentation.class),
127                                                                    sourceBr,
128                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
129                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE);
130                             OvsdbUtils.createGreTunnel(dataBroker,
131                                                         uniSource.getAugmentation(UniAugmentation.class),
132                                                         uniDestination.getAugmentation(UniAugmentation.class),
133                                                         sourceBr,
134                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
135                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
136                             OvsdbUtils.createTerminationPointNode(dataBroker,
137                                                                    uniDestination.getAugmentation(UniAugmentation.class),
138                                                                    destinationBr,
139                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
140                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE);
141                             OvsdbUtils.createGreTunnel(dataBroker,
142                                                         uniDestination.getAugmentation(UniAugmentation.class),
143                                                         uniSource.getAugmentation(UniAugmentation.class), destinationBr,
144                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
145                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
146                             EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
147                                                       evcKey,
148                                                       evc,
149                                                       sourceUniIid,
150                                                       destinationUniIid,
151                                                       dataBroker);
152                             EvcUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
153                                                       evcKey,
154                                                       evc,
155                                                       sourceUniIid,
156                                                       destinationUniIid,
157                                                       dataBroker);
158                         } else {
159                             LOG.info("Unable to retrieve the source and/or destination bridge.");
160                         }
161                     } else {
162                         LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
163                     }
164                 } else {
165                     LOG.info("Unable to retrieve the source and/or destination Uni.");
166                 }
167             }
168         }
169     }
170
171 }