Merge "make ConfigurationServiceFactoryImpl independent of OSGi"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalBundleServiceImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.services.sal;
10
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Matchers;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SalExperimenterMessageService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SendExperimenterInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.Messages;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.MessagesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.MessageBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddFlowCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddGroupCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveFlowCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveGroupCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleUpdateFlowCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleUpdateGroupCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleUpdatePortCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleAddMessageSalBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleControlSalBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.bundle.add.message.sal.SalAddMessageDataBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.bundle.control.sal.SalControlDataBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.common.grouping.BundleProperty;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class SalBundleServiceImplTest {
56
57     private static final NodeRef NODE_REF = new NodeRef(InstanceIdentifier.create(Nodes.class)
58             .child(Node.class, new NodeKey(new NodeId("openflow:1"))));
59     private static final BundleId BUNDLE_ID = new BundleId(1L);
60     private static final BundleFlags BUNDLE_FLAGS = new BundleFlags(true, false);
61
62     private SalBundleService service;
63     @Mock
64     private SalExperimenterMessageService experimenterMessageService;
65     @Mock
66     private List<BundleProperty> properties;
67
68     @Before
69     public void setUp() {
70         service = new SalBundleServiceImpl(experimenterMessageService);
71     }
72
73     @Test
74     public void testControlBundle() {
75         final ControlBundleInput input = new ControlBundleInputBuilder()
76                 .setNode(NODE_REF)
77                 .setBundleId(BUNDLE_ID)
78                 .setFlags(BUNDLE_FLAGS)
79                 .setType(BundleControlType.ONFBCTOPENREQUEST)
80                 .setBundleProperty(properties)
81                 .build();
82         final SendExperimenterInputBuilder experimenterBuilder = new SendExperimenterInputBuilder();
83         experimenterBuilder.setNode(NODE_REF);
84         experimenterBuilder.setExperimenterMessageOfChoice(new BundleControlSalBuilder()
85                 .setSalControlData(new SalControlDataBuilder(input).build())
86                 .build());
87         Mockito.when(experimenterMessageService.sendExperimenter(Matchers.any())).thenReturn(SettableFuture.create());
88         service.controlBundle(input);
89         Mockito.verify(experimenterMessageService).sendExperimenter(experimenterBuilder.build());
90     }
91
92     @Test
93     public void testAddBundleMessages() throws Exception {
94         final List<Message> innerMessages = createMessages();
95         final Messages messages = new MessagesBuilder().setMessage(innerMessages).build();
96         final AddBundleMessagesInput input = new AddBundleMessagesInputBuilder()
97                 .setNode(NODE_REF)
98                 .setBundleId(BUNDLE_ID)
99                 .setFlags(BUNDLE_FLAGS)
100                 .setBundleProperty(properties)
101                 .setMessages(messages)
102                 .build();
103         final SalAddMessageDataBuilder dataBuilder = new SalAddMessageDataBuilder();
104         dataBuilder.setBundleId(BUNDLE_ID).setFlags(BUNDLE_FLAGS).setBundleProperty(properties);
105
106         final BundleAddMessageSalBuilder addMessageBuilder = new BundleAddMessageSalBuilder();
107         final SendExperimenterInputBuilder experimenterBuilder = new SendExperimenterInputBuilder()
108                 .setNode(NODE_REF);
109         Mockito.when(experimenterMessageService.sendExperimenter(Matchers.any())).thenReturn(SettableFuture.create());
110         service.addBundleMessages(input);
111         for (Message msg : innerMessages) {
112             Mockito.verify(experimenterMessageService)
113                     .sendExperimenter(experimenterBuilder
114                                   .setExperimenterMessageOfChoice(addMessageBuilder
115                                               .setSalAddMessageData(
116                                                     dataBuilder.setNode(NODE_REF).setBundleInnerMessage(
117                                                                                         msg.getBundleInnerMessage())
118                                                                                 .build())
119                                                                 .build()).build());
120         }
121     }
122
123     private static List<Message> createMessages() {
124         List<Message> messages  = new ArrayList<>();
125         messages.add(
126                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleAddFlowCaseBuilder().build())
127                         .build());
128         messages.add(
129                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleUpdateFlowCaseBuilder().build())
130                         .build());
131         messages.add(
132                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleRemoveFlowCaseBuilder().build())
133                         .build());
134         messages.add(
135                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleAddGroupCaseBuilder().build())
136                         .build());
137         messages.add(
138                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleUpdateGroupCaseBuilder().build())
139                         .build());
140         messages.add(
141                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleRemoveGroupCaseBuilder().build())
142                         .build());
143         messages.add(
144                 new MessageBuilder().setNode(NODE_REF).setBundleInnerMessage(new BundleUpdatePortCaseBuilder().build())
145                         .build());
146         return messages;
147     }
148
149 }