04ed881e90785bef350b48e1947cbb5aa81b7ff8
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointCreateCommand.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.ovsdb.southbound.ovsdb.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.ovsdb.lib.notation.Mutator;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
21 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
22 import org.opendaylight.ovsdb.schema.openvswitch.Port;
23 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.collect.Sets;
33
34 public class TerminationPointCreateCommand implements TransactCommand {
35     private AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbTerminationPointAugmentation> changes;
36     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
37
38     public TerminationPointCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>,
39             OvsdbTerminationPointAugmentation> changes) {
40         this.changes = changes;
41     }
42
43     @Override
44     public void execute(TransactionBuilder transaction) {
45         for (OvsdbTerminationPointAugmentation terminationPoint: changes.getCreatedData().values()) {
46             LOG.debug("Received request to create termination point {} at managed node {}",
47                     terminationPoint.getName(),
48                     terminationPoint.getAttachedTo().getValue().firstIdentifierOf(Node.class));
49
50
51             // Configure interface
52             String interfaceUuid = "Interface_" + terminationPoint.getName();
53             Interface ovsInterface = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
54             ovsInterface.setName(terminationPoint.getName());
55             ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
56             Integer ofPort = terminationPoint.getOfport();
57             if (ofPort != null) {
58                 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
59             }
60             Integer ofPortRequest = terminationPoint.getOfportRequest();
61             if (ofPortRequest != null) {
62                 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest));
63             }
64
65             //Configure optional input
66             if (terminationPoint.getOptions() != null) {
67                 for (Options option : terminationPoint.getOptions()) {
68                     ovsInterface.setOptions(ImmutableMap.of(option.getOption(),option.getValue()));
69                 }
70             }
71             transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
72
73             // Configure port with the above interface details
74             String portUuid = "Port_" + terminationPoint.getName();
75             Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
76             port.setName(terminationPoint.getName());
77             port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
78             if (terminationPoint.getVlanTag() != null) {
79                 Set<Long> vlanTag = new HashSet<Long>();
80                 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
81                 port.setTag(vlanTag);
82             }
83             transaction.add(op.insert(port).withId(portUuid));
84
85             //Configure bridge with the above port details
86             Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
87             bridge.setName(terminationPoint.getBridgeName());
88             bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
89
90             transaction.add(op.mutate(bridge)
91                     .addMutation(bridge.getPortsColumn().getSchema(), Mutator.INSERT,bridge.getPortsColumn().getData())
92                     .where(bridge.getNameColumn().getSchema().opEqual(bridge.getNameColumn().getData())).build());
93         }
94
95     }
96
97 }