1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .apache .cloudstack .feature ;
21+
22+ import com .cloud .utils .component .ManagerBase ;
23+ import com .cloud .utils .component .PluggableService ;
24+ import org .apache .cloudstack .api .Coffee ;
25+ import org .apache .cloudstack .api .CoffeeManager ;
26+ import org .apache .cloudstack .api .command .CreateCoffeeCmd ;
27+ import org .apache .cloudstack .api .command .ListCoffeeCmd ;
28+ import org .apache .cloudstack .api .command .RemoveCoffeeCmd ;
29+ import org .apache .cloudstack .api .command .UpdateCoffeeCmd ;
30+ import org .apache .logging .log4j .Logger ;
31+ import org .apache .logging .log4j .LogManager ;
32+
33+ import javax .naming .ConfigurationException ;
34+ import java .util .ArrayList ;
35+ import java .util .List ;
36+ import java .util .Map ;
37+
38+ public class CoffeeManagerImpl extends ManagerBase implements CoffeeManager , PluggableService {
39+
40+ private static final Logger LOGGER = LogManager .getLogger (CoffeeManagerImpl .class );
41+
42+ private static class HardcodedCoffee implements Coffee {
43+ private final long id ;
44+ private final String uuid ;
45+ private final String name ;
46+ private final Offering offering ;
47+ private Size size ;
48+ private State state ;
49+
50+ public HardcodedCoffee (long id , String uuid , String name , Offering offering , Size size , State state ) {
51+ this .id = id ;
52+ this .uuid = uuid ;
53+ this .name = name ;
54+ this .offering = offering ;
55+ this .size = size ;
56+ this .state = state ;
57+ }
58+
59+ @ Override
60+ public long getId () { return id ; }
61+
62+ @ Override
63+ public String getUuid () { return uuid ; }
64+
65+ @ Override
66+ public String getName () { return name ; }
67+
68+ @ Override
69+ public Offering getOffering () { return offering ; }
70+
71+ @ Override
72+ public Size getSize () { return size ; }
73+
74+ public void setSize (Size size ) { this .size = size ; }
75+
76+ @ Override
77+ public State getState () { return state ; }
78+
79+ public void setState (State state ) { this .state = state ; }
80+ }
81+
82+ @ Override
83+ public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
84+ super .configure (name , params );
85+ LOGGER .info ("CoffeeManager is being configured" );
86+ return true ;
87+ }
88+
89+ @ Override
90+ public boolean start () {
91+ LOGGER .info ("CoffeeManager is starting" );
92+ return true ;
93+ }
94+
95+ @ Override
96+ public boolean stop () {
97+ LOGGER .info ("CoffeeManager is stopping" );
98+ return true ;
99+ }
100+
101+ @ Override
102+ public List <Class <?>> getCommands () {
103+ final List <Class <?>> cmdList = new ArrayList <>();
104+ cmdList .add (CreateCoffeeCmd .class );
105+ cmdList .add (ListCoffeeCmd .class );
106+ cmdList .add (UpdateCoffeeCmd .class );
107+ cmdList .add (RemoveCoffeeCmd .class );
108+ return cmdList ;
109+ }
110+
111+ @ Override
112+ public Coffee createCoffee (CreateCoffeeCmd cmd ) {
113+ LOGGER .info ("Creating coffee: " + cmd .getName ());
114+ Coffee coffee = new HardcodedCoffee (
115+ 3L ,
116+ "fake-uuid-3" ,
117+ cmd .getName (),
118+ Coffee .Offering .valueOf (cmd .getOffering ()),
119+ Coffee .Size .valueOf (cmd .getSize ()),
120+ Coffee .State .Created
121+ );
122+
123+ LOGGER .debug ("Created coffee with ID: " + coffee .getId ());
124+ return coffee ;
125+ }
126+
127+ @ Override
128+ public List <Coffee > listCoffees (ListCoffeeCmd cmd ) {
129+ LOGGER .info ("Listing coffees" );
130+ List <Coffee > coffees = new ArrayList <>();
131+
132+ coffees .add (new HardcodedCoffee (1L , "uuid-1" , "Espresso" ,
133+ Coffee .Offering .Espresso , Coffee .Size .SMALL , Coffee .State .Brewed ));
134+
135+ coffees .add (new HardcodedCoffee (2L , "uuid-2" , "Latte" ,
136+ Coffee .Offering .Latte , Coffee .Size .LARGE , Coffee .State .Created ));
137+
138+ coffees .add (new HardcodedCoffee (3L , "uuid-3" , "Cappuccino" ,
139+ Coffee .Offering .Cappuccino , Coffee .Size .MEDIUM , Coffee .State .Brewing ));
140+
141+ String id = cmd .getId ();
142+ String offering = cmd .getOffering ();
143+ String size = cmd .getSize ();
144+
145+ List <Coffee > filtered = new ArrayList <>();
146+ for (Coffee coffee : coffees ) {
147+ boolean match = true ;
148+
149+ if (id != null && !String .valueOf (coffee .getId ()).equals (id )) {
150+ match = false ;
151+ }
152+ if (offering != null && !coffee .getOffering ().name ().equalsIgnoreCase (offering )) {
153+ match = false ;
154+ }
155+ if (size != null && !coffee .getSize ().name ().equalsIgnoreCase (size )) {
156+ match = false ;
157+ }
158+
159+ if (match ) {
160+ filtered .add (coffee );
161+ }
162+ }
163+
164+ LOGGER .debug ("Returning " + filtered .size () + " coffees" );
165+ return filtered ;
166+ }
167+
168+ @ Override
169+ public Coffee updateCoffee (UpdateCoffeeCmd cmd ) {
170+ LOGGER .info ("Updating coffee with ID: " + cmd .getId ());
171+
172+ HardcodedCoffee coffee = new HardcodedCoffee (
173+ Long .parseLong (cmd .getId ()),
174+ "uuid-" + cmd .getId (),
175+ "Updated Coffee Order" ,
176+ Coffee .Offering .Espresso ,
177+ cmd .getSize () != null ? Coffee .Size .valueOf (cmd .getSize ()) : Coffee .Size .MEDIUM ,
178+ Coffee .State .Created
179+ );
180+
181+ LOGGER .debug ("Updated coffee: " + coffee .getName ());
182+ return coffee ;
183+ }
184+
185+ @ Override
186+ public boolean removeCoffee (RemoveCoffeeCmd cmd ) {
187+ if (cmd .getId () != null ) {
188+ LOGGER .info ("Removing coffee with ID: " + cmd .getId ());
189+ } else if (cmd .getIds () != null ) {
190+ LOGGER .info ("Removing " + cmd .getIds ().size () + " coffees" );
191+ }
192+
193+ return true ;
194+ }
195+ }
0 commit comments