2527e6f6942f724dba659cf4e95ca73540257e3e
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronL2gatewayInterface.java
1 /*
2  * Copyright (c) 2015 Hewlett-Packard Development Company, L.P. 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
9 package org.opendaylight.neutron.transcriber;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.neutron.spi.INeutronL2gatewayCRUD;
15 import org.opendaylight.neutron.spi.NeutronL2gateway;
16 import org.opendaylight.neutron.spi.NeutronL2gatewayDevice;
17 import org.opendaylight.neutron.spi.NeutronL2gatewayDeviceInterface;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.DevicesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.InterfacesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
26
27 public final class NeutronL2gatewayInterface
28         extends AbstractNeutronInterface<L2gateway, L2gateways, L2gatewayKey, NeutronL2gateway>
29         implements INeutronL2gatewayCRUD {
30     NeutronL2gatewayInterface(DataBroker db) {
31         super(L2gatewayBuilder.class, db);
32     }
33
34     @Override
35     protected List<L2gateway> getDataObjectList(L2gateways l2gateways) {
36         return l2gateways.getL2gateway();
37     }
38
39     @Override
40     protected NeutronL2gateway fromMd(L2gateway l2gateway) {
41         final NeutronL2gateway result = new NeutronL2gateway();
42         final List<NeutronL2gatewayDevice> neutronL2gatewayDevices = new ArrayList<NeutronL2gatewayDevice>();
43
44         if (l2gateway.getUuid() != null) {
45             result.setID(l2gateway.getUuid().getValue());
46         }
47         if (l2gateway.getL2gatewayName() != null) {
48             result.setName(String.valueOf(l2gateway.getL2gatewayName()));
49         }
50         if (l2gateway.getTenantId() != null) {
51             result.setTenantID(l2gateway.getTenantId());
52         }
53         if (l2gateway.getDevices() != null) {
54             for (final Devices device : l2gateway.getDevices()) {
55                 final NeutronL2gatewayDevice neutronL2gatewayDevice = new NeutronL2gatewayDevice();
56                 final List<NeutronL2gatewayDeviceInterface> neutronL2gatewayDeviceInterfaces = new ArrayList<
57                         NeutronL2gatewayDeviceInterface>();
58                 if (device.getDeviceName() != null) {
59                     neutronL2gatewayDevice.setDeviceName(device.getDeviceName().toString());
60                 }
61                 if (device.getUuid() != null) {
62                     neutronL2gatewayDevice.setID(device.getUuid().getValue());
63                 }
64                 if (device.getInterfaces() != null) {
65                     for (final Interfaces deviceInterface : device.getInterfaces()) {
66                         final NeutronL2gatewayDeviceInterface neutronL2gatewayDeviceInterface =
67                                 new NeutronL2gatewayDeviceInterface();
68                         String interfaceName = null;
69                         final List<Integer> segmentationIds = new ArrayList<Integer>();
70                         if (deviceInterface.getInterfaceName() != null) {
71                             interfaceName = deviceInterface.getInterfaceName().toString();
72                         }
73                         if (deviceInterface.getSegmentationIds() != null) {
74                             for (final Integer segmentId : deviceInterface.getSegmentationIds()) {
75                                 segmentationIds.add(segmentId);
76                             }
77                             neutronL2gatewayDeviceInterface.setSegmentationId(segmentationIds);
78                         }
79                         neutronL2gatewayDeviceInterface.setInterfaceName(interfaceName);
80                         neutronL2gatewayDeviceInterfaces.add(neutronL2gatewayDeviceInterface);
81                     }
82                 }
83                 neutronL2gatewayDevice.setNeutronL2gatewayDeviceInterfaces(neutronL2gatewayDeviceInterfaces);
84                 neutronL2gatewayDevices.add(neutronL2gatewayDevice);
85             }
86         }
87         result.setNeutronL2gatewayDevices(neutronL2gatewayDevices);
88         return result;
89     }
90
91     @Override
92     protected L2gateway toMd(NeutronL2gateway neutronObject) {
93         final L2gatewayBuilder l2gatewayBuilder = new L2gatewayBuilder();
94         if (neutronObject.getName() != null) {
95             l2gatewayBuilder.setL2gatewayName(neutronObject.getName());
96         }
97         if (neutronObject.getID() != null) {
98             l2gatewayBuilder.setUuid(toUuid(neutronObject.getID()));
99         }
100         if (neutronObject.getTenantID() != null) {
101             l2gatewayBuilder.setTenantId(toUuid(neutronObject.getTenantID()));
102         }
103
104         if (neutronObject.getNeutronL2gatewayDevices() != null) {
105             final List<Devices> devices = new ArrayList<>();
106             for (final NeutronL2gatewayDevice neutronL2gatewayDevice : neutronObject.getNeutronL2gatewayDevices()) {
107                 final DevicesBuilder deviceBuilder = new DevicesBuilder();
108                 final List<Interfaces> interfaces = new ArrayList<Interfaces>();
109                 for (final NeutronL2gatewayDeviceInterface neutronL2gatewayDeviceInterface : neutronL2gatewayDevice
110                         .getNeutronL2gatewayDeviceInterfaces()) {
111                     final InterfacesBuilder interfacesBuilder = new InterfacesBuilder();
112                     final List<Integer> segmentIds = new ArrayList<Integer>();
113                     interfacesBuilder.setInterfaceName(neutronL2gatewayDeviceInterface.getInterfaceName());
114                     if (neutronL2gatewayDeviceInterface.getSegmentationId() != null) {
115                         for (final Integer segmentationId : neutronL2gatewayDeviceInterface.getSegmentationId()) {
116                             segmentIds.add(segmentationId);
117                         }
118                         interfacesBuilder.setSegmentationIds(segmentIds);
119                     }
120                     interfaces.add(interfacesBuilder.build());
121                 }
122                 deviceBuilder.setDeviceName(neutronL2gatewayDevice.getDeviceName());
123                 deviceBuilder.setUuid(toUuid(neutronL2gatewayDevice.getID()));
124                 deviceBuilder.setInterfaces(interfaces);
125                 devices.add(deviceBuilder.build());
126             }
127             l2gatewayBuilder.setDevices(devices);
128         }
129         return l2gatewayBuilder.build();
130     }
131 }