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