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