add blueprint wiring for neutron/transcriber
[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
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.neutron.spi.INeutronL2gatewayCRUD;
18 import org.opendaylight.neutron.spi.NeutronL2gatewayDevice;
19 import org.opendaylight.neutron.spi.NeutronL2gatewayDeviceInterface;
20 import org.opendaylight.neutron.spi.NeutronL2gateway;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.DevicesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.InterfacesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 public class NeutronL2gatewayInterface extends
32     AbstractNeutronInterface<L2gateway, L2gateways, NeutronL2gateway>
33     implements INeutronL2gatewayCRUD {
34     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronL2gatewayInterface.class);
35
36     NeutronL2gatewayInterface(DataBroker db) {
37         super(db);
38     }
39
40     @Override
41     protected List<L2gateway> getDataObjectList(L2gateways l2gateways) {
42         return l2gateways.getL2gateway();
43     }
44
45     @Override
46     public boolean inUse(String l2gatewayID) {
47         return !exists(l2gatewayID);
48     }
49
50     @Override
51     protected InstanceIdentifier<L2gateway>
52     createInstanceIdentifier(L2gateway l2gateway) {
53         return InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
54                 .child(L2gateway.class,l2gateway.getKey());
55     }
56
57     @Override
58     protected InstanceIdentifier<L2gateways> createInstanceIdentifier() {
59         return InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
60     }
61
62     @Override
63     protected NeutronL2gateway fromMd(L2gateway l2gateway){
64         final NeutronL2gateway result = new NeutronL2gateway();
65         final List<NeutronL2gatewayDevice> neutronL2gatewayDevices = new ArrayList<NeutronL2gatewayDevice>();
66
67         if (l2gateway.getUuid() != null){
68             result.setID(l2gateway.getUuid().getValue());
69         }
70         if (l2gateway.getUuid() != null){
71         result.setID(l2gateway.getUuid().getValue());
72         }
73         if (l2gateway.getL2gatewayName() != null){
74             result.setL2gatewayName(String.valueOf(l2gateway.getL2gatewayName()));
75         }
76         if (l2gateway.getTenantId() != null){
77             result.setTenantID(l2gateway.getTenantId());
78         }
79         if (l2gateway.getDevices() != null){
80             for (final Devices device:l2gateway.getDevices()){
81                 final NeutronL2gatewayDevice neutronL2gatewayDevice = new NeutronL2gatewayDevice();
82                 final List<NeutronL2gatewayDeviceInterface> neutronL2gatewayDeviceInterfaces =
83                         new ArrayList<NeutronL2gatewayDeviceInterface>();
84                 if (device.getDeviceName() != null){
85                     neutronL2gatewayDevice.setDeviceName(device.getDeviceName().toString());
86                 }
87                 if (device.getUuid() != null) {
88                     neutronL2gatewayDevice.setID(device.getUuid().getValue());
89                 }
90                 if(device.getInterfaces() != null){
91                     for(final Interfaces deviceInterface:device.getInterfaces()){
92                         final NeutronL2gatewayDeviceInterface neutronL2gatewayDeviceInterface =
93                                 new NeutronL2gatewayDeviceInterface();
94                         String interfaceName = null;
95                         final List<Integer> segmentationIds = new ArrayList<Integer>();
96                         if (deviceInterface.getInterfaceName() != null){
97                             interfaceName = deviceInterface
98                                     .getInterfaceName().toString();
99                         }
100                         if (deviceInterface.getSegmentationIds() != null){
101                             for(final Integer segmentId:deviceInterface.getSegmentationIds()){
102                                 segmentationIds.add(segmentId);
103                             }
104                             neutronL2gatewayDeviceInterface.setSegmentationId(segmentationIds);
105                         }
106                         neutronL2gatewayDeviceInterface.setInterfaceName(interfaceName);
107                         neutronL2gatewayDeviceInterfaces.add(neutronL2gatewayDeviceInterface);
108                     }
109                 }
110                 neutronL2gatewayDevice.setNeutronL2gatewayDeviceInterfaces(neutronL2gatewayDeviceInterfaces);
111                 neutronL2gatewayDevices.add(neutronL2gatewayDevice);
112             }
113         }
114         result.setNeutronL2gatewayDevices(neutronL2gatewayDevices);
115         return result;
116     }
117
118     @Override
119     protected L2gateway toMd(NeutronL2gateway neutronObject) {
120         final L2gatewayBuilder l2gatewayBuilder = new L2gatewayBuilder();
121         if (neutronObject.getL2gatewayName() != null){
122             l2gatewayBuilder.setL2gatewayName(neutronObject.getL2gatewayName());
123         }
124         if (neutronObject.getID() != null){
125             l2gatewayBuilder.setUuid(toUuid(neutronObject.getID()));
126         }
127         if (neutronObject.getTenantID() != null){
128             l2gatewayBuilder.setTenantId(toUuid(neutronObject.getTenantID()));
129         }
130
131         if (neutronObject.getNeutronL2gatewayDevices() != null){
132             final List<Devices> devices = new ArrayList<>();
133             for(final NeutronL2gatewayDevice neutronL2gatewayDevice:neutronObject.getNeutronL2gatewayDevices()){
134                 final DevicesBuilder deviceBuilder = new DevicesBuilder();
135                 final List<Interfaces> interfaces = new ArrayList<Interfaces>();
136                 for(final NeutronL2gatewayDeviceInterface neutronL2gatewayDeviceInterface:neutronL2gatewayDevice
137                         .getNeutronL2gatewayDeviceInterfaces()){
138                     final InterfacesBuilder interfacesBuilder = new InterfacesBuilder();
139                     final List<Integer> segmentIds = new ArrayList<Integer>();
140                     interfacesBuilder.setInterfaceName(neutronL2gatewayDeviceInterface.getInterfaceName());
141                     if (neutronL2gatewayDeviceInterface.getSegmentationId() != null){
142                         for(final Integer segmentationId:neutronL2gatewayDeviceInterface
143                                 .getSegmentationId()){
144                             segmentIds.add(segmentationId);
145                         }
146                         interfacesBuilder.setSegmentationIds(segmentIds);
147                     }
148                     interfaces.add(interfacesBuilder.build());
149                 }
150                 deviceBuilder.setDeviceName(neutronL2gatewayDevice.getDeviceName());
151                 deviceBuilder.setUuid(toUuid(neutronL2gatewayDevice.getID()));
152                 deviceBuilder.setInterfaces(interfaces);
153                 devices.add(deviceBuilder.build());
154             }
155             l2gatewayBuilder.setDevices(devices);
156         }
157         return l2gatewayBuilder.build();
158     }
159
160     @Override
161     protected L2gateway toMd(String uuid) {
162         final L2gatewayBuilder l2gatewayBuilder = new L2gatewayBuilder();
163         l2gatewayBuilder.setUuid(toUuid(uuid));
164         return l2gatewayBuilder.build();
165     }
166 }