b4fa09bdf0257fcd2749c55091928122420aec41
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronRouterInterface.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation 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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
21 import org.opendaylight.neutron.spi.Neutron_IPs;
22 import org.opendaylight.neutron.spi.NeutronRouter;
23 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
24 import org.opendaylight.neutron.spi.NeutronRouter_NetworkReference;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfoBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIpsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.ServiceRegistration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class NeutronRouterInterface extends AbstractNeutronInterface<Router, Routers, NeutronRouter> implements INeutronRouterCRUD {
42     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronRouterInterface.class);
43     // methods needed for creating caches
44
45
46     NeutronRouterInterface(ProviderContext providerContext) {
47         super(providerContext);
48     }
49
50
51     // IfNBRouterCRUD Interface methods
52
53     @Override
54     public boolean routerExists(String uuid) {
55         return exists(uuid);
56     }
57
58     @Override
59     public NeutronRouter getRouter(String uuid) {
60         return get(uuid);
61     }
62
63     @Override
64     protected List<Router> getDataObjectList(Routers routers) {
65         return routers.getRouter();
66     }
67
68     @Override
69     public List<NeutronRouter> getAllRouters() {
70         return getAll();
71     }
72
73     @Override
74     public boolean addRouter(NeutronRouter input) {
75         return add(input);
76     }
77
78     @Override
79     public boolean removeRouter(String uuid) {
80         return remove(uuid);
81     }
82
83     @Override
84     public boolean updateRouter(String uuid, NeutronRouter delta) {
85         return update(uuid, delta);
86     }
87
88     @Override
89     public boolean routerInUse(String routerUUID) {
90         if (!exists(routerUUID)) {
91             return true;
92         }
93         NeutronRouter target = getRouter(routerUUID);
94         return (target.getInterfaces().size() > 0);
95     }
96
97     @Override
98     protected Router toMd(NeutronRouter router) {
99
100         RouterBuilder routerBuilder = new RouterBuilder();
101
102         if (router.getID() != null) {
103             routerBuilder.setUuid(toUuid(router.getID()));
104         }
105         if (router.getName() != null) {
106             routerBuilder.setName(router.getName());
107         }
108         if (router.getTenantID() != null && !router.getTenantID().isEmpty()) {
109             routerBuilder.setTenantId(toUuid(router.getTenantID()));
110         }
111         if (router.getStatus() != null) {
112             routerBuilder.setStatus(router.getStatus());
113         }
114         if (router.getGatewayPortId() != null && !router.getGatewayPortId().isEmpty()) {
115             routerBuilder.setGatewayPortId(toUuid(router.getGatewayPortId()));
116         }
117         routerBuilder.setAdminStateUp(router.getAdminStateUp());
118         routerBuilder.setDistributed(router.getDistributed());
119         if (router.getRoutes() != null) {
120             List<String> routes = new ArrayList<String>();
121             for (String route : router.getRoutes()) {
122                 routes.add(route);
123             }
124             routerBuilder.setRoutes(routes);
125         }
126         if (router.getExternalGatewayInfo() != null) {
127             ExternalGatewayInfo externalGatewayInfo = null;
128             List<NeutronRouter_NetworkReference> neutronRouter_NetworkReferences = new ArrayList<NeutronRouter_NetworkReference>();
129             neutronRouter_NetworkReferences.add(router.getExternalGatewayInfo());
130             for (NeutronRouter_NetworkReference externalGatewayInfos : neutronRouter_NetworkReferences) {
131                 ExternalGatewayInfoBuilder builder = new ExternalGatewayInfoBuilder();
132                 builder.setEnableSnat(externalGatewayInfos.getEnableSNAT());
133                 builder.setExternalNetworkId(toUuid(externalGatewayInfos.getNetworkID()));
134                 if (externalGatewayInfos.getExternalFixedIPs() != null) {
135                     List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
136                     for (Neutron_IPs eIP : externalGatewayInfos.getExternalFixedIPs()) {
137                         ExternalFixedIpsBuilder eFixedIpBuilder = new ExternalFixedIpsBuilder();
138                         eFixedIpBuilder.setIpAddress(new IpAddress(eIP.getIpAddress().toCharArray()));
139                         eFixedIpBuilder.setSubnetId(toUuid(eIP.getSubnetUUID()));
140                         externalFixedIps.add(eFixedIpBuilder.build());
141                     }
142                     builder.setExternalFixedIps(externalFixedIps);
143                 }
144                 externalGatewayInfo = builder.build();
145             }
146             routerBuilder.setExternalGatewayInfo(externalGatewayInfo);
147         }
148         if (router.getInterfaces() != null) {
149             Map<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
150             List<Interfaces> interfaces = new ArrayList<Interfaces>();
151             for (Entry<String, NeutronRouter_Interface> entry : mapInterfaces.entrySet()) {
152                 interfaces.add((Interfaces) entry.getValue());
153             }
154             routerBuilder.setInterfaces(interfaces);
155         }
156         if (router.getID() != null) {
157             routerBuilder.setUuid(toUuid(router.getID()));
158         } else {
159             LOGGER.warn("Attempting to write neutron router without UUID");
160         }
161         return routerBuilder.build();
162     }
163
164     @Override
165     protected InstanceIdentifier<Router> createInstanceIdentifier(Router router) {
166         return InstanceIdentifier.create(Neutron.class)
167                  .child(Routers.class)
168                  .child(Router.class, router.getKey());
169     }
170
171     @Override
172     protected InstanceIdentifier<Routers> createInstanceIdentifier() {
173         return InstanceIdentifier.create(Neutron.class).child(Routers.class);
174     }
175
176     @Override
177     protected Router toMd(String uuid) {
178         RouterBuilder routerBuilder = new RouterBuilder();
179         routerBuilder.setUuid(toUuid(uuid));
180         return routerBuilder.build();
181     }
182
183     public static void registerNewInterface(BundleContext context,
184                                             ProviderContext providerContext,
185                                             List<ServiceRegistration<?>> registrations) {
186         NeutronRouterInterface neutronRouterInterface = new NeutronRouterInterface(providerContext);
187         ServiceRegistration<INeutronRouterCRUD> neutronRouterInterfaceRegistration = context.registerService(INeutronRouterCRUD.class, neutronRouterInterface, null);
188         if(neutronRouterInterfaceRegistration != null) {
189             registrations.add(neutronRouterInterfaceRegistration);
190         }
191     }
192
193     public NeutronRouter fromMd(Router router) {
194         NeutronRouter result = new NeutronRouter();
195         result.setID(router.getUuid().getValue());
196         result.setName(router.getName());
197         result.setTenantID(router.getTenantId());
198         result.setAdminStateUp(router.isAdminStateUp());
199         result.setStatus(router.getStatus());
200         result.setDistributed(router.isDistributed());
201         if (router.getGatewayPortId() != null) {
202             result.setGatewayPortId(String.valueOf(router.getGatewayPortId().getValue()));
203         }
204         if (router.getRoutes() != null) {
205             List<String> routes = new ArrayList<String>();
206             for (String route : router.getRoutes()) {
207                 routes.add(route);
208             }
209             result.setRoutes(routes);
210         }
211
212         if (router.getExternalGatewayInfo() != null) {
213             NeutronRouter_NetworkReference extGwInfo = new NeutronRouter_NetworkReference();
214             extGwInfo.setNetworkID(String.valueOf(router.getExternalGatewayInfo().getExternalNetworkId().getValue()));
215             extGwInfo.setEnableSNAT(router.getExternalGatewayInfo().isEnableSnat());
216             if (router.getExternalGatewayInfo().getExternalFixedIps() != null) {
217                 List<Neutron_IPs> fixedIPs = new ArrayList<Neutron_IPs>();
218                 for (ExternalFixedIps mdFixedIP : router.getExternalGatewayInfo().getExternalFixedIps()) {
219                      Neutron_IPs fixedIP = new Neutron_IPs();
220                      fixedIP.setSubnetUUID(String.valueOf(mdFixedIP.getSubnetId().getValue()));
221                      fixedIP.setIpAddress(String.valueOf(mdFixedIP.getIpAddress().getValue()));
222                      fixedIPs.add(fixedIP);
223                 }
224                 extGwInfo.setExternalFixedIPs(fixedIPs);
225             }
226             result.setExternalGatewayInfo(extGwInfo);
227         }
228
229         if (router.getInterfaces() != null) {
230             Map<String, NeutronRouter_Interface> interfaces = new HashMap<String, NeutronRouter_Interface>();
231             for (Interfaces mdInterface : router.getInterfaces()) {
232                 NeutronRouter_Interface pojoInterface = new NeutronRouter_Interface();
233                 String id = mdInterface.getUuid().getValue();
234                 pojoInterface.setID(id);
235                 pojoInterface.setTenantID(mdInterface.getTenantId());
236                 pojoInterface.setSubnetUUID(String.valueOf(mdInterface.getSubnetId().getValue()));
237                 pojoInterface.setPortUUID(String.valueOf(mdInterface.getPortId().getValue()));
238                 interfaces.put(id, pojoInterface);
239             }
240             result.setInterfaces(interfaces);
241         }
242         return result;
243     }
244 }