fix re-create evc and uni
[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.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
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                 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,
70                                                                                      uni);
71                         Node ovsdbNode;
72                         if (optionalOvsdbNode.isPresent()) {
73                             ovsdbNode = optionalOvsdbNode.get();
74                             LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId());
75                             UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
76                                                       uniKey,
77                                                       uni,
78                                                       ovsdbNode,
79                                                       dataBroker);
80                         } else {
81                             ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker,
82                                                                     uni);
83                             LOG.info("Could not retrieve the OVSDB node,"
84                                    + " created a new one: {}", ovsdbNode.getNodeId());
85                             UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
86                                                       uniKey,
87                                                       uni,
88                                                       ovsdbNode,
89                                                       dataBroker);
90                         }
91                     }
92                 } else {
93                     // We assume the ovs is in passive mode
94                     // Check if the ovsdb node exist
95                     Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker,
96                                                                                  uni);
97                     Node ovsdbNode;
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,
103                                                      ovsdbIid,
104                                                      uni,
105                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
106                         UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
107                                                   uniKey,
108                                                   uni,
109                                                   ovsdbNode,
110                                                   dataBroker);
111                         UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniKey, uni, ovsdbNode, dataBroker);
112                     } else {
113                         ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker,
114                                                                 uni);
115                         if (ovsdbNode != null) {
116                             LOG.info("Could not retrieve the OVSDB node,"
117                                     + "created a new one: {}", ovsdbNode.getNodeId());
118                              UnimgrUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION,
119                                                        uniKey,
120                                                        uni,
121                                                        ovsdbNode,
122                                                        dataBroker);
123                         }
124                     }
125                 }
126             }
127             if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) {
128                 OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created
129                                                                                           .getValue();
130                 InstanceIdentifier<Node> ovsdbIid = created.getKey().firstIdentifierOf(Node.class);
131                 if (ovsdbNodeAugmentation != null) {
132                     LOG.info("Received an OVSDB node create {}",
133                             ovsdbNodeAugmentation.getConnectionInfo()
134                                                  .getRemoteIp()
135                                                  .getIpv4Address()
136                                                  .getValue());
137                     List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker);
138                     if (uniNodes != null && !uniNodes.isEmpty()) {
139                         for (Node uniNode: uniNodes) {
140                             UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class);
141                             if (uniAugmentation.getOvsdbNodeRef() != null
142                                     && uniAugmentation.getOvsdbNodeRef().getValue() != null) {
143                                 InstanceIdentifier<Node> ovsdbNodeRefIid = uniAugmentation
144                                                                             .getOvsdbNodeRef()
145                                                                             .getValue()
146                                                                             .firstIdentifierOf(Node.class);
147                                 if (ovsdbNodeRefIid.equals(ovsdbIid)) {
148                                     Optional<Node> optionalOvsdbNode = UnimgrUtils.readNode(dataBroker,
149                                                                                             LogicalDatastoreType.OPERATIONAL,
150                                                                                             ovsdbIid);
151                                     if (optionalOvsdbNode.isPresent()) {
152                                         InstanceIdentifier<Node> uniIid =
153                                                                     UnimgrMapper.getUniIid(dataBroker,
154                                                                                            uniAugmentation.getIpAddress(),
155                                                                                            LogicalDatastoreType.CONFIGURATION);
156                                         UnimgrUtils.createBridgeNode(dataBroker,
157                                                                      ovsdbIid,
158                                                                      uniAugmentation,
159                                                                      UnimgrConstants.DEFAULT_BRIDGE_NAME);
160                                         UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
161                                                                   uniIid,
162                                                                   uniAugmentation,
163                                                                   ovsdbIid,
164                                                                   dataBroker);
165                                     }
166                                 }
167                             } else if (ovsdbNodeAugmentation
168                                           .getConnectionInfo()
169                                           .getRemoteIp()
170                                           .equals(uniAugmentation.getIpAddress())) {
171                                 InstanceIdentifier<Node> uniIid = UnimgrMapper.getUniIid(dataBroker,
172                                                                                          uniAugmentation.getIpAddress(),
173                                                                                          LogicalDatastoreType.CONFIGURATION);
174                                 UnimgrUtils.createBridgeNode(dataBroker,
175                                                              ovsdbIid,
176                                                              uniAugmentation,
177                                                              UnimgrConstants.DEFAULT_BRIDGE_NAME);
178                                 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL,
179                                                           uniIid,
180                                                           uniAugmentation,
181                                                           ovsdbIid,
182                                                           dataBroker);
183                             }
184                         }
185                     } else {
186                         LOG.info("Received a new OVSDB node connection from {}"
187                                 + ovsdbNodeAugmentation.getConnectionInfo()
188                                         .getRemoteIp().getIpv4Address());
189                     }
190                 }
191             }
192         }
193     }
194
195 }