2 * Copyright (c) 2015 CableLabs and others. All rights reserved.
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
8 package org.opendaylight.unimgr.command;
10 import java.util.List;
12 import java.util.Map.Entry;
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;
30 import com.google.common.base.Optional;
32 public class UniCreateCommand extends AbstractCreateCommand {
34 private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class);
36 public UniCreateCommand(DataBroker dataBroker,
37 Map<InstanceIdentifier<?>, DataObject> changes) {
38 super.dataBroker = dataBroker;
39 super.changes = changes;
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
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
59 * Active mode (TCP): the UUID is in format ovsdb://UUID
60 * Passive mode (PTCP): the UUID is in format ovsdb://IP:6640
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,
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);
81 UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
87 ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker,
89 LOG.info("Could not retrieve the OVSDB node,"
90 + " created a new one: {}", ovsdbNode.getNodeId());
91 UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
99 // We assume the ovs is in passive mode
100 // Check if the ovsdb node exist
101 final Optional<Node> optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker,
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);
112 OvsdbUtils.createBridgeNode(dataBroker,
115 UnimgrConstants.DEFAULT_BRIDGE_NAME);
116 UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
121 UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniKey, uni, ovsdbNode, dataBroker);
123 ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker,
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,
137 if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
138 final OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
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()
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
156 .firstIdentifierOf(Node.class);
157 if (ovsdbNodeRefIid.equals(ovsdbIid)) {
158 final Optional<Node> optionalOvsdbNode = MdsalUtils.readNode(dataBroker,
159 LogicalDatastoreType.OPERATIONAL,
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);
170 OvsdbUtils.createBridgeNode(dataBroker,
173 UnimgrConstants.DEFAULT_BRIDGE_NAME);
174 UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
181 } else if (ovsdbNodeAugmentation
184 .equals(uniAugmentation.getIpAddress())) {
185 final InstanceIdentifier<Node> uniIid = UnimgrMapper.getUniIid(dataBroker,
186 uniAugmentation.getIpAddress(),
187 LogicalDatastoreType.CONFIGURATION);
188 OvsdbUtils.createBridgeNode(dataBroker,
191 UnimgrConstants.DEFAULT_BRIDGE_NAME);
192 UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
200 LOG.info("Received a new OVSDB node connection from {}"
201 + ovsdbNodeAugmentation.getConnectionInfo()
202 .getRemoteIp().getIpv4Address());