Modified scm section.
[unimgr.git] / impl / src / main / java / org / opendaylight / vcpe / command / UniCreateCommand.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.vcpe.command;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12
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.vcpe.impl.VcpeConstants;
16 import org.opendaylight.vcpe.impl.VcpeMapper;
17 import org.opendaylight.vcpe.impl.VcpeUtils;
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.vcpe.rev150622.Unis;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vcpe.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;
28
29 import com.google.common.base.Optional;
30
31 public class UniCreateCommand extends AbstractCreateCommand {
32
33     private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class);
34
35     public UniCreateCommand(DataBroker dataBroker,
36             Map<InstanceIdentifier<?>, DataObject> changes) {
37         super.dataBroker = dataBroker;
38         super.changes = changes;
39     }
40
41     @Override
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
50                  * active mode.
51                  *
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
55                  * in passive mode.
56                  *
57                  * Active mode (TCP): the UUID is in format ovsdb://UUID
58                  * Passwove mode (PTCP): the UUID is in format ovsdb://IP:6640
59                  *
60                  */
61                 NodeId ovsdbNodeId = uni.getOvsdbNodeId();
62                 if (ovsdbNodeId == null || ovsdbNodeId.getValue().isEmpty()) {
63                     // We assume the ovs is in passive mode
64                     ovsdbNodeId = VcpeMapper.createNodeId(uni.getIpAddress());
65                 }
66                 // We retrieve the node from the store
67                 Optional<Node> node = VcpeUtils.readNode(dataBroker, VcpeMapper.getOvsdbNodeIID(ovsdbNodeId));
68                 if (!node.isPresent()) {
69                     VcpeUtils.createOvsdbNode(dataBroker, ovsdbNodeId, uni);
70                 }
71             }
72             if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
73                 OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
74                         .getValue();
75                 if (ovsdbNodeAugmentation != null) {
76                     LOG.info("Received an OVSDB node create {}",
77                             ovsdbNodeAugmentation.getConnectionInfo()
78                                     .getRemoteIp().getIpv4Address().getValue());
79                     Unis unis = VcpeUtils.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())) {
87
88                                     VcpeUtils.createBridgeNode(dataBroker,
89                                             uni.getOvsdbNodeId(), uni,
90                                             VcpeConstants.DEFAULT_BRIDGE_NAME);
91
92                                     VcpeUtils.copyUniToDataStore(dataBroker, uni, LogicalDatastoreType.OPERATIONAL);
93                                 }
94                                 // The OVS instance is in ptcp mode.
95                             } else if (ovsdbNodeAugmentation
96                                             .getConnectionInfo()
97                                             .getRemoteIp()
98                                             .equals(uni.getIpAddress())) {
99                                 InstanceIdentifier<Node> ovsdbNodeIid = VcpeMapper.getOvsdbNodeIID(uni.getIpAddress());
100                                 Optional<Node> ovsdbNode = VcpeUtils.readNode(dataBroker, ovsdbNodeIid);
101                                 NodeId ovsdbNodeId;
102                                 if (ovsdbNode.isPresent()) {
103                                     ovsdbNodeId = ovsdbNode.get().getNodeId();
104                                     VcpeUtils.createBridgeNode(dataBroker,
105                                             ovsdbNodeId, uni,
106                                             VcpeConstants.DEFAULT_BRIDGE_NAME);
107
108                                     VcpeUtils.copyUniToDataStore(dataBroker, uni, LogicalDatastoreType.OPERATIONAL);
109                                 } else {
110                                     LOG.error("Unable to read node with IID {}", ovsdbNodeIid);
111                                 }
112                             }
113                         }
114                     } else {
115                         LOG.info("Received a new OVSDB node connection from {}"
116                                 + ovsdbNodeAugmentation.getConnectionInfo()
117                                         .getRemoteIp().getIpv4Address());
118                     }
119                 }
120             }
121         }
122     }
123
124 }