Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 59AFF200C07 for ; Sat, 17 Dec 2016 12:53:44 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 5834A160B28; Sat, 17 Dec 2016 11:53:44 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4EF84160B38 for ; Sat, 17 Dec 2016 12:53:42 +0100 (CET) Received: (qmail 26428 invoked by uid 500); 17 Dec 2016 11:53:41 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 26184 invoked by uid 99); 17 Dec 2016 11:53:41 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 17 Dec 2016 11:53:41 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 12D69DFADD; Sat, 17 Dec 2016 11:53:41 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: acosentino@apache.org To: commits@camel.apache.org Date: Sat, 17 Dec 2016 11:53:43 -0000 Message-Id: <31b5bd2b58a44480939660bb18db1740@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [04/17] camel git commit: CAMEL-9748: camel-openstack neutron archived-at: Sat, 17 Dec 2016 11:53:44 -0000 CAMEL-9748: camel-openstack neutron Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2847e099 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2847e099 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2847e099 Branch: refs/heads/master Commit: 2847e0998bd9e84a796c193405f2acc329ea9921 Parents: 8780c09 Author: Jan Bouska Authored: Fri Nov 18 19:22:50 2016 +0100 Committer: Andrea Cosentino Committed: Sat Dec 17 12:25:13 2016 +0100 ---------------------------------------------------------------------- .../openstack/neutron/NeutronComponent.java | 33 +++ .../openstack/neutron/NeutronConstants.java | 56 +++++ .../openstack/neutron/NeutronEndpoint.java | 161 ++++++++++++++ .../neutron/producer/NetworkProducer.java | 124 +++++++++++ .../neutron/producer/PortProducer.java | 125 +++++++++++ .../neutron/producer/RouterProducer.java | 147 ++++++++++++ .../neutron/producer/SubnetProducer.java | 113 ++++++++++ .../apache/camel/component/openstack-neutron | 1 + .../openstack/neutron/NetworkProducerTest.java | 148 +++++++++++++ .../neutron/NeutronProducerTestSupport.java | 60 +++++ .../openstack/neutron/PortProducerTest.java | 174 +++++++++++++++ .../openstack/neutron/RouterProducerTest.java | 221 +++++++++++++++++++ .../openstack/neutron/SubnetProducerTest.java | 143 ++++++++++++ 13 files changed, 1506 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronComponent.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronComponent.java new file mode 100644 index 0000000..e96865f --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronComponent.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import org.apache.camel.Endpoint; +import org.apache.camel.impl.DefaultComponent; + +import java.util.Map; + +public class NeutronComponent extends DefaultComponent { + + @Override protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { + NeutronEndpoint endpoint = new NeutronEndpoint(uri, this); + setProperties(endpoint, parameters); + endpoint.setHost(remaining); + return endpoint; + } +} + http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronConstants.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronConstants.java new file mode 100644 index 0000000..93360ce --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronConstants.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import org.apache.camel.component.openstack.common.OpenstackConstants; + +public final class NeutronConstants extends OpenstackConstants{ + + public static final String NEUTRON_NETWORK_SUBSYSTEM = "networks"; + public static final String NEUTRON_SUBNETS_SYSTEM = "subnets"; + public static final String NEUTRON_PORT_SYSTEM = "ports"; + public static final String NEUTRON_ROUTER_SYSTEM = "routers"; + public static final String TENANT_ID = "tenantId"; + public static final String NETWORK_ID = "networkId"; + + //network + public static final String ADMIN_STATE_UP = "adminStateUp"; + public static final String NETWORK_TYPE = "networkType"; + public static final String PHYSICAL_NETWORK = "physicalNetwork"; + public static final String SEGMENT_ID = "segmentId"; + public static final String IS_SHARED = "isShared"; + public static final String IS_ROUTER_EXTERNAL = "isRouterExternal"; + + //subnet + public static final String ENABLE_DHCP = "enableDHCP"; + public static final String GATEWAY = "gateway"; + + //port + public static final String DEVICE_ID = "deviceId"; + public static final String MAC_ADDRESS = "macAddress"; + + //router + public static final String ROUTER_ID = "routerId"; + public static final String SUBNET_ID = "subnetId"; + public static final String PORT_ID = "portId"; + public static final String ITERFACE_TYPE = "interfaceType"; + + public static final String ATTACH_INTERFACE = "attachInterface"; + public static final String DETACH_INTERFACE = "detachInterface"; + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronEndpoint.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronEndpoint.java new file mode 100644 index 0000000..44d5b84 --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/NeutronEndpoint.java @@ -0,0 +1,161 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import org.apache.camel.Producer; +import org.apache.camel.component.openstack.common.AbstractOpenstackEndpoint; +import org.apache.camel.component.openstack.neutron.producer.NetworkProducer; +import org.apache.camel.component.openstack.neutron.producer.PortProducer; +import org.apache.camel.component.openstack.neutron.producer.RouterProducer; +import org.apache.camel.component.openstack.neutron.producer.SubnetProducer; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; + +@UriEndpoint(scheme = "openstack-neutron", title = "OpenStack-Neutron", syntax = "openstack-neutron:host", label = "cloud") +public class NeutronEndpoint extends AbstractOpenstackEndpoint { + + @UriPath + @Metadata(required = "true") + private String host; + + @UriParam(enums = "networks, subnets, ports, routers") + @Metadata(required = "true") + String subsystem; + + @UriParam(defaultValue = "default") + private String domain = "default"; + + @UriParam + @Metadata(required = "true") + private String project; + + @UriParam + private String operation; + + @UriParam + @Metadata(required = "true") + private String username; + + @UriParam + @Metadata(required = "true") + private String password; + + public NeutronEndpoint(String uri, NeutronComponent component) { + super(uri, component); + } + + @Override + public Producer createProducer() throws Exception { + switch (getSubsystem()) { + case NeutronConstants.NEUTRON_NETWORK_SUBSYSTEM: + return new NetworkProducer(this, createClient()); + case NeutronConstants.NEUTRON_SUBNETS_SYSTEM: + return new SubnetProducer(this, createClient()); + case NeutronConstants.NEUTRON_PORT_SYSTEM: + return new PortProducer(this, createClient()); + case NeutronConstants.NEUTRON_ROUTER_SYSTEM: + return new RouterProducer(this, createClient()); + default: + throw new IllegalArgumentException("Can't create producer with subsystem " + subsystem); + } + } + + public String getSubsystem() { + return subsystem; + } + + /** + * OpenStack Nova subsystem + */ + public void setSubsystem(String subsystem) { + this.subsystem = subsystem; + } + + @Override + public String getDomain() { + return domain; + } + + /** + * Authentication domain + */ + public void setDomain(String domain) { + this.domain = domain; + } + + @Override + public String getProject() { + return project; + } + + /** + * The project ID + */ + public void setProject(String project) { + this.project = project; + } + + @Override + public String getOperation() { + return operation; + } + + /** + * The operation to do + */ + public void setOperation(String operation) { + this.operation = operation; + } + + @Override + public String getUsername() { + return username; + } + + /** + * OpenStack username + */ + public void setUsername(String username) { + this.username = username; + } + + @Override + public String getPassword() { + return password; + } + + /** + * OpenStack password + */ + public void setPassword(String password) { + this.password = password; + } + + @Override + public String getHost() { + return host; + } + + /** + * OpenStack host url + */ + public void setHost(String host) { + this.host = host; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/NetworkProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/NetworkProducer.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/NetworkProducer.java new file mode 100644 index 0000000..6e58c7f --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/NetworkProducer.java @@ -0,0 +1,124 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.openstack.common.AbstractOpenstackProducer; +import org.apache.camel.component.openstack.neutron.NeutronConstants; +import org.apache.camel.component.openstack.neutron.NeutronEndpoint; +import org.apache.camel.util.ObjectHelper; + +import org.openstack4j.api.Builders; +import org.openstack4j.api.OSClient; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.Network; +import org.openstack4j.model.network.NetworkType; +import org.openstack4j.model.network.builder.NetworkBuilder; + +import java.util.List; +import java.util.Map; + +public class NetworkProducer extends AbstractOpenstackProducer { + + public NetworkProducer(NeutronEndpoint endpoint, OSClient client) { + super(endpoint, client); + } + + @Override public void process(Exchange exchange) throws Exception { + final String operation = getOperation(exchange); + switch (operation) { + case NeutronConstants.CREATE: + doCreate(exchange); + break; + case NeutronConstants.GET: + doGet(exchange); + break; + case NeutronConstants.GET_ALL: + doGetAll(exchange); + break; + case NeutronConstants.DELETE: + doDelete(exchange); + break; + default: + throw new IllegalArgumentException("Unsupported operation " + operation); + } + } + + private void doCreate(Exchange exchange) { + final Network in = messageToNetwork(exchange.getIn()); + final Network out = os.networking().network().create(in); + exchange.getIn().setBody(out); + } + + private void doGet(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.NETWORK_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Network ID"); + final Network out = os.networking().network().get(id); + exchange.getIn().setBody(out); + } + + private void doGetAll(Exchange exchange) { + final List out = os.networking().network().list(); + exchange.getIn().setBody(out); + } + + private void doDelete(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.NETWORK_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Network ID"); + final ActionResponse response = os.networking().network().delete(id); + checkFailure(response, msg, "Delete network" + id); + } + + private Network messageToNetwork(Message message) { + Network network = message.getBody(Network.class); + if(network == null) { + Map headers = message.getHeaders(); + NetworkBuilder builder = Builders.network(); + + ObjectHelper.notEmpty(message.getHeader(NeutronConstants.NAME, String.class), "Name"); + builder.name(message.getHeader(NeutronConstants.NAME, String.class)); + + if(headers.containsKey(NeutronConstants.ADMIN_STATE_UP)) + builder.adminStateUp(message.getHeader(NeutronConstants.ADMIN_STATE_UP, Boolean.class)); + + if(headers.containsKey(NeutronConstants.NETWORK_TYPE)) + builder.networkType(message.getHeader(NeutronConstants.NETWORK_TYPE, NetworkType.class)); + + if(headers.containsKey(NeutronConstants.IS_SHARED)) + builder.isShared(message.getHeader(NeutronConstants.IS_SHARED, Boolean.class)); + + if(headers.containsKey(NeutronConstants.IS_ROUTER_EXTERNAL)) + builder.isRouterExternal(message.getHeader(NeutronConstants.IS_ROUTER_EXTERNAL, Boolean.class)); + + if(headers.containsKey(NeutronConstants.TENANT_ID)) + builder.tenantId(message.getHeader(NeutronConstants.TENANT_ID, String.class)); + + if(headers.containsKey(NeutronConstants.PHYSICAL_NETWORK)) + builder.physicalNetwork(message.getHeader(NeutronConstants.PHYSICAL_NETWORK, String.class)); + + if(headers.containsKey(NeutronConstants.SEGMENT_ID)) + builder.segmentId(message.getHeader(NeutronConstants.SEGMENT_ID, String.class)); + + network = builder.build(); + } + + return network; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/PortProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/PortProducer.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/PortProducer.java new file mode 100644 index 0000000..f7ed65d --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/PortProducer.java @@ -0,0 +1,125 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.openstack.common.AbstractOpenstackProducer; +import org.apache.camel.component.openstack.neutron.NeutronConstants; +import org.apache.camel.component.openstack.neutron.NeutronEndpoint; +import org.apache.camel.util.ObjectHelper; + +import org.openstack4j.api.Builders; +import org.openstack4j.api.OSClient; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.Port; +import org.openstack4j.model.network.builder.PortBuilder; + +import java.util.List; +import java.util.Map; + +public class PortProducer extends AbstractOpenstackProducer { + + public PortProducer(NeutronEndpoint endpoint, OSClient client) { + super(endpoint, client); + } + + @Override + public void process(Exchange exchange) throws Exception { + final String operation = getOperation(exchange); + switch (operation) { + case NeutronConstants.CREATE: + doCreate(exchange); + break; + case NeutronConstants.GET: + doGet(exchange); + break; + case NeutronConstants.GET_ALL: + doGetAll(exchange); + break; + case NeutronConstants.UPDATE: + doUpdate(exchange); + break; + case NeutronConstants.DELETE: + doDelete(exchange); + break; + default: + throw new IllegalArgumentException("Unsupported operation " + operation); + } + } + + private void doCreate(Exchange exchange) { + final Port in = messageToPort(exchange.getIn()); + final Port out = os.networking().port().create(in); + exchange.getIn().setBody(out); + } + + private void doGet(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.PORT_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Port ID"); + final Port result = os.networking().port().get(id); + msg.setBody(result); + } + + private void doGetAll(Exchange exchange) { + final List out = os.networking().port().list(); + exchange.getIn().setBody(out); + } + + private void doUpdate(Exchange exchange) { + final Message msg = exchange.getIn(); + final Port port = messageToPort(msg); + final Port updatedPort = os.networking().port().update(port); + msg.setBody(updatedPort); + } + + private void doDelete(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.PORT_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Port ID"); + final ActionResponse response = os.networking().port().delete(id); + checkFailure(response, msg, "Delete port with ID " + id); + } + + private Port messageToPort(Message message) { + Port port = message.getBody(Port.class); + + if (port == null) { + Map headers = message.getHeaders(); + PortBuilder builder = Builders.port(); + + ObjectHelper.notEmpty(message.getHeader(NeutronConstants.NAME, String.class), "Name"); + builder.name(message.getHeader(NeutronConstants.NAME, String.class)); + + if (headers.containsKey(NeutronConstants.TENANT_ID)) + builder.tenantId(message.getHeader(NeutronConstants.TENANT_ID, String.class)); + + if (headers.containsKey(NeutronConstants.NETWORK_ID)) + builder.networkId(message.getHeader(NeutronConstants.NETWORK_ID, String.class)); + + if (headers.containsKey(NeutronConstants.DEVICE_ID)) + builder.deviceId(message.getHeader(NeutronConstants.DEVICE_ID, String.class)); + + if (headers.containsKey(NeutronConstants.MAC_ADDRESS)) + builder.macAddress(message.getHeader(NeutronConstants.MAC_ADDRESS, String.class)); + + port = builder.build(); + } + return port; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/RouterProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/RouterProducer.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/RouterProducer.java new file mode 100644 index 0000000..28cf121 --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/RouterProducer.java @@ -0,0 +1,147 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.openstack.common.AbstractOpenstackProducer; +import org.apache.camel.component.openstack.neutron.NeutronConstants; +import org.apache.camel.component.openstack.neutron.NeutronEndpoint; +import org.apache.camel.util.ObjectHelper; + +import org.openstack4j.api.Builders; +import org.openstack4j.api.OSClient; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.AttachInterfaceType; +import org.openstack4j.model.network.Router; +import org.openstack4j.model.network.RouterInterface; +import org.openstack4j.model.network.builder.RouterBuilder; + +import java.util.List; +import java.util.Map; + +public class RouterProducer extends AbstractOpenstackProducer { + + public RouterProducer(NeutronEndpoint endpoint, OSClient client) { + super(endpoint, client); + } + + @Override + public void process(Exchange exchange) throws Exception { + final String operation = getOperation(exchange); + switch (operation) { + case NeutronConstants.CREATE: + doCreate(exchange); + break; + case NeutronConstants.GET: + doGet(exchange); + break; + case NeutronConstants.GET_ALL: + doGetAll(exchange); + break; + case NeutronConstants.UPDATE: + doUpdate(exchange); + break; + case NeutronConstants.DELETE: + doDelete(exchange); + break; + case NeutronConstants.ATTACH_INTERFACE: + doAttach(exchange); + break; + case NeutronConstants.DETACH_INTERFACE: + doDetach(exchange); + break; + default: + throw new IllegalArgumentException("Unsuproutered operation " + operation); + } + } + + private void doCreate(Exchange exchange) { + final Router in = messageToRouter(exchange.getIn()); + final Router out = os.networking().router().create(in); + exchange.getIn().setBody(out); + } + + private void doGet(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.ROUTER_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Router ID"); + final Router result = os.networking().router().get(id); + msg.setBody(result); + } + + private void doGetAll(Exchange exchange) { + final List out = os.networking().router().list(); + exchange.getIn().setBody(out); + } + + private void doUpdate(Exchange exchange) { + final Message msg = exchange.getIn(); + final Router router = messageToRouter(msg); + final Router updatedRouter = os.networking().router().update(router); + msg.setBody(updatedRouter); + } + + private void doDelete(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.ROUTER_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Router ID"); + final ActionResponse response = os.networking().router().delete(id); + checkFailure(response, msg, "Delete router with ID " + id); + } + + private void doDetach(Exchange exchange) { + final Message msg = exchange.getIn(); + final String routerId = msg.getHeader(NeutronConstants.ROUTER_ID, String.class); + final String subnetId = msg.getHeader(NeutronConstants.SUBNET_ID, String.class); + final String portId = msg.getHeader(NeutronConstants.PORT_ID, String.class); + ObjectHelper.notEmpty(routerId, "Router ID"); + RouterInterface iface = os.networking().router().detachInterface(routerId, subnetId, portId); + msg.setBody(iface); + } + + private void doAttach(Exchange exchange) { + final Message msg = exchange.getIn(); + final String routerId = msg.getHeader(NeutronConstants.ROUTER_ID, String.class); + final String subnetPortId = msg.getHeader(NeutronConstants.SUBNET_ID, msg.getHeader(NeutronConstants.PORT_ID), String.class); + final AttachInterfaceType type = msg.getHeader(NeutronConstants.ITERFACE_TYPE, AttachInterfaceType.class); + ObjectHelper.notEmpty(routerId, "Router ID"); + ObjectHelper.notEmpty(subnetPortId, "Subnet/Port ID"); + ObjectHelper.notNull(type, "AttachInterfaceType "); + RouterInterface routerInterface = os.networking().router().attachInterface(routerId, type, subnetPortId); + msg.setBody(routerInterface); + } + + //TODO: more headers? + private Router messageToRouter(Message message) { + Router router = message.getBody(Router.class); + + if (router == null) { + Map headers = message.getHeaders(); + RouterBuilder builder = Builders.router(); + + ObjectHelper.notEmpty(message.getHeader(NeutronConstants.NAME, String.class), "Name"); + builder.name(message.getHeader(NeutronConstants.NAME, String.class)); + + if (headers.containsKey(NeutronConstants.TENANT_ID)) + builder.tenantId(message.getHeader(NeutronConstants.TENANT_ID, String.class)); + + router = builder.build(); + } + return router; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/SubnetProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/SubnetProducer.java b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/SubnetProducer.java new file mode 100644 index 0000000..77bdc5c --- /dev/null +++ b/components/camel-openstack/src/main/java/org/apache/camel/component/openstack/neutron/producer/SubnetProducer.java @@ -0,0 +1,113 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.openstack.common.AbstractOpenstackProducer; +import org.apache.camel.component.openstack.neutron.NeutronConstants; +import org.apache.camel.component.openstack.neutron.NeutronEndpoint; +import org.apache.camel.util.ObjectHelper; + +import org.openstack4j.api.Builders; +import org.openstack4j.api.OSClient; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.Subnet; +import org.openstack4j.model.network.builder.SubnetBuilder; + +import java.util.List; +import java.util.Map; + +public class SubnetProducer extends AbstractOpenstackProducer { + + public SubnetProducer(NeutronEndpoint endpoint, OSClient client) { + super(endpoint, client); + } + + @Override public void process(Exchange exchange) throws Exception { + final String operation = getOperation(exchange); + switch (operation) { + case NeutronConstants.CREATE: + doCreate(exchange); + break; + case NeutronConstants.GET: + doGet(exchange); + break; + case NeutronConstants.GET_ALL: + doGetAll(exchange); + break; + case NeutronConstants.DELETE: + doDelete(exchange); + break; + default: + throw new IllegalArgumentException("Unsupported operation " + operation); + } + } + + private void doCreate(Exchange exchange) { + final Subnet in = messageToSubnet(exchange.getIn()); + final Subnet out = os.networking().subnet().create(in); + exchange.getIn().setBody(out); + } + + private void doGet(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.SUBNET_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Subnet ID"); + final Subnet out = os.networking().subnet().get(id); + exchange.getIn().setBody(out); + } + + private void doGetAll(Exchange exchange) { + final List out = os.networking().subnet().list(); + exchange.getIn().setBody(out); + } + + private void doDelete(Exchange exchange) { + final Message msg = exchange.getIn(); + final String id = msg.getHeader(NeutronConstants.ID, msg.getHeader(NeutronConstants.SUBNET_ID, String.class), String.class); + ObjectHelper.notEmpty(id, "Subnet ID"); + final ActionResponse response = os.networking().subnet().delete(id); + checkFailure(response, msg, "Delete network " + id); + } + + + //TODO: more headers + private Subnet messageToSubnet(Message message) { + Subnet subnet = message.getBody(Subnet.class); + if(subnet == null) { + Map headers = message.getHeaders(); + SubnetBuilder builder = Builders.subnet(); + + ObjectHelper.notEmpty(message.getHeader(NeutronConstants.NAME, String.class), "Name"); + builder.name(message.getHeader(NeutronConstants.NAME, String.class)); + + if(headers.containsKey(NeutronConstants.NETWORK_ID)) + builder.networkId(message.getHeader(NeutronConstants.NETWORK_ID, String.class)); + + if(headers.containsKey(NeutronConstants.ENABLE_DHCP)) + builder.enableDHCP(message.getHeader(NeutronConstants.ENABLE_DHCP, Boolean.class)); + + if(headers.containsKey(NeutronConstants.GATEWAY)) + builder.gateway(message.getHeader(NeutronConstants.GATEWAY, String.class)); + + subnet = builder.build(); + } + + return subnet; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/main/resources/META-INF/services/org/apache/camel/component/openstack-neutron ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/main/resources/META-INF/services/org/apache/camel/component/openstack-neutron b/components/camel-openstack/src/main/resources/META-INF/services/org/apache/camel/component/openstack-neutron new file mode 100644 index 0000000..b232275 --- /dev/null +++ b/components/camel-openstack/src/main/resources/META-INF/services/org/apache/camel/component/openstack-neutron @@ -0,0 +1 @@ +class=org.apache.camel.component.openstack.neutron.NeutronComponent http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NetworkProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NetworkProducerTest.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NetworkProducerTest.java new file mode 100644 index 0000000..888326a --- /dev/null +++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NetworkProducerTest.java @@ -0,0 +1,148 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.camel.component.openstack.neutron.producer.NetworkProducer; + +import org.junit.Before; +import org.junit.Test; + +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.openstack4j.api.Builders; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.compute.Flavor; +import org.openstack4j.model.network.Network; +import org.openstack4j.model.network.NetworkType; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class NetworkProducerTest extends NeutronProducerTestSupport { + + private Network dummyNetwork; + + @Mock + private Network testOSnetwork; + + @Before + public void setUp() { + producer = new NetworkProducer(endpoint, client); + when(networkService.create(any(Network.class))).thenReturn(testOSnetwork); + when(networkService.get(anyString())).thenReturn(testOSnetwork); + + List getAllList = new ArrayList<>(); + getAllList.add(testOSnetwork); + getAllList.add(testOSnetwork); + doReturn(getAllList).when(networkService).list(); + + dummyNetwork = createNetwork(); + when(testOSnetwork.getName()).thenReturn(dummyNetwork.getName()); + when(testOSnetwork.getTenantId()).thenReturn(dummyNetwork.getTenantId()); + when(testOSnetwork.getNetworkType()).thenReturn(dummyNetwork.getNetworkType()); + when(testOSnetwork.getId()).thenReturn(UUID.randomUUID().toString()); + } + + @Test + public void createTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.CREATE); + msg.setHeader(NeutronConstants.NAME, dummyNetwork.getName()); + msg.setHeader(NeutronConstants.NETWORK_TYPE, dummyNetwork.getNetworkType()); + msg.setHeader(NeutronConstants.TENANT_ID, dummyNetwork.getTenantId()); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Network.class); + verify(networkService).create(captor.capture()); + + assertEqualsNetwork(dummyNetwork, captor.getValue()); + assertNotNull(msg.getBody(Network.class).getId()); + } + + @Test + public void getTest() throws Exception { + final String networkID = "myNetID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET); + msg.setHeader(NeutronConstants.NETWORK_ID, networkID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(networkService).get(captor.capture()); + + assertEquals(networkID, captor.getValue()); + assertEqualsNetwork(testOSnetwork, msg.getBody(Network.class)); + } + + @Test + public void getAllTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET_ALL); + + producer.process(exchange); + + final List result = msg.getBody(List.class); + assertTrue(result.size() == 2); + assertEquals(testOSnetwork, result.get(0)); + } + + @Test + public void deleteTest() throws Exception { + when(networkService.delete(anyString())).thenReturn(ActionResponse.actionSuccess()); + final String networkID = "myNetID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.DELETE); + msg.setHeader(NeutronConstants.ID, networkID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(networkService).delete(captor.capture()); + assertEquals(networkID, captor.getValue()); + assertFalse(msg.isFault()); + + //in case of failure + final String failureMessage = "fail"; + when(networkService.delete(anyString())).thenReturn(ActionResponse.actionFailed(failureMessage, 404)); + producer.process(exchange); + assertTrue(msg.isFault()); + assertTrue(msg.getBody(String.class).contains(failureMessage)); + } + + private Network createNetwork() { + return Builders.network() + .name("name") + .tenantId("tenantID") + .networkType(NetworkType.LOCAL).build(); + } + + private void assertEqualsNetwork(Network old, Network newNetwork) { + assertEquals(old.getName(), newNetwork.getName()); + assertEquals(old.getTenantId(), newNetwork.getTenantId()); + assertEquals(old.getNetworkType(), newNetwork.getNetworkType()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NeutronProducerTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NeutronProducerTestSupport.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NeutronProducerTestSupport.java new file mode 100644 index 0000000..e5f5e63 --- /dev/null +++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/NeutronProducerTestSupport.java @@ -0,0 +1,60 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import static org.mockito.Mockito.when; + +import org.apache.camel.component.openstack.AbstractProducerTestSupport; + +import org.junit.Before; + +import org.mockito.Mock; +import org.openstack4j.api.networking.NetworkService; +import org.openstack4j.api.networking.NetworkingService; +import org.openstack4j.api.networking.PortService; +import org.openstack4j.api.networking.RouterService; +import org.openstack4j.api.networking.SubnetService; + +public class NeutronProducerTestSupport extends AbstractProducerTestSupport { + + @Mock + protected NeutronEndpoint endpoint; + + @Mock + protected NetworkingService networkingService; + + @Mock + PortService portService; + + @Mock + RouterService routerService; + + @Mock + SubnetService subnetService; + + @Mock + NetworkService networkService; + + @Before + public void setUpComputeService(){ + when(client.networking()).thenReturn(networkingService); + when(networkingService.port()).thenReturn(portService); + when(networkingService.router()).thenReturn(routerService); + when(networkingService.subnet()).thenReturn(subnetService); + when(networkingService.network()).thenReturn(networkService); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/PortProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/PortProducerTest.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/PortProducerTest.java new file mode 100644 index 0000000..6e46a5b --- /dev/null +++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/PortProducerTest.java @@ -0,0 +1,174 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.camel.component.openstack.neutron.producer.PortProducer; + +import org.junit.Before; +import org.junit.Test; + +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.openstack4j.api.Builders; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.Port; +import org.openstack4j.model.network.Router; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class PortProducerTest extends NeutronProducerTestSupport { + + private Port dummyPort; + + @Mock + private Port testOSport; + + @Before + public void setUp() { + producer = new PortProducer(endpoint, client); + when(portService.create(any(Port.class))).thenReturn(testOSport); + when(portService.get(anyString())).thenReturn(testOSport); + + List getAllList = new ArrayList<>(); + getAllList.add(testOSport); + getAllList.add(testOSport); + doReturn(getAllList).when(portService).list(); + + dummyPort = createPort(); + when(testOSport.getName()).thenReturn(dummyPort.getName()); + when(testOSport.getNetworkId()).thenReturn(dummyPort.getNetworkId()); + when(testOSport.getMacAddress()).thenReturn(dummyPort.getMacAddress()); + when(testOSport.getDeviceId()).thenReturn(dummyPort.getDeviceId()); + when(testOSport.getId()).thenReturn(UUID.randomUUID().toString()); + } + + @Test + public void createTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.CREATE); + msg.setHeader(NeutronConstants.NAME, dummyPort.getName()); + msg.setHeader(NeutronConstants.TENANT_ID, dummyPort.getTenantId()); + msg.setHeader(NeutronConstants.NETWORK_ID, dummyPort.getNetworkId()); + msg.setHeader(NeutronConstants.MAC_ADDRESS, dummyPort.getMacAddress()); + msg.setHeader(NeutronConstants.DEVICE_ID, dummyPort.getDeviceId()); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Port.class); + verify(portService).create(captor.capture()); + + assertEqualsPort(dummyPort, captor.getValue()); + assertNotNull(msg.getBody(Port.class).getId()); + } + + @Test + public void getTest() throws Exception { + final String portID = "myNetID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET); + msg.setHeader(NeutronConstants.ID, portID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(portService).get(captor.capture()); + + assertEquals(portID, captor.getValue()); + assertEqualsPort(testOSport, msg.getBody(Port.class)); + } + + @Test + public void getAllTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET_ALL); + + producer.process(exchange); + + final List result = msg.getBody(List.class); + assertTrue(result.size() == 2); + assertEquals(testOSport, result.get(0)); + } + + @Test + public void updateTest() throws Exception { + final String portID = "myID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.UPDATE); + final String newDevId = "dev"; + when(testOSport.getDeviceId()).thenReturn(newDevId); + when(testOSport.getId()).thenReturn(portID); + when(portService.update(any(Port.class))).thenReturn(testOSport); + + msg.setBody(testOSport); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Port.class); + verify(portService).update(captor.capture()); + + assertEqualsPort(testOSport, captor.getValue()); + assertNotNull(captor.getValue().getId()); + } + + @Test + public void deleteTest() throws Exception { + when(portService.delete(anyString())).thenReturn(ActionResponse.actionSuccess()); + final String portID = "myNetID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.DELETE); + msg.setHeader(NeutronConstants.ID, portID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(portService).delete(captor.capture()); + assertEquals(portID, captor.getValue()); + assertFalse(msg.isFault()); + + //in case of failure + final String failureMessage = "fail"; + when(portService.delete(anyString())).thenReturn(ActionResponse.actionFailed(failureMessage, 404)); + producer.process(exchange); + assertTrue(msg.isFault()); + assertTrue(msg.getBody(String.class).contains(failureMessage)); + } + + private Port createPort() { + return Builders.port() + .name("name") + .tenantId("tenantID") + .networkId("netId") + .deviceId("devID") + .macAddress("mac").build(); + } + + private void assertEqualsPort(Port old, Port newPort) { + assertEquals(old.getName(), newPort.getName()); + assertEquals(old.getTenantId(), newPort.getTenantId()); + assertEquals(old.getNetworkId(), newPort.getNetworkId()); + assertEquals(old.getDeviceId(), newPort.getDeviceId()); + assertEquals(old.getMacAddress(), newPort.getMacAddress()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/RouterProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/RouterProducerTest.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/RouterProducerTest.java new file mode 100644 index 0000000..18d0ecc --- /dev/null +++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/RouterProducerTest.java @@ -0,0 +1,221 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.camel.component.openstack.neutron.producer.RouterProducer; + +import org.junit.Before; +import org.junit.Test; + +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.openstack4j.api.Builders; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.AttachInterfaceType; +import org.openstack4j.model.network.NetworkType; +import org.openstack4j.model.network.Router; +import org.openstack4j.model.network.RouterInterface; +import org.openstack4j.openstack.networking.domain.NeutronRouterInterface; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class RouterProducerTest extends NeutronProducerTestSupport { + + private Router dummyRouter; + + @Mock + private Router testOSrouter; + + @Before + public void setUp() { + producer = new RouterProducer(endpoint, client); + when(routerService.create(any(Router.class))).thenReturn(testOSrouter); + when(routerService.get(anyString())).thenReturn(testOSrouter); + + List getAllList = new ArrayList<>(); + getAllList.add(testOSrouter); + getAllList.add(testOSrouter); + doReturn(getAllList).when(routerService).list(); + + dummyRouter = createRouter(); + when(testOSrouter.getName()).thenReturn(dummyRouter.getName()); + when(testOSrouter.getTenantId()).thenReturn(dummyRouter.getTenantId()); + when(testOSrouter.getId()).thenReturn(UUID.randomUUID().toString()); + } + + @Test + public void createTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.CREATE); + msg.setHeader(NeutronConstants.NAME, dummyRouter.getName()); + msg.setHeader(NeutronConstants.TENANT_ID, dummyRouter.getTenantId()); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Router.class); + verify(routerService).create(captor.capture()); + + assertEqualsRouter(dummyRouter, captor.getValue()); + assertNotNull(msg.getBody(Router.class).getId()); + } + + @Test + public void getTest() throws Exception { + final String routerID = "myRouterID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET); + msg.setHeader(NeutronConstants.ROUTER_ID, routerID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(routerService).get(captor.capture()); + + assertEquals(routerID, captor.getValue()); + assertEqualsRouter(testOSrouter, msg.getBody(Router.class)); + } + + @Test + public void getAllTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET_ALL); + + producer.process(exchange); + + final List result = msg.getBody(List.class); + assertTrue(result.size() == 2); + assertEquals(testOSrouter, result.get(0)); + } + + @Test + public void updateTest() throws Exception { + final String routerID = "myRouterID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.UPDATE); + final Router tmp = createRouter(); + final String newName = "newName"; + tmp.setName(newName); + when(routerService.update(any(Router.class))).thenReturn(tmp); + dummyRouter.setId(routerID); + msg.setBody(dummyRouter); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Router.class); + verify(routerService).update(captor.capture()); + + assertEqualsRouter(dummyRouter, captor.getValue()); + assertNotNull(captor.getValue().getId()); + assertEquals(newName, msg.getBody(Router.class).getName()); + } + + @Test + public void deleteTest() throws Exception { + when(routerService.delete(anyString())).thenReturn(ActionResponse.actionSuccess()); + final String routerID = "myRouterID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.DELETE); + msg.setHeader(NeutronConstants.ID, routerID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(routerService).delete(captor.capture()); + assertEquals(routerID, captor.getValue()); + assertFalse(msg.isFault()); + + //in case of failure + final String failureMessage = "fail"; + when(routerService.delete(anyString())).thenReturn(ActionResponse.actionFailed(failureMessage, 404)); + producer.process(exchange); + assertTrue(msg.isFault()); + assertTrue(msg.getBody(String.class).contains(failureMessage)); + } + + @Test + public void detachTest() throws Exception { + final String routerID = "myRouterID"; + final String portId = "port"; + final String subnetId = "subnet"; + final RouterInterface ifce = new NeutronRouterInterface(subnetId, portId); + when(routerService.detachInterface(anyString(), anyString(), anyString())).thenReturn(ifce); + + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.DETACH_INTERFACE); + msg.setHeader(NeutronConstants.ROUTER_ID, routerID); + msg.setHeader(NeutronConstants.SUBNET_ID, subnetId); + msg.setHeader(NeutronConstants.PORT_ID, portId); + + producer.process(exchange); + + ArgumentCaptor routerC = ArgumentCaptor.forClass(String.class); + ArgumentCaptor portC = ArgumentCaptor.forClass(String.class); + ArgumentCaptor subnetC = ArgumentCaptor.forClass(String.class); + verify(routerService).detachInterface(routerC.capture(), subnetC.capture(), portC.capture()); + + assertEquals(routerID, routerC.getValue()); + assertEquals(subnetId, subnetC.getValue()); + assertEquals(portId, portC.getValue()); + + assertEquals(ifce, msg.getBody(RouterInterface.class)); + } + + @Test + public void attachTest() throws Exception { + final String routerID = "myRouterID"; + final String subnetId = "subnet"; + final RouterInterface ifce = new NeutronRouterInterface(subnetId, null); + when(routerService.attachInterface(anyString(), any(AttachInterfaceType.class), anyString())).thenReturn(ifce); + + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.ATTACH_INTERFACE); + msg.setHeader(NeutronConstants.ROUTER_ID, routerID); + msg.setHeader(NeutronConstants.SUBNET_ID, subnetId); + msg.setHeader(NeutronConstants.ITERFACE_TYPE, AttachInterfaceType.SUBNET); + + producer.process(exchange); + + ArgumentCaptor routerC = ArgumentCaptor.forClass(String.class); + ArgumentCaptor itfType = ArgumentCaptor.forClass(AttachInterfaceType.class); + ArgumentCaptor subnetC = ArgumentCaptor.forClass(String.class); + verify(routerService).attachInterface(routerC.capture(), itfType.capture(), subnetC.capture()); + + assertEquals(routerID, routerC.getValue()); + assertEquals(AttachInterfaceType.SUBNET, itfType.getValue()); + assertEquals(subnetId, subnetC.getValue()); + + assertEquals(ifce, msg.getBody(RouterInterface.class)); + } + + private Router createRouter() { + return Builders.router() + .name("name") + .tenantId("tenantID") + .build(); + } + + private void assertEqualsRouter(Router old, Router newRouter) { + assertEquals(old.getName(), newRouter.getName()); + assertEquals(old.getTenantId(), newRouter.getTenantId()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2847e099/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/SubnetProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/SubnetProducerTest.java b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/SubnetProducerTest.java new file mode 100644 index 0000000..9aa45d4 --- /dev/null +++ b/components/camel-openstack/src/test/java/org/apache/camel/component/openstack/neutron/SubnetProducerTest.java @@ -0,0 +1,143 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openstack.neutron; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.camel.component.openstack.neutron.producer.SubnetProducer; + +import org.junit.Before; +import org.junit.Test; + +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.openstack4j.api.Builders; +import org.openstack4j.model.common.ActionResponse; +import org.openstack4j.model.network.Subnet; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class SubnetProducerTest extends NeutronProducerTestSupport { + + private Subnet dummySubnet; + + @Mock + private Subnet testOSsubnet; + + @Before + public void setUp() { + producer = new SubnetProducer(endpoint, client); + when(subnetService.create(any(Subnet.class))).thenReturn(testOSsubnet); + when(subnetService.get(anyString())).thenReturn(testOSsubnet); + + List getAllList = new ArrayList<>(); + getAllList.add(testOSsubnet); + getAllList.add(testOSsubnet); + doReturn(getAllList).when(subnetService).list(); + + dummySubnet = createSubnet(); + when(testOSsubnet.getName()).thenReturn(dummySubnet.getName()); + when(testOSsubnet.getNetworkId()).thenReturn(dummySubnet.getNetworkId()); + when(testOSsubnet.getId()).thenReturn(UUID.randomUUID().toString()); + } + + @Test + public void createTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.CREATE); + msg.setHeader(NeutronConstants.NAME, dummySubnet.getName()); + msg.setHeader(NeutronConstants.NETWORK_ID , dummySubnet.getNetworkId()); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Subnet.class); + verify(subnetService).create(captor.capture()); + + assertEqualsSubnet(dummySubnet, captor.getValue()); + assertNotNull(msg.getBody(Subnet.class).getId()); + } + + @Test + public void getTest() throws Exception { + final String subnetID = "myNetID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET); + msg.setHeader(NeutronConstants.SUBNET_ID, subnetID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(subnetService).get(captor.capture()); + + assertEquals(subnetID, captor.getValue()); + assertEqualsSubnet(testOSsubnet, msg.getBody(Subnet.class)); + } + + @Test + public void getAllTest() throws Exception { + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.GET_ALL); + + producer.process(exchange); + + final List result = msg.getBody(List.class); + assertTrue(result.size() == 2); + assertEquals(testOSsubnet, result.get(0)); + } + + @Test + public void deleteTest() throws Exception { + when(subnetService.delete(anyString())).thenReturn(ActionResponse.actionSuccess()); + final String subnetID = "myNetID"; + msg.setHeader(NeutronConstants.OPERATION, NeutronConstants.DELETE); + msg.setHeader(NeutronConstants.ID, subnetID); + + producer.process(exchange); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(subnetService).delete(captor.capture()); + assertEquals(subnetID, captor.getValue()); + assertFalse(msg.isFault()); + + //in case of failure + final String failureMessage = "fail"; + when(subnetService.delete(anyString())).thenReturn(ActionResponse.actionFailed(failureMessage, 404)); + producer.process(exchange); + assertTrue(msg.isFault()); + assertTrue(msg.getBody(String.class).contains(failureMessage)); + } + + private Subnet createSubnet() { + return Builders.subnet() + .name("name") + .networkId("netId") + .build(); + } + + private void assertEqualsSubnet(Subnet old, Subnet newSubnet) { + assertEquals(old.getName(), newSubnet.getName()); + assertEquals(old.getNetworkId(), newSubnet.getNetworkId()); + } +}