Integrate the new yang model with Network Topology augmentation
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / 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.unimgr.command;
9
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Map.Entry;
13
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;
27
28 import com.google.common.base.Optional;
29
30 public class UniCreateCommand extends AbstractCreateCommand {
31
32     private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class);
33
34     public UniCreateCommand(DataBroker dataBroker,
35             Map<InstanceIdentifier<?>, DataObject> changes) {
36         super.dataBroker = dataBroker;
37         super.changes = changes;
38     }
39
40     @Override
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
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                  * Passive mode (PTCP): the UUID is in format ovsdb://IP:6640
59                  *
60                  */
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,
67                                                      ovsdbNode, uni,
68                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
69                     } else {
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,
76                                                       uniKey,
77                                                       uni,
78                                                       ovsdbNode,
79                                                       dataBroker);
80                             UnimgrUtils.createBridgeNode(dataBroker,
81                                     ovsdbNode, uni,
82                                     UnimgrConstants.DEFAULT_BRIDGE_NAME);
83                         } else {
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,
88                                           uniKey,
89                                           uni,
90                                           ovsdbNode,
91                                           dataBroker);
92                             UnimgrUtils.createBridgeNode(dataBroker,
93                                                          ovsdbNode,
94                                                          uni,
95                                     UnimgrConstants.DEFAULT_BRIDGE_NAME);
96                         }
97                     }
98                 } else {
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,
105                                                   uniKey,
106                                                   uni,
107                                                   ovsdbNode,
108                                                   dataBroker);
109                         UnimgrUtils.createBridgeNode(dataBroker,
110                                                      ovsdbNode,
111                                                      uni,
112                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
113                     } else {
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,
118                                                   uniKey,
119                                                   uni,
120                                                   ovsdbNode,
121                                                   dataBroker);
122                         UnimgrUtils.createBridgeNode(dataBroker,
123                                                      ovsdbNode,
124                                                      uni,
125                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
126                     }
127                 }
128             }
129             if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
130                 OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
131                         .getValue();
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);
157                                     }
158                                 }
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);
170                                 } else {
171                                     LOG.error("Unable to read node with IID {}", ovsdbNodeIid);
172                                 }
173                             }
174                         }
175                     } else {
176                         LOG.info("Received a new OVSDB node connection from {}"
177                                 + ovsdbNodeAugmentation.getConnectionInfo()
178                                         .getRemoteIp().getIpv4Address());
179                     }
180                 }
181             }
182         }
183     }
184
185 }