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;
11 import java.util.Map.Entry;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.unimgr.impl.UnimgrConstants;
16 import org.opendaylight.unimgr.impl.UnimgrMapper;
17 import org.opendaylight.unimgr.impl.UnimgrUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.Unis;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev150622.unis.Uni;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.common.base.Optional;
31 public class UniCreateCommand extends AbstractCreateCommand {
33 private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class);
35 public UniCreateCommand(DataBroker dataBroker,
36 Map<InstanceIdentifier<?>, DataObject> changes) {
37 super.dataBroker = dataBroker;
38 super.changes = changes;
42 public void execute() {
43 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
44 if (created.getValue() != null && created.getValue() instanceof Uni) {
45 Uni uni = (Uni) created.getValue();
46 LOG.info("New UNI created with id {}.", uni.getId());
47 /* We assume that when the user specifies the
48 * ovsdb-node-id 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 * Passwove mode (PTCP): the UUID is in format ovsdb://IP:6640
61 NodeId ovsdbNodeId = uni.getOvsdbNodeId();
62 if (ovsdbNodeId == null || ovsdbNodeId.getValue().isEmpty()) {
63 // We assume the ovs is in passive mode
64 ovsdbNodeId = UnimgrMapper.createNodeId(uni.getIpAddress());
66 // We retrieve the node from the store
67 Optional<Node> node = UnimgrUtils.readNode(dataBroker, UnimgrMapper.getOvsdbNodeIID(ovsdbNodeId));
68 if (!node.isPresent()) {
69 UnimgrUtils.createOvsdbNode(dataBroker, ovsdbNodeId, uni);
72 if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
73 OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
75 if (ovsdbNodeAugmentation != null) {
76 LOG.info("Received an OVSDB node create {}",
77 ovsdbNodeAugmentation.getConnectionInfo()
78 .getRemoteIp().getIpv4Address().getValue());
79 Unis unis = UnimgrUtils.readUnisFromStore(dataBroker, LogicalDatastoreType.CONFIGURATION);
80 if (unis != null && unis.getUni() != null) {
81 // This will not scale up very well when the UNI quantity gets to higher numbers.
82 for (Uni uni: unis.getUni()) {
83 if (uni.getOvsdbNodeId() != null && uni.getOvsdbNodeId().getValue() != null) {
84 // The OVS instance is in tcp mode.
85 NodeKey key = created.getKey().firstKeyOf(Node.class, NodeKey.class);
86 if (uni.getOvsdbNodeId().equals(key.getNodeId())) {
88 UnimgrUtils.createBridgeNode(dataBroker,
89 uni.getOvsdbNodeId(), uni,
90 UnimgrConstants.DEFAULT_BRIDGE_NAME);
92 UnimgrUtils.copyUniToDataStore(dataBroker, uni, LogicalDatastoreType.OPERATIONAL);
94 // The OVS instance is in ptcp mode.
95 } else if (ovsdbNodeAugmentation
98 .equals(uni.getIpAddress())) {
99 InstanceIdentifier<Node> ovsdbNodeIid = UnimgrMapper
100 .getOvsdbNodeIID(uni.getIpAddress());
101 Optional<Node> ovsdbNode = UnimgrUtils.readNode(dataBroker, ovsdbNodeIid);
103 if (ovsdbNode.isPresent()) {
104 ovsdbNodeId = ovsdbNode.get().getNodeId();
105 UnimgrUtils.createBridgeNode(dataBroker,
107 UnimgrConstants.DEFAULT_BRIDGE_NAME);
109 UnimgrUtils.copyUniToDataStore(dataBroker, uni, LogicalDatastoreType.OPERATIONAL);
111 LOG.error("Unable to read node with IID {}", ovsdbNodeIid);
116 LOG.info("Received a new OVSDB node connection from {}"
117 + ovsdbNodeAugmentation.getConnectionInfo()
118 .getRemoteIp().getIpv4Address());