Implemented EVC delete
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcCreateCommand.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 EvcCreateCommand extends AbstractCreateCommand {
29
30     private static final Logger LOG = LoggerFactory.getLogger(EvcCreateCommand.class);
31
32     public EvcCreateCommand(DataBroker dataBroker,
33                             Map<InstanceIdentifier<?>, DataObject> changes) {
34         super.dataBroker = dataBroker;
35         super.changes = changes;
36     }
37
38     @Override
39     public void execute() {
40         for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
41             if (created.getValue() != null && created.getValue() instanceof EvcAugmentation) {
42                 EvcAugmentation evc = (EvcAugmentation) created.getValue();
43                 InstanceIdentifier<?> evcKey = created.getKey();
44                 LOG.trace("New EVC created, source IP: {} destination IP {}.",
45                         evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
46                         evc.getUniDest().iterator().next().getIpAddress().getIpv4Address());
47                 // For now, we assume that there is 1 uni per source/destination
48                 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
49                     LOG.error("Destination UNI cannot be null.");
50                     break;
51                 }
52                 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
53                     LOG.error("Source UNI cannot be null.");
54                     break;
55                 }
56                 InstanceIdentifier<Node> sourceUniIid;
57                 InstanceIdentifier<Node> destinationUniIid;
58                 if (evc.getUniSource().iterator().next().getUni() != null) {
59                     sourceUniIid = evc.getUniSource().iterator().next().getUni().firstIdentifierOf(Node.class);
60                 } else {
61                     sourceUniIid = UnimgrMapper.getUniIid(dataBroker,
62                                                           evc.getUniSource().iterator().next().getIpAddress(),
63                                                           LogicalDatastoreType.OPERATIONAL);
64                 }
65                 if (evc.getUniDest().iterator().next().getUni() != null) {
66                     destinationUniIid = evc.getUniDest().iterator().next().getUni().firstIdentifierOf(Node.class);
67                 } else {
68                     destinationUniIid = UnimgrMapper.getUniIid(dataBroker,
69                                                                evc.getUniDest().iterator().next().getIpAddress(),
70                                                                LogicalDatastoreType.OPERATIONAL);
71                 }
72                 Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
73                                                                         LogicalDatastoreType.OPERATIONAL,
74                                                                         sourceUniIid);
75                 Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
76                                                                              LogicalDatastoreType.OPERATIONAL,
77                                                                              destinationUniIid);
78                 Node uniSource;
79                 Node uniDestination;
80                 // Retrieve the source and destination Unis
81                 if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) {
82                     uniSource = optionalUniSource.get();
83                     uniDestination = optionalUniDestination.get();
84                     // Set source and destination
85                     UniAugmentation sourceUniAugmentation =
86                                         uniSource.getAugmentation(UniAugmentation.class);
87                     UniAugmentation destinationUniAugmentation =
88                                         uniDestination.getAugmentation(UniAugmentation.class);
89                     Optional<Node> optionalSourceOvsdbNode =
90                                        UnimgrUtils.readNode(dataBroker,
91                                                             LogicalDatastoreType.OPERATIONAL,
92                                                             sourceUniAugmentation
93                                                                 .getOvsdbNodeRef()
94                                                                 .getValue());
95                     Optional<Node> optionalDestinationOvsdbNode =
96                                        UnimgrUtils.readNode(dataBroker,
97                                                             LogicalDatastoreType.OPERATIONAL,
98                                                             destinationUniAugmentation
99                                                                 .getOvsdbNodeRef()
100                                                                 .getValue());
101                     if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
102                         InstanceIdentifier<Node> sourceBridgeIid =
103                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
104                         Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
105                                                                                LogicalDatastoreType.OPERATIONAL,
106                                                                                sourceBridgeIid);
107                         InstanceIdentifier<Node> destinationBridgeIid =
108                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
109                         Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
110                                                                                     LogicalDatastoreType.OPERATIONAL,
111                                                                                     destinationBridgeIid);
112                         Node sourceBr;
113                         Node destinationBr;
114                         if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
115                             sourceBr = optionalSourceBr.get();
116                             destinationBr = optionalDestinationBr.get();
117                             UnimgrUtils.createTerminationPointNode(dataBroker,
118                                                                    uniSource.getAugmentation(UniAugmentation.class),
119                                                                    sourceBr,
120                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
121                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE,
122                                                                    UnimgrConstants.DEFAULT_GRE_NAME);
123                             UnimgrUtils.createGreTunnel(dataBroker,
124                                                         uniSource.getAugmentation(UniAugmentation.class),
125                                                         uniDestination.getAugmentation(UniAugmentation.class),
126                                                         sourceBr,
127                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
128                                                         "gre0");
129                             UnimgrUtils.createTerminationPointNode(dataBroker, 
130                                                                    uniSource.getAugmentation(UniAugmentation.class),
131                                                                    destinationBr,
132                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
133                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE,
134                                                                    UnimgrConstants.DEFAULT_GRE_NAME);
135                             UnimgrUtils.createGreTunnel(dataBroker,
136                                                         uniDestination.getAugmentation(UniAugmentation.class),
137                                                         uniSource.getAugmentation(UniAugmentation.class), destinationBr,
138                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
139                                                         "gre0");
140                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
141                                                       evcKey,
142                                                       evc,
143                                                       sourceUniIid,
144                                                       destinationUniIid,
145                                                       dataBroker);
146                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
147                                                       evcKey,
148                                                       evc,
149                                                       sourceUniIid,
150                                                       destinationUniIid,
151                                                       dataBroker);
152                         } else {
153                             LOG.info("Unable to retrieve the source and/or destination bridge.");
154                         }
155                     } else {
156                         LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
157                     }
158                 } else {
159                     LOG.info("Unable to retrieve the source and/or destination Uni.");
160                 }
161             }
162         }
163     }
164
165 }