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.trace("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 if (uni.getOvsdbNodeRef() != null) {
62 OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
63 Optional<Node> optionalNode = UnimgrUtils.readNode(dataBroker,
64 LogicalDatastoreType.OPERATIONAL,
65 ovsdbNodeRef.getValue());
66 if (!optionalNode.isPresent()) {
67 LOG.info("Invalid OVSDB node instance identifier specified, "
68 + "attempting to retrieve the node.");
69 Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker,
72 if (optionalOvsdbNode.isPresent()) {
73 ovsdbNode = optionalOvsdbNode.get();
74 LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
75 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
81 ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker,
83 LOG.info("Could not retrieve the OVSDB node,"
84 + " created a new one: {}", ovsdbNode.getNodeId());
85 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
93 // We assume the ovs is in passive mode
94 // Check if the ovsdb node exist
95 Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker,
98 if (optionalOvsdbNode.isPresent()) {
99 ovsdbNode = optionalOvsdbNode.get();
100 InstanceIdentifier<Node> ovsdbIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId());
101 LOG.info("Retrieved the OVSDB node");
102 UnimgrUtils.createBridgeNode(dataBroker,
105 UnimgrConstants.DEFAULT_BRIDGE_NAME);
106 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
112 ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker,
114 if (ovsdbNode != null) {
115 LOG.info("Could not retrieve the OVSDB node,"
116 + "created a new one: {}", ovsdbNode.getNodeId());
117 UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
126 if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
127 OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
129 InstanceIdentifier<Node> ovsdbIid = created.getKey().firstIdentifierOf(Node.class);
130 if (ovsdbNodeAugmentation != null) {
131 LOG.info("Received an OVSDB node create {}",
132 ovsdbNodeAugmentation.getConnectionInfo()
136 List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker);
137 if (uniNodes != null && !uniNodes.isEmpty()) {
138 for (Node uniNode: uniNodes) {
139 UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
140 if (uniAugmentation.getOvsdbNodeRef() != null
141 && uniAugmentation.getOvsdbNodeRef().getValue() != null) {
142 InstanceIdentifier<Node> ovsdbNodeRefIid = uniAugmentation
145 .firstIdentifierOf(Node.class);
146 if (ovsdbNodeRefIid.equals(ovsdbIid)) {
147 Optional<Node> optionalOvsdbNode = UnimgrUtils.readNode(dataBroker,
148 LogicalDatastoreType.OPERATIONAL,
150 if (optionalOvsdbNode.isPresent()) {
151 InstanceIdentifier<Node> uniIid =
152 UnimgrMapper.getUniIid(dataBroker,
153 uniAugmentation.getIpAddress(),
154 LogicalDatastoreType.CONFIGURATION);
155 UnimgrUtils.createBridgeNode(dataBroker,
158 UnimgrConstants.DEFAULT_BRIDGE_NAME);
159 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
166 } else if (ovsdbNodeAugmentation
169 .equals(uniAugmentation.getIpAddress())) {
170 InstanceIdentifier<Node> uniIid = UnimgrMapper.getUniIid(dataBroker,
171 uniAugmentation.getIpAddress(),
172 LogicalDatastoreType.CONFIGURATION);
173 UnimgrUtils.createBridgeNode(dataBroker,
176 UnimgrConstants.DEFAULT_BRIDGE_NAME);
177 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
185 LOG.info("Received a new OVSDB node connection from {}"
186 + ovsdbNodeAugmentation.getConnectionInfo()
187 .getRemoteIp().getIpv4Address());