Activation status handling mechanism for ForwardingConstruct provisioning
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / impl / ForwardingConstructTestUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems Inc 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 package org.opendaylight.unimgr.impl;
9
10 import org.opendaylight.yang.gen.v1.urn.mef.unimgr.ext.rev160725.ActivationStatus;
11 import org.opendaylight.yang.gen.v1.urn.mef.unimgr.ext.rev160725.ForwardingConstruct1;
12 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.ForwardingConstructs;
13 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.forwarding.constructs.ForwardingConstruct;
14 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.forwarding.constructs.ForwardingConstructBuilder;
15 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.forwarding.constructs.ForwardingConstructKey;
16 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPort;
17 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPortBuilder;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.junit.Assert;
23
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 /**
32  * @author krzysztof.bijakowski@amartus.com
33  */
34 class ForwardingConstructTestUtils {
35     private static final ForwardingConstructKey fcKey = new ForwardingConstructKey("fc");
36
37     static ForwardingConstructKey fcKey() {
38         return fcKey;
39     }
40
41     static InstanceIdentifier<ForwardingConstruct> fcIid() {
42         return InstanceIdentifier
43                 .builder(ForwardingConstructs.class)
44                 .child(ForwardingConstruct.class, fcKey)
45                 .build();
46     }
47     static ForwardingConstruct fcSingleNode() {
48         return fc(
49                 port("a", "localhost", "80"),
50                 port("z", "localhost", "8080")
51         );
52     }
53
54     static ForwardingConstruct fcTwoNodes() {
55         return fc(
56                 port("a", "192.168.1.1", "80"),
57                 port("z", "192.168.1.2", "80")
58         );
59     }
60
61     static void assertEquals(ForwardingConstruct expectedFc, ForwardingConstruct actualFc) {
62         assertNotNull(expectedFc);
63         assertNotNull(actualFc);
64
65         assertNotNull(expectedFc.getFcPort());
66         assertNotNull(actualFc.getFcPort());
67
68         Set<FcPort> expectedFcPorts = new HashSet<>(expectedFc.getFcPort());
69         Set<FcPort> actualFcPorts = new HashSet<>(actualFc.getFcPort());
70
71         assertTrue(expectedFcPorts.size() == actualFcPorts.size());
72
73         for (FcPort expectedFcPort : expectedFcPorts) {
74             boolean equal = false;
75
76             for (FcPort actualFcPort : actualFcPorts) {
77                 equal = compareFcPort(expectedFcPort, actualFcPort);
78
79                 if(equal) {
80                     break;
81                 }
82             }
83
84             assertTrue(equal);
85         }
86         //TODO assertions for other parameters
87     }
88
89     static void assertActivationState(ForwardingConstruct fc, ActivationStatus expectedActivationStatus) {
90         assertNotNull(fc.getAugmentation(ForwardingConstruct1.class));
91         assertNotNull((fc.getAugmentation(ForwardingConstruct1.class).getUnimgrAttrs()));
92
93         ActivationStatus actualActivationStatus = fc.getAugmentation(ForwardingConstruct1.class).getUnimgrAttrs().getStatus();
94         assertNotNull(actualActivationStatus);
95
96         Assert.assertEquals(expectedActivationStatus, actualActivationStatus);
97     }
98
99     private static boolean compareFcPort(FcPort expectedFcPort, FcPort actualFcPort) {
100         assertNotNull(expectedFcPort);
101         assertNotNull(actualFcPort);
102
103         assertNotNull(expectedFcPort.getTopology());
104         assertNotNull(expectedFcPort.getTopology().getValue());
105         assertNotNull(actualFcPort.getTopology());
106         assertNotNull(actualFcPort.getTopology().getValue());
107
108         assertNotNull(expectedFcPort.getNode());
109         assertNotNull(expectedFcPort.getNode().getValue());
110         assertNotNull(actualFcPort.getNode());
111         assertNotNull(actualFcPort.getNode().getValue());
112
113         assertNotNull(expectedFcPort.getTp());
114         assertNotNull(expectedFcPort.getTp().getValue());
115         assertNotNull(actualFcPort.getTp());
116         assertNotNull(actualFcPort.getTp().getValue());
117
118         //TODO assertions for other parameters
119         //TODO add possibility of null paramaters
120
121         boolean result =
122             expectedFcPort.getTopology().getValue().equals(actualFcPort.getTopology().getValue()) &&
123             expectedFcPort.getNode().getValue().equals(actualFcPort.getNode().getValue()) &&
124             expectedFcPort.getTp().getValue().equals(actualFcPort.getTp().getValue());
125
126         return result;
127     }
128
129     private static ForwardingConstruct fc(FcPort... ports) {
130         return new ForwardingConstructBuilder()
131                 .setFcPort(Arrays.asList(ports))
132                 .setKey(fcKey)
133                 .build();
134     }
135
136     private static FcPort port(String topo, String host, String port) {
137         return new FcPortBuilder()
138                 .setTopology(new TopologyId(topo))
139                 .setNode(new NodeId(host))
140                 .setTp(new TpId(port))
141                 .build();
142     }
143 }