d34ae90b6dba1631f2bf8cb170c690bf2c547937
[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.StringTokenizer;
13
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.ovsdb.lib.notation.Mutator;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
17 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
18 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
19 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
20 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
21 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
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<?>, OvsdbTerminationPointAugmentation> changes){
39         this.changes = changes;
40     }
41
42     @Override
43     public void execute(TransactionBuilder transaction) {
44         for(OvsdbTerminationPointAugmentation terminationPoint: changes.getCreatedData().values()){
45             LOG.debug("Received request to create termination point {} at managed node {}",
46                     terminationPoint.getName(),
47                     terminationPoint.getAttachedTo().getValue().firstIdentifierOf(Node.class));
48
49
50             // Configure interface
51             String interfaceUuid = "Interface_" + terminationPoint.getName();
52             Interface ovsInterface = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
53             ovsInterface.setName(terminationPoint.getName());
54             ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
55             Integer ofPort = terminationPoint.getOfport();
56             if(ofPort != null) {
57                 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
58             }
59             Integer ofPortRequest = terminationPoint.getOfportRequest();
60             if(ofPortRequest != null) {
61                 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest));
62             }
63
64             //Configure optional input
65             if (terminationPoint.getOptions() != null) {
66                 for(Options option : terminationPoint.getOptions()){
67                     ovsInterface.setOptions(ImmutableMap.of(option.getOption(),option.getValue()));
68                 }
69             }
70             transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
71
72             // Configure port with the above interface details
73             String portUuid = "Port_" + terminationPoint.getName();
74             Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
75             port.setName(terminationPoint.getName());
76             port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
77             transaction.add(op.insert(port).withId(portUuid));
78
79             //Configure bridge with the above port details
80             Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
81             bridge.setName(terminationPoint.getBridgeName());
82             bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
83
84             transaction.add(op.mutate(bridge)
85                     .addMutation(bridge.getPortsColumn().getSchema(), Mutator.INSERT,bridge.getPortsColumn().getData())
86                     .where(bridge.getNameColumn().getSchema().opEqual(bridge.getNameColumn().getData())).build());
87         }
88
89     }
90
91 }