Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / UniCreateCommand.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.List;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.unimgr.impl.UnimgrConstants;
17 import org.opendaylight.unimgr.impl.UnimgrMapper;
18 import org.opendaylight.unimgr.utils.MdsalUtils;
19 import org.opendaylight.unimgr.utils.OvsdbUtils;
20 import org.opendaylight.unimgr.utils.UniUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Optional;
31
32 public class UniCreateCommand extends AbstractCreateCommand {
33
34     private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class);
35
36     public UniCreateCommand(DataBroker dataBroker,
37             Map<InstanceIdentifier<?>, DataObject> changes) {
38         super.dataBroker = dataBroker;
39         super.changes = changes;
40     }
41
42     @Override
43     public void execute() {
44         for (final Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
45             if (created.getValue() != null && created.getValue() instanceof UniAugmentation) {
46                 final UniAugmentation uni = (UniAugmentation) created.getValue();
47                 final InstanceIdentifier<?> uniKey = created.getKey();
48                 LOG.trace("New UNI created {}.", uni.getIpAddress().getIpv4Address());
49                 /* We assume that when the user specifies the
50                  * ovsdb-node-ref that the node already exists in
51                  * the controller and that the OVS instance is in
52                  * active mode.
53                  *
54                  * We assume that when the user doesn't specify the
55                  * ovsdb-node-id that the node doesn't exist therefor
56                  * has to be created with the IP address because it's
57                  * in passive mode.
58                  *
59                  * Active mode (TCP): the UUID is in format ovsdb://UUID
60                  * Passive mode (PTCP): the UUID is in format ovsdb://IP:6640
61                  *
62                  */
63                 if (uni.getOvsdbNodeRef() != null) {
64                     final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
65                     final Optional<Node> optionalNode = MdsalUtils.readNode(dataBroker,
66                                                                        LogicalDatastoreType.OPERATIONAL,
67                                                                        ovsdbNodeRef.getValue());
68                     if (!optionalNode.isPresent()) {
69                         LOG.info("Invalid OVSDB node instance identifier specified, "
70                                + "attempting to retrieve the node.");
71                         final Optional<Node> optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker,
72                                                                                      uni);
73                         Node ovsdbNode;
74                         if (optionalOvsdbNode.isPresent()) {
75                             ovsdbNode = optionalOvsdbNode.get();
76                             LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
77                             // Update QoS entries to ovsdb if speed is configured to UNI node
78                             if (uni.getSpeed() != null) {
79                                 OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni);
80                             }
81                             UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
82                                                       uniKey,
83                                                       uni,
84                                                       ovsdbNode,
85                                                       dataBroker);
86                         } else {
87                             ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker,
88                                                                     uni);
89                             LOG.info("Could not retrieve the OVSDB node,"
90                                    + " created a new one: {}", ovsdbNode.getNodeId());
91                             UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
92                                                       uniKey,
93                                                       uni,
94                                                       ovsdbNode,
95                                                       dataBroker);
96                         }
97                     }
98                 } else {
99                     // We assume the ovs is in passive mode
100                     // Check if the ovsdb node exist
101                     final Optional<Node> optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker,
102                                                                                  uni);
103                     Node ovsdbNode;
104                     if (optionalOvsdbNode.isPresent()) {
105                         ovsdbNode = optionalOvsdbNode.get();
106                         final InstanceIdentifier<Node> ovsdbIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
107                         LOG.info("Retrieved the OVSDB node");
108                         // Update QoS entries to ovsdb if speed is configured to UNI node
109                         if (uni.getSpeed() != null) {
110                             OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni);
111                         }
112                         OvsdbUtils.createBridgeNode(dataBroker,
113                                                      ovsdbIid,
114                                                      uni,
115                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
116                         UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
117                                                   uniKey,
118                                                   uni,
119                                                   ovsdbNode,
120                                                   dataBroker);
121                         UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniKey, uni, ovsdbNode, dataBroker);
122                     } else {
123                         ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker,
124                                                                 uni);
125                         if (ovsdbNode != null) {
126                             LOG.info("Could not retrieve the OVSDB node,"
127                                     + "created a new one: {}", ovsdbNode.getNodeId());
128                             UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
129                                                        uniKey,
130                                                        uni,
131                                                        ovsdbNode,
132                                                        dataBroker);
133                         }
134                     }
135                 }
136             }
137             if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
138                 final OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
139                                                                                           .getValue();
140                 final InstanceIdentifier<Node> ovsdbIid = created.getKey().firstIdentifierOf(Node.class);
141                 if (ovsdbNodeAugmentation != null) {
142                     LOG.info("Received an OVSDB node create {}",
143                             ovsdbNodeAugmentation.getConnectionInfo()
144                                                  .getRemoteIp()
145                                                  .getIpv4Address()
146                                                  .getValue());
147                     final List<Node> uniNodes = UniUtils.getUniNodes(dataBroker);
148                     if (uniNodes != null && !uniNodes.isEmpty()) {
149                         for (final Node uniNode: uniNodes) {
150                             final UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
151                             if (uniAugmentation.getOvsdbNodeRef() != null
152                                     && uniAugmentation.getOvsdbNodeRef().getValue() != null) {
153                                 final InstanceIdentifier<Node> ovsdbNodeRefIid = uniAugmentation
154                                                                             .getOvsdbNodeRef()
155                                                                             .getValue()
156                                                                             .firstIdentifierOf(Node.class);
157                                 if (ovsdbNodeRefIid.equals(ovsdbIid)) {
158                                     final Optional<Node> optionalOvsdbNode = MdsalUtils.readNode(dataBroker,
159                                                                                             LogicalDatastoreType.OPERATIONAL,
160                                                                                             ovsdbIid);
161                                     if (optionalOvsdbNode.isPresent()) {
162                                         final InstanceIdentifier<Node> uniIid =
163                                                                     UnimgrMapper.getUniIid(dataBroker,
164                                                                                            uniAugmentation.getIpAddress(),
165                                                                                            LogicalDatastoreType.CONFIGURATION);
166                                         // Update QoS entries to ovsdb if speed is configured to UNI node
167                                         if (uniAugmentation.getSpeed() != null) {
168                                             OvsdbUtils.createQoSForOvsdbNode(dataBroker, uniAugmentation);
169                                         }
170                                         OvsdbUtils.createBridgeNode(dataBroker,
171                                                                      ovsdbIid,
172                                                                      uniAugmentation,
173                                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
174                                         UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
175                                                                   uniIid,
176                                                                   uniAugmentation,
177                                                                   ovsdbIid,
178                                                                   dataBroker);
179                                     }
180                                 }
181                             } else if (ovsdbNodeAugmentation
182                                           .getConnectionInfo()
183                                           .getRemoteIp()
184                                           .equals(uniAugmentation.getIpAddress())) {
185                                 final InstanceIdentifier<Node> uniIid = UnimgrMapper.getUniIid(dataBroker,
186                                                                                          uniAugmentation.getIpAddress(),
187                                                                                          LogicalDatastoreType.CONFIGURATION);
188                                 OvsdbUtils.createBridgeNode(dataBroker,
189                                                              ovsdbIid,
190                                                              uniAugmentation,
191                                                              UnimgrConstants.DEFAULT_BRIDGE_NAME);
192                                 UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
193                                                           uniIid,
194                                                           uniAugmentation,
195                                                           ovsdbIid,
196                                                           dataBroker);
197                             }
198                         }
199                     } else {
200                         LOG.info("Received a new OVSDB node connection from {}"
201                                 + ovsdbNodeAugmentation.getConnectionInfo()
202                                         .getRemoteIp().getIpv4Address());
203                     }
204                 }
205             }
206         }
207     }
208
209 }