Modified scm section.
[unimgr.git] / impl / src / main / java / org / opendaylight / vcpe / 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.vcpe.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.vcpe.impl.VcpeConstants;
15 import org.opendaylight.vcpe.impl.VcpeMapper;
16 import org.opendaylight.vcpe.impl.VcpeUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vcpe.rev150622.evcs.Evc;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vcpe.rev150622.unis.Uni;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
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
41                 .entrySet()) {
42             if (created.getValue() != null && created.getValue() instanceof Evc) {
43                 Evc evc = (Evc) created.getValue();
44                 LOG.info("New EVC created with id {}.", evc.getId());
45                 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
46                     LOG.error("Destination UNI cannot be null.");
47                     break;
48                 }
49                 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
50                     LOG.error("Source UNI cannot be null.");
51                     break;
52                 }
53                 // Get the destination UNI
54                 NodeId destUniNodeID = evc.getUniDest().get(0).getUni();
55                 InstanceIdentifier<Uni> destinationNodeIid = VcpeMapper.getUniIid(destUniNodeID);
56                 Optional<Uni> optionalDestination = VcpeUtils.readUniNode(dataBroker, destinationNodeIid);
57                 Uni destinationUni = optionalDestination.get();
58                 NodeId ovsdbDestinationNodeId = VcpeMapper.createNodeId(destinationUni.getIpAddress());
59                 // Get the source UNI
60                 NodeId sourceUniNodeID = evc.getUniSource().get(0).getUni();
61                 InstanceIdentifier<Uni> sourceNodeIid = VcpeMapper.getUniIid(sourceUniNodeID);
62                 Optional<Uni> optionalSource = VcpeUtils.readUniNode(dataBroker, sourceNodeIid);
63                 Uni sourceUni = optionalSource.get();
64                 NodeId ovsdbSourceNodeId = VcpeMapper.createNodeId(sourceUni.getIpAddress());
65
66                 // Set source
67                 Node sourceBr1 = VcpeUtils.readNode(
68                         dataBroker,
69                         VcpeMapper.getOvsdbBridgeNodeIID(ovsdbSourceNodeId,
70                                 VcpeConstants.DEFAULT_BRIDGE_NAME)).get();
71                 VcpeUtils.createTerminationPointNode(dataBroker,
72                         destinationUni, sourceBr1,
73                         VcpeConstants.DEFAULT_BRIDGE_NAME,
74                         VcpeConstants.DEFAULT_INTERNAL_IFACE, null);
75                 Node sourceBr2 = VcpeUtils.readNode(
76                         dataBroker,
77                         VcpeMapper.getOvsdbBridgeNodeIID(ovsdbSourceNodeId,
78                                 VcpeConstants.DEFAULT_BRIDGE_NAME)).get();
79                 VcpeUtils.createGreTunnel(dataBroker, sourceUni,
80                         destinationUni, sourceBr2,
81                         VcpeConstants.DEFAULT_BRIDGE_NAME, "gre0");
82
83                 // Set destination
84                 Node destinationBr1 = VcpeUtils.readNode(
85                         dataBroker,
86                         VcpeMapper.getOvsdbBridgeNodeIID(ovsdbDestinationNodeId,
87                                 VcpeConstants.DEFAULT_BRIDGE_NAME)).get();
88                 VcpeUtils.createTerminationPointNode(dataBroker,
89                         destinationUni, destinationBr1,
90                         VcpeConstants.DEFAULT_BRIDGE_NAME,
91                         VcpeConstants.DEFAULT_INTERNAL_IFACE, null);
92                 Node destinationBr2 = VcpeUtils.readNode(
93                         dataBroker,
94                         VcpeMapper.getOvsdbBridgeNodeIID(ovsdbDestinationNodeId,
95                                 VcpeConstants.DEFAULT_BRIDGE_NAME)).get();
96                 VcpeUtils.createGreTunnel(dataBroker, destinationUni,
97                         sourceUni, destinationBr2,
98                         VcpeConstants.DEFAULT_BRIDGE_NAME, "gre0");
99             }
100         }
101     }
102
103 }