manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / UniUpdateCommand.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.unimgr.command;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.unimgr.api.AbstractCommand;
15 import org.opendaylight.unimgr.utils.MdsalUtils;
16 import org.opendaylight.unimgr.utils.OvsdbUtils;
17 import org.opendaylight.unimgr.utils.UniUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
27
28 public class UniUpdateCommand extends AbstractCommand<Node> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(UniUpdateCommand.class);
31
32     public UniUpdateCommand(final DataBroker dataBroker, final DataTreeModification<Node> updatedUniNode) {
33         super(dataBroker, updatedUniNode);
34     }
35
36     @Override
37     public void execute() {
38         final UniAugmentation updatedUni = dataObject.getRootNode().getDataAfter().getAugmentation(UniAugmentation.class);
39         final UniAugmentation formerUni = dataObject.getRootNode().getDataBefore().getAugmentation(UniAugmentation.class);
40         if (formerUni != null) {
41             final String formerUniIp = formerUni.getIpAddress().getIpv4Address().getValue();
42             final String updatedUniIp = updatedUni.getIpAddress().getIpv4Address().getValue();
43             final InstanceIdentifier<?> updatedUniIid = dataObject.getRootPath().getRootIdentifier();
44             final String uniKey = updatedUniIid.firstKeyOf(Node.class).toString();
45             Preconditions.checkArgument(formerUniIp.equals(updatedUniIp),
46                     "Can't update UNI with a different IP address. Former IP was %s"
47                             + "Updated IP is %s. Please create a UNI instead of updating this one %s",
48                             formerUniIp, updatedUniIp, uniKey);
49             Node ovsdbNode;
50             if (updatedUni.getOvsdbNodeRef() != null) {
51                 LOG.info("OVSDB NODE ref retreive for updated UNI {}", updatedUni.getOvsdbNodeRef());
52                 final OvsdbNodeRef ovsdbNodeRef = updatedUni.getOvsdbNodeRef();
53                 final Optional<Node> optOvsdbNode = MdsalUtils.readNode(dataBroker,LogicalDatastoreType.OPERATIONAL, ovsdbNodeRef.getValue());
54                 if(optOvsdbNode.isPresent()) {
55                     ovsdbNode= optOvsdbNode.get();
56                     LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
57                     // Update QoS entries to ovsdb if speed is configured to UNI node
58                     if (updatedUni.getSpeed() != null) {
59                         OvsdbUtils.createQoSForOvsdbNode(dataBroker, updatedUni);
60                     }
61                     UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, updatedUniIid, updatedUni,
62                                            ovsdbNode, dataBroker);
63                 }  else {
64                     // This should never happen, because on creation,
65                     // the UNI is assigned and OVSDB node
66                     LOG.error("OVSDB node not found for UNI {}, but got OVSDB ref", uniKey, updatedUni.getOvsdbNodeRef());
67                     return;
68                 }
69             } else {
70                 final Optional<Node> optOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, updatedUni);
71                 if (optOvsdbNode.isPresent()) {
72                     ovsdbNode = optOvsdbNode.get();
73                     LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
74                     // Update QoS entries to ovsdb if speed is configured to UNI node
75                     if (updatedUni.getSpeed() != null) {
76                         OvsdbUtils.createQoSForOvsdbNode(dataBroker, updatedUni);
77                     }
78                     UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, updatedUniIid, updatedUni,
79                                            ovsdbNode, dataBroker);
80                 } else {
81                     // This should never happen, because on creation,
82                     // the UNI is assigned and OVSDB node
83                     LOG.error("OVSDB node not found for UNI {}", uniKey);
84                     return;
85                 }
86             }
87             LOG.info("UNI {} updated", uniKey);
88         }
89     }
90 }