Fix FindBugs violations
[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         fromMdBaseAttributes(l2gateway, result);
43         final List<NeutronL2gatewayDevice> neutronL2gatewayDevices = new ArrayList<>();
44
45         if (l2gateway.getDevices() != null) {
46             for (final Devices device : l2gateway.getDevices()) {
47                 final NeutronL2gatewayDevice neutronL2gatewayDevice = new NeutronL2gatewayDevice();
48                 final List<NeutronL2gatewayDeviceInterface> neutronL2gatewayDeviceInterfaces = new ArrayList<>();
49                 if (device.getDeviceName() != null) {
50                     neutronL2gatewayDevice.setDeviceName(device.getDeviceName());
51                 }
52                 if (device.getUuid() != null) {
53                     neutronL2gatewayDevice.setID(device.getUuid().getValue());
54                 }
55                 if (device.getInterfaces() != null) {
56                     for (final Interfaces deviceInterface : device.getInterfaces()) {
57                         final NeutronL2gatewayDeviceInterface neutronL2gatewayDeviceInterface =
58                                 new NeutronL2gatewayDeviceInterface();
59                         String interfaceName = null;
60                         final List<Integer> segmentationIds = new ArrayList<>();
61                         if (deviceInterface.getInterfaceName() != null) {
62                             interfaceName = deviceInterface.getInterfaceName();
63                         }
64                         if (deviceInterface.getSegmentationIds() != null) {
65                             for (final Integer segmentId : deviceInterface.getSegmentationIds()) {
66                                 segmentationIds.add(segmentId);
67                             }
68                             neutronL2gatewayDeviceInterface.setSegmentationId(segmentationIds);
69                         }
70                         neutronL2gatewayDeviceInterface.setInterfaceName(interfaceName);
71                         neutronL2gatewayDeviceInterfaces.add(neutronL2gatewayDeviceInterface);
72                     }
73                 }
74                 neutronL2gatewayDevice.setNeutronL2gatewayDeviceInterfaces(neutronL2gatewayDeviceInterfaces);
75                 neutronL2gatewayDevices.add(neutronL2gatewayDevice);
76             }
77         }
78         result.setNeutronL2gatewayDevices(neutronL2gatewayDevices);
79         return result;
80     }
81
82     @Override
83     protected L2gateway toMd(NeutronL2gateway neutronObject) {
84         final L2gatewayBuilder l2gatewayBuilder = new L2gatewayBuilder();
85         toMdBaseAttributes(neutronObject, l2gatewayBuilder);
86
87         if (neutronObject.getNeutronL2gatewayDevices() != null) {
88             final List<Devices> devices = new ArrayList<>();
89             for (final NeutronL2gatewayDevice neutronL2gatewayDevice : neutronObject.getNeutronL2gatewayDevices()) {
90                 final DevicesBuilder deviceBuilder = new DevicesBuilder();
91                 final List<Interfaces> interfaces = new ArrayList<>();
92                 for (final NeutronL2gatewayDeviceInterface neutronL2gatewayDeviceInterface : neutronL2gatewayDevice
93                         .getNeutronL2gatewayDeviceInterfaces()) {
94                     final InterfacesBuilder interfacesBuilder = new InterfacesBuilder();
95                     final List<Integer> segmentIds = new ArrayList<>();
96                     interfacesBuilder.setInterfaceName(neutronL2gatewayDeviceInterface.getInterfaceName());
97                     if (neutronL2gatewayDeviceInterface.getSegmentationId() != null) {
98                         for (final Integer segmentationId : neutronL2gatewayDeviceInterface.getSegmentationId()) {
99                             segmentIds.add(segmentationId);
100                         }
101                         interfacesBuilder.setSegmentationIds(segmentIds);
102                     }
103                     interfaces.add(interfacesBuilder.build());
104                 }
105                 deviceBuilder.setDeviceName(neutronL2gatewayDevice.getDeviceName());
106                 deviceBuilder.setUuid(toUuid(neutronL2gatewayDevice.getID()));
107                 deviceBuilder.setInterfaces(interfaces);
108                 devices.add(deviceBuilder.build());
109             }
110             l2gatewayBuilder.setDevices(devices);
111         }
112         return l2gatewayBuilder.build();
113     }
114 }