manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / UniAddCommand.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.impl.UnimgrConstants;
16 import org.opendaylight.unimgr.impl.UnimgrMapper;
17 import org.opendaylight.unimgr.utils.MdsalUtils;
18 import org.opendaylight.unimgr.utils.OvsdbUtils;
19 import org.opendaylight.unimgr.utils.UniUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
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.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Optional;
28
29 public class UniAddCommand extends AbstractCommand<Node>{
30
31     private static final Logger LOG = LoggerFactory.getLogger(UniAddCommand.class);
32
33     public UniAddCommand(final DataBroker dataBroker, final DataTreeModification<Node> newUniNode) {
34         super(dataBroker, newUniNode);
35     }
36
37     @Override
38     public void execute() {
39         final InstanceIdentifier<?> uniKey = dataObject.getRootPath().getRootIdentifier();
40         final Optional<Node> optNode = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, uniKey);
41         if(!optNode.isPresent()) {
42             final Node uniNode = dataObject.getRootNode().getDataAfter();
43             final UniAugmentation uni = uniNode.getAugmentation(UniAugmentation.class);
44             if (uni != null) {
45                 LOG.info("New UNI created {}.", uni.getIpAddress().getIpv4Address());
46                 // We assume the ovs is in active mode tcp:ipAddress:6640
47                 if (uni.getOvsdbNodeRef() != null) {
48                     final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
49                     final Optional<Node> optionalNode = MdsalUtils.readNode(dataBroker,
50                                                                        LogicalDatastoreType.OPERATIONAL,
51                                                                        ovsdbNodeRef.getValue());
52                     if (!optionalNode.isPresent()) {
53                         LOG.info("Invalid OVSDB node instance identifier specified, "
54                                + "attempting to retrieve the node.");
55                         final Optional<Node> optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker,
56                                                                                      uni);
57                         Node ovsdbNode;
58                         if (optionalOvsdbNode.isPresent()) {
59                             ovsdbNode = optionalOvsdbNode.get();
60                             LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
61                             // Update QoS entries to ovsdb if speed is configured to UNI node
62                             if (uni.getSpeed() != null) {
63                                 OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni);
64                             }
65                             UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
66                                                       uniKey,
67                                                       uni,
68                                                       ovsdbNode,
69                                                       dataBroker);
70                         } else {
71                             ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker,
72                                                                     uni);
73                             LOG.info("Could not retrieve the OVSDB node,"
74                                    + " created a new one: {}", ovsdbNode.getNodeId());
75                             UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
76                                                       uniKey,
77                                                       uni,
78                                                       ovsdbNode,
79                                                       dataBroker);
80                         }
81                     }
82                 } else {
83                     // We assume the ovs is in passive mode
84                     // Check if the ovsdb node exist
85                     final Optional<Node> optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker,
86                                                                                  uni);
87                     Node ovsdbNode;
88                     if (optionalOvsdbNode.isPresent()) {
89                         ovsdbNode = optionalOvsdbNode.get();
90                         final InstanceIdentifier<Node> ovsdbIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
91                         LOG.info("Retrieved the OVSDB node");
92                         // Update QoS entries to ovsdb if speed is configured to UNI node
93                         if (uni.getSpeed() != null) {
94                             OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni);
95                         }
96                         OvsdbUtils.createBridgeNode(dataBroker,
97                                                      ovsdbIid,
98                                                      uni,
99                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
100                         UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
101                                                   uniKey,
102                                                   uni,
103                                                   ovsdbNode,
104                                                   dataBroker);
105                         UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniKey, uni, ovsdbNode, dataBroker);
106                     } else {
107                         ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker,
108                                                                 uni);
109                         if (ovsdbNode != null) {
110                             LOG.info("Could not retrieve the OVSDB node,"
111                                     + "created a new one: {}", ovsdbNode.getNodeId());
112                             UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
113                                                        uniKey,
114                                                        uni,
115                                                        ovsdbNode,
116                                                        dataBroker);
117                         }
118                     }
119                 }
120             }
121         }
122     }
123
124 }