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.impl.UnimgrUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
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.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Optional;
30 public class UniCreateCommand extends AbstractCreateCommand {
32 private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class);
34 public UniCreateCommand(DataBroker dataBroker,
35 Map<InstanceIdentifier<?>, DataObject> changes) {
36 super.dataBroker = dataBroker;
37 super.changes = changes;
41 public void execute() {
42 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
43 if (created.getValue() != null && created.getValue() instanceof UniAugmentation) {
44 UniAugmentation uni = (UniAugmentation) created.getValue();
45 InstanceIdentifier<?> uniKey = created.getKey();
46 LOG.info("New UNI created {}.", uni.getIpAddress().getIpv4Address());
47 /* We assume that when the user specifies the
48 * ovsdb-node-ref that the node already exists in
49 * the controller and that the OVS instance is in
52 * We assume that when the user doesn't specify the
53 * ovsdb-node-id that the node doesn't exist therefor
54 * has to be created with the IP address because it's
57 * Active mode (TCP): the UUID is in format ovsdb://UUID
58 * Passive mode (PTCP): the UUID is in format ovsdb://IP:6640
61 OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
62 if (ovsdbNodeRef != null || ovsdbNodeRef.getValue() != null) {
63 Optional<Node> optionalNode = UnimgrUtils.readNode(dataBroker, ovsdbNodeRef.getValue());
64 if (optionalNode.isPresent()) {
65 Node ovsdbNode = optionalNode.get();
66 UnimgrUtils.createBridgeNode(dataBroker,
68 UnimgrConstants.DEFAULT_BRIDGE_NAME);
70 LOG.info("Invalid OVSDB node instance identifier specified, "
71 + "attempting to retrieve the node.");
72 Node ovsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
73 if (ovsdbNode != null) {
74 LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
75 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
80 UnimgrUtils.createBridgeNode(dataBroker,
82 UnimgrConstants.DEFAULT_BRIDGE_NAME);
84 ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker, uni);
85 LOG.info("Could not retrieve the OVSDB node,"
86 + "created a new one: {}", ovsdbNode.getNodeId());
87 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
92 UnimgrUtils.createBridgeNode(dataBroker,
95 UnimgrConstants.DEFAULT_BRIDGE_NAME);
99 // We assume the ovs is in passive mode
100 // Check if the ovsdb node exist
101 Node ovsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
102 if (ovsdbNode != null) {
103 LOG.info("Retrieved the OVSDB node");
104 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
109 UnimgrUtils.createBridgeNode(dataBroker,
112 UnimgrConstants.DEFAULT_BRIDGE_NAME);
114 ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker, uni);
115 LOG.info("Could not retrieve the OVSDB node,"
116 + "created a new one: {}", ovsdbNode.getNodeId());
117 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
122 UnimgrUtils.createBridgeNode(dataBroker,
125 UnimgrConstants.DEFAULT_BRIDGE_NAME);
129 if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
130 OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
132 InstanceIdentifier<Node> ovsdbIid = created.getKey().firstIdentifierOf(Node.class);
133 if (ovsdbNodeAugmentation != null) {
134 LOG.info("Received an OVSDB node create {}",
135 ovsdbNodeAugmentation.getConnectionInfo()
136 .getRemoteIp().getIpv4Address().getValue());
137 List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker);
138 if (uniNodes != null && !uniNodes.isEmpty()) {
139 // This will not scale up very well when the UNI quantity gets to higher numbers.
140 for (Node node: uniNodes) {
141 UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
142 if (uniAugmentation.getOvsdbNodeRef() != null
143 && uniAugmentation.getOvsdbNodeRef().getValue() != null) {
144 // The OVS instance is in tcp mode.
145 InstanceIdentifier<Node> ovsdbNodeRefIid =
146 uniAugmentation.getOvsdbNodeRef().getValue().firstIdentifierOf(Node.class);
147 if (ovsdbNodeRefIid.equals(ovsdbIid)) {
148 UnimgrUtils.createBridgeNode(dataBroker, node, uniAugmentation,
149 UnimgrConstants.DEFAULT_BRIDGE_NAME);
150 Optional<Node> optionalOvsdbNode = UnimgrUtils.readNode(dataBroker, ovsdbIid);
151 if (optionalOvsdbNode.isPresent()) {
152 Node ovsdbNode = optionalOvsdbNode.get();
153 InstanceIdentifier<Node> uniIid = UnimgrMapper.createUniIid(dataBroker,
154 uniAugmentation.getIpAddress());
155 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIid,
156 uniAugmentation, ovsdbNode, dataBroker);
159 // The OVS instance is in ptcp mode.
160 } else if (ovsdbNodeAugmentation.getConnectionInfo().getRemoteIp()
161 .equals(uniAugmentation.getIpAddress())) {
162 InstanceIdentifier<Node> ovsdbNodeIid = uniAugmentation.getOvsdbNodeRef().getValue()
163 .firstIdentifierOf(Node.class);
164 Optional<Node> ovsdbNode = UnimgrUtils.readNode(dataBroker, ovsdbNodeIid);
165 if (ovsdbNode.isPresent()) {
166 InstanceIdentifier<Node> uniIid = UnimgrMapper.createUniIid(dataBroker,
167 uniAugmentation.getIpAddress());
168 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIid, uniAugmentation,
169 ovsdbNode.get(), dataBroker);
171 LOG.error("Unable to read node with IID {}", ovsdbNodeIid);
176 LOG.info("Received a new OVSDB node connection from {}"
177 + ovsdbNodeAugmentation.getConnectionInfo()
178 .getRemoteIp().getIpv4Address());