Update EVC
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcUpdateCommand.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.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.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.unimgr.rev151012.EvcAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Optional;
27
28 public class EvcUpdateCommand extends AbstractUpdateCommand {
29
30     private static final Logger LOG = LoggerFactory.getLogger(EvcUpdateCommand.class);
31
32     public EvcUpdateCommand(final DataBroker dataBroker,
33             final Map<InstanceIdentifier<?>, DataObject> changes) {
34         super.dataBroker = dataBroker;
35         super.changes = changes;
36     }
37
38     @Override
39     public void execute() {
40         for (final Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
41             if (created.getValue() != null && created.getValue() instanceof EvcAugmentation) {
42                 final EvcAugmentation evc = (EvcAugmentation) created.getValue();
43                 final InstanceIdentifier<?> evcKey = created.getKey();
44
45                 // FIXME: For now, we assume that there is 1 uni per
46                 // source/destination
47                 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
48                     LOG.error("Destination UNI cannot be null.");
49                     break;
50                 }
51                 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
52                     LOG.error("Source UNI cannot be null.");
53                     break;
54                 }
55                 LOG.trace("New EVC created, source IP: {} destination IP {}.",
56                         evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
57                         evc.getUniDest().iterator().next().getIpAddress().getIpv4Address());
58                 InstanceIdentifier<Node> sourceUniIid;
59                 InstanceIdentifier<Node> destinationUniIid;
60
61                 final InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
62                 if (iidSource != null) {
63                     sourceUniIid = iidSource.firstIdentifierOf(Node.class);
64                 } else {
65                     sourceUniIid = UnimgrMapper.getUniIid(dataBroker,
66                             evc.getUniSource().iterator().next().getIpAddress(),
67                             LogicalDatastoreType.OPERATIONAL);
68                 }
69                 final InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
70                 if (iidDest != null) {
71                     destinationUniIid = iidDest.firstIdentifierOf(Node.class);
72                 } else {
73                     destinationUniIid = UnimgrMapper.getUniIid(dataBroker,
74                             evc.getUniDest().iterator().next().getIpAddress(),
75                             LogicalDatastoreType.OPERATIONAL);
76                 }
77
78                 // Retrieve the source and destination UNIs
79                 final Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
80                         LogicalDatastoreType.OPERATIONAL,
81                         sourceUniIid);
82                 final Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
83                         LogicalDatastoreType.OPERATIONAL,
84                         destinationUniIid);
85
86                 Node uniSource = null;
87                 Node uniDestination = null;
88
89                 if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) {
90                     uniSource = optionalUniSource.get();
91                     uniDestination = optionalUniDestination.get();
92
93                     // Retrieve the source and/or destination OVSDB node
94                     final UniAugmentation sourceUniAugmentation =
95                             uniSource.getAugmentation(UniAugmentation.class);
96                     final UniAugmentation destinationUniAugmentation =
97                             uniDestination.getAugmentation(UniAugmentation.class);
98                     final Optional<Node> optionalSourceOvsdbNode =
99                             UnimgrUtils.readNode(dataBroker,
100                                     LogicalDatastoreType.OPERATIONAL,
101                                     sourceUniAugmentation
102                                     .getOvsdbNodeRef()
103                                     .getValue());
104                     final Optional<Node> optionalDestinationOvsdbNode =
105                             UnimgrUtils.readNode(dataBroker,
106                                     LogicalDatastoreType.OPERATIONAL,
107                                     destinationUniAugmentation
108                                     .getOvsdbNodeRef()
109                                     .getValue());
110                     if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
111                         // Retrieve the source and/or destination bridge
112                         final InstanceIdentifier<Node> sourceBridgeIid =
113                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
114                         final Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
115                                 LogicalDatastoreType.OPERATIONAL,
116                                 sourceBridgeIid);
117                         final InstanceIdentifier<Node> destinationBridgeIid =
118                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
119                         final Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
120                                 LogicalDatastoreType.OPERATIONAL,
121                                 destinationBridgeIid);
122                         Node sourceBr = null;
123                         Node destinationBr = null;
124                         if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
125                             sourceBr = optionalSourceBr.get();
126                             destinationBr = optionalDestinationBr.get();
127
128                             // Creating termination points (OVSDB CONFIG
129                             // datastore)
130                             UnimgrUtils.createTerminationPointNode(dataBroker,
131                                     uniSource.getAugmentation(UniAugmentation.class),
132                                     sourceBr,
133                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
134                                     UnimgrConstants.DEFAULT_TUNNEL_IFACE);
135
136                             // Create GRE tunnel (OVSDB CONFIG datastore)
137                             UnimgrUtils.createGreTunnel(dataBroker,
138                                     uniSource.getAugmentation(UniAugmentation.class),
139                                     uniDestination.getAugmentation(UniAugmentation.class),
140                                     sourceBr,
141                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
142                                     UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
143
144                             // Create termination points (CONFIG datastore)
145                             UnimgrUtils.createTerminationPointNode(dataBroker,
146                                     uniSource.getAugmentation(UniAugmentation.class),
147                                     destinationBr,
148                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
149                                     UnimgrConstants.DEFAULT_TUNNEL_IFACE);
150
151                             // Create GRE tunnel (OVSDB CONFIG datastore)
152                             UnimgrUtils.createGreTunnel(dataBroker,
153                                     uniDestination.getAugmentation(UniAugmentation.class),
154                                     uniSource.getAugmentation(UniAugmentation.class), destinationBr,
155                                     UnimgrConstants.DEFAULT_BRIDGE_NAME,
156                                     UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
157
158                             // Update EVC
159                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
160                                     evcKey,
161                                     evc,
162                                     sourceUniIid,
163                                     destinationUniIid,
164                                     dataBroker);
165                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
166                                     evcKey,
167                                     evc,
168                                     sourceUniIid,
169                                     destinationUniIid,
170                                     dataBroker);
171                         } else {
172                             LOG.info("Unable to retrieve the source and/or destination bridge.");
173                         }
174                     } else {
175                         LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
176                     }
177                 } else {
178                     LOG.info("Unable to retrieve the source and/or destination Uni.");
179                 }
180             }
181         }
182     }
183 }